Skip to content

Commit

Permalink
Issue #1228 add ditto header for new 'liveChannelCondition' header
Browse files Browse the repository at this point in the history
Similar to the 'condition' header, the only useful validation could be a RQL-Syntax check.
Such a check is not supported by the current RQL-Parser and there is also no type for it yet.
For now, only use 'String' as type.

Signed-off-by: Joel Bartelheimer <joel.bartelheimer@bosch.io>
  • Loading branch information
jbartelh committed Nov 10, 2021
1 parent ae52fbd commit 7e10176
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ public Optional<String> getCondition() {
return getStringForDefinition(DittoHeaderDefinition.CONDITION);
}

@Override
public Optional<String> getLiveChannelCondition() {
return getStringForDefinition(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION);
}

@Override
public Optional<String> getOrigin() {
return getStringForDefinition(DittoHeaderDefinition.ORIGIN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ public S condition(final String condition) {
return myself;
}

@Override
public S liveChannelCondition(final String liveChannelCondition) {
putCharSequence(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION, liveChannelCondition);
return myself;
}

@Override
public S putHeader(final CharSequence key, final CharSequence value) {
validateKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@ public enum DittoHeaderDefinition implements HeaderDefinition {
* @since 2.1.0
*/
CONDITION("condition", String.class, true, false,
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
* 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
*/
LIVE_CHANNEL_CONDITION("live-channel-condition", String.class, true, false,
HeaderValueValidators.getNoOpValidator());

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ static DittoHeadersBuilder newBuilder(final JsonObject jsonObject) {
*/
Optional<String> getCondition();

/**
* 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
*/
Optional<String> getLiveChannelCondition();

/**
* 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 @@ -312,6 +312,15 @@ B acknowledgementRequest(AcknowledgementRequest acknowledgementRequest,
*/
B condition(String condition);

/**
* Sets the channel condition which decides, if the live channel shall be used for the request.
*
* @param liveChannelCondition the channel condition to check before applying the request.
* @return this builder for method chaining.
* @since 2.2.0
*/
B liveChannelCondition(String liveChannelCondition);

/**
* Puts an arbitrary header with the specified {@code name} and String {@code value} to this builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public final class ImmutableDittoHeadersTest {
private static final List<String> KNOWN_JOURNAL_TAGS = Lists.list("tag-a", "tag-b");
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 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 @@ -175,6 +176,7 @@ public void settingAllKnownHeadersWorksAsExpected() {
.putHeader(DittoHeaderDefinition.W3C_TRACEPARENT.getKey(), KNOWN_TRACEPARENT)
.putHeader(DittoHeaderDefinition.W3C_TRACESTATE.getKey(), KNOWN_TRACESTATE)
.condition(KNOWN_CONDITION)
.liveChannelCondition(KNOWN_LIVE_CHANNEL_CONDITION)
.build();

assertThat(underTest).isEqualTo(expectedHeaderMap);
Expand Down Expand Up @@ -331,6 +333,16 @@ public void isSudoIsTrueWhenTrue() {
assertThat(underTest.isSudo()).isTrue();
}

@Test
public void liveChannelConditionNotEmptyWhenSet() {
final String testValue = "some-condition";
final DittoHeaders underTest = DittoHeaders.newBuilder()
.putHeader(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION.getKey(), testValue)
.build();

assertThat(underTest.getLiveChannelCondition()).hasValue(testValue);
}

@Test
public void getRequestedAckLabelsReturnsExpected() {
final DittoHeaders underTest = DittoHeaders.newBuilder()
Expand Down Expand Up @@ -423,6 +435,7 @@ public void toJsonReturnsExpected() {
.set(DittoHeaderDefinition.W3C_TRACEPARENT.getKey(), KNOWN_TRACEPARENT)
.set(DittoHeaderDefinition.W3C_TRACESTATE.getKey(), KNOWN_TRACESTATE)
.set(DittoHeaderDefinition.CONDITION.getKey(), KNOWN_CONDITION)
.set(DittoHeaderDefinition.LIVE_CHANNEL_CONDITION.getKey(), KNOWN_LIVE_CHANNEL_CONDITION)
.build();

final Map<String, String> allKnownHeaders = createMapContainingAllKnownHeaders();
Expand Down Expand Up @@ -652,6 +665,7 @@ private static Map<String, String> createMapContainingAllKnownHeaders() {
result.put(DittoHeaderDefinition.W3C_TRACEPARENT.getKey(), KNOWN_TRACEPARENT);
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);

return result;
}
Expand Down

0 comments on commit 7e10176

Please sign in to comment.