Skip to content

Commit

Permalink
fixed regression that merge with empty path "/" did no longer work in…
Browse files Browse the repository at this point in the history
… DittoCachingSignalEnrichmentFacade

* also added unit test which failed before and now is green

Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Aug 22, 2022
1 parent bc7ff6d commit 02f8910
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,14 @@ private CompletionStage<JsonObject> handleNextExpectedThingEvents(final SignalEn
private static JsonObject getMergeJsonObject(final JsonValue jsonObject, final ThingEvent<?> thingEvent) {
final var thingMerged = (ThingMerged) thingEvent;
final JsonValue mergedValue = thingMerged.getValue();
final JsonObjectBuilder jsonObjectBuilder = JsonObject.newBuilder();
jsonObjectBuilder.set(thingMerged.getResourcePath(), mergedValue);
final JsonObjectBuilder mergePatchBuilder = JsonFactory.newObject(thingMerged.getResourcePath(), mergedValue)
.toBuilder();
thingMerged.getMetadata()
.ifPresent(metadata -> jsonObjectBuilder.set(
.ifPresent(metadata -> mergePatchBuilder.set(
Thing.JsonFields.METADATA.getPointer().append(thingMerged.getResourcePath()), metadata)
.build());

return JsonFactory.mergeJsonValues(jsonObjectBuilder.build(), jsonObject).asObject();
return JsonFactory.mergeJsonValues(mergePatchBuilder.build(), jsonObject).asObject();
}

private static JsonObject getDeleteJsonObject(final JsonObject jsonObject, final WithResource thingEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,55 @@ public void alreadyLoadedCacheEntryIsReusedForMergedEvent() {
});
}

@Test
public void alreadyLoadedCacheEntryIsReusedForMergedEventOnRootLevel() {
DittoTestSystem.run(this, kit -> {
// GIVEN: SignalEnrichmentFacade.retrievePartialThing()
final SignalEnrichmentFacade underTest =
createSignalEnrichmentFacadeUnderTest(kit, Duration.ofSeconds(10L));
final ThingId thingId = ThingId.generateRandom();
final String userId = ISSUER_PREFIX + "user";
final DittoHeaders headers = DittoHeaders.newBuilder()
.authorizationContext(AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance(userId)))
.randomCorrelationId()
.build();
final CompletionStage<JsonObject> askResult =
underTest.retrievePartialThing(thingId, SELECTOR, headers, THING_EVENT);

// WHEN: Command handler receives expected RetrieveThing and responds with RetrieveThingResponse
final RetrieveThing retrieveThing = kit.expectMsgClass(RetrieveThing.class);
softly.assertThat(retrieveThing.getDittoHeaders().getAuthorizationContext().getAuthorizationSubjectIds())
.contains(userId);
softly.assertThat(retrieveThing.getSelectedFields()).contains(actualSelectedFields(SELECTOR));
// WHEN: response is handled so that it is also added to the cache
kit.reply(RetrieveThingResponse.of(thingId, getThingResponseThingJson(), headers));
askResult.toCompletableFuture().join();
softly.assertThat(askResult).isCompletedWithValue(getExpectedThingJson());

// WHEN: same thing is asked again with same selector for an event with one revision ahead
final ThingMerged mergeAttributes = ThingMerged.of(thingId, JsonPointer.of("/"),
JsonObject.newBuilder()
.set("attributes",
JsonObject.newBuilder()
.set("x", 42)
.set("foo", "bar")
.build())
.build(),
THING_EVENT.getRevision() + 1,
null,
DittoHeaders.empty(),
null
);
final CompletionStage<JsonObject> askResultCached =
underTest.retrievePartialThing(thingId, SELECTOR, headers, mergeAttributes);

// THEN: no cache lookup should be done
kit.expectNoMessage(Duration.ofSeconds(1));
askResultCached.toCompletableFuture().join();
});
}

@Test
public void alreadyLoadedCacheEntryIsInvalidatedForUnexpectedEventRevision() {
DittoTestSystem.run(this, kit -> {
Expand Down

0 comments on commit 02f8910

Please sign in to comment.