Skip to content

Commit

Permalink
Issue #106: Added getters for optional thing and policy ID to Mapping…
Browse files Browse the repository at this point in the history
…Context.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Dec 8, 2021
1 parent 3f25ee5 commit b386630
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.MessagePath;
import org.eclipse.ditto.protocol.Payload;
Expand Down Expand Up @@ -84,14 +85,14 @@ ThingId getThingId() {
return ThingId.of(topicPath.getNamespace(), topicPath.getEntityName());
}

Thing getThingOrThrow() {
final Thing result;
Optional<Thing> getThing() {
final Optional<Thing> result;
final Payload payload = adaptable.getPayload();
final Optional<JsonValue> payloadValueOptional = payload.getValue();
if (payloadValueOptional.isPresent()) {
final JsonValue jsonValue = payloadValueOptional.get();
if (jsonValue.isObject()) {
result = ThingsModelFactory.newThing(jsonValue.asObject());
result = Optional.of(ThingsModelFactory.newThing(jsonValue.asObject()));
} else {
throw new IllegalAdaptableException(
MessageFormat.format("Payload value is not a Thing as JSON object but <{0}>.", jsonValue),
Expand All @@ -100,16 +101,21 @@ Thing getThingOrThrow() {
);
}
} else {
throw new IllegalAdaptableException(
MessageFormat.format("Payload does not contain a {0} as JSON object" +
" because it has no value at all.",
Thing.class.getSimpleName()),
adaptable.getDittoHeaders()
);
result = Optional.empty();
}
return result;
}

Thing getThingOrThrow() {
return getThing().orElseThrow(() ->
new IllegalAdaptableException(
MessageFormat.format("Payload does not contain a {0} as JSON object" +
" because it has no value at all.",
Thing.class.getSimpleName()),
adaptable.getDittoHeaders()
));
}

JsonPointer getAttributePointerOrThrow() {
final Payload payload = adaptable.getPayload();
final MessagePath messagePath = payload.getPath();
Expand Down Expand Up @@ -256,6 +262,28 @@ Optional<ThingDefinition> getThingDefinition() {
return result;
}

Optional<PolicyId> getPolicyId() {
final Optional<PolicyId> result;
final Payload payload = adaptable.getPayload();
final Optional<JsonValue> payloadValueOptional = payload.getValue();
if (payloadValueOptional.isPresent()) {
final JsonValue jsonValue = payloadValueOptional.get();
if (jsonValue.isString()) {
result = Optional.of(PolicyId.of(jsonValue.asString()));
} else {
throw new IllegalAdaptableException(
MessageFormat.format("Payload value is not a {0} as JSON string but <{1}>.",
PolicyId.class.getSimpleName(),
jsonValue),
adaptable.getDittoHeaders()
);
}
} else {
result = Optional.empty();
}
return result;
}

@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.Payload;
import org.eclipse.ditto.protocol.ProtocolFactory;
Expand Down Expand Up @@ -482,4 +483,41 @@ public void getThingDefinitionReturnsEmptyOptionalIfPayloadContainsNoValue() {
assertThat(underTest.getThingDefinition()).isEmpty();
}

@Test
public void getPolicyIdReturnsPolicyIfContainedInPayload() {
final PolicyId policyId = PolicyId.inNamespaceWithRandomName("org.ditto");
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withValue(JsonValue.of(policyId.toString()))
.build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThat(underTest.getPolicyId()).contains(policyId);
}

@Test
public void getPolicyIdThrowsExceptionIfPayloadContainsNoJsonStringValue() {
final JsonValue jsonValue = JsonValue.of(true);
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withValue(jsonValue)
.build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThatExceptionOfType(IllegalAdaptableException.class)
.isThrownBy(underTest::getPolicyId)
.withMessage("Payload value is not a PolicyId as JSON string but <%s>.", jsonValue)
.withNoCause();
}

@Test
public void getPolicyIdReturnsEmptyOptionalIfPayloadContainsNoValue() {
final JsonValue jsonValue = JsonValue.of(true);
final Payload payload = ProtocolFactory.newPayloadBuilder().build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThat(underTest.getPolicyId()).isEmpty();
}

}

0 comments on commit b386630

Please sign in to comment.