Skip to content

Commit

Permalink
Merge pull request #671 from bosch-io/bugfix/header-mapping
Browse files Browse the repository at this point in the history
Bugfix/header mapping
  • Loading branch information
yufei-cai authored May 6, 2020
2 parents 546d3d8 + a7f531c commit 57a6f85
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.function.Predicate;
Expand Down Expand Up @@ -45,7 +44,7 @@ public void singleStringResolverAcceptsDoubleQuotes() {
final String params = "(\"" + KNOWN_VALUE + "\")";
assertThat(parameterResolver.apply(params, expressionResolver, DUMMY)).contains(KNOWN_VALUE);

verifyZeroInteractions(expressionResolver);
verifyNoInteractions(expressionResolver);
}

@Test
Expand All @@ -56,7 +55,7 @@ public void singleStringResolverAcceptsSingleQuotes() {
final String params = "(\'" + KNOWN_VALUE + "\')";
assertThat(parameterResolver.apply(params, expressionResolver, DUMMY)).contains(KNOWN_VALUE);

verifyZeroInteractions(expressionResolver);
verifyNoInteractions(expressionResolver);
}

@Test
Expand All @@ -75,7 +74,7 @@ public void singleResolverThrowsExceptionOnMultipleParameters() {
assertThatExceptionOfType(PlaceholderFunctionSignatureInvalidException.class)
.isThrownBy(() -> parameterResolver.apply(paramsPlaceholders, expressionResolver, DUMMY));

verifyZeroInteractions(expressionResolver);
verifyNoInteractions(expressionResolver);
}

@Test
Expand Down Expand Up @@ -147,7 +146,7 @@ public void singleStringResolverThrowsExceptionIfNotResolvable() {
assertThatExceptionOfType(PlaceholderFunctionSignatureInvalidException.class)
.isThrownBy(() -> stringResolver.apply(stringPlaceholder, expressionResolver, DUMMY));

verifyZeroInteractions(expressionResolver);
verifyNoInteractions(expressionResolver);
}

@Test
Expand All @@ -164,7 +163,7 @@ public void singleStringResolverDoesNotResolvePlaceholders() {
assertThatExceptionOfType(PlaceholderFunctionSignatureInvalidException.class)
.isThrownBy(() -> stringResolver.apply(stringPlaceholder, expressionResolver, DUMMY));

verifyZeroInteractions(expressionResolver);
verifyNoInteractions(expressionResolver);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.ditto.model.base.headers.DittoHeadersBuilder;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.protocoladapter.adaptables.MappingStrategies;
import org.eclipse.ditto.signals.commands.base.CommandResponse;

/**
* Abstract implementation of {@link Adapter} to provide common functionality.
Expand Down Expand Up @@ -152,29 +151,11 @@ private static DittoHeaders mapTopicPathToHeaders(final TopicPath topicPath) {
@Override
public final Adaptable toAdaptable(final T signal, final TopicPath.Channel channel) {
final Adaptable adaptable = mapSignalToAdaptable(signal, channel);
final Map<String, String> externalHeaders;
if (filterOutUnknownExternalHeaders(signal, channel)) {
externalHeaders = headerTranslator.toExternalAndRetainKnownHeaders(adaptable.getDittoHeaders());
} else {
externalHeaders = headerTranslator.toExternalHeaders(adaptable.getDittoHeaders());
}
final Map<String, String> externalHeaders = headerTranslator.toExternalHeaders(adaptable.getDittoHeaders());

return adaptable.setDittoHeaders(DittoHeaders.of(externalHeaders));
}

/**
* Called in order to determine whether to filter out unknown headers (then return {@code true}) or to keep all
* unknown headers (then return {@code false}).
* By default for CommandResponses the unknown headers will be filtered.
*
* @param signal the signal which can be used to determine the decision on.
* @param channel the channel of the signal.
* @return {@code true} when unknown external headers should be filtered, {@code false} otherwise.
*/
protected boolean filterOutUnknownExternalHeaders(final T signal, final TopicPath.Channel channel) {
return channel == TopicPath.Channel.TWIN && signal instanceof CommandResponse;
}

/**
* Subclasses must implement the method to map from the given {@link org.eclipse.ditto.signals.base.Signal} to an
* {@link Adaptable}.
Expand Down

0 comments on commit 57a6f85

Please sign in to comment.