-
Notifications
You must be signed in to change notification settings - Fork 215
/
ThingPersistenceActorTest.java
executable file
·1712 lines (1426 loc) · 82.4 KB
/
ThingPersistenceActorTest.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.things.service.persistence.actors;
import static org.assertj.core.api.Assertions.fail;
import static org.eclipse.ditto.things.model.assertions.DittoThingsAssertions.assertThat;
import java.time.Instant;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.assertj.core.api.Assertions;
import org.awaitility.Awaitility;
import org.eclipse.ditto.base.api.common.Shutdown;
import org.eclipse.ditto.base.api.common.ShutdownReasonFactory;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.entity.Revision;
import org.eclipse.ditto.base.model.entity.metadata.Metadata;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeader;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaderKey;
import org.eclipse.ditto.base.model.headers.metadata.MetadataHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.internal.utils.cluster.DistPubSubAccess;
import org.eclipse.ditto.internal.utils.test.Retry;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonParseOptions;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.model.Attributes;
import org.eclipse.ditto.things.model.Feature;
import org.eclipse.ditto.things.model.FeatureDefinition;
import org.eclipse.ditto.things.model.Features;
import org.eclipse.ditto.things.model.PolicyIdMissingException;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingBuilder;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.ThingLifecycle;
import org.eclipse.ditto.things.model.ThingRevision;
import org.eclipse.ditto.things.model.ThingTooLargeException;
import org.eclipse.ditto.things.model.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.TestConstants;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.exceptions.FeatureNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.MetadataHeadersConflictException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingUnavailableException;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing;
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThingResponse;
import org.eclipse.ditto.things.model.signals.commands.modify.DeleteAttribute;
import org.eclipse.ditto.things.model.signals.commands.modify.DeleteAttributeResponse;
import org.eclipse.ditto.things.model.signals.commands.modify.DeleteThing;
import org.eclipse.ditto.things.model.signals.commands.modify.DeleteThingResponse;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyAttribute;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyAttributes;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyFeatureProperty;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyFeatures;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThingResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveAttribute;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveAttributeResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveAttributes;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeature;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatureDefinition;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatureDefinitionResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatureProperty;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeaturePropertyResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveFeatures;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThings;
import org.eclipse.ditto.things.model.signals.events.ThingCreated;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingModified;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.slf4j.LoggerFactory;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.testkit.TestActorRef;
import akka.testkit.javadsl.TestKit;
import scala.PartialFunction;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import scala.runtime.BoxedUnit;
/**
* Unit test for the {@link ThingPersistenceActor}.
*/
public final class ThingPersistenceActorTest extends PersistenceActorTestBase {
private static final JsonParseOptions JSON_PARSE_OPTIONS =
JsonFactory.newParseOptionsBuilder().withoutUrlDecoding().build();
private static final Instant TIMESTAMP = Instant.EPOCH;
private static void assertThingInResponse(final Thing actualThing, final Thing expectedThing) {
// Policy entries are ignored by things-persistence.
assertThat(actualThing).hasEqualJson(expectedThing, FieldType.notHidden()
.and(IS_MODIFIED.negate()));
assertThat(actualThing.getModified()).isPresent(); // we cannot check exact timestamp
assertThat(actualThing.getCreated()).isPresent(); // we cannot check exact timestamp
}
private static void assertThingInResponseV2(final Thing actualThing, final Thing expectedThing) {
assertThat(actualThing).hasEqualJson(expectedThing, FieldType.notHidden()
.and(IS_MODIFIED.negate()));
assertThat(actualThing.getModified()).isPresent(); // we cannot check exact timestamp
}
@Rule
public final TestWatcher watchman = new TestedMethodLoggingWatcher(LoggerFactory.getLogger(getClass()));
@Before
public void setUp() {
setup(ConfigFactory.empty());
}
@Test
public void unavailableExpectedIfPersistenceActorTerminates() throws Exception {
new TestKit(actorSystem) {
{
final Thing thing = createThingV2WithRandomId();
final ThingId thingId = getIdOrThrow(thing);
final ActorRef underTest = createSupervisorActorFor(thingId);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
// retrieve created thing
final RetrieveThing retrieveThing = RetrieveThing.of(thingId, dittoHeadersV2);
underTest.tell(retrieveThing, getRef());
expectMsgEquals(ETagTestUtils.retrieveThingResponse(thing, null, dittoHeadersV2));
// terminate thing persistence actor
final String thingActorPath = String.format("akka://AkkaTestSystem/user/%s/pa", thingId);
final ActorSelection thingActorSelection = actorSystem.actorSelection(thingActorPath);
final Future<ActorRef> thingActorFuture =
thingActorSelection.resolveOne(Duration.create(5, TimeUnit.SECONDS));
Await.result(thingActorFuture, Duration.create(6, TimeUnit.SECONDS));
final ActorRef thingActor = watch(thingActorFuture.value().get().get());
watch(thingActor);
thingActor.tell(PoisonPill.getInstance(), getRef());
expectTerminated(thingActor);
// wait for supervisor to react to termination message
Thread.sleep(500L);
// retrieve unavailable thing
underTest.tell(retrieveThing, getRef());
expectMsgClass(ThingUnavailableException.class);
}
};
}
@Test
public void tryToModifyFeaturePropertyAndReceiveCorrectErrorCode() {
final ThingId thingId = ThingId.of("org.eclipse.ditto", "myThing");
final Thing thing = Thing.newBuilder()
.setId(thingId)
.setPolicyId(PolicyId.of(thingId))
.setFeatures(JsonFactory.newObject())
.build();
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
final String featureId = "myFeature";
final JsonPointer jsonPointer = JsonPointer.of("/state");
final JsonValue jsonValue = JsonFactory.newValue("on");
final ModifyFeatureProperty modifyFeatureProperty =
ModifyFeatureProperty.of(thingId, featureId, jsonPointer, jsonValue, dittoHeadersV2);
final FeatureNotAccessibleException featureNotAccessibleException =
FeatureNotAccessibleException.newBuilder(thingId, featureId)
.dittoHeaders(dittoHeadersV2)
.build();
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thingId);
underTest.tell(createThing, getRef());
expectMsgClass(CreateThingResponse.class);
underTest.tell(modifyFeatureProperty, getRef());
final Object actual = receiveOne(scala.concurrent.duration.Duration.apply(1, TimeUnit.SECONDS));
assertThat(actual).isInstanceOf(DittoRuntimeException.class);
assertThat(((DittoRuntimeException) actual).getErrorCode()).isEqualTo(
featureNotAccessibleException.getErrorCode());
}
};
}
@Test
public void tryToRetrieveThingWhichWasNotYetCreated() {
final ThingId thingId = ThingId.of("test.ns", "23420815");
final ThingCommand retrieveThingCommand = RetrieveThing.of(thingId, dittoHeadersV2);
new TestKit(actorSystem) {
{
final ActorRef thingPersistenceActor = createPersistenceActorFor(thingId);
thingPersistenceActor.tell(retrieveThingCommand, getRef());
expectMsgClass(ThingNotAccessibleException.class);
}
};
}
/**
* The ThingPersistenceActor is created with a Thing ID. Any command it receives which belongs to a Thing with a
* different ID should lead to an exception as the command was obviously sent to the wrong ThingPersistenceActor.
*/
@Test
public void tryToCreateThingWithDifferentThingId() {
final ThingId thingIdOfActor = ThingId.of("test.ns", "23420815");
final Thing thing = createThingV2WithRandomId();
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
final Props props = ThingPersistenceActor.props(thingIdOfActor, getDistributedPub(), pubSubMediator);
final TestActorRef<ThingPersistenceActor> underTest = TestActorRef.create(actorSystem, props);
final ThingPersistenceActor thingPersistenceActor = underTest.underlyingActor();
final PartialFunction<Object, BoxedUnit> receiveCommand = thingPersistenceActor.receiveCommand();
try {
receiveCommand.apply(createThing);
fail("Expected IllegalArgumentException to be thrown.");
} catch (final Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
}
@Test
public void createThingV2() {
new TestKit(actorSystem) {
{
final Thing thing = createThingV2WithRandomId();
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
}
};
}
@Test
public void modifyThingV2() {
final Thing thing = createThingV2WithRandomId();
final Thing modifiedThing = thing.setAttribute(JsonFactory.newPointer("foo/bar"), JsonFactory.newValue("baz"));
testModifyThing(dittoHeadersV2, thing, modifiedThing);
}
private void testModifyThing(final DittoHeaders dittoHeaders, final Thing thing, final Thing modifiedThing) {
final ModifyThing modifyThingCommand = ModifyThing.of(getIdOrThrow(thing), modifiedThing, null, dittoHeaders);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeaders);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
underTest.tell(modifyThingCommand, getRef());
expectMsgEquals(ETagTestUtils.modifyThingResponse(thing, modifiedThing, dittoHeaders, false));
}
};
}
/**
* Makes sure that it is not possible to modify a thing without a previous create. If this was possible, a thing
* could contain old data (in case of a recreate).
*/
@Test
public void modifyThingWithoutPreviousCreate() {
final Thing thing = createThingV2WithRandomId();
final ModifyThing modifyThingCommand = ModifyThing.of(getIdOrThrow(thing), thing, null, dittoHeadersV2);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
underTest.tell(modifyThingCommand, getRef());
expectMsgClass(ThingNotAccessibleException.class);
}
};
}
@Test
public void modifyThingOverwritesExistingFirstLevelFieldsWhenExplicitlySpecifiedV2() {
final Thing thingWithFirstLevelFields = createThingV2WithRandomId();
final Thing thingWithDifferentFirstLevelFields = Thing.newBuilder()
.setId(getIdOrThrow(thingWithFirstLevelFields))
.setPolicyId(PolicyId.of("org.eclipse.ditto:changedPolicyId"))
.setAttributes(Attributes.newBuilder().set("changedAttrKey", "changedAttrVal").build())
.setFeatures(Features.newBuilder().set(Feature.newBuilder().withId("changedFeatureId").build()))
.build();
doTestModifyThingKeepsOverwritesExistingFirstLevelFieldsWhenExplicitlySpecified(thingWithFirstLevelFields,
thingWithDifferentFirstLevelFields, dittoHeadersV2);
}
private void doTestModifyThingKeepsOverwritesExistingFirstLevelFieldsWhenExplicitlySpecified(
final Thing thingWithFirstLevelFields, final Thing thingWithDifferentFirstLevelFields,
final DittoHeaders dittoHeaders) {
final ThingId thingId = getIdOrThrow(thingWithFirstLevelFields);
final ModifyThing modifyThingCommand =
ModifyThing.of(thingId, thingWithDifferentFirstLevelFields, null, dittoHeaders);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thingWithFirstLevelFields);
final CreateThing createThing = CreateThing.of(thingWithFirstLevelFields, null, dittoHeaders);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thingWithFirstLevelFields);
assertPublishEvent(ThingCreated.of(thingWithFirstLevelFields, 1L, TIMESTAMP, dittoHeaders,
null));
underTest.tell(modifyThingCommand, getRef());
expectMsgEquals(
ETagTestUtils.modifyThingResponse(thingWithFirstLevelFields, thingWithDifferentFirstLevelFields,
dittoHeaders, false));
assertPublishEvent(ThingModified.of(thingWithDifferentFirstLevelFields, 2L, TIMESTAMP, dittoHeaders,
null));
final RetrieveThing retrieveThing =
RetrieveThing.getBuilder(thingId, dittoHeaders)
.withSelectedFields(ALL_FIELDS_SELECTOR)
.build();
underTest.tell(retrieveThing, getRef());
final RetrieveThingResponse retrieveThingResponse = expectMsgClass(RetrieveThingResponse.class);
assertThingInResponse(retrieveThingResponse.getThing(), thingWithDifferentFirstLevelFields);
}
};
}
@Test
public void modifyThingOverwritesExistingFirstLevelFieldsWhenNotExplicitlySpecifiedV2() {
final Thing thingWithFirstLevelFields = createThingV2WithRandomId();
doTestModifyThingOverwritesExistingFirstLevelFieldsWhenNotExplicitlySpecified(thingWithFirstLevelFields,
dittoHeadersV2);
}
private void doTestModifyThingOverwritesExistingFirstLevelFieldsWhenNotExplicitlySpecified(
final Thing thingWithFirstLevelFields, final DittoHeaders dittoHeaders) {
final ThingId thingId = getIdOrThrow(thingWithFirstLevelFields);
final Thing minimalThing = Thing.newBuilder()
.setId(thingId)
.setPolicyId(PolicyId.of(thingId))
.build();
final Thing expectedThing = minimalThing.toBuilder().setPolicyId(PolicyId.of(thingId)).build();
final ModifyThing modifyThingCommand = ModifyThing.of(thingId, minimalThing, null, dittoHeaders);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thingWithFirstLevelFields);
final CreateThing createThing = CreateThing.of(thingWithFirstLevelFields, null, dittoHeaders);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thingWithFirstLevelFields);
assertPublishEvent(ThingCreated.of(thingWithFirstLevelFields, 1L, TIMESTAMP, dittoHeaders, null));
underTest.tell(modifyThingCommand, getRef());
expectMsgEquals(ETagTestUtils.modifyThingResponse(thingWithFirstLevelFields, minimalThing, dittoHeaders,
false));
assertPublishEvent(ThingModified.of(expectedThing, 2L, TIMESTAMP, dittoHeaders, null));
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, dittoHeaders)
.withSelectedFields(ALL_FIELDS_SELECTOR)
.build();
underTest.tell(retrieveThing, getRef());
final RetrieveThingResponse retrieveThingResponse = expectMsgClass(RetrieveThingResponse.class);
assertThingInResponse(retrieveThingResponse.getThing(), expectedThing);
}
};
}
@Test
public void modifyAttributeSoThatThingGetsTooLarge() {
new TestKit(actorSystem) {
{
final ThingId thingId = ThingId.of("thing", "id");
final ThingBuilder.FromScratch thingBuilder = Thing.newBuilder()
.setId(thingId)
.setPolicyId(PolicyId.of(thingId));
int i = 0;
Thing thing;
do {
thingBuilder.setAttribute(JsonPointer.of("attr" + i), JsonValue.of(i));
thing = thingBuilder.build();
i++;
} while (thing.toJsonString().length() < TestConstants.THING_SIZE_LIMIT_BYTES);
thing = thing.removeAttribute("attr" + (i - 1));
final ActorRef underTest = createPersistenceActorFor(thing);
// creating the Thing should be possible as we are below the limit:
final CreateThing createThing = CreateThing.of(thing, null, DittoHeaders.newBuilder()
.schemaVersion(JsonSchemaVersion.V_2).build());
underTest.tell(createThing, getRef());
expectMsgClass(CreateThingResponse.class);
// but modifying the Thing attribute which would cause the Thing to exceed the limit should not be allowed:
final ModifyAttribute modifyAttribute = ModifyAttribute.of(getIdOrThrow(thing), JsonPointer.of("foo"),
JsonValue.of("bar"), DittoHeaders.newBuilder().schemaVersion(JsonSchemaVersion.V_2).build());
underTest.tell(modifyAttribute, getRef());
expectMsgClass(ThingTooLargeException.class);
}
};
}
@Test
public void retrieveThingV2() {
final Thing thing = createThingV2WithRandomId();
final ThingCommand retrieveThingCommand = RetrieveThing.of(getIdOrThrow(thing), dittoHeadersV2);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
underTest.tell(retrieveThingCommand, getRef());
expectMsgEquals(ETagTestUtils.retrieveThingResponse(thing, null, dittoHeadersV2));
}
};
}
@Test
public void retrieveThingsWithoutThingIdOfActor() {
final Thing thing = createThingV2WithRandomId();
final RetrieveThings retrieveThingsCommand =
RetrieveThings.getBuilder(ThingId.of("foo", "bar"), ThingId.of("bum", "lux")).build();
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
underTest.tell(retrieveThingsCommand, getRef());
expectNoMessage();
}
};
}
@Test
public void deleteThingV2() {
new TestKit(actorSystem) {
{
final Thing thing = createThingV2WithRandomId();
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
final DeleteThing deleteThing = DeleteThing.of(getIdOrThrow(thing), dittoHeadersV2);
underTest.tell(deleteThing, getRef());
expectMsgEquals(DeleteThingResponse.of(getIdOrThrow(thing), dittoHeadersV2));
}
};
}
/**
* Make sure that a re-created thing does not contain data from the previously deleted thing.
*/
@Test
public void deleteAndRecreateThingWithMinimumData() {
new TestKit(actorSystem) {
{
final Thing initialThing = createThingV2WithRandomId();
final ThingId thingId = getIdOrThrow(initialThing);
final PolicyId policyId = initialThing.getPolicyEntityId().orElseThrow(IllegalStateException::new);
final ActorRef underTest = createPersistenceActorFor(initialThing);
final CreateThing createThing = CreateThing.of(initialThing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), initialThing);
final DeleteThing deleteThing = DeleteThing.of(thingId, dittoHeadersV2);
underTest.tell(deleteThing, getRef());
expectMsgEquals(DeleteThingResponse.of(thingId, dittoHeadersV2));
final Thing minimalThing = Thing.newBuilder()
.setId(thingId)
.setPolicyId(policyId)
.build();
final CreateThing recreateThing = CreateThing.of(minimalThing, null, dittoHeadersV2);
underTest.tell(recreateThing, getRef());
final CreateThingResponse recreateThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(recreateThingResponse.getThingCreated().orElse(null), minimalThing);
final RetrieveThing retrieveThing =
RetrieveThing.getBuilder(thingId, dittoHeadersV2)
.withSelectedFields(ALL_FIELDS_SELECTOR)
.build();
underTest.tell(retrieveThing, getRef());
final RetrieveThingResponse retrieveThingResponse = expectMsgClass(RetrieveThingResponse.class);
assertThingInResponse(retrieveThingResponse.getThing(), minimalThing);
}
};
}
@Test
public void modifyFeatures() {
new TestKit(actorSystem) {
{
final DittoHeaders headersMockWithOtherAuth =
createDittoHeadersMock(JsonSchemaVersion.V_2, AUTH_SUBJECT);
final ThingId thingId = ThingId.of("org.eclipse.ditto", "myThing");
final Feature smokeDetector = ThingsModelFactory.newFeature("smokeDetector");
final Feature fireExtinguisher = ThingsModelFactory.newFeature("fireExtinguisher");
final Thing thing = ThingsModelFactory.newThingBuilder()
.setId(thingId)
.setPolicyId(POLICY_ID)
.setFeature(smokeDetector)
.build();
final Features featuresToModify = ThingsModelFactory.newFeatures(fireExtinguisher);
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponseV2(createThingResponse.getThingCreated().orElse(null), thing);
final ModifyFeatures modifyFeatures =
ModifyFeatures.of(thingId, featuresToModify, headersMockWithOtherAuth);
underTest.tell(modifyFeatures, getRef());
expectMsgEquals(
ETagTestUtils.modifyFeaturesResponse(thingId, featuresToModify, headersMockWithOtherAuth,
false));
final RetrieveFeatures retrieveFeatures = RetrieveFeatures.of(thingId, headersMockWithOtherAuth);
underTest.tell(retrieveFeatures, getRef());
expectMsgEquals(
ETagTestUtils.retrieveFeaturesResponse(thingId, featuresToModify, featuresToModify.toJson(),
headersMockWithOtherAuth));
}
};
}
@Test
public void modifyAttributes() {
new TestKit(actorSystem) {
{
final DittoHeaders headersMockWithOtherAuth =
createDittoHeadersMock(JsonSchemaVersion.V_2, AUTH_SUBJECT);
final ThingId thingId = ThingId.of("org.eclipse.ditto", "myThing");
final JsonPointer fooPointer = JsonFactory.newPointer("foo");
final JsonValue fooValue = JsonFactory.newValue("bar");
final JsonPointer bazPointer = JsonFactory.newPointer("baz");
final JsonValue bazValue = JsonFactory.newValue(42);
final Thing thing = ThingsModelFactory.newThingBuilder()
.setId(thingId)
.setPolicyId(POLICY_ID)
.setAttribute(fooPointer, fooValue)
.build();
final Attributes attributesToModify =
ThingsModelFactory.newAttributesBuilder().set(bazPointer, bazValue).build();
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponseV2(createThingResponse.getThingCreated().orElse(null), thing);
final ModifyAttributes modifyAttributes =
ModifyAttributes.of(thingId, attributesToModify, headersMockWithOtherAuth);
underTest.tell(modifyAttributes, getRef());
expectMsgEquals(
ETagTestUtils.modifyAttributesResponse(thingId, attributesToModify, headersMockWithOtherAuth,
false));
final RetrieveAttributes retrieveAttributes = RetrieveAttributes.of(thingId, headersMockWithOtherAuth);
underTest.tell(retrieveAttributes, getRef());
expectMsgEquals(ETagTestUtils.retrieveAttributesResponse(thingId, attributesToModify,
attributesToModify.toJson(JsonSchemaVersion.LATEST), headersMockWithOtherAuth));
}
};
}
@Test
public void modifyAttribute() {
final JsonObjectBuilder attributesBuilder = JsonFactory.newObjectBuilder();
attributesBuilder.set("foo", "bar").set("isValid", false).set("answer", 42);
final JsonObject attributes = attributesBuilder.build();
final Thing thing = ThingsModelFactory.newThingBuilder()
.setAttributes(ThingsModelFactory.newAttributes(attributes))
.setId(THING_ID)
.setPolicyId(POLICY_ID)
.build();
final JsonPointer attributeKey = JsonFactory.newPointer("isValid");
final JsonValue newAttributeValue = JsonFactory.newValue(true);
final ThingId thingId = getIdOrThrow(thing);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponseV2(createThingResponse.getThingCreated().orElse(null), thing);
// Modify attribute as authorized subject.
final ThingCommand authorizedCommand =
ModifyAttribute.of(thingId, attributeKey, newAttributeValue, dittoHeadersV2);
underTest.tell(authorizedCommand, getRef());
expectMsgEquals(
ETagTestUtils.modifyAttributeResponse(thingId, attributeKey, newAttributeValue, dittoHeadersV2,
false));
}
};
}
@Test
public void retrieveAttribute() {
final JsonPointer attributeKey = JsonFactory.newPointer("isValid");
final JsonValue attributeValue = JsonFactory.newValue(false);
final JsonObjectBuilder attributesBuilder = JsonFactory.newObjectBuilder();
attributesBuilder.set("foo", "bar").set(attributeKey, attributeValue).set("answer", 42);
final JsonObject attributes = attributesBuilder.build();
final Thing thing = ThingsModelFactory.newThingBuilder()
.setAttributes(ThingsModelFactory.newAttributes(attributes))
.setId(THING_ID)
.setPolicyId(POLICY_ID)
.build();
final ThingId thingId = getIdOrThrow(thing);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse =
expectMsgClass(java.time.Duration.ofSeconds(5), CreateThingResponse.class);
assertThingInResponseV2(createThingResponse.getThingCreated().orElse(null), thing);
// Retrieve attribute as authorized subject.
final ThingCommand authorizedCommand =
RetrieveAttribute.of(thingId, attributeKey, dittoHeadersV2);
underTest.tell(authorizedCommand, getRef());
expectMsgClass(RetrieveAttributeResponse.class);
}
};
}
@Test
public void deleteAttribute() {
final JsonPointer attributeKey = JsonFactory.newPointer("isValid");
final JsonObjectBuilder attributesBuilder = JsonFactory.newObjectBuilder();
attributesBuilder.set("foo", "bar").set(attributeKey, false).set("answer", 42);
final JsonObject attributes = attributesBuilder.build();
final Thing thing = ThingsModelFactory.newThingBuilder()
.setAttributes(ThingsModelFactory.newAttributes(attributes))
.setId(THING_ID)
.setPolicyId(POLICY_ID)
.build();
final ThingId thingId = getIdOrThrow(thing);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponseV2(createThingResponse.getThingCreated().orElse(null), thing);
// Delete attribute as authorized subject.
final ThingCommand authorizedCommand = DeleteAttribute.of(thingId, attributeKey, dittoHeadersV2);
underTest.tell(authorizedCommand, getRef());
expectMsgEquals(DeleteAttributeResponse.of(thingId, attributeKey, dittoHeadersV2));
}
};
}
@Test
public void tryToRetrieveThingAfterDeletion() {
final Thing thing = createThingV2WithRandomId();
final ThingId thingId = getIdOrThrow(thing);
final DeleteThing deleteThingCommand = DeleteThing.of(thingId, dittoHeadersV2);
final RetrieveThing retrieveThingCommand = RetrieveThing.of(thingId, dittoHeadersV2);
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
underTest.tell(deleteThingCommand, getRef());
expectMsgEquals(DeleteThingResponse.of(thingId, dittoHeadersV2));
underTest.tell(retrieveThingCommand, getRef());
expectMsgClass(ThingNotAccessibleException.class);
}
};
}
@Test
public void recoverThingCreated() {
new TestKit(actorSystem) {
{
final Thing thing = createThingV2WithRandomId();
final ThingId thingId = getIdOrThrow(thing);
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
// restart actor to recover thing state
watch(underTest);
underTest.tell(PoisonPill.getInstance(), getRef());
expectTerminated(underTest);
final ActorRef underTestAfterRestart = Retry.untilSuccess(() -> createPersistenceActorFor(thing));
final RetrieveThing retrieveThing = RetrieveThing.of(thingId, dittoHeadersV2);
Awaitility.await().atMost(10L, TimeUnit.SECONDS).untilAsserted(() -> {
underTestAfterRestart.tell(retrieveThing, getRef());
final RetrieveThingResponse retrieveThingResponse = expectMsgClass(RetrieveThingResponse.class);
final Thing thingAsPersisted = retrieveThingResponse.getThing();
assertThat(thingAsPersisted.getEntityId()).contains(getIdOrThrow(thing));
assertThat(thingAsPersisted.getAttributes()).isEqualTo(thing.getAttributes());
assertThat(thingAsPersisted.getFeatures()).isEqualTo(thing.getFeatures());
});
}
};
}
@Test
public void recoverThingDeleted() {
new TestKit(actorSystem) {
{
final Thing thing = createThingV2WithRandomId();
final ThingId thingId = getIdOrThrow(thing);
ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
final DeleteThing deleteThing = DeleteThing.of(thingId, dittoHeadersV2);
underTest.tell(deleteThing, getRef());
expectMsgEquals(DeleteThingResponse.of(thingId, dittoHeadersV2));
// restart actor to recover thing state
watch(underTest);
underTest.tell(PoisonPill.getInstance(), getRef());
expectTerminated(underTest);
underTest = Retry.untilSuccess(() -> createPersistenceActorFor(thing));
final RetrieveThing retrieveThing = RetrieveThing.of(thingId, dittoHeadersV2);
underTest.tell(retrieveThing, getRef());
// A deleted Thing cannot be retrieved anymore (or is not accessible during initiation on slow systems)
expectMsgClass(ThingNotAccessibleException.class);
}
};
}
@Test
public void ensureSequenceNumberCorrectness() {
new TestKit(actorSystem) {
{
final Thing thing = createThingV2WithRandomId();
final ThingId thingId = getIdOrThrow(thing);
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
// modify the thing's attributes - results in sequence number 2
final Thing thingToModify = thing.setAttributes(THING_ATTRIBUTES.setValue("foo", "bar"));
final ModifyThing modifyThing = ModifyThing.of(thingId, thingToModify, null, dittoHeadersV2);
underTest.tell(modifyThing, getRef());
expectMsgEquals(ETagTestUtils.modifyThingResponse(thing, thingToModify, dittoHeadersV2, false));
// retrieve the thing's sequence number
final JsonFieldSelector versionFieldSelector =
JsonFactory.newFieldSelector(Thing.JsonFields.REVISION.getPointer().toString(),
JSON_PARSE_OPTIONS);
final long versionExpected = 2;
final Thing thingExpected = ThingsModelFactory.newThingBuilder(thingToModify)
.setRevision(versionExpected)
.build();
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, dittoHeadersV2)
.withSelectedFields(versionFieldSelector)
.build();
underTest.tell(retrieveThing, getRef());
expectMsgEquals(
ETagTestUtils.retrieveThingResponse(thingExpected, versionFieldSelector, dittoHeadersV2));
}
};
}
@Test
public void ensureSequenceNumberCorrectnessAfterRecovery() {
new TestKit(actorSystem) {
{
final Thing thing = createThingV2WithRandomId();
final ThingId thingId = getIdOrThrow(thing);
final ActorRef underTest = createPersistenceActorFor(thing);
final CreateThing createThing = CreateThing.of(thing, null, dittoHeadersV2);
underTest.tell(createThing, getRef());
final CreateThingResponse createThingResponse = expectMsgClass(CreateThingResponse.class);
assertThingInResponse(createThingResponse.getThingCreated().orElse(null), thing);
// modify the thing's attributes - results in sequence number 2
final Thing thingToModify = thing.setAttributes(THING_ATTRIBUTES.setValue("foo", "bar"));
final ModifyThing modifyThing = ModifyThing.of(thingId, thingToModify, null, dittoHeadersV2);
underTest.tell(modifyThing, getRef());
expectMsgEquals(ETagTestUtils.modifyThingResponse(thing, thingToModify, dittoHeadersV2, false));
// retrieve the thing's sequence number from recovered actor
final JsonFieldSelector versionFieldSelector =
JsonFactory.newFieldSelector(Thing.JsonFields.REVISION.getPointer().toString(),
JSON_PARSE_OPTIONS);
final long versionExpected = 2;
final Thing thingExpected = ThingsModelFactory.newThingBuilder(thingToModify)
.setRevision(versionExpected)
.build();
// restart actor to recover thing state
watch(underTest);
underTest.tell(PoisonPill.getInstance(), getRef());
expectTerminated(underTest);
final ActorRef underTestAfterRestart = Retry.untilSuccess(() -> createPersistenceActorFor(thing));
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, dittoHeadersV2)
.withSelectedFields(versionFieldSelector)
.build();
Awaitility.await().atMost(10L, TimeUnit.SECONDS).untilAsserted(() -> {
underTestAfterRestart.tell(retrieveThing, getRef());
expectMsgEquals(ETagTestUtils.retrieveThingResponse(thingExpected,
versionFieldSelector,
dittoHeadersV2));
});
}
};
}
@Test
public void createThingInV2WithMissingPolicyIdThrowsPolicyIdMissingException() {
final ThingId thingIdOfActor = ThingId.of("test.ns.v1", "createThingInV2WithMissingPolicyId");
final Thing thingV2 = ThingsModelFactory.newThingBuilder()
.setAttributes(THING_ATTRIBUTES)
.setId(thingIdOfActor)
.build();
new TestKit(actorSystem) {
{
final ActorRef underTest = createPersistenceActorFor(thingV2);
final CreateThing createThingV2 = CreateThing.of(thingV2, null, dittoHeadersV2);
underTest.tell(createThingV2, getRef());
expectMsgClass(PolicyIdMissingException.class);
}
};
}
@Test
public void responsesDuringInitializationAreSentWithDittoHeaders() {
new TestKit(actorSystem) {
{
final ThingId thingId = ThingId.of("org.eclipse.ditto", "myThing");
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder()
.authorizationContext(
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance("authSubject")))
.correlationId("correlationId")
.schemaVersion(JsonSchemaVersion.LATEST)
.build();
final ActorRef underTest = createPersistenceActorFor(thingId);
final RetrieveThing retrieveThing = RetrieveThing.of(thingId, dittoHeaders);
final ThingNotAccessibleException thingNotAccessibleException =
ThingNotAccessibleException.newBuilder(thingId)