Skip to content

Commit

Permalink
eclipse-ditto#760 fixed unit test
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 Aug 24, 2020
1 parent 63a15d6 commit fd26ddb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.services.utils.akka.logging.DittoLogger;
import org.eclipse.ditto.services.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.things.modify.CreateThing;

/**
Expand All @@ -53,7 +54,7 @@
)
public class ImplicitThingCreationMessageMapper extends AbstractMessageMapper {

private static final DittoLogger LOGGER = DittoLoggerFactory.getLogger(ConnectionStatusMessageMapper.class);
private static final DittoLogger LOGGER = DittoLoggerFactory.getLogger(ImplicitThingCreationMessageMapper.class);

private static final List<Adaptable> EMPTY_RESULT = Collections.emptyList();
private static final DittoProtocolAdapter DITTO_PROTOCOL_ADAPTER = DittoProtocolAdapter.newInstance();
Expand Down Expand Up @@ -83,9 +84,7 @@ protected void doConfigure(final MappingConfig mappingConfig, final MessageMappe

// Do not throw an exception because policyId is not required in mapping config. But still needs to be valid if
// given.
final JsonValue policyId =
thingTemplate.getField(POLICY_ID).isPresent() ?
thingTemplate.getField(POLICY_ID).get().getValue() : thingId;
final JsonValue policyId = thingTemplate.getValue(POLICY_ID).orElse(thingId);

try {

Expand Down Expand Up @@ -130,7 +129,7 @@ private List<Adaptable> doMap(final ExternalMessage externalMessage) {

final Thing newThing = ThingsModelFactory.newThing(mappingOptionThingTemplate);

final CreateThing createThing = CreateThing.of(newThing, null, externalMessage.getInternalHeaders());
final Signal<CreateThing> createThing = CreateThing.of(newThing, null, externalMessage.getInternalHeaders());

final Adaptable adaptable = DITTO_PROTOCOL_ADAPTER.toAdaptable(createThing);

Expand Down
Expand Up @@ -15,18 +15,20 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.connectivity.MessageMapperConfigurationInvalidException;
import org.eclipse.ditto.model.placeholders.UnresolvedPlaceholderException;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingsModelFactory;
import org.eclipse.ditto.protocoladapter.Adaptable;
import org.eclipse.ditto.protocoladapter.Payload;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.services.models.connectivity.ExternalMessageFactory;
import org.junit.Before;
Expand Down Expand Up @@ -64,7 +66,7 @@ public void doForwardMappingContextWithSubstitutedPlaceholders() {

final Map<String, String> headers = createValidHeaders();

underTest.configure(mappingConfig, createMapperConfig(null, null));
underTest.configure(mappingConfig, createMapperConfig(null));

final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers).build();

Expand All @@ -73,10 +75,9 @@ public void doForwardMappingContextWithSubstitutedPlaceholders() {
final Thing expectedThing =
createExpectedThing("headerNamespace:headerDeviceId", "headerNamespace:headerEntityId");

assertThat(mappingResult.get(0).getPayload().getValue().isPresent()).isEqualTo(true);
assertThat(mappingResult.get(0).getPayload().getValue()).isPresent();

final Thing mappedThing =
ThingsModelFactory.newThing(mappingResult.get(0).getPayload().getValue().get().toString());
final Thing mappedThing = getMappedThing(mappingResult);

assertThat(mappedThing.getEntityId())
.isEqualTo(expectedThing.getEntityId());
Expand All @@ -88,7 +89,7 @@ public void doForwardMappingContextWithSubstitutedPlaceholders() {
@Test
public void throwErrorIfMappingConfigIsMissing() {

final DefaultMessageMapperConfiguration invalidMapperConfig =createMapperConfig("{}", null);
final DefaultMessageMapperConfiguration invalidMapperConfig = createMapperConfig("{}");

assertThatExceptionOfType(MessageMapperConfigurationInvalidException.class)
.isThrownBy(() -> underTest.configure(mappingConfig, invalidMapperConfig));
Expand All @@ -101,31 +102,32 @@ public void throwErrorIfThingIdIsMissingInConfig() {
"\"policyId\": \"{{ header:entity_id }}\"" +
"}";

final DefaultMessageMapperConfiguration invalidMapperConfig = createMapperConfig(thingMissing, null);
final DefaultMessageMapperConfiguration invalidMapperConfig = createMapperConfig(thingMissing);

assertThatExceptionOfType(MessageMapperConfigurationInvalidException.class)
.isThrownBy(() -> underTest.configure(mappingConfig, invalidMapperConfig));
}

@Test // TODO: resolve test
@Test
public void throwErrorIfHeaderForPlaceholderIsMissing() {
underTest.configure(mappingConfig, createMapperConfig(null, null));
underTest.configure(mappingConfig, createMapperConfig(null));

final Map<String, String> missingEntityHeader = new HashMap<>();
missingEntityHeader.put(HEADER_HONO_DEVICE_ID, "headerNamespace:headerDeviceId");

final ExternalMessage externalMessage =
ExternalMessageFactory.newExternalMessageBuilder(missingEntityHeader).build();

assertThatExceptionOfType(UnresolvedPlaceholderException.class)
.isThrownBy(() -> underTest.map(externalMessage));
final List<Adaptable> mappingResult = underTest.map(externalMessage);

assertThat(mappingResult).isEmpty();
}

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

underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE_WITHOUT_PLACEHOLDERS, null));
underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE_WITHOUT_PLACEHOLDERS));

final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(headers).build();

Expand All @@ -134,8 +136,7 @@ public void doForwardEvenWithoutAnyPlaceholders() {
final Thing expectedThing =
createExpectedThing("some:validThingId!", "some:validPolicyId!");

final Thing mappedThing =
ThingsModelFactory.newThing(mappingResult.get(0).getPayload().getValue().get().toString());
final Thing mappedThing = getMappedThing(mappingResult);

assertThat(mappedThing.getEntityId())
.isEqualTo(expectedThing.getEntityId());
Expand All @@ -144,6 +145,15 @@ public void doForwardEvenWithoutAnyPlaceholders() {
.isEqualTo(expectedThing.getPolicyEntityId());
}

private static Thing getMappedThing(final List<Adaptable> mappingResult) {
return mappingResult.stream().findFirst()
.map(Adaptable::getPayload)
.flatMap(Payload::getValue)
.map(JsonValue::asObject)
.map(ThingsModelFactory::newThing)
.orElseGet(() -> fail("Mapping Result did not contain a Thing."));
}

private Map<String, String> createValidHeaders() {
final Map<String, String> validHeader = new HashMap<>();
validHeader.put(HEADER_HONO_DEVICE_ID, "headerNamespace:headerDeviceId");
Expand All @@ -158,14 +168,13 @@ private Thing createExpectedThing(final String thingId, final String policyId) {
"}");
}

private DefaultMessageMapperConfiguration createMapperConfig(@Nullable String customTemplate,
@Nullable String customId) {
private DefaultMessageMapperConfiguration createMapperConfig(@Nullable String customTemplate) {
final Map<String, String> configPropsWithoutPolicyId = new HashMap<>();

configPropsWithoutPolicyId.put(ImplicitThingCreationMessageMapper.THING_TEMPLATE,
customTemplate != null ? customTemplate : THING_TEMPLATE);

return DefaultMessageMapperConfiguration.of(customId != null ? customId : "valid", configPropsWithoutPolicyId);
return DefaultMessageMapperConfiguration.of("valid", configPropsWithoutPolicyId);
}

}

0 comments on commit fd26ddb

Please sign in to comment.