Skip to content

Commit

Permalink
[#1228] added DittoHeaderDefinition "live-channel-condition-matched" …
Browse files Browse the repository at this point in the history
…which will also be written to external headers to find out whether the live channel condition matched

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Nov 29, 2021
1 parent bbcc567 commit 7e31e76
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ public Optional<String> getLiveChannelCondition() {
return getStringForDefinition(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION);
}

@Override
public boolean didLiveChannelConditionMatch() {
return isExpectedBoolean(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED, Boolean.TRUE);
}

@Override
public Optional<String> getOrigin() {
return getStringForDefinition(DittoHeaderDefinition.ORIGIN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,36 @@ public enum DittoHeaderDefinition implements HeaderDefinition {
HeaderValueValidators.getNoOpValidator()),

/**
* Header definition to identify a request with a conditional channel.
* The condition is defined via a RQL expressions, which is evaluated in the things service. If the condition is
* Header definition containing a condition which, when evaluating to {@code true}, shall switch the
* {@link #CHANNEL} to use to the {@code "live"} channel.
* The condition is defined via a RQL expressions, which is evaluated in the things persistence. If the condition is
* evaluated to 'true', then the live-channel is used instead of the regular twin-channel.
*
* <p>
* Key {@code "live-channel-condition"}, Java type: {@link String}.
* </p>
*
* @since 2.2.0
* @since 2.3.0
*/
LIVE_CHANNEL_CONDITION("live-channel-condition", String.class, true, false,
HeaderValueValidators.getNoOpValidator());
HeaderValueValidators.getNoOpValidator()),

/**
* Header containing the result of the {@link #LIVE_CHANNEL_CONDITION} evaluated in the things persistence.
* If this contains {@code true}, the {@code "live"} channel shall be used in order to handle a processed command.
* If this header is missing or contains value {@code false}, the live channel shall not be used and the twin
* response this header is transported with shall be returned instead.
*
* <p>
* Key {@code "live-channel-condition-matched"}, Java type: {@link Boolean}.
* </p>
*
* @since 2.3.0
*/
LIVE_CHANNEL_CONDITION_MATCHED("live-channel-condition-matched", Boolean.class,
false, true, HeaderValueValidators.getBooleanValidator())

;

/**
* Map to speed up lookup of header definition by key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,19 @@ static DittoHeadersBuilder newBuilder(final JsonObject jsonObject) {
* Returns the channel condition, if the live-channel shall be used for the request.
*
* @return the condition contained in the Condition header.
* @since 2.2.0
* @since 2.3.0
*/
Optional<String> getLiveChannelCondition();

/**
* Returns whether the live channel condition passed to the things persistence via the
* {@link #getLiveChannelCondition()} header did match the persisted state of a thing or not.
*
* @return whether the live channel condition passed to the things persistence matched or not.
* @since 2.3.0
*/
boolean didLiveChannelConditionMatch();

/**
* Returns the id of the originating session (e.g. WebSocket, AMQP, ...)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public final class ImmutableDittoHeadersTest {
private static final boolean KNOWN_IS_SUDO = true;
private static final String KNOWN_CONDITION = "eq(attributes/value)";
private static final String KNOWN_LIVE_CHANNEL_CONDITION = "eq(attributes/value,\"livePolling\")";
private static final boolean KNOWN_LIVE_CHANNEL_CONDITION_MATCHED = true;
private static final String KNOWN_TRACEPARENT = "00-dfca0d990402884d22e909a87ac677ec-94fc4da95e842f96-01";
private static final String KNOWN_TRACESTATE = "eclipse=ditto";
private static final boolean KNOWN_DITTO_RETRIEVE_DELETED = true;
Expand Down Expand Up @@ -178,6 +179,8 @@ public void settingAllKnownHeadersWorksAsExpected() {
.putHeader(DittoHeaderDefinition.W3C_TRACESTATE.getKey(), KNOWN_TRACESTATE)
.condition(KNOWN_CONDITION)
.liveChannelCondition(KNOWN_LIVE_CHANNEL_CONDITION)
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(),
String.valueOf(KNOWN_LIVE_CHANNEL_CONDITION_MATCHED))
.build();

assertThat(underTest).isEqualTo(expectedHeaderMap);
Expand Down Expand Up @@ -334,6 +337,31 @@ public void isSudoIsTrueWhenTrue() {
assertThat(underTest.isSudo()).isTrue();
}

@Test
public void didLiveChannelConditionMatchIsFalseOnMissingHeader() {
final DittoHeaders underTest = DittoHeaders.empty();

assertThat(underTest.didLiveChannelConditionMatch()).isFalse();
}

@Test
public void didLiveChannelConditionMatchIsFalseWhenFalse() {
final DittoHeaders underTest = DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(), "false")
.build();

assertThat(underTest.didLiveChannelConditionMatch()).isFalse();
}

@Test
public void didLiveChannelConditionMatchIsTrueWhenTrue() {
final DittoHeaders underTest = DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(), "true")
.build();

assertThat(underTest.didLiveChannelConditionMatch()).isTrue();
}

@Test
public void liveChannelConditionNotEmptyWhenSet() {
final String testValue = "some-condition";
Expand Down Expand Up @@ -453,6 +481,8 @@ public void toJsonReturnsExpected() {
.set(DittoHeaderDefinition.W3C_TRACESTATE.getKey(), KNOWN_TRACESTATE)
.set(DittoHeaderDefinition.CONDITION.getKey(), KNOWN_CONDITION)
.set(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION.getKey(), KNOWN_LIVE_CHANNEL_CONDITION)
.set(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(),
KNOWN_LIVE_CHANNEL_CONDITION_MATCHED)
.build();

final Map<String, String> allKnownHeaders = createMapContainingAllKnownHeaders();
Expand Down Expand Up @@ -684,6 +714,8 @@ private static Map<String, String> createMapContainingAllKnownHeaders() {
result.put(DittoHeaderDefinition.W3C_TRACESTATE.getKey(), KNOWN_TRACESTATE);
result.put(DittoHeaderDefinition.CONDITION.getKey(), KNOWN_CONDITION);
result.put(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION.getKey(), KNOWN_LIVE_CHANNEL_CONDITION);
result.put(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION_MATCHED.getKey(),
String.valueOf(KNOWN_LIVE_CHANNEL_CONDITION_MATCHED));

return result;
}
Expand Down

0 comments on commit 7e31e76

Please sign in to comment.