/
DittoProtocolAdapterTest.java
executable file
·595 lines (506 loc) · 27.2 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
* 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.protocoladapter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.ditto.model.base.acks.DittoAcknowledgementLabel.TWIN_PERSISTED;
import static org.eclipse.ditto.protocoladapter.TestConstants.DITTO_HEADERS_V_2;
import static org.eclipse.ditto.protocoladapter.TestConstants.DITTO_HEADERS_V_2_NO_STATUS;
import static org.eclipse.ditto.protocoladapter.TestConstants.POLICY_ID;
import static org.eclipse.ditto.protocoladapter.TestConstants.THING_ID;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.acks.AcknowledgementLabel;
import org.eclipse.ditto.model.base.common.DittoConstants;
import org.eclipse.ditto.model.base.common.HttpStatus;
import org.eclipse.ditto.model.base.headers.DittoHeaderDefinition;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.model.policies.PolicyId;
import org.eclipse.ditto.model.policies.SubjectId;
import org.eclipse.ditto.model.things.ThingId;
import org.eclipse.ditto.signals.acks.base.Acknowledgement;
import org.eclipse.ditto.signals.acks.base.Acknowledgements;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.policies.PolicyErrorResponse;
import org.eclipse.ditto.signals.commands.policies.exceptions.PolicyNotAccessibleException;
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.ModifyFeatureDesiredProperty;
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.commands.thingsearch.ThingSearchCommand;
import org.eclipse.ditto.signals.commands.thingsearch.subscription.CreateSubscription;
import org.eclipse.ditto.signals.events.things.ThingEvent;
import org.eclipse.ditto.signals.events.things.ThingModified;
import org.eclipse.ditto.signals.events.thingsearch.SubscriptionCreated;
import org.eclipse.ditto.signals.events.thingsearch.SubscriptionEvent;
import org.eclipse.ditto.signals.announcements.policies.SubjectDeletionAnnouncement;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link DittoProtocolAdapter}.
*/
public final class DittoProtocolAdapterTest implements ProtocolAdapterTest {
private ProtocolAdapter underTest;
@Before
public void setUp() {
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.getHttpStatus())
.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 policyErrorResponseFromAdaptable() {
final PolicyNotAccessibleException policyNotAccessibleException =
PolicyNotAccessibleException.newBuilder(POLICY_ID).build();
final TopicPath topicPath = TopicPath.newBuilder(POLICY_ID)
.policies()
.errors()
.build();
final JsonPointer path = JsonPointer.empty();
final Adaptable adaptable = Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withStatus(policyNotAccessibleException.getHttpStatus())
.withValue(policyNotAccessibleException.toJson())
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final PolicyErrorResponse expected =
PolicyErrorResponse.of(POLICY_ID, policyNotAccessibleException, DITTO_HEADERS_V_2);
final PolicyErrorResponse actual = (PolicyErrorResponse) 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.regularOrSpecial()))
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingModifyCommand actualCommand = (ThingModifyCommand) underTest.fromAdaptable(adaptable);
assertWithExternalHeadersThat(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.regularOrSpecial())
.set(ModifyThing.JSON_COPY_POLICY_FROM, policyIdToCopy))
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingModifyCommand actualCommand = (ThingModifyCommand) underTest.fromAdaptable(adaptable);
assertWithExternalHeadersThat(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(HttpStatus.CREATED)
.withValue(TestConstants.THING.toJson(FieldType.regularOrSpecial()))
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build());
assertWithExternalHeadersThat(actualCommandResponseCreated).isEqualTo(modifyThingResponseCreated);
final ThingModifyCommandResponse actualCommandResponseModified =
(ThingModifyCommandResponse) underTest.fromAdaptable(Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withStatus(HttpStatus.NO_CONTENT)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build());
assertWithExternalHeadersThat(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());
assertWithExternalHeadersThat(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());
assertWithExternalHeadersThat(actualCommandWithFields).isEqualTo(retrieveThingWithFields);
}
@Test
public void thingLiveQueryCommandFromAdaptable() {
final RetrieveThing retrieveThing = RetrieveThing.of(THING_ID, DITTO_HEADERS_V_2.toBuilder()
.putHeader(DittoHeaderDefinition.CHANNEL.getKey(), "live")
.build());
final TopicPath topicPath = TopicPath.newBuilder(THING_ID)
.things()
.live()
.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());
assertWithExternalHeadersThat(actualCommand).isEqualTo(retrieveThing);
assertThat(actualCommand.getDittoHeaders().getChannel()).contains("live");
}
@Test
public void thingQueryCommandResponseFromAdaptable() {
final JsonFieldSelector selectedFields =
JsonFieldSelector.newInstance("id", "definition", "attributes", "features", "_revision", "_modified",
"_created");
final JsonObject expectedThing = TestConstants.THING.toJson(selectedFields);
final RetrieveThingResponse retrieveThingResponse =
RetrieveThingResponse.of(TestConstants.THING_ID, expectedThing, 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(HttpStatus.OK)
.withValue(expectedThing)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingQueryCommandResponse actual = (ThingQueryCommandResponse) underTest.fromAdaptable(adaptable);
assertWithExternalHeadersThat(actual).isEqualTo(retrieveThingResponse);
}
@Test
public void thingSearchCommandFromAdaptable() {
final CreateSubscription createSubscription =
CreateSubscription.of(null, null, null, Collections.emptySet(),
DITTO_HEADERS_V_2_NO_STATUS);
final TopicPath topicPath = TopicPath.fromNamespace("_")
.things()
.twin()
.search()
.subscribe()
.build();
final ThingSearchCommand actualCommand =
(ThingSearchCommand) underTest.fromAdaptable(Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder().build())
.withHeaders(TestConstants.HEADERS_V_2_NO_CONTENT_TYPE)
.build());
assertWithExternalHeadersThat(actualCommand).isEqualTo(createSubscription);
final JsonFieldSelector selectedFields = JsonFieldSelector.newInstance("thingId");
final CreateSubscription createSubscriptionWithFields =
CreateSubscription.of(null, null, selectedFields, Collections.emptySet(),
DITTO_HEADERS_V_2);
final ThingSearchCommand actualCommandWithFields =
(ThingSearchCommand) underTest.fromAdaptable(Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder()
.withFields(selectedFields)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build());
assertWithExternalHeadersThat(actualCommandWithFields).isEqualTo(createSubscriptionWithFields);
}
@Test
public void subscriptionEventFromAdaptable() {
final SubscriptionCreated expected =
SubscriptionCreated.of(TestConstants.SUBSCRIPTION_ID, DITTO_HEADERS_V_2_NO_STATUS);
final TopicPath topicPath = TopicPath.fromNamespace("_")
.things()
.twin()
.search()
.generated()
.build();
final JsonPointer path = JsonPointer.empty();
final Adaptable adaptable = Adaptable.newBuilder(topicPath)
.withPayload(Payload.newBuilder(path)
.withValue(JsonObject.of(
String.format("{\"subscriptionId\": \"%s\"}", TestConstants.SUBSCRIPTION_ID)))
.build())
.withHeaders(DITTO_HEADERS_V_2_NO_STATUS)
.build();
final SubscriptionEvent actual = (SubscriptionEvent) underTest.fromAdaptable(adaptable);
assertWithExternalHeadersThat(actual).isEqualTo(expected);
}
@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.regularOrSpecial()))
.withRevision(TestConstants.REVISION)
.build())
.withHeaders(TestConstants.HEADERS_V_2)
.build();
final ThingEvent actual = (ThingEvent) underTest.fromAdaptable(adaptable);
assertWithExternalHeadersThat(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 modifyFeatureDesiredPropertyFromAdaptable() {
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/desiredProperties/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(ModifyFeatureDesiredProperty.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);
}
@Test
public void acknowledgementToAdaptable() {
final Acknowledgement acknowledgement =
Acknowledgement.of(AcknowledgementLabel.of("my-twin-persisted"), ThingId.of("thing:id"),
HttpStatus.CONTINUE, DittoHeaders.empty());
final Adaptable adaptable = underTest.toAdaptable((Signal<?>) acknowledgement);
assertThat(adaptable.getTopicPath())
.isEqualTo(ProtocolFactory.newTopicPath("thing/id/things/twin/acks/my-twin-persisted"));
assertThat((Iterable<?>) adaptable.getPayload().getPath()).isEmpty();
assertThat(adaptable.getPayload().getHttpStatus()).contains(HttpStatus.CONTINUE);
}
@Test
public void acknowledgementFromAdaptable() {
final Adaptable adaptable = ProtocolFactory.jsonifiableAdaptableFromJson(JsonObject.of("{\n" +
" \"topic\": \"thing/id/things/twin/acks/the-ack-label\",\n" +
" \"path\": \"/\",\n" +
" \"status\": 508\n" +
"}"));
final Signal<?> acknowledgement = underTest.fromAdaptable(adaptable);
assertThat(acknowledgement).isEqualTo(
Acknowledgement.of(AcknowledgementLabel.of("the-ack-label"), ThingId.of("thing:id"),
HttpStatus.LOOP_DETECTED, DittoHeaders.empty())
);
}
@Test
public void acknowledgementsToAdaptable() {
final Acknowledgement ack1 =
Acknowledgement.of(TWIN_PERSISTED, ThingId.of("thing:id"), HttpStatus.CONTINUE,
DittoHeaders.empty());
final Acknowledgement ack2 =
Acknowledgement.of(AcknowledgementLabel.of("the-ack-label"), ThingId.of("thing:id"),
HttpStatus.LOOP_DETECTED, DittoHeaders.empty());
final Acknowledgements acks = Acknowledgements.of(Arrays.asList(ack1, ack2), DittoHeaders.empty());
final Adaptable adaptable = underTest.toAdaptable((Signal<?>) acks);
final JsonObject expectedPayloadJson = JsonObject.of("{\n" +
" \"twin-persisted\":{\"status\":100,\"headers\":{\"response-required\":false}},\n" +
" \"the-ack-label\":{\"status\":508,\"headers\":{\"response-required\":false}}\n" +
"}");
assertThat(adaptable.getTopicPath())
.isEqualTo(ProtocolFactory.newTopicPath("thing/id/things/twin/acks"));
assertThat((Iterable<?>) adaptable.getPayload().getPath()).isEmpty();
assertThat(adaptable.getPayload().getHttpStatus()).contains(HttpStatus.FAILED_DEPENDENCY);
assertThat(adaptable.getPayload().getValue()).contains(expectedPayloadJson);
}
@Test
public void acknowledgementsFromJson() {
final Adaptable adaptable = ProtocolFactory.jsonifiableAdaptableFromJson(JsonObject.of("{\n" +
" \"topic\": \"thing/id/things/twin/acks\",\n" +
" \"path\": \"/\",\n" +
" \"value\": {\n" +
" \"twin-persisted\": { \"status\": 100,\"headers\":{\"response-required\":false} },\n" +
" \"the-ack-label\": { \"status\": 508,\"headers\":{\"response-required\":false} }\n" +
" },\n" +
" \"status\": 424,\n" +
" \"headers\": { \"content-type\": \"" + DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE +
"\",\"response-required\":false }\n" +
"}"));
final Signal<?> acknowledgement = underTest.fromAdaptable(adaptable);
assertThat(acknowledgement).isEqualTo(Acknowledgements.of(
Arrays.asList(Acknowledgement.of(TWIN_PERSISTED, ThingId.of("thing:id"), HttpStatus.CONTINUE,
DittoHeaders.empty()),
Acknowledgement.of(AcknowledgementLabel.of("the-ack-label"), ThingId.of("thing:id"),
HttpStatus.LOOP_DETECTED, DittoHeaders.empty())
),
DittoHeaders.newBuilder()
.contentType(DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE)
.build()
));
final Adaptable reverseAdaptable =
ProtocolFactory.wrapAsJsonifiableAdaptable(underTest.toAdaptable(acknowledgement));
assertThat(reverseAdaptable).isEqualTo(adaptable);
}
@Test
public void policyAnnouncementToAdaptable() {
final PolicyId policyId = PolicyId.of("policy:id");
final Instant expiry = Instant.now();
final List<SubjectId> expiringSubjects =
Arrays.asList(SubjectId.newInstance("ditto:sub1"), SubjectId.newInstance("ditto:sub2"));
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().randomCorrelationId().build();
final SubjectDeletionAnnouncement announcement =
SubjectDeletionAnnouncement.of(policyId, expiry, expiringSubjects, dittoHeaders);
final Adaptable adaptable = underTest.toAdaptable(announcement);
assertThat(adaptable.getTopicPath().getPath()).isEqualTo("policy/id/policies/announcements/subjectExpiry");
assertThat(adaptable.getDittoHeaders().getCorrelationId()).isEqualTo(dittoHeaders.getCorrelationId());
assertThat(adaptable.getPayload().getPath().isEmpty()).isTrue();
final JsonValue payload = adaptable.getPayload().getValue().orElseThrow(NoSuchElementException::new);
final JsonValue expectedPayload = JsonObject.newBuilder()
.set(SubjectDeletionAnnouncement.JsonFields.DELETED_AT, expiry.toString())
.set(SubjectDeletionAnnouncement.JsonFields.SUBJECT_IDS,
JsonArray.of("[\"ditto:sub1\",\"ditto:sub2\"]"))
.build();
assertThat(payload).isEqualTo(expectedPayload);
}
@Test
public void policyAnnouncementFromAdaptable() {
final Instant expiry = Instant.now();
final String correlationId = UUID.randomUUID().toString();
final JsonObject json = JsonObject.of(String.format("{\n" +
" \"topic\": \"policy/id/policies/announcements/subjectExpiry\",\n" +
" \"headers\": {\"correlation-id\": \"%s\"},\n" +
" \"path\": \"/\",\n" +
" \"value\": {\n" +
" \"expiry\": \"%s\",\n" +
" \"expiringSubjects\": [\n" +
" \"ditto:sub1\",\n" +
" \"ditto:sub2\"\n" +
" ]\n" +
" }\n" +
"}",
correlationId,
expiry
));
final Adaptable adaptable = ProtocolFactory.jsonifiableAdaptableFromJson(json);
final SubjectDeletionAnnouncement announcement = (SubjectDeletionAnnouncement) underTest.fromAdaptable(adaptable);
assertThat((CharSequence) announcement.getEntityId()).isEqualTo(PolicyId.of("policy:id"));
assertThat(announcement.getDeletedAt()).isEqualTo(expiry);
assertThat(announcement.getSubjectIds())
.isEqualTo(Arrays.asList(SubjectId.newInstance("ditto:sub1"), SubjectId.newInstance("ditto:sub2")));
assertThat(announcement.getDittoHeaders().getCorrelationId()).contains(correlationId);
}
}