ThingCreatedStrategy extends AbstractThingEventStrategy<ThingCreated> { @Override public Thing handle(final ThingCreated event, final @Nullable Thing thing, final long revision) { return event.getThing() .toBuilder() .setLifecycle(ThingLifecycle.ACTIVE) .setRevision(revision) .setModified(event.getTimestamp().orElseGet(() -> event.getThing().getModified().orElse(null))) .setCreated(event.getTimestamp().orElseGet(() -> event.getThing().getCreated().orElse(null))) .setMetadata(event.getMetadata().orElseGet(() -> event.getThing().getMetadata().orElse(null))) .build(); }  @Override Thing handle(final ThingCreated event, final @Nullable Thing thing, final long revision);  }
@Test public void appliesEventCorrectly() { final ThingCreatedStrategy strategy = new ThingCreatedStrategy(); final ThingCreated event = ThingCreated.of(THING, REVISION, TIMESTAMP, DittoHeaders.empty(), METADATA); final Thing thingWithEventApplied = strategy.handle(event, null, NEXT_REVISION); final Thing expected = THING.toBuilder() .setLifecycle(ThingLifecycle.ACTIVE) .setRevision(NEXT_REVISION) .setModified(TIMESTAMP) .setCreated(TIMESTAMP) .setMetadata(METADATA) .build(); assertThat(thingWithEventApplied).isEqualTo(expected); }
@Test public void appliesThingCreatedWithMetadata() { final ThingCreatedStrategy strategy = new ThingCreatedStrategy(); final DittoHeaders dittoHeaders = DittoHeaders.newBuilder() .putMetadata(MetadataHeaderKey.of(JsonPointer.of("*/answer")), JsonValue.of(42)) .build(); final JsonObject metadataJson = JsonObject.newBuilder() .set("answer", 42) .build(); final ThingCreated event = ThingCreated.of(THING, REVISION, TIMESTAMP, dittoHeaders, Metadata.newBuilder() .set(Thing.JsonFields.ID.getPointer(), metadataJson) .build()); final Thing thingWithEventApplied = strategy.handle(event, null, NEXT_REVISION); final Thing expected = THING.toBuilder() .setLifecycle(ThingLifecycle.ACTIVE) .setRevision(NEXT_REVISION) .setModified(TIMESTAMP) .setCreated(TIMESTAMP) .setMetadata(Metadata.newBuilder() .set(Thing.JsonFields.ID.getPointer(), metadataJson) .build()) .build(); assertThat(thingWithEventApplied).isEqualTo(expected); }