Skip to content

Commit

Permalink
fix error mapping in ImplicitThingCreationMessageMapper, add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Guggemos <dominik.guggemos@bosch.io>
  • Loading branch information
dguggemos committed Nov 9, 2020
1 parent d342a2e commit e9e4ad5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,12 @@ private static String getCopyPolicyFrom(final JsonObject thingJson) {
@Override
public List<ExternalMessage> map(final Adaptable adaptable) {
if (TopicPath.Criterion.ERRORS.equals(adaptable.getTopicPath().getCriterion())) {
throw GlobalErrorRegistry.getInstance().parse(adaptable.getPayload().toJson(), adaptable.getDittoHeaders());
adaptable.getPayload().getValue().filter(JsonValue::isObject).map(JsonValue::asObject)
.ifPresent(jsonObject -> {
throw GlobalErrorRegistry.getInstance().parse(jsonObject,
adaptable.getDittoHeaders());
});
}

return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.headers.DittoHeaderDefinition;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.MessageMapperConfigurationInvalidException;
import org.eclipse.ditto.model.placeholders.UnresolvedPlaceholderException;
import org.eclipse.ditto.model.policies.PoliciesResourceType;
import org.eclipse.ditto.model.policies.Policy;
import org.eclipse.ditto.model.policies.SubjectIssuer;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingId;
import org.eclipse.ditto.model.things.ThingsModelFactory;
import org.eclipse.ditto.protocoladapter.Adaptable;
import org.eclipse.ditto.protocoladapter.DittoProtocolAdapter;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.services.models.connectivity.ExternalMessageFactory;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.things.ThingErrorResponse;
import org.eclipse.ditto.signals.commands.things.exceptions.ThingConflictException;
import org.eclipse.ditto.signals.commands.things.modify.CreateThing;
import org.eclipse.ditto.signals.commands.things.modify.CreateThingResponse;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -282,6 +287,27 @@ public void throwErrorIfHeaderForPlaceholderIsMissing() {
.isThrownBy(() -> underTest.map(externalMessage));
}

@Test
public void throwExceptionOnErrorResponse() {
underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE, COMMAND_HEADERS));
final ThingConflictException conflictException =
ThingConflictException.newBuilder(ThingId.generateRandom()).build();
final Signal<?> thingErrorResponse = ThingErrorResponse.of(conflictException);
final Adaptable errorAdaptable = DittoProtocolAdapter.newInstance().toAdaptable(thingErrorResponse);
assertThatExceptionOfType(ThingConflictException.class)
.isThrownBy(() -> underTest.map(errorAdaptable));
}

@Test
public void regularCommandResponsesAreNotMapped() {
underTest.configure(mappingConfig, createMapperConfig(THING_TEMPLATE, COMMAND_HEADERS));
final Signal<?> thingResponse =
CreateThingResponse.of(Thing.newBuilder().setId(ThingId.generateRandom()).build(),
DittoHeaders.empty());
final Adaptable adaptable = DittoProtocolAdapter.newInstance().toAdaptable(thingResponse);
assertThat(underTest.map(adaptable)).isEmpty();
}

private static Signal<?> getFirstMappedSignal(final List<Adaptable> mappingResult) {
return mappingResult.stream().findFirst()
.map(DITTO_PROTOCOL_ADAPTER::fromAdaptable)
Expand Down

0 comments on commit e9e4ad5

Please sign in to comment.