ImplicitThingCreationMessageMapper extends AbstractMessageMapper { @Override public List<Adaptable> map(final ExternalMessage message) { LOGGER.withCorrelationId(message.getInternalHeaders()).debug("Received ExternalMessage: {}", message); final Map<String, String> externalHeaders = message.getHeaders(); final ExpressionResolver expressionResolver = getExpressionResolver(externalHeaders); final String resolvedTemplate; if (Placeholders.containsAnyPlaceholder(thingTemplate)) { resolvedTemplate = applyPlaceholderReplacement(thingTemplate, expressionResolver); } else { resolvedTemplate = thingTemplate; } if (Placeholders.containsAnyPlaceholder(thingTemplate)) { commandHeaders = resolveCommandHeaders(message, commandHeaders); } final Signal<CreateThing> createThing = getCreateThingSignal(message, resolvedTemplate); final Adaptable adaptable = DITTO_PROTOCOL_ADAPTER.toAdaptable(createThing); final DittoHeaders modifiedHeaders = adaptable.getDittoHeaders().toBuilder().allowPolicyLockout(allowPolicyLockout).build(); final Adaptable adaptableWithModifiedHeaders = adaptable.setDittoHeaders(modifiedHeaders); LOGGER.withCorrelationId(message.getInternalHeaders()) .debug("Mapped ExternalMessage to Adaptable: {}", adaptableWithModifiedHeaders); return Collections.singletonList(adaptableWithModifiedHeaders); }  @Override List<Adaptable> map(final ExternalMessage message); @Override List<ExternalMessage> map(final Adaptable adaptable);  }
@Test public void doForwardMappingContextWithCommandHeaderPlaceholder() { final Map<String, String> headers = createValidHeaders(); underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE, COMMAND_HEADERS)); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); final Signal<?> firstMappedSignal = getFirstMappedSignal(mappingResult); assertThat(firstMappedSignal).isInstanceOf(CreateThing.class); final CreateThing createThing = (CreateThing) firstMappedSignal; final Thing expectedThing = createExpectedThing(DEVICE_ID, DEVICE_ID, GATEWAY_ID); assertThat(createThing.getThing().getEntityId()).isEqualTo(expectedThing.getEntityId()); assertThat(createThing.getThing().getPolicyEntityId()).isEmpty(); assertThat(createThing.getThing().getAttributes()).isEqualTo(expectedThing.getAttributes()); assertThat(createThing.getDittoHeaders().get("other-test-header")).isEqualTo(GATEWAY_ID); assertThat(createThing.getDittoHeaders().get("test-header")).isEqualTo("this-is-a-test-header"); assertThat(createThing.getDittoHeaders().getContentType()).contains(DITTO_PROTOCOL_CONTENT_TYPE); assertThat(createThing.getDittoHeaders().isAllowPolicyLockout()).isEqualTo(true); assertThat(createThing.getPolicyIdOrPlaceholder()).contains(GATEWAY_ID); }
@Test public void doForwardMappingContextWithDeviceIdPlaceholder() { final Map<String, String> headers = createValidHeaders(); underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE, COMMAND_HEADERS)); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); final Signal<?> firstMappedSignal = getFirstMappedSignal(mappingResult); assertThat(firstMappedSignal).isInstanceOf(CreateThing.class); final CreateThing createThing = (CreateThing) firstMappedSignal; final Thing expectedThing = createExpectedThing(DEVICE_ID, DEVICE_ID, GATEWAY_ID); assertThat(createThing.getThing().getEntityId()).isEqualTo(expectedThing.getEntityId()); assertThat(createThing.getThing().getPolicyEntityId()).isEmpty(); assertThat(createThing.getThing().getAttributes()).isEqualTo(expectedThing.getAttributes()); assertThat(createThing.getDittoHeaders().getContentType()).contains(DITTO_PROTOCOL_CONTENT_TYPE); assertThat(createThing.getPolicyIdOrPlaceholder()).contains(GATEWAY_ID); assertThat(createThing.getDittoHeaders().isAllowPolicyLockout()).isEqualTo(true); }
@Test public void doForwardMappingContextWithPolicyPlaceholder() { final Map<String, String> headers = createValidHeaders(); underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE_WITH_POLICY, COMMAND_HEADERS)); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); final Signal<?> firstMappedSignal = getFirstMappedSignal(mappingResult); assertThat(firstMappedSignal).isInstanceOf(CreateThing.class); final CreateThing createThing = (CreateThing) firstMappedSignal; final Thing expectedThing = createExpectedThing(DEVICE_ID, DEVICE_ID, GATEWAY_ID); assertThat(createThing.getThing().getEntityId()).isEqualTo(expectedThing.getEntityId()); assertThat(createThing.getThing().getPolicyEntityId()).isEqualTo(expectedThing.getPolicyEntityId()); assertThat(createThing.getThing().getAttributes()).isEqualTo(expectedThing.getAttributes()); assertThat(createThing.getInitialPolicy()).contains(INITIAL_POLICY); assertThat(createThing.getDittoHeaders().isAllowPolicyLockout()).isEqualTo(true); }
@Test public void doForwardMappingTwice() { underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE_WITH_POLICY, COMMAND_HEADERS)); final Map<String, String> headers1 = new HashMap<>(); headers1.put(HEADER_HONO_DEVICE_ID, "headerNamespace:headerDeviceId1"); headers1.put(HEADER_HONO_GATEWAY_ID, GATEWAY_ID); 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", GATEWAY_ID); 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, GATEWAY_ID); 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", GATEWAY_ID); 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(); underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE_WITHOUT_PLACEHOLDERS, COMMAND_HEADERS)); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); final Signal<?> firstMappedSignal = getFirstMappedSignal(mappingResult); assertThat(firstMappedSignal).isInstanceOf(CreateThing.class); final CreateThing createThing = (CreateThing) firstMappedSignal; final Thing expectedThing = createExpectedThing("some:validThingId!", "some:validPolicyId!", "some:validGatewayId!"); assertThat(createThing.getThing().getEntityId()).isEqualTo(expectedThing.getEntityId()); assertThat(createThing.getThing().getPolicyEntityId()).isEqualTo(expectedThing.getPolicyEntityId()); }
@Test public void throwErrorIfHeaderForPlaceholderIsMissing() { underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE, COMMAND_HEADERS)); final Map<String, String> missingEntityHeader = new HashMap<>(); missingEntityHeader.put(HEADER_HONO_DEVICE_ID, DEVICE_ID); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(missingEntityHeader).build(); assertThatExceptionOfType(UnresolvedPlaceholderException.class) .isThrownBy(() -> underTest.map(externalMessage)); }