/
EnforcementFlowTest.java
865 lines (740 loc) · 45.7 KB
/
EnforcementFlowTest.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
/*
* Copyright (c) 2021 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.thingsearch.service.persistence.write.streaming;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.stream.KillSwitches;
import org.apache.pekko.stream.javadsl.Keep;
import org.apache.pekko.stream.testkit.TestPublisher;
import org.apache.pekko.stream.testkit.TestSubscriber;
import org.apache.pekko.stream.testkit.javadsl.TestSink;
import org.apache.pekko.stream.testkit.javadsl.TestSource;
import org.apache.pekko.testkit.TestActor;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.api.PolicyTag;
import org.eclipse.ditto.policies.api.commands.sudo.SudoRetrievePolicy;
import org.eclipse.ditto.policies.api.commands.sudo.SudoRetrievePolicyResponse;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.PolicyImport;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThing;
import org.eclipse.ditto.things.api.commands.sudo.SudoRetrieveThingResponse;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.AttributeDeleted;
import org.eclipse.ditto.things.model.signals.events.AttributeModified;
import org.eclipse.ditto.things.model.signals.events.ThingCreated;
import org.eclipse.ditto.things.model.signals.events.ThingDeleted;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.things.model.signals.events.ThingMerged;
import org.eclipse.ditto.things.model.signals.events.ThingModified;
import org.eclipse.ditto.thingsearch.service.common.config.DefaultStreamConfig;
import org.eclipse.ditto.thingsearch.service.common.config.StreamConfig;
import org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.AbstractWriteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.Metadata;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.ThingDeleteModel;
import org.eclipse.ditto.thingsearch.service.persistence.write.model.ThingWriteModel;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.typesafe.config.ConfigFactory;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
/**
* Unit tests for {@link EnforcementFlow}. Contains fix method order to allow for longer setup during first test.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public final class EnforcementFlowTest {
private ActorSystem system;
private TestPublisher.Probe<Collection<Metadata>> sourceProbe;
private TestSubscriber.Probe<List<AbstractWriteModel>> sinkProbe;
@Before
public void init() {
system = ActorSystem.create("test", ConfigFactory.load("actors-test.conf"));
}
@After
public void cleanup() {
if (system != null) {
TestKit.shutdownActorSystem(system);
}
}
@Test
public void updateThingAndPolicyRevisions() {
new TestKit(system) {{
// GIVEN: enqueued metadata is out of date
final long thingRev1 = 1L;
final long thingRev2 = 99L;
final long policyRev1 = 2L;
final long policyRev2 = 98L;
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Metadata metadata =
Metadata.of(thingId, thingRev1, PolicyTag.of(policyId, policyRev1), null, Set.of(), null);
final Collection<Metadata> input = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(input);
sourceProbe.sendComplete();
// WHEN: thing and policy are retrieved with up-to-date revisions
thingsProbe.expectMsgClass(FiniteDuration.apply(10, TimeUnit.SECONDS), SudoRetrieveThing.class);
final var thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).setRevision(thingRev2).build();
final var thingJson = thing.toJson(FieldType.regularOrSpecial());
assertThat(thingJson.getValue(Thing.JsonFields.REVISION)).contains(thingRev2);
thingsProbe.reply(SudoRetrieveThingResponse.of(thingJson, DittoHeaders.empty()));
policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
final var policy = Policy.newBuilder(policyId).setRevision(policyRev2).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty()));
// THEN: the write model contains up-to-date revisions
final AbstractWriteModel writeModel = sinkProbe.expectNext().get(0);
sinkProbe.expectComplete();
assertThat(writeModel).isInstanceOf(ThingWriteModel.class);
final var document = JsonObject.of(((ThingWriteModel) writeModel).getThingDocument().toJson());
assertThat(document.getValue("_id")).contains(JsonValue.of(thingId));
assertThat(document.getValue("policyId")).contains(JsonValue.of(policyId));
assertThat(document.getValue("_revision")).contains(JsonValue.of(thingRev2));
assertThat(document.getValue("__policyRev")).contains(JsonValue.of(policyRev2));
}};
}
@Test
public void ignoreCacheWhenRequestedToUpdate() {
// GIVEN: the enqueued metadata is out-of-date
final long thingRev1 = 1L;
final long thingRev2 = 99L;
final long policyRev1 = 2L;
final long policyRev2 = 98L;
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Metadata metadata1 = Metadata.of(thingId, thingRev1, PolicyTag.of(policyId, policyRev1), null, Set.of(), null);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(2);
sourceProbe.sendNext(List.of(metadata1));
thingsProbe.expectMsgClass(SudoRetrieveThing.class);
final var thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).setRevision(thingRev2).build();
final var thingJson = thing.toJson(FieldType.regularOrSpecial());
thingsProbe.reply(SudoRetrieveThingResponse.of(thingJson, DittoHeaders.empty()));
// GIVEN: enforcer cache is loaded with out-of-date policy
policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
final var policy1 = Policy.newBuilder(policyId).setRevision(policyRev1).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(policyId, policy1, DittoHeaders.empty()));
final AbstractWriteModel writeModel1 = sinkProbe.expectNext().get(0);
assertThat(writeModel1).isInstanceOf(ThingWriteModel.class);
final var document1 = JsonObject.of(((ThingWriteModel) writeModel1).getThingDocument().toJson());
assertThat(document1.getValue("_revision")).contains(JsonValue.of(thingRev2));
assertThat(document1.getValue("__policyRev")).contains(JsonValue.of(policyRev1));
// WHEN: a metadata with 'invalidateCache' flag is enqueued
final Metadata metadata2 = metadata1.invalidateCaches(true, true);
sourceProbe.sendNext(List.of(metadata2));
sourceProbe.sendComplete();
thingsProbe.expectMsgClass(SudoRetrieveThing.class);
thingsProbe.reply(SudoRetrieveThingResponse.of(thingJson, DittoHeaders.empty()));
// THEN: policy cache is reloaded
policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
final var policy2 = Policy.newBuilder(policyId).setRevision(policyRev2).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(policyId, policy2, DittoHeaders.empty()));
// THEN: write model contains up-to-date policy revisions.
final AbstractWriteModel writeModel2 = sinkProbe.expectNext().get(0);
sinkProbe.expectComplete();
assertThat(writeModel2).isInstanceOf(ThingWriteModel.class);
final var document2 = JsonObject.of(((ThingWriteModel) writeModel2).getThingDocument().toJson());
assertThat(document2.getValue("_revision")).contains(JsonValue.of(thingRev2));
assertThat(document2.getValue("__policyRev")).contains(JsonValue.of(policyRev2));
}
@Test
public void importedPolicyIsOnlyLoadedOnceWhenTwoDifferentPoliciesImportFromItAndInvalidatesImportedOnlyOnce() {
final long thing1Rev1 = 1L;
final long thing1Rev2 = 1L;
final long thing2Rev1 = 2L;
final long thing2Rev2 = 2L;
final long importedPolicyRev1 = 3L;
final long importedPolicyRev2 = 4L;
final long policy1Rev1 = 2L;
final long policy2Rev1 = 3L;
final ThingId thingId1 = ThingId.of("thing:id-1");
final ThingId thingId2 = ThingId.of("thing:id-2");
final PolicyId importedPolicyId = PolicyId.of("policy:imported");
final PolicyId importingPolicy1Id = PolicyId.of("policy:importing-1");
final PolicyId importingPolicy2Id = PolicyId.of("policy:importing-2");
final Metadata metadata1 = Metadata.of(thingId1, thing1Rev1, PolicyTag.of(importingPolicy1Id, policy1Rev1),
PolicyTag.of(importedPolicyId, importedPolicyRev2), Set.of(), null);
final Metadata metadata2 = Metadata.of(thingId2, thing2Rev1, PolicyTag.of(importingPolicy2Id, policy2Rev1),
PolicyTag.of(importedPolicyId, importedPolicyRev2), Set.of(), null);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(2);
sourceProbe.sendNext(List.of(metadata1));
thingsProbe.expectMsgClass(SudoRetrieveThing.class);
final var thing = Thing.newBuilder().setId(thingId1).setPolicyId(importingPolicy1Id).setRevision(thing1Rev2).build();
final var thingJson = thing.toJson(FieldType.regularOrSpecial());
thingsProbe.reply(SudoRetrieveThingResponse.of(thingJson, DittoHeaders.empty()));
final SudoRetrievePolicy sudoRetrievePolicy1 = policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat(sudoRetrievePolicy1.getEntityId().toString()).isEqualTo(importingPolicy1Id.toString());
final var policy1 = Policy.newBuilder(importingPolicy1Id)
.setPolicyImport(PolicyImport.newInstance(importedPolicyId, null))
.setRevision(policy1Rev1)
.build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(importingPolicy1Id, policy1, DittoHeaders.empty()));
final SudoRetrievePolicy sudoRetrieveImportedPolicy = policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat(sudoRetrieveImportedPolicy.getEntityId().toString()).isEqualTo(importedPolicyId.toString());
final var importedPolicy = Policy.newBuilder(importedPolicyId).setRevision(importedPolicyRev1).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(importedPolicyId, importedPolicy, DittoHeaders.empty()));
final AbstractWriteModel writeModel1 = sinkProbe.expectNext().get(0);
assertThat(writeModel1).isInstanceOf(ThingWriteModel.class);
final var document1 = JsonObject.of(((ThingWriteModel) writeModel1).getThingDocument().toJson());
assertThat(document1.getValue(PersistenceConstants.FIELD_REVISION)).contains(JsonValue.of(thing1Rev2));
assertThat(document1.getValue(PersistenceConstants.FIELD_POLICY_REVISION)).contains(JsonValue.of(policy1Rev1));
assertThat(document1.getValue(PersistenceConstants.FIELD_REFERENCED_POLICIES))
.contains(JsonArray.of(
PolicyTag.of(importedPolicyId, importedPolicyRev1).toJson(),
PolicyTag.of(importingPolicy1Id, policy1Rev1).toJson()
));
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(2);
sourceProbe.sendNext(List.of(metadata2));
thingsProbe.expectMsgClass(SudoRetrieveThing.class);
final var thing2 = Thing.newBuilder().setId(thingId2).setPolicyId(importingPolicy2Id).setRevision(thing2Rev2).build();
final var thing2Json = thing2.toJson(FieldType.regularOrSpecial());
thingsProbe.reply(SudoRetrieveThingResponse.of(thing2Json, DittoHeaders.empty()));
// GIVEN: enforcer cache is loaded with out-of-date policy
final SudoRetrievePolicy sudoRetrievePolicy2 = policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat(sudoRetrievePolicy2.getEntityId().toString()).isEqualTo(importingPolicy2Id.toString());
final var policy2 = Policy.newBuilder(importingPolicy2Id)
.setPolicyImport(PolicyImport.newInstance(importedPolicyId, null))
.setRevision(policy2Rev1)
.build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(importingPolicy2Id, policy2, DittoHeaders.empty()));
policiesProbe.expectNoMessage();
final AbstractWriteModel writeModel2 = sinkProbe.expectNext().get(0);
assertThat(writeModel2).isInstanceOf(ThingWriteModel.class);
final var document2 = JsonObject.of(((ThingWriteModel) writeModel2).getThingDocument().toJson());
assertThat(document2.getValue(PersistenceConstants.FIELD_REVISION)).contains(JsonValue.of(thing2Rev2));
assertThat(document2.getValue(PersistenceConstants.FIELD_POLICY_REVISION)).contains(JsonValue.of(policy2Rev1));
assertThat(document2.getValue(PersistenceConstants.FIELD_REFERENCED_POLICIES))
.contains(JsonArray.of(
PolicyTag.of(importedPolicyId, importedPolicyRev1).toJson(),
PolicyTag.of(importingPolicy2Id, policy2Rev1).toJson()
));
// WHEN: a metadata with 'invalidateCache' flag is enqueued
final Metadata metadata1_2 = metadata1
.withCausingPolicyTag(PolicyTag.of(importedPolicyId, importedPolicyRev2))
.invalidateCaches(false, true);
sinkProbe.request(2);
sourceProbe.sendNext(List.of(metadata1_2));
thingsProbe.expectMsgClass(SudoRetrieveThing.class);
thingsProbe.reply(SudoRetrieveThingResponse.of(thingJson, DittoHeaders.empty()));
// THEN: policy cache is reloaded
final SudoRetrievePolicy sudoRetrievePolicy1_2 = policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat(sudoRetrievePolicy1_2.getEntityId().toString()).isEqualTo(importingPolicy1Id.toString());
final var policy1_2 = Policy.newBuilder(importingPolicy1Id)
.setPolicyImport(PolicyImport.newInstance(importedPolicyId, null))
.setRevision(policy1Rev1)
.build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(importingPolicy1Id, policy1_2, DittoHeaders.empty()));
final SudoRetrievePolicy sudoRetrieveImportedPolicy_2 = policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat(sudoRetrieveImportedPolicy_2.getEntityId().toString()).isEqualTo(importedPolicyId.toString());
final var importedPolicy_2 = Policy.newBuilder(importedPolicyId).setRevision(importedPolicyRev2).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(importedPolicyId, importedPolicy_2, DittoHeaders.empty()));
// THEN: write model contains up-to-date policy revisions.
final AbstractWriteModel writeModel1_2 = sinkProbe.expectNext().get(0);
assertThat(writeModel1_2).isInstanceOf(ThingWriteModel.class);
final var document1_2 = JsonObject.of(((ThingWriteModel) writeModel1_2).getThingDocument().toJson());
assertThat(document1_2.getValue(PersistenceConstants.FIELD_REVISION)).contains(JsonValue.of(thing1Rev2));
assertThat(document1_2.getValue(PersistenceConstants.FIELD_POLICY_REVISION)).contains(JsonValue.of(policy1Rev1));
assertThat(document1_2.getValue(PersistenceConstants.FIELD_REFERENCED_POLICIES))
.contains(JsonArray.of(
PolicyTag.of(importedPolicyId, importedPolicyRev2).toJson(),
PolicyTag.of(importingPolicy1Id, policy1Rev1).toJson()
));
final Metadata metadata2_2 = metadata2
.withCausingPolicyTag(PolicyTag.of(importedPolicyId, importedPolicyRev2))
.invalidateCaches(false, true);
sinkProbe.request(2);
sourceProbe.sendNext(List.of(metadata2_2));
sourceProbe.sendComplete();
thingsProbe.expectMsgClass(SudoRetrieveThing.class);
thingsProbe.reply(SudoRetrieveThingResponse.of(thing2Json, DittoHeaders.empty()));
// THEN: policy cache is reloaded
final SudoRetrievePolicy sudoRetrievePolicy2_2 = policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat(sudoRetrievePolicy2_2.getEntityId().toString()).isEqualTo(importingPolicy2Id.toString());
final var policy2_2 = Policy.newBuilder(importingPolicy2Id)
.setPolicyImport(PolicyImport.newInstance(importedPolicyId, null))
.setRevision(policy2Rev1)
.build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(importingPolicy2Id, policy2_2, DittoHeaders.empty()));
policiesProbe.expectNoMessage();
// THEN: write model contains up-to-date policy revisions.
final AbstractWriteModel writeModel2_2 = sinkProbe.expectNext().get(0);
sinkProbe.expectComplete();
assertThat(writeModel2_2).isInstanceOf(ThingWriteModel.class);
final var document2_2 = JsonObject.of(((ThingWriteModel) writeModel2_2).getThingDocument().toJson());
assertThat(document2_2.getValue(PersistenceConstants.FIELD_REVISION)).contains(JsonValue.of(thing2Rev2));
assertThat(document2_2.getValue(PersistenceConstants.FIELD_POLICY_REVISION)).contains(JsonValue.of(policy2Rev1));
assertThat(document2_2.getValue(PersistenceConstants.FIELD_REFERENCED_POLICIES))
.contains(JsonArray.of(
PolicyTag.of(importedPolicyId, importedPolicyRev2).toJson(),
PolicyTag.of(importingPolicy2Id, policy2Rev1).toJson()
));
}
@Test
public void computeThingCacheValueFromThingEvents() {
new TestKit(system) {{
// GIVEN: enqueued metadata contains events sufficient to determine the thing state
final DittoHeaders headers = DittoHeaders.empty();
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).build();
final List<ThingEvent<?>> events = List.of(
ThingModified.of(thing, 1, null, headers, null),
ThingDeleted.of(thingId, 2, null, headers, null),
ThingCreated.of(thing.toBuilder()
.setAttribute(JsonPointer.of("w"), JsonValue.of(4))
.setAttribute(JsonPointer.of("x"), JsonValue.of(5))
.build(), 3,
null, headers, null),
ThingMerged.of(thingId, JsonPointer.of("attributes/y"), JsonValue.of(6), 4, null, headers, null),
AttributeModified.of(thingId, JsonPointer.of("z"), JsonValue.of(7), 5, null, headers, null),
AttributeDeleted.of(thingId, JsonPointer.of("w"), 6, null, headers, null)
);
final Metadata metadata =
Metadata.of(thingId, 5L, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null);
final List<Metadata> inputMap = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(inputMap);
sourceProbe.sendComplete();
thingsProbe.expectMsgClass(Duration.apply(30, TimeUnit.SECONDS), SudoRetrieveThing.class);
thingsProbe.reply(SudoRetrieveThingResponse.of(JsonObject.empty(), DittoHeaders.empty()));
// WHEN: policy is retrieved with up-to-date revisions
policiesProbe.expectMsgClass(Duration.apply(30, TimeUnit.SECONDS), SudoRetrievePolicy.class);
final var policy = Policy.newBuilder(policyId).setRevision(1).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty()));
// THEN: the write model contains up-to-date revisions
final AbstractWriteModel writeModel = sinkProbe.expectNext().get(0);
sinkProbe.expectComplete();
assertThat(writeModel).isInstanceOf(ThingWriteModel.class);
final var document = JsonObject.of(((ThingWriteModel) writeModel).getThingDocument().toJson());
assertThat(document.getValue("_id")).contains(JsonValue.of(thingId));
assertThat(document.getValue("policyId")).contains(JsonValue.of(policyId));
assertThat(document.getValue("_revision")).contains(JsonValue.of(6));
assertThat(document.getValue("__policyRev")).contains(JsonValue.of(1));
assertThat(document.getValue("t/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));
// THEN: thing is computed in the cache
thingsProbe.expectNoMessage(FiniteDuration.Zero());
}};
}
@Test
public void computeThingCacheValueFromThingEventsWhenLastEventWasDeleted() {
new TestKit(system) {{
// GIVEN: enqueued metadata contains events sufficient to determine the thing state
final DittoHeaders headers = DittoHeaders.empty();
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).build();
final Instant deletedTime = Instant.now();
final List<ThingEvent<?>> events = List.of(
ThingModified.of(thing, 1, deletedTime.minusSeconds(1), headers, null),
ThingDeleted.of(thingId, 2, deletedTime, headers, null),
ThingCreated.of(thing.toBuilder()
.setAttribute(JsonPointer.of("w"), JsonValue.of(4))
.setAttribute(JsonPointer.of("x"), JsonValue.of(5))
.build(), 3,
deletedTime.minusSeconds(5), headers, null),
ThingMerged.of(thingId, JsonPointer.of("attributes/y"), JsonValue.of(6), 4,
deletedTime.minusSeconds(4), headers, null),
AttributeModified.of(thingId, JsonPointer.of("z"), JsonValue.of(7), 5, deletedTime.minusSeconds(3),
headers, null),
AttributeDeleted.of(thingId, JsonPointer.of("w"), 6, deletedTime.minusSeconds(2), headers, null)
);
final Metadata metadata =
Metadata.of(thingId, 5L, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null);
final List<Metadata> inputMap = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(inputMap);
sourceProbe.sendComplete();
// WHEN
thingsProbe.expectMsgClass(Duration.apply(30, TimeUnit.SECONDS), SudoRetrieveThing.class);
thingsProbe.reply(SudoRetrieveThingResponse.of(JsonObject.empty(), DittoHeaders.empty()));
// THEN: the write model contains up-to-date revisions
final AbstractWriteModel deleteModel = sinkProbe.expectNext().get(0);
sinkProbe.expectComplete();
assertThat(deleteModel).isInstanceOf(ThingDeleteModel.class);
final var modelMetadata = deleteModel.getMetadata();
assertThat(modelMetadata.getThingId().toString()).hasToString(thingId.toString());
// THEN: thing is computed in the cache
thingsProbe.expectNoMessage(FiniteDuration.Zero());
}};
}
@Test
public void forceRetrieveThing() {
new TestKit(system) {{
final DittoHeaders headers = DittoHeaders.empty();
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).build();
final List<ThingEvent<?>> events = List.of(
ThingModified.of(thing, 1, null, headers, null),
ThingDeleted.of(thingId, 2, null, headers, null),
ThingCreated.of(thing.toBuilder()
.setAttribute(JsonPointer.of("w"), JsonValue.of(4))
.setAttribute(JsonPointer.of("x"), JsonValue.of(5))
.build(), 3,
null, headers, null),
ThingMerged.of(thingId, JsonPointer.of("attributes/y"), JsonValue.of(6), 4, null, headers, null),
AttributeModified.of(thingId, JsonPointer.of("z"), JsonValue.of(7), 5, null, headers, null),
AttributeDeleted.of(thingId, JsonPointer.of("w"), 6, null, headers, null)
);
final Metadata metadata = Metadata.of(thingId, 6L, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null)
.invalidateCaches(true, true);
final List<Metadata> inputMap = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(inputMap);
sourceProbe.sendComplete();
assertThat((CharSequence) thingsProbe.expectMsgClass(SudoRetrieveThing.class).getEntityId())
.isEqualTo(thingId);
}};
}
@Test
public void eventSequenceNumberTooLow() {
new TestKit(system) {{
final DittoHeaders headers = DittoHeaders.empty();
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).build();
final List<ThingEvent<?>> events = List.of(
ThingModified.of(thing, 1, null, headers, null),
ThingDeleted.of(thingId, 2, null, headers, null),
ThingCreated.of(thing.toBuilder()
.setAttribute(JsonPointer.of("w"), JsonValue.of(4))
.setAttribute(JsonPointer.of("x"), JsonValue.of(5))
.build(), 3,
null, headers, null),
ThingMerged.of(thingId, JsonPointer.of("attributes/y"), JsonValue.of(6), 4, null, headers, null),
AttributeModified.of(thingId, JsonPointer.of("z"), JsonValue.of(7), 5, null, headers, null),
AttributeDeleted.of(thingId, JsonPointer.of("w"), 6, null, headers, null)
);
final Metadata metadata =
Metadata.of(thingId, 7L, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null);
final List<Metadata> inputMap = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(inputMap);
sourceProbe.sendComplete();
assertThat((CharSequence) thingsProbe.expectMsgClass(SudoRetrieveThing.class).getEntityId())
.isEqualTo(thingId);
}};
}
@Test
public void eventMissed() {
new TestKit(system) {{
final DittoHeaders headers = DittoHeaders.empty();
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).build();
final List<ThingEvent<?>> events = List.of(
ThingModified.of(thing, 1, null, headers, null),
ThingDeleted.of(thingId, 2, null, headers, null),
ThingCreated.of(thing.toBuilder()
.setAttribute(JsonPointer.of("w"), JsonValue.of(4))
.setAttribute(JsonPointer.of("x"), JsonValue.of(5))
.build(), 3,
null, headers, null),
AttributeModified.of(thingId, JsonPointer.of("z"), JsonValue.of(7), 5, null, headers, null),
AttributeDeleted.of(thingId, JsonPointer.of("w"), 6, null, headers, null)
);
final Metadata metadata =
Metadata.of(thingId, 6L, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null);
final List<Metadata> inputMap = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(inputMap);
sourceProbe.sendComplete();
assertThat((CharSequence) thingsProbe.expectMsgClass(SudoRetrieveThing.class).getEntityId())
.isEqualTo(thingId);
}};
}
@Test
public void noInitialCreatedOrDeletedEvent() {
new TestKit(system) {{
final DittoHeaders headers = DittoHeaders.empty();
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).build();
final List<ThingEvent<?>> events = List.of(
ThingModified.of(thing.toBuilder()
.setAttribute(JsonPointer.of("w"), JsonValue.of(4))
.setAttribute(JsonPointer.of("x"), JsonValue.of(5))
.build(), 3,
null, headers, null),
ThingMerged.of(thingId, JsonPointer.of("attributes/y"), JsonValue.of(6), 4, null, headers, null),
AttributeModified.of(thingId, JsonPointer.of("z"), JsonValue.of(7), 5, null, headers, null),
AttributeDeleted.of(thingId, JsonPointer.of("w"), 6, null, headers, null)
);
final Metadata metadata =
Metadata.of(thingId, 6L, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null);
final List<Metadata> inputMap = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(inputMap);
sourceProbe.sendComplete();
// WHEN: thing and policy caches are loaded
final var sudoRetrieveThing = thingsProbe.expectMsgClass(SudoRetrieveThing.class);
thingsProbe.reply(SudoRetrieveThingResponse.of(
thing.toBuilder().setRevision(2).build().toJson(FieldType.all()),
sudoRetrieveThing.getDittoHeaders()));
policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
final var policy = Policy.newBuilder(policyId).setRevision(1).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty()));
// THEN: the write model contains up-to-date revisions
final AbstractWriteModel writeModel = sinkProbe.expectNext().get(0);
sinkProbe.expectComplete();
assertThat(writeModel).isInstanceOf(ThingWriteModel.class);
final var document = JsonObject.of(((ThingWriteModel) writeModel).getThingDocument().toJson());
assertThat(document.getValue("_id")).contains(JsonValue.of(thingId));
assertThat(document.getValue("policyId")).contains(JsonValue.of(policyId));
assertThat(document.getValue("_revision")).contains(JsonValue.of(6));
assertThat(document.getValue("__policyRev")).contains(JsonValue.of(1));
assertThat(document.getValue("t/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));
// THEN: thing is computed in the cache
thingsProbe.expectNoMessage(FiniteDuration.Zero());
}};
}
@Test
public void onlyApplyRelevantEvents() {
new TestKit(system) {{
final DittoHeaders headers = DittoHeaders.empty();
final ThingId thingId = ThingId.of("thing:id");
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setId(thingId).setPolicyId(policyId).build();
final List<ThingEvent<?>> events = List.of(
// this event should be skipped because cache loader returns revision 2
ThingMerged.of(thingId, JsonPointer.of("attributes/v"), JsonValue.of(3), 2, null, headers, null),
// these events should be applied
ThingModified.of(thing.toBuilder()
.setAttribute(JsonPointer.of("w"), JsonValue.of(4))
.setAttribute(JsonPointer.of("x"), JsonValue.of(5))
.build(), 3,
null, headers, null),
ThingMerged.of(thingId, JsonPointer.of("attributes/y"), JsonValue.of(6), 4, null, headers, null),
AttributeModified.of(thingId, JsonPointer.of("z"), JsonValue.of(7), 5, null, headers, null),
AttributeDeleted.of(thingId, JsonPointer.of("w"), 6, null, headers, null)
);
final Metadata metadata =
Metadata.of(thingId, 6L, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null);
final List<Metadata> inputMap = List.of(metadata);
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.empty());
final EnforcementFlow underTest =
EnforcementFlow.of(system, streamConfig, thingsProbe.ref(), policiesProbe.ref(),
system.getScheduler());
materializeTestProbes(underTest);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(1);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
sourceProbe.sendNext(inputMap);
sourceProbe.sendComplete();
// WHEN: thing and policy caches are loaded
final var sudoRetrieveThing = thingsProbe.expectMsgClass(SudoRetrieveThing.class);
thingsProbe.reply(SudoRetrieveThingResponse.of(
thing.toBuilder().setRevision(2).build().toJson(FieldType.all()),
sudoRetrieveThing.getDittoHeaders()));
policiesProbe.expectMsgClass(SudoRetrievePolicy.class);
final var policy = Policy.newBuilder(policyId).setRevision(1).build();
policiesProbe.reply(SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty()));
// THEN: the write model contains up-to-date revisions
final AbstractWriteModel writeModel = sinkProbe.expectNext().get(0);
sinkProbe.expectComplete();
assertThat(writeModel).isInstanceOf(ThingWriteModel.class);
final var document = JsonObject.of(((ThingWriteModel) writeModel).getThingDocument().toJson());
assertThat(document.getValue("_id")).contains(JsonValue.of(thingId));
assertThat(document.getValue("policyId")).contains(JsonValue.of(policyId));
assertThat(document.getValue("_revision")).contains(JsonValue.of(6));
assertThat(document.getValue("__policyRev")).contains(JsonValue.of(1));
assertThat(document.getValue("t/attributes")).contains(JsonObject.of("{\"x\":5,\"y\":6,\"z\":7}"));
// THEN: thing is computed in the cache
thingsProbe.expectNoMessage(FiniteDuration.Zero());
}};
}
@Test
public void thereCanBeMultipleUpdatesPerBulk() {
new TestKit(system) {{
final DittoHeaders headers = DittoHeaders.empty();
final PolicyId policyId = PolicyId.of("policy:id");
final Thing thing = Thing.newBuilder().setPolicyId(policyId).build();
final var policy = Policy.newBuilder(policyId).setRevision(1).build();
final List<List<Metadata>> changeMaps = List.of(IntStream.range(1, 5).mapToObj(i -> {
final ThingId thingId = ThingId.of("thing:" + i);
final Thing ithThing = thing.toBuilder().setId(thingId).setRevision(i).build();
final List<ThingEvent<?>> events = List.of(ThingModified.of(ithThing, i, null, headers, null));
return Metadata.of(thingId, i, PolicyTag.of(policyId, 1L), null, Set.of(), events, null, null);
}).toList());
final TestProbe thingsProbe = TestProbe.apply(system);
final TestProbe policiesProbe = TestProbe.apply(system);
final StreamConfig streamConfig = DefaultStreamConfig.of(ConfigFactory.parseString(
"stream.ask-with-retry.ask-timeout=30s"));
final EnforcementFlow underTest = EnforcementFlow.of(system, streamConfig, thingsProbe.ref(),
policiesProbe.ref(), system.getScheduler());
final var sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
policiesProbe.setAutoPilot(new TestActor.AutoPilot() {
@Override
public TestActor.AutoPilot run(final ActorRef sender, final Object msg) {
sender.tell(sudoRetrievePolicyResponse, policiesProbe.ref());
return keepRunning();
}
});
thingsProbe.setAutoPilot(new TestActor.AutoPilot() {
@Override
public TestActor.AutoPilot run(final ActorRef sender, final Object msg) {
if (msg instanceof final SudoRetrieveThing command) {
final var thingId = (ThingId) command.getEntityId();
final int i = Integer.parseInt(thingId.getName());
final var response = SudoRetrieveThingResponse.of(
thing.toBuilder()
.setId(thingId)
.setRevision(i)
.setAttribute(JsonPointer.of("x"), JsonValue.of(i))
.build()
.toJson(FieldType.all()),
command.getDittoHeaders()
);
sender.tell(response, getRef());
}
return keepRunning();
}
});
materializeTestProbes(underTest, 16, 16);
sinkProbe.ensureSubscription();
sourceProbe.ensureSubscription();
sinkProbe.request(4);
assertThat(sourceProbe.expectRequest()).isEqualTo(16);
changeMaps.forEach(sourceProbe::sendNext);
sourceProbe.sendComplete();
final var list = sinkProbe.expectNext(FiniteDuration.apply(60, "s"));
assertThat(list).hasSameSizeAs(changeMaps.get(0));
sinkProbe.expectComplete();
}};
}
private void materializeTestProbes(final EnforcementFlow enforcementFlow) {
materializeTestProbes(enforcementFlow, 16, 1);
}
private void materializeTestProbes(final EnforcementFlow enforcementFlow, final int parallelism,
final int bulkSize) {
final var source = TestSource.<Collection<Metadata>>probe(system);
final var sink = TestSink.<List<AbstractWriteModel>>probe(system);
final var runnableGraph = enforcementFlow.create(source, parallelism, bulkSize)
.viaMat(KillSwitches.single(), Keep.both())
.toMat(sink, Keep.both());
final var materializedValue = runnableGraph.run(() -> system);
sourceProbe = materializedValue.first().first();
sinkProbe = materializedValue.second();
}
}