/
RetrieveThingsTest.java
executable file
·219 lines (178 loc) · 8.92 KB
/
RetrieveThingsTest.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
/*
* 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.signals.commands.things.query;
import static org.eclipse.ditto.signals.commands.things.assertions.ThingCommandAssertions.assertThat;
import static org.mutabilitydetector.unittesting.AllowedReason.provided;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseOptions;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.auth.AuthorizationContext;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.signals.commands.base.Command;
import org.eclipse.ditto.signals.commands.base.GlobalCommandRegistry;
import org.eclipse.ditto.signals.commands.things.TestConstants;
import org.eclipse.ditto.signals.commands.things.ThingCommand;
import org.eclipse.ditto.signals.commands.things.exceptions.MissingThingIdsException;
import org.junit.Test;
import nl.jqno.equalsverifier.EqualsVerifier;
/**
* Unit test for {@link RetrieveThings}.
*/
public final class RetrieveThingsTest {
private static final JsonArray THING_IDS = JsonFactory.newArrayBuilder()
.add(TestConstants.Thing.THING_ID)
.add(":AnotherThingId")
.build();
private static final JsonArray THING_IDS_WITH_DISTINCT_NAMESPACE = JsonFactory.newArrayBuilder()
.add(TestConstants.Thing.THING_ID)
.add(TestConstants.Thing.THING_ID + "1")
.add(TestConstants.Thing.THING_ID + "2")
.build();
private static final String SELECTED_FIELDS = "field1,field2,field3";
private static final JsonObject KNOWN_JSON = JsonFactory.newObjectBuilder()
.set(ThingCommand.JsonFields.TYPE, RetrieveThings.TYPE)
.set(RetrieveThings.JSON_THING_IDS, THING_IDS)
.build();
private static final JsonObject KNOWN_JSON_WITH_DISTINCT_NAMESPACE = JsonFactory.newObjectBuilder()
.set(ThingCommand.JsonFields.TYPE, RetrieveThings.TYPE)
.set(RetrieveThings.JSON_THING_IDS, THING_IDS_WITH_DISTINCT_NAMESPACE)
.set(RetrieveThings.JSON_NAMESPACE, "example.com")
.build();
private static final JsonObject KNOWN_JSON_WITH_FIELD_SELECTION = JsonFactory.newObjectBuilder()
.set(ThingCommand.JsonFields.TYPE, RetrieveThings.TYPE)
.set(RetrieveThings.JSON_THING_IDS, THING_IDS)
.set(RetrieveThings.JSON_SELECTED_FIELDS, SELECTED_FIELDS)
.build();
private static final JsonParseOptions JSON_PARSE_OPTIONS =
JsonFactory.newParseOptionsBuilder().withoutUrlDecoding().build();
private static List<String> getThingIds() {
return THING_IDS.stream().map(JsonValue::asString).collect(Collectors.toList());
}
private static List<String> getThingIdsWithDistinctNamespace() {
return THING_IDS_WITH_DISTINCT_NAMESPACE.stream().map(JsonValue::asString).collect(Collectors.toList());
}
private static JsonFieldSelector getJsonFieldSelector() {
return JsonFactory.newFieldSelector(SELECTED_FIELDS, JSON_PARSE_OPTIONS);
}
@Test
public void assertImmutability() {
assertInstancesOf(RetrieveThings.class,
areImmutable(),
provided(AuthorizationContext.class, JsonFieldSelector.class).isAlsoImmutable());
}
@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(RetrieveThings.class)
.withRedefinedSuperclass()
.verify();
}
@Test
public void toJsonReturnsExpected() {
final RetrieveThings underTest = RetrieveThings.getBuilder(getThingIds()).build();
final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial());
assertThat(actualJson).isEqualTo(KNOWN_JSON);
}
@Test
public void toJsonReturnsExpectedWithNamespace() {
final RetrieveThings underTest =
RetrieveThings.getBuilder(getThingIdsWithDistinctNamespace()).namespace("example.com").build();
final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial());
assertThat(actualJson).isEqualTo(KNOWN_JSON_WITH_DISTINCT_NAMESPACE);
}
@Test
public void createInstanceFromValidJson() {
final RetrieveThings underTest =
RetrieveThings.fromJson(KNOWN_JSON.toString(), TestConstants.EMPTY_DITTO_HEADERS);
assertThat(underTest).isNotNull();
assertThat(underTest.getThingIds()).isEqualTo(getThingIds());
assertThat(underTest.getSelectedFields()).isEmpty();
assertThat(underTest.getNamespace()).isEmpty();
}
@Test
public void createInstanceWithNamespaceFromValidJson() {
final RetrieveThings underTest =
RetrieveThings.fromJson(KNOWN_JSON_WITH_DISTINCT_NAMESPACE.toString(),
TestConstants.EMPTY_DITTO_HEADERS);
assertThat(underTest).isNotNull();
assertThat(underTest.getThingIds()).isEqualTo(
THING_IDS_WITH_DISTINCT_NAMESPACE.stream().map(JsonValue::asString).collect(Collectors.toList()));
assertThat(underTest.getSelectedFields()).isEmpty();
assertThat(underTest.getNamespace()).contains("example.com");
}
@Test
public void jsonSerializationWorksAsExpectedWithSelectedFields() {
final RetrieveThings underTest =
RetrieveThings.getBuilder(getThingIds()).selectedFields(getJsonFieldSelector()).build();
final JsonObject actualJson = underTest.toJson(FieldType.regularOrSpecial());
assertThat(actualJson).isEqualTo(KNOWN_JSON_WITH_FIELD_SELECTION);
}
@Test
public void createInstanceFromValidJsonWithSelectedFields() {
final RetrieveThings underTest =
RetrieveThings.fromJson(KNOWN_JSON_WITH_FIELD_SELECTION.toString(),
TestConstants.EMPTY_DITTO_HEADERS);
assertThat(underTest).isNotNull();
assertThat(underTest.getThingIds()).isEqualTo(getThingIds());
assertThat(underTest.getSelectedFields()).contains(getJsonFieldSelector());
}
@Test(expected = IllegalArgumentException.class)
public void createInstanceWithInvalidNamespacesThrowsException() {
RetrieveThings.getBuilder(getThingIds()).namespace("").build();
}
@Test
public void checkRetrieveThingsWithEmptyJsonFieldSelectorBehavesEquallyAsOmittingFields() {
final JsonFieldSelector selectedFields = JsonFactory.newFieldSelector(null, JSON_PARSE_OPTIONS);
final RetrieveThings retrieveThings = RetrieveThings.getBuilder(TestConstants.Thing.THING_ID, ":AnotherThingId")
.selectedFields(selectedFields)
.build();
final RetrieveThings retrieveThings2 =
RetrieveThings.getBuilder(TestConstants.Thing.THING_ID, ":AnotherThingId")
.build();
assertThat(retrieveThings).isEqualTo(retrieveThings2);
}
@Test(expected = NullPointerException.class)
public void initializationWithNullForThingIdsArrayThrowsNullPointerException(){
//This cast is used to resolve the ambiguous call
RetrieveThings.getBuilder( (String[]) null).build();
}
@Test(expected = NullPointerException.class)
public void initializationWithNullForThingIdsListThrowsNullPointerException(){
//This cast is used to resolve the ambiguous call
RetrieveThings.getBuilder( (List<String>) null).build();
}
@Test(expected = MissingThingIdsException.class)
public void initializationWithoutThingIdsThrowsMissingThingIdsException(){
RetrieveThings.getBuilder().build();
}
@Test(expected = MissingThingIdsException.class)
public void initializationWithEmptyThingIdsListThrowsMissingThingIdsException(){
RetrieveThings.getBuilder(new ArrayList<>()).build();
}
@Test
public void parseRetrieveThingCommand() {
final GlobalCommandRegistry commandRegistry = GlobalCommandRegistry.getInstance();
final RetrieveThing command = RetrieveThing.of(
TestConstants.Thing.THING_ID, TestConstants.DITTO_HEADERS);
final JsonObject jsonObject = command.toJson(FieldType.regularOrSpecial());
final Command parsedCommand = commandRegistry.parse(jsonObject, TestConstants.DITTO_HEADERS);
assertThat(parsedCommand).isEqualTo(command);
}
}