Skip to content

Commit

Permalink
fix some javadoc, unignore and adapt unit test to changed behavior
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 Dec 10, 2021
1 parent a2d8588 commit 3f0a71f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private Result<ThingEvent<?>> applyModifyCommand(final Context<ThingId> context,
// required e.g. for updating the search-index)
final DittoHeaders dittoHeaders = command.getDittoHeaders();

final Thing modifiedThing = mergeThingModifications(command.getThing(), thing, eventTs, nextRevision);
final Thing modifiedThing = applyThingModifications(command.getThing(), thing, eventTs, nextRevision);

final ThingEvent<?> event =
ThingModified.of(modifiedThing, nextRevision, eventTs, dittoHeaders, metadata);
Expand All @@ -104,18 +104,16 @@ private Result<ThingEvent<?>> applyModifyCommand(final Context<ThingId> context,
}

/**
* Merges the modifications from {@code thingWithModifications} to {@code builder}. Merge is implemented very
* simple: All first level fields of {@code thingWithModifications} overwrite the first level fields of {@code
* builder}. If a field does not exist in {@code thingWithModifications}, a maybe existing field in {@code builder}
* remains unchanged.
* Applies the modifications from {@code thingWithModifications} to {@code builder}. The modified thing
* overwrites the existing thing of {@code builder}.
*
* @param thingWithModifications the thing containing the modifications.
* @param existingThing the existing thing to merge into.
* @param existingThing the existing thing.
* @param eventTs the timestamp of the modification event.
* @param nextRevision the next revision number.
* @return the merged Thing.
* @return the modified Thing.
*/
private static Thing mergeThingModifications(final Thing thingWithModifications, final Thing existingThing,
private static Thing applyThingModifications(final Thing thingWithModifications, final Thing existingThing,
final Instant eventTs, final long nextRevision) {

final ThingBuilder.FromCopy builder = existingThing.toBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ protected ThingModifiedStrategy() {
}

/**
* Merges the modifications from {@code thingWithModifications} to {@code thingBuilder}.
* Merge is implemented very simple: All first level fields of {@code thingWithModifications} overwrite the first
* level fields of {@code thingBuilder}.
* If a field does not exist in the event's Thing, a maybe existing field in {@code thingBuilder} remains
* unchanged.
* Applies the modifications from {@code thingWithModifications} to {@code thingBuilder} by overwriting the
* existing thing with the modified thing.
*/
@Override
protected ThingBuilder.FromCopy applyEvent(final ThingModified event, final ThingBuilder.FromCopy thingBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public void modifyThingWithoutPreviousCreate() {
}

@Test
public void modifyThingKeepsOverwritesExistingFirstLevelFieldsWhenExplicitlySpecifiedV2() {
public void modifyThingOverwritesExistingFirstLevelFieldsWhenExplicitlySpecifiedV2() {
final Thing thingWithFirstLevelFields = createThingV2WithRandomId();
final Thing thingWithDifferentFirstLevelFields = Thing.newBuilder()
.setId(getIdOrThrow(thingWithFirstLevelFields))
Expand Down Expand Up @@ -379,26 +379,28 @@ private void doTestModifyThingKeepsOverwritesExistingFirstLevelFieldsWhenExplici
}

@Test
@Ignore("Since Ditto 2.2.0, this is no longer the case - ModifyThing overwrites and does no longer merge on top-level")
public void modifyThingKeepsAlreadyExistingFirstLevelFieldsWhenNotExplicitlyOverwrittenV2() {
public void modifyThingOverwritesExistingFirstLevelFieldsWhenNotExplicitlySpecifiedV2() {
final Thing thingWithFirstLevelFields = createThingV2WithRandomId();
doTestModifyThingKeepsAlreadyExistingFirstLevelFieldsWhenNotExplicitlyOverwritten(thingWithFirstLevelFields,
doTestModifyThingOverwritesExistingFirstLevelFieldsWhenNotExplicitlySpecified(thingWithFirstLevelFields,
dittoHeadersV2);
}

private void doTestModifyThingKeepsAlreadyExistingFirstLevelFieldsWhenNotExplicitlyOverwritten(
private void doTestModifyThingOverwritesExistingFirstLevelFieldsWhenNotExplicitlySpecified(
final Thing thingWithFirstLevelFields, final DittoHeaders dittoHeaders) {

final ThingId thingId = getIdOrThrow(thingWithFirstLevelFields);

final Thing minimalThing = Thing.newBuilder()
.setId(thingId)
.setPolicyId(PolicyId.of(thingId))
.build();

final Thing expectedThing = minimalThing.toBuilder().setPolicyId(PolicyId.of(thingId)).build();

final ModifyThing modifyThingCommand = ModifyThing.of(thingId, minimalThing, null, dittoHeaders);

new TestKit(actorSystem) {
{
final TestKit pubSub = new TestKit(actorSystem);
final ActorRef underTest = createPersistenceActorFor(thingWithFirstLevelFields);

final CreateThing createThing = CreateThing.of(thingWithFirstLevelFields, null, dittoHeaders);
Expand All @@ -407,25 +409,21 @@ private void doTestModifyThingKeepsAlreadyExistingFirstLevelFieldsWhenNotExplici
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thingWithFirstLevelFields);

assertPublishEvent(ThingCreated.of(thingWithFirstLevelFields, 1L, TIMESTAMP, dittoHeaders,
null));
assertPublishEvent(ThingCreated.of(thingWithFirstLevelFields, 1L, TIMESTAMP, dittoHeaders, null));

underTest.tell(modifyThingCommand, getRef());

expectMsgEquals(
ETagTestUtils.modifyThingResponse(thingWithFirstLevelFields, minimalThing, dittoHeaders, false));
expectMsgEquals(ETagTestUtils.modifyThingResponse(thingWithFirstLevelFields, minimalThing, dittoHeaders, false));

// we expect that in the Event the minimalThing was merged with thingWithFirstLevelFields:
assertPublishEvent(ThingModified.of(thingWithFirstLevelFields, 2L, TIMESTAMP, dittoHeaders,
null));
assertPublishEvent(ThingModified.of(expectedThing, 2L, TIMESTAMP, dittoHeaders, null));

final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, dittoHeaders)
.withSelectedFields(ALL_FIELDS_SELECTOR)
.build();
underTest.tell(retrieveThing, getRef());

final RetrieveThingResponse retrieveThingResponse = expectMsgClass(RetrieveThingResponse.class);
assertThingInResponse(retrieveThingResponse.getThing(), thingWithFirstLevelFields);
assertThingInResponse(retrieveThingResponse.getThing(), expectedThing);
}
};
}
Expand Down

0 comments on commit 3f0a71f

Please sign in to comment.