Skip to content

Commit

Permalink
eclipse-ditto#760 fix template placeholder replacement
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schneider <johannes.schneider@bosch.io>
  • Loading branch information
jokraehe committed Sep 4, 2020
1 parent 2f2bfab commit 8c8d84d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
Expand Up @@ -129,11 +129,14 @@ public List<Adaptable> map(final ExternalMessage message) {
final Map<String, String> externalHeaders = message.getHeaders();
final ExpressionResolver expressionResolver = getExpressionResolver(externalHeaders);

final String resolvedTemplate;
if (Placeholders.containsAnyPlaceholder(thingTemplate)) {
thingTemplate = applyPlaceholderReplacement(thingTemplate, expressionResolver);
resolvedTemplate = applyPlaceholderReplacement(thingTemplate, expressionResolver);
} else {
resolvedTemplate = thingTemplate;
}

final Signal<CreateThing> createThing = getCreateThingSignal(message);
final Signal<CreateThing> createThing = getCreateThingSignal(message, resolvedTemplate);
final Adaptable adaptable = DITTO_PROTOCOL_ADAPTER.toAdaptable(createThing);

LOGGER.withCorrelationId(message.getInternalHeaders())
Expand All @@ -151,16 +154,15 @@ private static String applyPlaceholderReplacement(final String template, final E
return PlaceholderFilter.apply(template, resolver);
}

private Signal<CreateThing> getCreateThingSignal(final ExternalMessage message) {
final JsonObject thingJson = wrapJsonRuntimeException(() -> JsonFactory.newObject(thingTemplate));
private Signal<CreateThing> getCreateThingSignal(final ExternalMessage message, final String template) {
final JsonObject thingJson = wrapJsonRuntimeException(() -> JsonFactory.newObject(template));
final Thing newThing = ThingsModelFactory.newThing(thingJson);
final JsonObject inlinePolicyJson = createInlinePolicyJson(thingJson);
final String copyPolicyFrom = getCopyPolicyFrom(thingJson);
//Sets content-type to application/json to allow Ditto PayloadMapping
final DittoHeaders dittoHeaders =
message.getInternalHeaders().toBuilder().contentType("application/json").build();
return CreateThing.of(newThing, inlinePolicyJson, copyPolicyFrom,
dittoHeaders);
final DittoHeaders dittoHeaders = message.getInternalHeaders().toBuilder()
.contentType("application/json")
.build();
return CreateThing.of(newThing, inlinePolicyJson, copyPolicyFrom, dittoHeaders);
}

@Nullable
Expand Down
Expand Up @@ -143,6 +143,51 @@ public void doForwardMappingContextWithPolicyPlaceholder() {
assertThat(createThing.getInitialPolicy()).contains(INITIAL_POLICY);
}

@Test
public void doForwardMappingTwice() {
underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE_WITH_POLICY));

final Map<String, String> headers1 = new HashMap<>();
headers1.put(HEADER_HONO_DEVICE_ID, "headerNamespace:headerDeviceId1");
headers1.put(HEADER_HONO_GATEWAY_ID, "headerNamespace:headerGatewayId");

final ExternalMessage externalMessage1 = ExternalMessageFactory.newExternalMessageBuilder(headers1).build();
final List<Adaptable> mappingResult1 = underTest.map(externalMessage1);

final Signal<?> firstMappedSignal1 = getFirstMappedSignal(mappingResult1);
assertThat(firstMappedSignal1).isInstanceOf(CreateThing.class);
final CreateThing createThing1 = (CreateThing) firstMappedSignal1;

final Thing expectedThing1 =
createExpectedThing("headerNamespace:headerDeviceId1", "headerNamespace:headerDeviceId1",
"headerNamespace:headerGatewayId");
assertThat(createThing1.getThing().getEntityId()).isEqualTo(expectedThing1.getEntityId());
assertThat(createThing1.getThing().getPolicyEntityId()).isEqualTo(expectedThing1.getPolicyEntityId());
assertThat(createThing1.getThing().getAttributes()).isEqualTo(expectedThing1.getAttributes());
assertThat(createThing1.getInitialPolicy()).contains(INITIAL_POLICY);



final Map<String, String> headers2 = new HashMap<>();
headers2.put(HEADER_HONO_DEVICE_ID, "headerNamespace:headerDeviceId2");
headers2.put(HEADER_HONO_GATEWAY_ID, "headerNamespace:headerGatewayId");

final ExternalMessage externalMessage2 = ExternalMessageFactory.newExternalMessageBuilder(headers2).build();
final List<Adaptable> mappingResult2 = underTest.map(externalMessage2);

final Signal<?> firstMappedSignal2 = getFirstMappedSignal(mappingResult2);
assertThat(firstMappedSignal2).isInstanceOf(CreateThing.class);
final CreateThing createThing2 = (CreateThing) firstMappedSignal2;

final Thing expectedThing2 =
createExpectedThing("headerNamespace:headerDeviceId2", "headerNamespace:headerDeviceId2",
"headerNamespace:headerGatewayId");
assertThat(createThing2.getThing().getEntityId()).isEqualTo(expectedThing2.getEntityId());
assertThat(createThing2.getThing().getPolicyEntityId()).isEqualTo(expectedThing2.getPolicyEntityId());
assertThat(createThing2.getThing().getAttributes()).isEqualTo(expectedThing2.getAttributes());
assertThat(createThing2.getInitialPolicy()).contains(INITIAL_POLICY);
}

@Test
public void doForwardWithoutPlaceholders() {
final Map<String, String> headers = createValidHeaders();
Expand Down

0 comments on commit 8c8d84d

Please sign in to comment.