Skip to content

Commit

Permalink
Issue #106: Added getter for feature desired property pointer and fix…
Browse files Browse the repository at this point in the history
…ed some bugs.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Dec 9, 2021
1 parent 1299b14 commit 6cf438f
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,38 +212,35 @@ String getFeatureIdOrThrow() {
FEATURE_PATH_PREFIX.asPointer()),
adaptable.getDittoHeaders()
);
} else {
return messagePath.get(1)
.map(JsonKey::toString)
.orElseThrow(() -> new IllegalAdaptableException(
"Message path of payload does not contain a feature ID.",
MessageFormat.format("Please ensure that the message path of the payload consists" +
" of two segments, starting with {0}/ and ending with" +
" the feature ID.",
FEATURE_PATH_PREFIX),
adaptable.getDittoHeaders()
)
);
}
return messagePath.getLeaf()
.filter(leaf -> 2 == messagePath.getLevelCount())
.map(JsonKey::toString)
.orElseThrow(() -> new IllegalAdaptableException(
"Message path of payload does not contain a feature ID.",
MessageFormat.format("Please ensure that the message path of the payload consists" +
" of two segments, starting with {0}/ and ending with the feature ID.",
FEATURE_PATH_PREFIX),
adaptable.getDittoHeaders())
);
}

Feature getFeatureOrThrow() {
final Feature result;
Optional<Feature> getFeature() {
final Optional<Feature> result;
final Optional<JsonValue> payloadValueOptional = getPayloadValue();
if (payloadValueOptional.isPresent()) {
final JsonValue jsonValue = payloadValueOptional.get();
if (jsonValue.isObject()) {
result = ThingsModelFactory.newFeatureBuilder(jsonValue.asObject())
result = Optional.of(ThingsModelFactory.newFeatureBuilder(jsonValue.asObject())
.useId(getFeatureIdOrThrow())
.build();
.build());
} else {
throw newPayloadValueNotJsonObjectException(Feature.class, jsonValue);
}
} else {
throw new IllegalAdaptableException(
MessageFormat.format("Payload does not contain a {0} as JSON object" +
" because it has no value at all.",
Feature.class.getSimpleName()),
adaptable.getDittoHeaders()
);
result = Optional.empty();
}
return result;
}
Expand Down Expand Up @@ -306,8 +303,12 @@ Optional<FeatureProperties> getFeatureProperties() {
return result;
}

@SuppressWarnings("java:S3655")
JsonPointer getFeaturePropertyPointerOrThrow() {
return getFeaturePropertyPointerOrThrow(JsonKey.of("properties"));
}

@SuppressWarnings("java:S3655")
private JsonPointer getFeaturePropertyPointerOrThrow(final JsonKey levelTwoKey) {
final Payload payload = adaptable.getPayload();
final MessagePath messagePath = payload.getPath();
if (!messagePath.getRoot().filter(FEATURE_PATH_PREFIX::equals).isPresent()) {
Expand All @@ -324,21 +325,20 @@ JsonPointer getFeaturePropertyPointerOrThrow() {
" the required <{1,number}> levels.",
messagePath.getLevelCount(),
FEATURE_PROPERTY_PATH_LEVEL + 1),
"Please ensure that the message path complies to the schema" +
"Please ensure that the message path complies to schema" +
" \"features/${FEATURE_ID}/properties/${PROPERTY_SUB_PATH_OR_PROPERTY_NAME}\".",
adaptable.getDittoHeaders()
);
}
final JsonKey propertiesKey = JsonKey.of("properties");
final boolean hasExpectedPropertiesSegment = messagePath.get(FEATURE_PROPERTY_PATH_LEVEL - 1)
.filter(propertiesKey::equals)
.filter(levelTwoKey::equals)
.isPresent();
if (!hasExpectedPropertiesSegment) {
throw new IllegalAdaptableException(
MessageFormat.format("Message path of payload is not <{0}> at level <{1,number}>.",
propertiesKey,
levelTwoKey,
FEATURE_PROPERTY_PATH_LEVEL - 1),
"Please ensure that the message path complies to the schema" +
"Please ensure that the message path complies to schema" +
" \"features/${FEATURE_ID}/properties/${PROPERTY_SUB_PATH_OR_PROPERTY_NAME}\".",
adaptable.getDittoHeaders()
);
Expand All @@ -347,6 +347,10 @@ JsonPointer getFeaturePropertyPointerOrThrow() {
}
}

JsonPointer getFeatureDesiredPropertyPointerOrThrow() {
return getFeaturePropertyPointerOrThrow(JsonKey.of("desiredProperties"));
}

Optional<JsonValue> getFeaturePropertyValue() {
return getPayloadValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,20 @@ public void getFeatureIdOrThrowReturnsFeatureIdIfContainedInPayload() {
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThat(underTest.getFeatureIdOrThrow()).isEqualTo(featureId.toString());
assertThat(underTest.getFeatureIdOrThrow()).isEqualTo(featureId);
}

@Test
public void getFeatureIdOrThrowReturnsFeatureIdIfContainedInPayload2() {
final String featureId = "HX711";
final JsonPointer path = JsonPointer.of("features/" + featureId + "/definition");
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withPath(path)
.build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThat(underTest.getFeatureIdOrThrow()).isEqualTo(featureId);
}

@Test
Expand All @@ -366,8 +379,8 @@ public void getFeatureIdOrThrowThrowsExceptionIfPayloadHasPathWithUnexpectedPref
}

@Test
public void getFeatureIdOrThrowThrowsExceptionIfPayloadHasPathWithTooManySegments() {
final JsonPointer path = JsonPointer.of("features/unexpected/HX711");
public void getFeatureIdOrThrowThrowsExceptionIfPayloadHasPathWithTooLessSegments() {
final JsonPointer path = JsonPointer.of("features");
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withPath(path)
.build();
Expand All @@ -381,7 +394,7 @@ public void getFeatureIdOrThrowThrowsExceptionIfPayloadHasPathWithTooManySegment
}

@Test
public void getFeatureOrThrowReturnsFeatureIfContainedInPayload() {
public void getFeatureReturnsExpectedFeatureIfContainedInPayload() {
final Feature feature = Feature.newBuilder()
.properties(FeatureProperties.newBuilder()
.set("uint8_t HX711_DT[3]", "{A1, 11, 7}")
Expand All @@ -395,19 +408,16 @@ public void getFeatureOrThrowReturnsFeatureIfContainedInPayload() {
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThat(underTest.getFeatureOrThrow()).isEqualTo(feature);
assertThat(underTest.getFeature()).contains(feature);
}

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

assertThatExceptionOfType(IllegalAdaptableException.class)
.isThrownBy(underTest::getFeatureOrThrow)
.withMessage("Payload does not contain a Feature as JSON object because it has no value at all.")
.withNoCause();
assertThat(underTest.getFeature()).isEmpty();
}

@Test
Expand All @@ -422,13 +432,13 @@ public void getFeatureOrThrowThrowsExceptionIfPayloadContainsNoFeatureJsonObject
final MappingContext underTest = MappingContext.of(adaptable);

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

@Test
public void getFeatureOrThrowThrowsExceptionIfMessagePathProvidesNoFeatureId() {
public void getFeatureThrowsExceptionIfMessagePathProvidesNoFeatureId() {
final Feature feature = Feature.newBuilder()
.properties(FeatureProperties.newBuilder()
.set("uint8_t HX711_DT[3]", "{A1, 11, 7}")
Expand All @@ -443,7 +453,7 @@ public void getFeatureOrThrowThrowsExceptionIfMessagePathProvidesNoFeatureId() {
final MappingContext underTest = MappingContext.of(adaptable);

assertThatExceptionOfType(IllegalAdaptableException.class)
.isThrownBy(underTest::getFeatureOrThrow)
.isThrownBy(underTest::getFeature)
.withMessageStartingWith("Message path of payload does not start with")
.withNoCause();
}
Expand Down Expand Up @@ -604,6 +614,66 @@ public void getFeaturePropertyPointerOrThrowThrowsExceptionIfMessagePathHasUnexp
.withNoCause();
}

@Test
public void getFeatureDesiredPropertyPointerOrThrowThrowsExceptionIfMessagePathHasNoPropertiesSegment() {
final JsonPointer path = JsonPointer.of("features/HX711/foo/uint8_t HX711_DT[3]");
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withPath(path)
.build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThatExceptionOfType(IllegalAdaptableException.class)
.isThrownBy(underTest::getFeatureDesiredPropertyPointerOrThrow)
.withMessage("Message path of payload is not <desiredProperties> at level <2>.")
.withNoCause();
}

@Test
public void getFeatureDesiredPropertyPointerOrThrowReturnsPointerIfMessagePathHasAppropriatePrefix() {
final JsonPointer path = JsonPointer.of("features/HX711/desiredProperties/uint8_t HX711_DT[3]");
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withPath(path)
.build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

final JsonPointer featurePropertyPointer = underTest.getFeatureDesiredPropertyPointerOrThrow();

assertThat((CharSequence) featurePropertyPointer).isEqualTo(path.getSubPointer(3).get());
}

@Test
public void getFeatureDesiredPropertyPointerOrThrowThrowsExceptionIfMessagePathHasAnInappropriatePrefix() {
final JsonPointer path = JsonPointer.of("rfeatures/HX711/desiredProperties/uint8_t HX711_DT[3]");
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withPath(path)
.build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThatExceptionOfType(IllegalAdaptableException.class)
.isThrownBy(underTest::getFeatureDesiredPropertyPointerOrThrow)
.withMessage("Message path of payload does not start with <%s>.", "/features")
.withNoCause();
}

@Test
public void getFeatureDesiredPropertyPointerOrThrowThrowsExceptionIfMessagePathHasUnexpectedLevelCount() {
final JsonPointer path = JsonPointer.of("features/HX711/uint8_t HX711_DT[3]");
final Payload payload = ProtocolFactory.newPayloadBuilder()
.withPath(path)
.build();
Mockito.when(adaptable.getPayload()).thenReturn(payload);
final MappingContext underTest = MappingContext.of(adaptable);

assertThatExceptionOfType(IllegalAdaptableException.class)
.isThrownBy(underTest::getFeatureDesiredPropertyPointerOrThrow)
.withMessage("Message path of payload has <%d> levels which is less than the required <4> levels.",
path.getLevelCount())
.withNoCause();
}

@Test
public void getFeaturePropertyPointerOrThrowThrowsExceptionIfMessagePathHasNoPropertiesSegment() {
final JsonPointer path = JsonPointer.of("features/HX711/foo/uint8_t HX711_DT[3]");
Expand Down

0 comments on commit 6cf438f

Please sign in to comment.