-
Notifications
You must be signed in to change notification settings - Fork 215
/
MultiStageCommandTest.java
562 lines (469 loc) · 28.4 KB
/
MultiStageCommandTest.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
/*
* Copyright (c) 2022 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.enforcement;
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat;
import static org.eclipse.ditto.things.service.enforcement.TestSetup.THING_ID;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
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.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.policies.api.Permission;
import org.eclipse.ditto.policies.api.commands.sudo.SudoRetrievePolicy;
import org.eclipse.ditto.policies.api.commands.sudo.SudoRetrievePolicyResponse;
import org.eclipse.ditto.policies.model.Permissions;
import org.eclipse.ditto.policies.model.PoliciesModelFactory;
import org.eclipse.ditto.policies.model.PoliciesResourceType;
import org.eclipse.ditto.policies.model.Policy;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.policies.model.Subject;
import org.eclipse.ditto.policies.model.SubjectIssuer;
import org.eclipse.ditto.policies.model.signals.commands.PolicyCommand;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyConflictException;
import org.eclipse.ditto.policies.model.signals.commands.exceptions.PolicyNotAccessibleException;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy;
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicyResponse;
import org.eclipse.ditto.policies.model.signals.commands.query.RetrievePolicy;
import org.eclipse.ditto.policies.model.signals.commands.query.RetrievePolicyResponse;
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.ThingsModelFactory;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotAccessibleException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotCreatableException;
import org.eclipse.ditto.things.model.signals.commands.exceptions.ThingNotModifiableException;
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.ModifyThing;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThingResponse;
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.service.persistence.actors.ThingSupervisorActor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.pattern.AskTimeoutException;
import akka.testkit.TestActorRef;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
/**
* Tests commands that triggers different or multiple commands internally.
*/
public final class MultiStageCommandTest {
private static final Subject DEFAULT_SUBJECT =
Subject.newInstance(SubjectIssuer.GOOGLE, "defaultSubject");
private static final Subject NOT_DEFAULT_SUBJECT =
Subject.newInstance(SubjectIssuer.GOOGLE, "notDefaultSubject");
public static final AuthorizationSubject AUTHORIZATION_SUBJECT =
AuthorizationSubject.newInstance(DEFAULT_SUBJECT.getId());
private static final DittoHeaders DEFAULT_HEADERS = DittoHeaders.newBuilder()
.authorizationContext(AuthorizationContext.newInstance(
DittoAuthorizationContextType.UNSPECIFIED,
AUTHORIZATION_SUBJECT))
.build();
private static final String THING = ThingCommand.RESOURCE_TYPE;
private static final String POLICY = PolicyCommand.RESOURCE_TYPE;
private ActorSystem system;
private TestProbe pubSubMediatorProbe;
private TestProbe thingPersistenceActorProbe;
private TestProbe policiesShardRegionProbe;
private ActorRef supervisor;
private ThingSupervisorActor mockThingPersistenceSupervisor;
@Before
public void init() {
system = ActorSystem.create("test", ConfigFactory.load("test"));
pubSubMediatorProbe = createPubSubMediatorProbe();
thingPersistenceActorProbe = createThingPersistenceActorProbe();
policiesShardRegionProbe = getTestProbe(createUniqueName("policiesShardRegionProbe-"));
final TestActorRef<ThingSupervisorActor> thingPersistenceSupervisorTestActorRef =
createThingPersistenceSupervisor();
supervisor = thingPersistenceSupervisorTestActorRef;
mockThingPersistenceSupervisor = thingPersistenceSupervisorTestActorRef.underlyingActor();
}
@After
public void shutdown() {
if (system != null) {
TestKit.shutdownActorSystem(system);
}
}
@Test
public void retrieveThingAndPolicy() {
new TestKit(system) {{
// GIVEN: Thing and its Policy exist
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
final Policy policy = defaultPolicy(policyId);
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
// WHEN: received RetrieveThing
final JsonFieldSelector selectedFields = JsonFieldSelector.newInstance("_policy", "thingId");
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, DEFAULT_HEADERS)
.withSelectedFields(selectedFields)
.build();
supervisor.tell(retrieveThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(policyId, sudoRetrievePolicyResponse);
final RetrieveThing expectedRetrieveThing = retrieveThing.setDittoHeaders(DEFAULT_HEADERS.toBuilder()
.readGrantedSubjects(List.of(AUTHORIZATION_SUBJECT)).build());
thingPersistenceActorProbe.expectMsg(expectedRetrieveThing);
thingPersistenceActorProbe.reply(
RetrieveThingResponse.of(thingId, thing, null, null, DEFAULT_HEADERS));
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
policiesShardRegionProbe.expectMsgClass(RetrievePolicy.class);
policiesShardRegionProbe.reply(RetrievePolicyResponse.of(policyId, policy, DEFAULT_HEADERS));
// THEN: initial requester receives Thing with inline policy
final RetrieveThingResponse response = TestSetup.fishForMsgClass(this, RetrieveThingResponse.class);
assertThat((CharSequence) response.getEntityId()).isEqualTo(thingId);
assertThat(response.getEntity()).isObject();
assertThat(response.getEntity().asObject().getValue("_policy/policyId"))
.contains(JsonFactory.newValue(policyId.toString()));
}};
}
@Test
public void retrieveThingWithoutPolicyWhenPermissionIsMissing() {
new TestKit(system) {{
// GIVEN: Thing and its Policy exist but default subject has no permission on the policy
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
final Policy policy = thingOnlyPolicy(policyId);
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
// WHEN: received RetrieveThing
final JsonFieldSelector selectedFields = JsonFieldSelector.newInstance("_policy", "thingId");
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, DEFAULT_HEADERS)
.withSelectedFields(selectedFields)
.build();
supervisor.tell(retrieveThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(policyId, sudoRetrievePolicyResponse);
final DittoHeaders expectedHeaders = DEFAULT_HEADERS.toBuilder()
.readGrantedSubjects(List.of(AUTHORIZATION_SUBJECT)).build();
final RetrieveThing expectedRetrieveThing = retrieveThing.setDittoHeaders(expectedHeaders);
thingPersistenceActorProbe.expectMsg(expectedRetrieveThing);
thingPersistenceActorProbe.reply(
RetrieveThingResponse.of(thingId, thing, null, null, expectedHeaders));
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
policiesShardRegionProbe.expectMsgClass(RetrievePolicy.class);
policiesShardRegionProbe.reply(PolicyNotAccessibleException.newBuilder(policyId).build());
// THEN: initial requester receives Thing with inline policy
final RetrieveThingResponse response = expectMsgClass(RetrieveThingResponse.class);
assertThat((CharSequence) response.getEntityId()).isEqualTo(thingId);
assertThat(response.getEntity()).isObject();
assertThat(response.getEntity().asObject()).doesNotContain(JsonKey.of("_policy"));
}};
}
@Test
public void retrieveThingErrorIfCacheIsOutOfDate() {
new TestKit(system) {{
// GIVEN: Thing and its Policy exist in cache but not actually
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
final Policy policy = thingOnlyPolicy(policyId);
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
// WHEN: received RetrieveThing
final JsonFieldSelector selectedFields = JsonFieldSelector.newInstance("_policy", "thingId");
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, DEFAULT_HEADERS)
.withSelectedFields(selectedFields)
.build();
supervisor.tell(retrieveThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(policyId, sudoRetrievePolicyResponse);
final RetrieveThing expectedRetrieveThing = retrieveThing.setDittoHeaders(DEFAULT_HEADERS.toBuilder()
.readGrantedSubjects(List.of(AUTHORIZATION_SUBJECT)).build());
thingPersistenceActorProbe.expectMsg(expectedRetrieveThing);
thingPersistenceActorProbe.reply(ThingNotAccessibleException.newBuilder(thingId).build());
// THEN: initial requester receives error
TestSetup.fishForMsgClass(this, ThingNotAccessibleException.class);
// WHEN: Thing exists but Policy exists only in cache
supervisor.tell(retrieveThing, getRef());
thingPersistenceActorProbe.expectMsg(expectedRetrieveThing);
thingPersistenceActorProbe.reply(RetrieveThingResponse.of(thingId, thing, null, null, DEFAULT_HEADERS));
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
policiesShardRegionProbe.expectMsgClass(RetrievePolicy.class);
policiesShardRegionProbe.reply(PolicyNotAccessibleException.newBuilder(policyId).build());
// THEN: initial requester receives Thing without Policy
final RetrieveThingResponse response = expectMsgClass(RetrieveThingResponse.class);
assertThat((CharSequence) response.getEntityId()).isEqualTo(thingId);
assertThat(response.getEntity()).isObject();
assertThat(response.getEntity().asObject()).doesNotContain(JsonKey.of("_policy"));
}};
}
@Test
public void retrieveThingErrorOnTimeout() {
new TestKit(system) {{
// GIVEN: Thing and its Policy exist in cache
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
final Policy policy = defaultPolicy(policyId);
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
// WHEN: received RetrieveThing but both shard regions time out
final JsonFieldSelector selectedFields = JsonFieldSelector.newInstance("_policy", "thingId");
final RetrieveThing retrieveThing = RetrieveThing.getBuilder(thingId, DEFAULT_HEADERS)
.withSelectedFields(selectedFields)
.build();
supervisor.tell(retrieveThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(policyId, sudoRetrievePolicyResponse);
final RetrieveThing expectedRetrieveThing = retrieveThing.setDittoHeaders(DEFAULT_HEADERS.toBuilder()
.readGrantedSubjects(List.of(AUTHORIZATION_SUBJECT)).build());
thingPersistenceActorProbe.expectMsg(expectedRetrieveThing);
thingPersistenceActorProbe.reply(new AskTimeoutException("thing timeout"));
// THEN: initial requester receives error
TestSetup.fishForMsgClass(this, ThingUnavailableException.class);
// WHEN: Thing is responsive but Policy times out
supervisor.tell(retrieveThing, getRef());
thingPersistenceActorProbe.expectMsg(expectedRetrieveThing);
thingPersistenceActorProbe.reply(RetrieveThingResponse.of(thingId, thing, null, null, DEFAULT_HEADERS));
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
policiesShardRegionProbe.expectMsgClass(RetrievePolicy.class);
policiesShardRegionProbe.reply(new AskTimeoutException("policy timeout"));
// THEN: initial requester receives Thing without Policy
final RetrieveThingResponse response = expectMsgClass(RetrieveThingResponse.class);
assertThat((CharSequence) response.getEntityId()).isEqualTo(thingId);
assertThat(response.getEntity()).isObject();
assertThat(response.getEntity().asObject()).doesNotContain(JsonKey.of("_policy"));
}};
}
@Test
public void modifyExistingThing() {
new TestKit(system) {{
// GIVEN: Thing and its Policy exist
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
final Policy policy = defaultPolicy(policyId);
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
// WHEN: received ModifyThing
final ModifyThing modifyThing = ModifyThing.of(thingId, thing, null, DEFAULT_HEADERS);
supervisor.tell(modifyThing, getRef());
expectAndAnswerSudoRetrieveThing(sudoRetrieveThingResponse);
expectAndAnswerSudoRetrievePolicy(policyId, sudoRetrievePolicyResponse);
final DittoHeaders expectedHeaders = DEFAULT_HEADERS.toBuilder()
.readGrantedSubjects(List.of(AUTHORIZATION_SUBJECT)).build();
final ModifyThing expectedModifyThing = modifyThing.setDittoHeaders(expectedHeaders);
thingPersistenceActorProbe.expectMsg(expectedModifyThing);
thingPersistenceActorProbe.reply(ModifyThingResponse.modified(thingId, expectedHeaders));
expectAndAnswerSudoRetrievePolicy(policyId, sudoRetrievePolicyResponse);
// THEN: initial requester receives success
expectMsgClass(ModifyThingResponse.class);
}};
}
@Test
public void createThingWithExistingPolicy() {
new TestKit(system) {{
// GIVEN: Thing is nonexistent but its Policy exists
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final Policy policy = defaultPolicy(policyId);
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
// WHEN: received ModifyThing
final ModifyThing modifyThing = ModifyThing.of(thingId, thing, null, DEFAULT_HEADERS);
supervisor.tell(modifyThing, getRef());
expectAndAnswerSudoRetrieveThing(ThingNotAccessibleException.newBuilder(thingId).build());
expectAndAnswerSudoRetrievePolicy(policyId, sudoRetrievePolicyResponse);
thingPersistenceActorProbe.expectMsgClass(CreateThing.class);
thingPersistenceActorProbe.reply(CreateThingResponse.of(thing, DEFAULT_HEADERS));
// THEN: initial requester receives success
TestSetup.fishForMsgClass(this, CreateThingResponse.class);
}};
}
@Test
public void createThingAndPolicy() {
new TestKit(system) {{
// GIVEN: Thing and Policy are nonexistent
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of(thingId);
final Thing thing = emptyThing(thingId, null);
final Policy policy = defaultPolicy(policyId);
// WHEN: received ModifyThing
final ModifyThing modifyThing = ModifyThing.of(thingId, thing, null, DEFAULT_HEADERS);
supervisor.tell(modifyThing, getRef());
expectAndAnswerSudoRetrieveThing(ThingNotAccessibleException.newBuilder(thingId).build());
policiesShardRegionProbe.expectMsgClass(CreatePolicy.class);
policiesShardRegionProbe.reply(CreatePolicyResponse.of(policyId, policy, DEFAULT_HEADERS));
thingPersistenceActorProbe.expectMsgClass(CreateThing.class);
thingPersistenceActorProbe.reply(CreateThingResponse.of(thing, DEFAULT_HEADERS));
// THEN: initial requester receives success
TestSetup.fishForMsgClass(this, CreateThingResponse.class);
}};
}
@Test
public void createThingWithPolicyConflict() {
new TestKit(system) {{
// GIVEN: Thing is nonexistent but Policy exists
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of(thingId);
final Thing thing = emptyThing(thingId, null);
final Policy policy = defaultPolicy(policyId);
// WHEN: received ModifyThing
final ModifyThing modifyThing = ModifyThing.of(thingId, thing, null, DEFAULT_HEADERS);
supervisor.tell(modifyThing, getRef());
expectAndAnswerSudoRetrieveThing(ThingNotAccessibleException.newBuilder(thingId).build());
policiesShardRegionProbe.expectMsgClass(CreatePolicy.class);
policiesShardRegionProbe.reply(PolicyConflictException.newBuilder(policyId).build());
// THEN: initial requester receives failure
final ThingNotCreatableException error = TestSetup.fishForMsgClass(this, ThingNotCreatableException.class);
assertThat(error.getMessage()).contains("implicit Policy", "creation", "failed");
}};
}
@Test
public void createThingWithExplicitPolicy() {
new TestKit(system) {{
// GIVEN: Thing and Policy do not exist
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final Policy policy = defaultPolicy(policyId);
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
SudoRetrievePolicyResponse.of(policyId, policy, DittoHeaders.empty());
// WHEN: received ModifyThing
final ModifyThing modifyThing =
ModifyThing.of(thingId, thing, policy.toJson(), DEFAULT_HEADERS);
supervisor.tell(modifyThing, getRef());
expectAndAnswerSudoRetrieveThing(ThingNotAccessibleException.newBuilder(thingId).build());
policiesShardRegionProbe.expectMsgClass(CreatePolicy.class);
policiesShardRegionProbe.reply(CreatePolicyResponse.of(policyId, policy, DEFAULT_HEADERS));
thingPersistenceActorProbe.expectMsgClass(CreateThing.class);
thingPersistenceActorProbe.reply(CreateThingResponse.of(thing, DEFAULT_HEADERS));
// THEN: initial requester receives success
expectMsgClass(CreateThingResponse.class);
}};
}
@Test
public void createThingWithExplicitPolicyNotAuthorizedBySelf() {
new TestKit(system) {{
// GIVEN: Thing and Policy do not exist
final ThingId thingId = ThingId.of("thing", UUID.randomUUID().toString());
final PolicyId policyId = PolicyId.of("policy", UUID.randomUUID().toString());
final Thing thing = emptyThing(thingId, policyId);
final Policy policy = thingOnlyPolicy(policyId);
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(thing.toJson(FieldType.all()), DittoHeaders.empty());
// WHEN: received ModifyThing whose inline policy does not permit creation of itself
final ModifyThing modifyThing =
ModifyThing.of(thingId, thing, policy.toJson(), DEFAULT_HEADERS);
supervisor.tell(modifyThing, getRef());
expectAndAnswerSudoRetrieveThing(ThingNotAccessibleException.newBuilder(thingId).build());
policiesShardRegionProbe.expectMsgClass(CreatePolicy.class);
policiesShardRegionProbe.reply(CreatePolicyResponse.of(policyId, policy, DEFAULT_HEADERS));
thingPersistenceActorProbe.expectMsgClass(CreateThing.class);
thingPersistenceActorProbe.reply(ThingNotModifiableException.newBuilder(thingId).build());
// THEN: initial requester receives error
expectMsgClass(ThingNotModifiableException.class);
}};
}
private TestActorRef<ThingSupervisorActor> createThingPersistenceSupervisor() {
return new TestActorRef<>(system, ThingSupervisorActor.props(
pubSubMediatorProbe.ref(),
policiesShardRegionProbe.ref(),
new TestSetup.DummyLiveSignalPub(pubSubMediatorProbe.ref()),
thingPersistenceActorProbe.ref(),
null,
CompletableFuture::completedStage
), system.guardian(), URLEncoder.encode(THING_ID.toString(), Charset.defaultCharset()));
}
private TestProbe createPubSubMediatorProbe() {
return getTestProbe(createUniqueName("pubSubMediatorProbe-"));
}
private TestProbe createThingPersistenceActorProbe() {
return getTestProbe(createUniqueName("thingPersistenceActorProbe-"));
}
private TestProbe getTestProbe(final String uniqueName) {
return new TestProbe(system, uniqueName);
}
private static String createUniqueName(final String prefix) {
return prefix + UUID.randomUUID();
}
private void expectAndAnswerSudoRetrieveThing(final Object sudoRetrieveThingResponse) {
final SudoRetrieveThing sudoRetrieveThing =
thingPersistenceActorProbe.expectMsgClass(SudoRetrieveThing.class);
assertThat((CharSequence) sudoRetrieveThing.getEntityId()).isEqualTo(THING_ID);
thingPersistenceActorProbe.reply(sudoRetrieveThingResponse);
}
private void expectAndAnswerSudoRetrievePolicy(final PolicyId policyId, final Object sudoRetrievePolicyResponse) {
final SudoRetrievePolicy sudoRetrievePolicy =
policiesShardRegionProbe.expectMsgClass(SudoRetrievePolicy.class);
assertThat((CharSequence) sudoRetrievePolicy.getEntityId()).isEqualTo(policyId);
policiesShardRegionProbe.reply(sudoRetrievePolicyResponse);
}
private static Thing emptyThing(final ThingId thingId, @Nullable final PolicyId policyId) {
return ThingsModelFactory.newThingBuilder()
.setId(thingId)
.setPolicyId(policyId)
.setRevision(1L)
.build();
}
private static Policy defaultPolicy(final PolicyId policyId) {
final Permissions permissions = Permissions.newInstance(Permission.READ, Permission.WRITE);
return PoliciesModelFactory.newPolicyBuilder(policyId)
.forLabel(POLICY)
.setSubject(DEFAULT_SUBJECT)
.setGrantedPermissions(PoliciesResourceType.policyResource(JsonPointer.empty()), permissions)
.forLabel(THING)
.setSubject(DEFAULT_SUBJECT)
.setGrantedPermissions(PoliciesResourceType.thingResource(JsonPointer.empty()), permissions)
.setRevision(1L)
.build();
}
private static Policy thingOnlyPolicy(final PolicyId policyId) {
final Permissions permissions = Permissions.newInstance(Permission.READ, Permission.WRITE);
return PoliciesModelFactory.newPolicyBuilder(policyId)
.forLabel(POLICY)
.setSubject(NOT_DEFAULT_SUBJECT)
.setGrantedPermissions(PoliciesResourceType.policyResource(JsonPointer.empty()), permissions)
.forLabel(THING)
.setSubject(DEFAULT_SUBJECT)
.setGrantedPermissions(PoliciesResourceType.thingResource(JsonPointer.empty()), permissions)
.setRevision(1L)
.build();
}
}