-
Notifications
You must be signed in to change notification settings - Fork 214
/
ThingSnapshotTakenTest.java
199 lines (172 loc) · 8.69 KB
/
ThingSnapshotTakenTest.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (c) 2021 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.api;
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat;
import static org.mutabilitydetector.unittesting.AllowedReason.provided;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.base.api.persistence.PersistenceLifecycle;
import org.eclipse.ditto.base.api.persistence.SnapshotTaken;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.base.model.signals.events.EventsourcedEvent;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import nl.jqno.equalsverifier.EqualsVerifier;
/**
* Unit tests for {@link ThingSnapshotTaken}.
*/
public final class ThingSnapshotTakenTest {
private static final PolicyId POLICY_ID = TestConstants.Thing.POLICY_ID;
private static final long REVISION = 23L;
private static Instant timestamp;
private static JsonObject thingJson;
@Rule
public final TestName testName = new TestName();
@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
@BeforeClass
public static void beforeClass() {
final var localDateTime = LocalDateTime.of(2021, Month.MARCH, 9, 10, 31, 50);
timestamp = localDateTime.toInstant(ZoneOffset.UTC);
thingJson = TestConstants.Thing.THING.toJson();
}
@Test
public void assertImmutability() {
assertInstancesOf(ThingSnapshotTaken.class, areImmutable(), provided(PolicyId.class).isAlsoImmutable());
}
@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(ThingSnapshotTaken.class)
.usingGetClass()
.withRedefinedSuperclass()
.verify();
}
@Test
public void gettersReturnExpected() {
final var lifecycle = PersistenceLifecycle.ACTIVE;
final var dittoHeaders = getDittoHeadersWithCorrelationId();
final var underTest = ThingSnapshotTaken.newBuilder(TestConstants.Thing.THING_ID, REVISION, lifecycle, thingJson)
.policyId(POLICY_ID)
.timestamp(timestamp)
.dittoHeaders(dittoHeaders)
.build();
softly.assertThat((CharSequence) underTest.getEntityId()).as("entity ID").isEqualTo(TestConstants.Thing.THING_ID);
softly.assertThat(underTest.getPolicyId()).as("policy ID").hasValue(POLICY_ID);
softly.assertThat(underTest.getLifecycle()).as("lifecycle").isEqualTo(lifecycle);
softly.assertThat(underTest.getPubSubTopic())
.as("pub-sub topic")
.isEqualTo(ThingSnapshotTaken.PUB_SUB_TOPIC);
softly.assertThat(underTest.getEntity()).as("entity").hasValue(thingJson);
softly.assertThat(underTest.getRevision()).as("revision").isEqualTo(REVISION);
softly.assertThat(underTest.getType()).as("type").isEqualTo(ThingSnapshotTaken.TYPE);
softly.assertThat(underTest.getManifest()).as("manifest").isEqualTo(underTest.getType());
softly.assertThat(underTest.getTimestamp()).as("timestamp").hasValue(timestamp);
softly.assertThat(underTest.getMetadata()).as("metadata").isEmpty();
softly.assertThat(underTest.getDittoHeaders()).as("Ditto headers").isEqualTo(dittoHeaders);
softly.assertThat((CharSequence) underTest.getResourcePath())
.as("resource path")
.isEqualTo(JsonPointer.empty());
softly.assertThat(underTest.getResourceType())
.as("resource type")
.isEqualTo(ThingSnapshotTaken.RESOURCE_TYPE);
}
@Test
public void toJsonReturnsExpected() {
final var lifecycle = PersistenceLifecycle.DELETED;
final var underTest = ThingSnapshotTaken.newBuilder(TestConstants.Thing.THING_ID, REVISION, lifecycle, thingJson)
.policyId(POLICY_ID)
.timestamp(timestamp)
.build();
final var jsonObject = JsonObject.newBuilder()
.set(Event.JsonFields.TYPE, underTest.getType())
.set(EventsourcedEvent.JsonFields.REVISION, underTest.getRevision())
.set(Event.JsonFields.TIMESTAMP, timestamp.toString())
.set(SnapshotTaken.JsonFields.ENTITY_ID, String.valueOf(underTest.getEntityId()))
.set(SnapshotTaken.JsonFields.ENTITY, thingJson)
.set(SnapshotTaken.JsonFields.LIFECYCLE, lifecycle.name())
.set(ThingSnapshotTaken.JSON_POLICY_ID, POLICY_ID.toString())
.build();
assertThat(underTest.toJson()).isEqualTo(jsonObject);
}
@Test
public void fromJsonWorksAsExpected() {
final var dittoHeaders = getDittoHeadersWithCorrelationId();
final var thingSnapshotTaken =
ThingSnapshotTaken.newBuilder(TestConstants.Thing.THING_ID, REVISION, PersistenceLifecycle.ACTIVE, thingJson)
.timestamp(timestamp)
.dittoHeaders(dittoHeaders)
.build();
final var underTest = ThingSnapshotTaken.fromJson(thingSnapshotTaken.toJson(), dittoHeaders);
assertThat(underTest).isEqualTo(thingSnapshotTaken);
}
@Test
public void setDittoHeadersReturnsInstanceWithNewDittoHeaders() {
final var lifecycle = PersistenceLifecycle.ACTIVE;
final var dittoHeaders = getDittoHeadersWithCorrelationId();
final var underTest = ThingSnapshotTaken.newBuilder(TestConstants.Thing.THING_ID, REVISION, lifecycle, thingJson)
.policyId(POLICY_ID)
.timestamp(timestamp)
.build();
final var thingSnapshotTakenWithNewDittoHeaders = underTest.setDittoHeaders(dittoHeaders);
softly.assertThat((CharSequence) underTest.getEntityId()).as("entity ID").isEqualTo(TestConstants.Thing.THING_ID);
softly.assertThat(underTest.getPolicyId()).as("policy ID").isEqualTo(underTest.getPolicyId());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getDittoHeaders())
.as("Ditto headers")
.isEqualTo(dittoHeaders);
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getRevision())
.as("revision")
.isEqualTo(underTest.getRevision());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getLifecycle())
.as("lifecycle")
.isEqualTo(underTest.getLifecycle());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getPubSubTopic())
.as("pub-sub topic")
.isEqualTo(underTest.getPubSubTopic());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getEntity())
.as("entity")
.isEqualTo(underTest.getEntity());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getType())
.as("type")
.isEqualTo(underTest.getType());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getManifest())
.as("manifest")
.isEqualTo(underTest.getManifest());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getTimestamp())
.as("timestamp")
.isEqualTo(underTest.getTimestamp());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getMetadata())
.as("metadata")
.isEqualTo(underTest.getMetadata());
softly.assertThat((CharSequence) thingSnapshotTakenWithNewDittoHeaders.getResourcePath())
.as("resource path")
.isEqualTo(underTest.getResourcePath());
softly.assertThat(thingSnapshotTakenWithNewDittoHeaders.getResourceType())
.as("resource type")
.isEqualTo(underTest.getResourceType());
}
private DittoHeaders getDittoHeadersWithCorrelationId() {
return DittoHeaders.newBuilder().correlationId(testName.getMethodName()).build();
}
}