/
ThingMongoSnapshotAdapterTest.java
124 lines (105 loc) · 5.12 KB
/
ThingMongoSnapshotAdapterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.things.service.persistence.serializer;
import static org.assertj.core.api.Assertions.within;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Map;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.cluster.pubsub.DistributedPubSubMediator;
import org.apache.pekko.persistence.SnapshotMetadata;
import org.apache.pekko.persistence.SnapshotOffer;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
import org.assertj.core.api.JUnitSoftAssertions;
import org.bson.BsonDocument;
import org.eclipse.ditto.base.api.persistence.PersistenceLifecycle;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.things.api.ThingSnapshotTaken;
import org.eclipse.ditto.things.model.TestConstants;
import org.eclipse.ditto.things.model.Thing;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import com.typesafe.config.ConfigFactory;
/**
* Unit test for {@link ThingMongoSnapshotAdapter}.
*/
public final class ThingMongoSnapshotAdapterTest {
private static final String PERSISTENCE_ID = "thing:fajofj904q2";
private static final SnapshotMetadata SNAPSHOT_METADATA = new SnapshotMetadata(PERSISTENCE_ID, 0, 0);
@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
private ActorSystem system;
private TestProbe pubSubProbe;
private ThingMongoSnapshotAdapter underTest = null;
@Before
public void setUp() {
system = ActorSystem.create();
pubSubProbe = TestProbe.apply(system);
underTest = new ThingMongoSnapshotAdapter(pubSubProbe.ref(), ConfigFactory.parseMap(
Map.of(ThingMongoSnapshotAdapter.THING_SNAPSHOT_TAKEN_EVENT_PUBLISHING_ENABLED, true)
));
}
@After
public void cleanUp() {
if (system != null) {
TestKit.shutdownActorSystem(system);
}
}
@Test
public void toSnapshotStoreFromSnapshotStoreRoundTripV2ReturnsExpected() {
final var thingV2 = TestConstants.Thing.THING_V2;
toSnapshotStoreFromSnapshotStoreRoundTripReturnsExpected(thingV2);
expectSnapshotPublished(thingV2);
}
private void toSnapshotStoreFromSnapshotStoreRoundTripReturnsExpected(final Thing thing) {
final Object rawSnapshotEntity = underTest.toSnapshotStore(thing);
softly.assertThat(rawSnapshotEntity).as("snapshot entity is BSON document").isInstanceOf(BsonDocument.class);
final BsonDocument dbObject = (BsonDocument) rawSnapshotEntity;
final Thing restoredThing = underTest.fromSnapshotStore(new SnapshotOffer(SNAPSHOT_METADATA, dbObject));
softly.assertThat(restoredThing).as("restored Thing").isEqualTo(thing);
}
private void expectSnapshotPublished(final Thing thing) {
final var thingJson = thing.toJson(thing.getImplementedSchemaVersion(), FieldType.regularOrSpecial());
final var timestamp = Instant.now();
final var expectedSnapshotTaken = ThingSnapshotTaken.newBuilder(TestConstants.Thing.THING_ID,
TestConstants.Thing.REVISION_NUMBER,
PersistenceLifecycle.ACTIVE,
thingJson).build();
final var receivedPublish = pubSubProbe.expectMsgClass(DistributedPubSubMediator.Publish.class);
softly.assertThat(receivedPublish.topic())
.as("topic")
.isEqualTo(DistPubSubAccess.getGroupTopic(expectedSnapshotTaken.getPubSubTopic()));
softly.assertThat(receivedPublish.message())
.as("message")
.isInstanceOfSatisfying(ThingSnapshotTaken.class, actualSnapshotTaken -> {
softly.assertThat((CharSequence) actualSnapshotTaken.getEntityId())
.as("entity ID")
.isEqualTo(TestConstants.Thing.THING_ID);
softly.assertThat(actualSnapshotTaken.getRevision())
.as("revision number")
.isEqualTo(TestConstants.Thing.REVISION_NUMBER);
softly.assertThat(actualSnapshotTaken.getTimestamp())
.as("timestamp")
.hasValueSatisfying(actualTimestamp -> softly.assertThat(actualTimestamp)
.isCloseTo(timestamp, within(5, ChronoUnit.SECONDS)));
softly.assertThat(actualSnapshotTaken.getLifecycle())
.as("lifecycle")
.isEqualTo(PersistenceLifecycle.ACTIVE);
softly.assertThat(actualSnapshotTaken.getEntity()).as("entity").hasValue(thingJson);
});
}
}