-
Notifications
You must be signed in to change notification settings - Fork 215
/
MetadataFromCommandTest.java
237 lines (208 loc) · 10.8 KB
/
MetadataFromCommandTest.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (c) 2022 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.actors.strategies.commands;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.mutabilitydetector.unittesting.AllowedReason.provided;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaderKey;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Feature;
import org.eclipse.ditto.things.model.FeatureDefinition;
import org.eclipse.ditto.things.model.FeatureProperties;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.modify.MergeThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyFeature;
import org.eclipse.ditto.things.model.signals.commands.modify.ThingModifyCommand;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
/**
* Unit test for {@link MetadataFromCommand}.
*/
@RunWith(MockitoJUnitRunner.class)
public final class MetadataFromCommandTest {
private static FeatureProperties fluxCapacitorProperties;
private static Feature fluxCapacitor;
private static Thing thingWithoutMetadata;
@Mock private ThingModifyCommand command;
@BeforeClass
public static void setUpClass() {
fluxCapacitorProperties = FeatureProperties.newBuilder()
.set("capacity", JsonObject.newBuilder()
.set("value", 3.2)
.set("unit", "gallons per football")
.build())
.build();
fluxCapacitor = Feature.newBuilder()
.properties(fluxCapacitorProperties)
.withId("flux-capacitor")
.build();
thingWithoutMetadata = Thing.newBuilder()
.setId(ThingId.generateRandom())
.setAttribute(JsonPointer.of("productionYear"), JsonValue.of(2020))
.setFeature(fluxCapacitor)
.build();
}
@Before
public void setUp() {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.schemaVersion(JsonSchemaVersion.LATEST)
.build();
Mockito.when(command.getDittoHeaders()).thenReturn(dittoHeaders);
}
@Test
public void assertImmutability() {
assertInstancesOf(MetadataFromCommand.class,
areImmutable(),
provided(Command.class, JsonValue.class, Thing.class, Metadata.class).areAlsoImmutable());
}
@Test
public void tryToGetInstanceWithNullCommand() {
assertThatNullPointerException()
.isThrownBy(() -> MetadataFromCommand.of(null, null, null))
.withMessage("The command must not be null!")
.withNoCause();
}
@Test
public void getMetadataWhenEventHasNoEntityAndEntityHasNullExistingMetadata() {
Mockito.when(command.getResourcePath()).thenReturn(JsonPointer.empty());
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, null);
assertThat(underTest.get()).isNull();
}
@Test
public void entityHasNoMetadataAndEventDittoHeadersHaveNoMetadata() {
Mockito.when(command.getResourcePath()).thenReturn(JsonPointer.empty());
Mockito.when(command.getDittoHeaders()).thenReturn(DittoHeaders.empty());
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, null);
assertThat(underTest.get()).isNull();
}
@Test
public void entityMetadataButEventDittoHeadersHaveNoMetadata() {
final Metadata existingMetadata = Metadata.newBuilder().set("/scruplusFine", JsonValue.of("^6,00.32")).build();
Mockito.when(command.getResourcePath()).thenReturn(JsonPointer.empty());
Mockito.when(command.getDittoHeaders()).thenReturn(DittoHeaders.empty());
final MetadataFromCommand underTest = MetadataFromCommand.of(command, null, existingMetadata);
assertThat(underTest.get()).isEqualTo(existingMetadata);
}
@Test
public void createMetadataFromScratch() {
final Feature modifiedFeature = fluxCapacitor.toBuilder()
.properties(fluxCapacitorProperties.toBuilder()
.set("grumbo", 2)
.build())
.definition(FeatureDefinition.fromIdentifier("foo:bar:1"))
.build();
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.putMetadata(MetadataHeaderKey.parse("/scruplusFine"), JsonValue.of("^6,00.32"))
.putMetadata(MetadataHeaderKey.parse("/properties/grumbo/froodNoops"), JsonValue.of(5))
.putMetadata(MetadataHeaderKey.parse("/properties/*/lastSeen"), JsonValue.of(1955))
.build();
final ModifyFeature modifyFeature = ModifyFeature.of(thingWithoutMetadata.getEntityId().orElseThrow(),
modifiedFeature, dittoHeaders);
final Metadata expected = Metadata.newBuilder()
.set(JsonPointer.of("scruplusFine"), "^6,00.32")
.set(JsonPointer.of("properties/grumbo/froodNoops"), 5)
.set(JsonPointer.of("properties/grumbo/lastSeen"), 1955)
.set(JsonPointer.of("properties/capacity/lastSeen"), 1955)
.build();
final MetadataFromCommand underTest = MetadataFromCommand.of(modifyFeature, thingWithoutMetadata, null);
assertThat(underTest.get()).isEqualTo(expected);
}
@Test
public void modifyExistingMetadataWithMergeCommand() {
final Metadata existingMetadata = Metadata.newBuilder()
.set(JsonPointer.of("airplaneMode"), "forbidden")
.set(JsonPointer.of("definition/lastSeen"), 2023)
.build();
final Feature modifiedFeature = fluxCapacitor.toBuilder()
.properties(fluxCapacitorProperties.toBuilder()
.set("grumbo", 2)
.build())
.definition(FeatureDefinition.fromIdentifier("foo:bar:1"))
.build();
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.putMetadata(MetadataHeaderKey.parse("/scruplusFine"), JsonValue.of("^6,00.32"))
.putMetadata(MetadataHeaderKey.parse("/properties/grumbo/froodNoops"), JsonValue.of(5))
.putMetadata(MetadataHeaderKey.parse("/properties/*/lastSeen"), JsonValue.of(1955))
.build();
final MergeThing mergeFeature = MergeThing.withFeature(thingWithoutMetadata.getEntityId().orElseThrow(),
modifiedFeature, dittoHeaders);
final Metadata expected = existingMetadata.toBuilder()
.set(JsonPointer.of("airplaneMode"), "forbidden")
.set(JsonPointer.of("scruplusFine"), "^6,00.32")
.set(JsonPointer.of("definition/lastSeen"), 2023)
.set(JsonPointer.of("properties/capacity/lastSeen"), 1955)
.set(JsonPointer.of("properties/grumbo/lastSeen"), 1955)
.set(JsonPointer.of("properties/grumbo/froodNoops"), 5)
.build();
final MetadataFromCommand underTest = MetadataFromCommand.of(mergeFeature, thingWithoutMetadata, existingMetadata);
assertThat(underTest.get()).isEqualTo(expected);
}
@Test
public void keyWithSpecificPathOverwritesKeyWithWildcardPath() {
final JsonValue metric = JsonValue.of("metric");
final JsonValue nonMetric = JsonValue.of("non-metric");
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.putMetadata(MetadataHeaderKey.parse("/properties/capacity/type"), nonMetric)
.putMetadata(MetadataHeaderKey.parse("/properties/*/type"), metric)
.build();
final Feature modifiedFeature = fluxCapacitor.toBuilder()
.properties(fluxCapacitorProperties.toBuilder()
.set("grumbo", 2)
.build())
.definition(FeatureDefinition.fromIdentifier("foo:bar:1"))
.build();
final ModifyFeature modifyFeature = ModifyFeature.of(thingWithoutMetadata.getEntityId().orElseThrow(),
modifiedFeature, dittoHeaders);
final Metadata expected = Metadata.newBuilder()
.set(JsonPointer.of("properties/grumbo/type"), metric)
.set(JsonPointer.of("properties/capacity/type"), nonMetric)
.build();
final MetadataFromCommand underTest = MetadataFromCommand.of(modifyFeature, thingWithoutMetadata,
thingWithoutMetadata.getMetadata().orElse(null));
assertThat(underTest.get()).isEqualTo(expected);
}
@Test
public void ensureThatLeafsCanOnlyBeObjects() {
final Feature modifiedFeature = fluxCapacitor.toBuilder()
.properties(fluxCapacitorProperties.toBuilder()
.set("capacitorNr", 99)
.build())
.definition(FeatureDefinition.fromIdentifier("foo:bar:1"))
.build();
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.putMetadata(MetadataHeaderKey.parse("/scruplusFine"), JsonValue.of("^6,00.32"))
.putMetadata(MetadataHeaderKey.parse("/properties/capacitorNr"), JsonValue.of("unlimited"))
.build();
final ModifyFeature modifyFeature = ModifyFeature.of(thingWithoutMetadata.getEntityId().orElseThrow(),
modifiedFeature, dittoHeaders);
final MetadataFromCommand underTest = MetadataFromCommand.of(modifyFeature, null, null);
assertThat(underTest.get())
.isNotEmpty()
.doesNotContain(JsonField.newInstance("/properties/capacitorNr", JsonValue.of("unlimited")));
}
}