ConnectionStatusMessageMapper extends AbstractMessageMapper { @Override public List<Adaptable> map(final ExternalMessage externalMessage) { try { return doMap(externalMessage); } catch (final Exception e) { LOGGER.withCorrelationId(externalMessage.getInternalHeaders()) .info("Error occurred during mapping: <{}>: {}", e.getClass().getSimpleName(), e.getMessage()); return EMPTY_RESULT; } }  @Override void doConfigure(final MappingConfig mappingConfig,
            final MessageMapperConfiguration messageMapperConfiguration); @Override List<Adaptable> map(final ExternalMessage externalMessage); @Override List<ExternalMessage> map(final Adaptable adaptable);  }
@Test public void doForwardMapWithValidUseCase() { underTest.configure(mappingConfig, validMapperConfig); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isNotEmpty(); final Adaptable adaptable = mappingResult.get(0); final Signal<?> signal = DittoProtocolAdapter.newInstance().fromAdaptable(adaptable); assertThat(signal).isInstanceOf(ModifyFeature.class); final ModifyFeature modifyFeature = (ModifyFeature) signal; assertThat(getDefinitionIdentifier(modifyFeature)).contains(ConnectionStatusMessageMapper.FEATURE_DEFINITION); assertThat(extractProperty(modifyFeature, FEATURE_PROPERTY_READY_SINCE)).contains( JsonValue.of(CREATION_TIME.toString())); assertThat(extractProperty(modifyFeature, FEATURE_PROPERTY_READY_UNTIL)).contains( JsonValue.of(CREATION_TIME.plusSeconds(Long.parseLong(TTD_STR)).toString())); }
@Test public void doForwardMapWithValidUseCaseTtdZero() { underTest.configure(mappingConfig, validMapperConfig); validHeader.put(HEADER_HONO_TTD, "0"); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isNotEmpty(); final Adaptable adaptable = mappingResult.get(0); final Signal<?> signal = DittoProtocolAdapter.newInstance().fromAdaptable(adaptable); assertThat(signal).isInstanceOf(ModifyFeatureProperty.class); final ModifyFeatureProperty modifyFeatureProperty = (ModifyFeatureProperty) signal; assertThat(modifyFeatureProperty.getPropertyValue()) .isEqualTo(JsonValue.of(CREATION_TIME.toString())); }
@Test public void doForwardMapWithValidUseCaseTtdMinusOne() { underTest.configure(mappingConfig, validMapperConfig); validHeader.put(HEADER_HONO_TTD, "-1"); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); final Adaptable adaptable = mappingResult.get(0); final Signal<?> signal = DITTO_PROTOCOL_ADAPTER.fromAdaptable(adaptable); assertThat(signal).isInstanceOf(ModifyFeature.class); final ModifyFeature modifyFeature = (ModifyFeature) signal; assertThat(getDefinitionIdentifier(modifyFeature)).contains(ConnectionStatusMessageMapper.FEATURE_DEFINITION); assertThat(extractProperty(modifyFeature, FEATURE_PROPERTY_READY_SINCE)).contains( JsonValue.of(CREATION_TIME.toString())); assertThat(extractProperty(modifyFeature, FEATURE_PROPERTY_READY_UNTIL)).contains( JsonValue.of(EXPECTED_READY_UNTIL_IN_DISTANT_FUTURE)); }
@Test public void doForwardMapWithMissingHeaderTTD() { underTest.configure(mappingConfig, validMapperConfig); final Map<String, String> invalidHeader = validHeader; invalidHeader.remove(HEADER_HONO_TTD); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(invalidHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isEmpty(); }
@Test public void doForwardMapWithMissingHeaderCreationTime() { underTest.configure(mappingConfig, validMapperConfig); final Map<String, String> invalidHeader = validHeader; invalidHeader.remove(HEADER_HONO_CREATION_TIME); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(invalidHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isEmpty(); }
@Test public void doForwardMapWithMissingHeader() { final Map<String, JsonValue> props = Map.of(MAPPING_OPTIONS_PROPERTIES_THING_ID, JsonValue.of("{{ header:thing-id }}")); final MessageMapperConfiguration thingIdWithPlaceholder = DefaultMessageMapperConfiguration.of("placeholder", props, validConditions, validConditions); underTest.configure(mappingConfig, thingIdWithPlaceholder); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isEmpty(); }
@Test public void doForwardMapWithInvalidHeaderTTD() { underTest.configure(mappingConfig, validMapperConfig); final Map<String, String> invalidHeader = validHeader; invalidHeader.replace(HEADER_HONO_TTD, "Invalid Value"); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(invalidHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isEmpty(); }
@Test public void doForwardMapWithInvalidHeaderCreationTime() { underTest.configure(mappingConfig, validMapperConfig); final Map<String, String> invalidHeader = validHeader; invalidHeader.replace(HEADER_HONO_CREATION_TIME, "Invalid Value"); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(invalidHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isEmpty(); }
@Test public void doForwardMapWithInvalidTTDValue() { underTest.configure(mappingConfig, validMapperConfig); final Map<String, String> invalidHeader = validHeader; final String invalidTTDValue = "-5625"; invalidHeader.replace(HEADER_HONO_TTD, invalidTTDValue); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(invalidHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isEmpty(); }
@Test public void doForwardMappingContextWithoutFeatureId() { underTest.configure(mappingConfig, validMapperConfig); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult.get(0).getPayload().getPath().getFeatureId()) .contains(DEFAULT_FEATURE_ID); }
@Test public void doForwardMappingContextWithIndividualFeatureId() { final String individualFeatureId = "individualFeatureId"; final Map<String, JsonValue> props = new HashMap<>(validConfigProps); props.put(MAPPING_OPTIONS_PROPERTIES_FEATURE_ID, JsonValue.of(individualFeatureId)); final MessageMapperConfiguration individualFeatureIdConfig = DefaultMessageMapperConfiguration.of("placeholder", props, validConditions, validConditions); underTest.configure(mappingConfig, individualFeatureIdConfig); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult.get(0).getPayload().getPath().getFeatureId()) .contains(individualFeatureId); }
@Test public void doForwardMappingContextWithThingIdMapping() { final Map<String, JsonValue> props = Map.of(MAPPING_OPTIONS_PROPERTIES_THING_ID, JsonValue.of("{{ header:device_id }}")); final MessageMapperConfiguration thingIdWithPlaceholder = DefaultMessageMapperConfiguration.of("placeholder", props, validConditions, validConditions); underTest.configure(mappingConfig, thingIdWithPlaceholder); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult.get(0).getTopicPath().getId()) .isEqualTo(ThingId.of(validHeader.get(HEADER_HONO_DEVICE_ID)).getName()); }
@Test public void doForwardMappingContextWithWrongThingIdMapping() { final Map<String, JsonValue> props = Map.of(MAPPING_OPTIONS_PROPERTIES_THING_ID, JsonValue.of("{{ header:device_id }}")); final MessageMapperConfiguration thingIdWithPlaceholder = DefaultMessageMapperConfiguration.of("placeholder", props, validConditions, validConditions); underTest.configure(mappingConfig, thingIdWithPlaceholder); final Map<String, String> invalidHeader = validHeader; invalidHeader.replace(HEADER_HONO_DEVICE_ID, "Invalid Value"); final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(validHeader).build(); final List<Adaptable> mappingResult = underTest.map(externalMessage); assertThat(mappingResult).isEmpty(); }