/
DittoProtocolAdapterTest.java
executable file
·299 lines (256 loc) · 12.4 KB
/
DittoProtocolAdapterTest.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.protocoladapter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.protocoladapter.TestConstants.DITTO_HEADERS_V_2;
import static org.eclipse.ditto.protocoladapter.TestConstants.THING_ID;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.signals.commands.things.ThingErrorResponse;
import org.eclipse.ditto.signals.commands.things.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.signals.commands.things.modify.ModifyFeatureProperty;
import org.eclipse.ditto.signals.commands.things.modify.ModifyThing;
import org.eclipse.ditto.signals.commands.things.modify.ModifyThingResponse;
import org.eclipse.ditto.signals.commands.things.modify.ThingModifyCommand;
import org.eclipse.ditto.signals.commands.things.modify.ThingModifyCommandResponse;
import org.eclipse.ditto.signals.commands.things.query.RetrieveThing;
import org.eclipse.ditto.signals.commands.things.query.RetrieveThingResponse;
import org.eclipse.ditto.signals.commands.things.query.ThingQueryCommand;
import org.eclipse.ditto.signals.commands.things.query.ThingQueryCommandResponse;
import org.eclipse.ditto.signals.events.things.ThingEvent;
import org.eclipse.ditto.signals.events.things.ThingModified;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link DittoProtocolAdapter}.
*/
public final class DittoProtocolAdapterTest {
private DittoProtocolAdapter underTest;
@Before
public void setUp() throws Exception {
underTest = DittoProtocolAdapter.newInstance();
}
/** */
@Test
public void topicPathFromString() {
final TopicPath expected = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.commands()
.modify()
.build();
final TopicPath actual =
ProtocolFactory.newTopicPath("org.eclipse.ditto.test/myThing/things/twin/commands/modify");
assertThat(actual).isEqualTo(expected);
}
/** */
@Test
public void thingErrorResponseFromAdaptable() {
final ThingNotAccessibleException thingNotAccessibleException =
ThingNotAccessibleException.newBuilder(THING_ID).build();
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.errors()
.build();
final JsonPointer path = JsonPointer.empty();
final Adaptable adaptable = Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withStatus(thingNotAccessibleException.getStatusCode())
.withValue(thingNotAccessibleException.toJson())
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingErrorResponse expected =
ThingErrorResponse.of(TestConstants.THING_ID, thingNotAccessibleException, DITTO_HEADERS_V_2);
final ThingErrorResponse actual = (ThingErrorResponse) underTest.fromAdaptable(adaptable);
assertThat(actual.toJson()).isEqualTo(expected.toJson());
}
/** */
@Test
public void thingModifyCommandFromAdaptable() {
final ModifyThing modifyThing =
ModifyThing.of(TestConstants.THING_ID, TestConstants.THING, null, DITTO_HEADERS_V_2);
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.commands()
.modify()
.build();
final JsonPointer path = JsonPointer.empty();
final Adaptable adaptable = Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withValue(TestConstants.THING.toJson(FieldType.notHidden()))
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingModifyCommand actualCommand = (ThingModifyCommand) underTest.fromAdaptable(adaptable);
assertThat(actualCommand).isEqualTo(modifyThing);
}
/** */
@Test
public void thingModifyCommandFromAdaptableWithPolicyToCopy() {
final String policyIdToCopy = "someNameSpace:someId";
final ModifyThing modifyThing =
ModifyThing.withCopiedPolicy(TestConstants.THING_ID, TestConstants.THING, policyIdToCopy,
DITTO_HEADERS_V_2);
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.commands()
.modify()
.build();
final JsonPointer path = JsonPointer.empty();
final Adaptable adaptable = Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withValue(TestConstants.THING.toJson(FieldType.notHidden())
.set(ModifyThing.JSON_COPY_POLICY_FROM, policyIdToCopy))
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingModifyCommand actualCommand = (ThingModifyCommand) underTest.fromAdaptable(adaptable);
assertThat(actualCommand).isEqualTo(modifyThing);
}
/** */
@Test
public void thingModifyCommandResponseFromAdaptable() {
final ModifyThingResponse modifyThingResponseCreated =
ModifyThingResponse.created(TestConstants.THING, DITTO_HEADERS_V_2);
final ModifyThingResponse modifyThingResponseModified =
ModifyThingResponse.modified(TestConstants.THING_ID, DITTO_HEADERS_V_2);
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.commands()
.modify()
.build();
final JsonPointer path = JsonPointer.empty();
final ThingModifyCommandResponse actualCommandResponseCreated =
(ThingModifyCommandResponse) underTest.fromAdaptable(Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withStatus(HttpStatusCode.CREATED)
.withValue(TestConstants.THING.toJson(FieldType.notHidden()))
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build());
assertThat(actualCommandResponseCreated).isEqualTo(modifyThingResponseCreated);
final ThingModifyCommandResponse actualCommandResponseModified =
(ThingModifyCommandResponse) underTest.fromAdaptable(Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withStatus(HttpStatusCode.NO_CONTENT)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build());
assertThat(actualCommandResponseModified).isEqualTo(modifyThingResponseModified);
}
/** */
@Test
public void thingQueryCommandFromAdaptable() {
final RetrieveThing retrieveThing = RetrieveThing.of(THING_ID, DITTO_HEADERS_V_2);
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.commands()
.retrieve()
.build();
final JsonPointer path = JsonPointer.empty();
final ThingQueryCommand actualCommand =
(ThingQueryCommand) underTest.fromAdaptable(Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build());
assertThat(actualCommand).isEqualTo(retrieveThing);
final JsonFieldSelector selectedFields = JsonFieldSelector.newInstance("thingId");
final RetrieveThing retrieveThingWithFields = RetrieveThing.getBuilder(THING_ID, DITTO_HEADERS_V_2)
.withSelectedFields(selectedFields)
.build();
final ThingQueryCommand actualCommandWithFields =
(ThingQueryCommand) underTest.fromAdaptable(Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withFields(selectedFields)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build());
assertThat(actualCommandWithFields).isEqualTo(retrieveThingWithFields);
}
/** */
@Test
public void thingQueryCommandResponseFromAdaptable() {
final RetrieveThingResponse retrieveThingResponse =
RetrieveThingResponse.of(TestConstants.THING_ID, TestConstants.THING, DITTO_HEADERS_V_2);
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.commands()
.retrieve()
.build();
final JsonPointer path = JsonPointer.empty();
final Adaptable adaptable = Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withStatus(HttpStatusCode.OK)
.withValue(TestConstants.THING.toJson())
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingQueryCommandResponse actual = (ThingQueryCommandResponse) underTest.fromAdaptable(adaptable);
assertThat(actual).isEqualTo(retrieveThingResponse);
}
/** */
@Test
public void thingEventFromAdaptable() {
final ThingModified expected =
ThingModified.of(TestConstants.THING, TestConstants.REVISION, DITTO_HEADERS_V_2);
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.twin()
.events()
.modified()
.build();
final JsonPointer path = JsonPointer.empty();
final Adaptable adaptable = Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withValue(TestConstants.THING.toJson(FieldType.notHidden()))
.withRevision(TestConstants.REVISION)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingEvent actual = (ThingEvent) underTest.fromAdaptable(adaptable);
assertThat(actual).isEqualTo(expected);
}
/** */
@Test
public void modifyFeaturePropertyFromAdaptable() {
final String topicPathString =
"org.eclipse.ditto.client.test/7a96e7a4-b20c-43eb-b669-f75514af30d0/things/twin/commands/modify";
final TopicPath topicPath = ProtocolFactory.newTopicPath(topicPathString);
final String jsonString = "{\"path\":\"/features/feature_id_2/properties/complex/bum\",\"value\":\"bar\"}";
final Payload payload = ProtocolFactory.newPayload(jsonString);
final Adaptable adaptable = ProtocolFactory.newAdaptableBuilder(topicPath).withPayload(payload).build();
final Jsonifiable<JsonObject> jsonifiable = underTest.fromAdaptable(adaptable);
assertThat(jsonifiable).isInstanceOf(ModifyFeatureProperty.class);
}
/** */
@Test
public void thingMessageFromAdaptable() {
final String subject = "this/is/all/part/of/subject";
final String thingId = "7a96e7a4-b20c-43eb-b669-f75514af30d0";
final String topicPathString =
"org.eclipse.ditto.client.test/" + thingId + "/things/live/messages/" + subject;
final TopicPath topicPath = ProtocolFactory.newTopicPath(topicPathString);
assertThat(topicPath.getSubject()).contains(subject);
assertThat(topicPath.getId()).isEqualTo(thingId);
}
}