26
26
import org .eclipse .ditto .base .model .auth .AuthorizationContext ;
27
27
import org .eclipse .ditto .base .model .auth .AuthorizationSubject ;
28
28
import org .eclipse .ditto .base .model .auth .DittoAuthorizationContextType ;
29
+ import org .eclipse .ditto .base .model .entity .id .WithEntityId ;
29
30
import org .eclipse .ditto .base .model .headers .DittoHeaders ;
31
+ import org .eclipse .ditto .base .model .headers .WithDittoHeaders ;
30
32
import org .eclipse .ditto .base .model .json .FieldType ;
31
33
import org .eclipse .ditto .base .model .json .JsonSchemaVersion ;
32
34
import org .eclipse .ditto .base .model .signals .events .Event ;
@@ -119,7 +121,7 @@ public void rejectMessageCommandByPolicy() {
119
121
mockEntitiesActorInstance .setReply (TestSetup .POLICY_SUDO , sudoRetrievePolicyResponse );
120
122
121
123
final ActorRef underTest = newEnforcerActor (getRef ());
122
- underTest .tell (thingMessageCommand (), getRef ());
124
+ underTest .tell (thingMessageCommand ("abc" ), getRef ());
123
125
TestSetup .fishForMsgClass (this , MessageSendNotAllowedException .class );
124
126
}};
125
127
}
@@ -253,6 +255,118 @@ public void retrieveLiveThingCommandAndResponseByPolicy() {
253
255
}};
254
256
}
255
257
258
+ @ Test
259
+ public void correlationIdSameAfterResponseSuccessful () {
260
+ final PolicyId policyId = PolicyId .of ("policy" , "id" );
261
+ final JsonObject thingWithPolicy = newThingWithPolicyId (policyId );
262
+ final JsonObject policy = PoliciesModelFactory .newPolicyBuilder (policyId )
263
+ .setRevision (1L )
264
+ .forLabel ("authorize-self" )
265
+ .setSubject (GOOGLE , SUBJECT_ID )
266
+ .setGrantedPermissions (PoliciesResourceType .thingResource ("/" ),
267
+ Permissions .newInstance (Permission .READ , Permission .WRITE ))
268
+ .setRevokedPermissions (PoliciesResourceType .thingResource ("/features/x/properties/key2" ),
269
+ Permissions .newInstance (Permission .READ ))
270
+ .build ()
271
+ .toJson (FieldType .all ());
272
+ final SudoRetrieveThingResponse sudoRetrieveThingResponse =
273
+ SudoRetrieveThingResponse .of (thingWithPolicy , DittoHeaders .empty ());
274
+ final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
275
+ SudoRetrievePolicyResponse .of (policyId , policy , DittoHeaders .empty ());
276
+
277
+ new TestKit (system ) {{
278
+ mockEntitiesActorInstance .setReply (TestSetup .THING_SUDO , sudoRetrieveThingResponse );
279
+ mockEntitiesActorInstance .setReply (TestSetup .POLICY_SUDO , sudoRetrievePolicyResponse );
280
+
281
+ final ActorRef underTest = newEnforcerActor (getRef ());
282
+
283
+ final DittoHeaders headers = headers ();
284
+ final ThingCommand <?> read = getRetrieveThingCommand (headers );
285
+
286
+ underTest .tell (read , getRef ());
287
+
288
+ final var responseHeaders = headers .toBuilder ()
289
+ .authorizationContext (AuthorizationContext .newInstance (
290
+ DittoAuthorizationContextType .PRE_AUTHENTICATED_CONNECTION ,
291
+ AuthorizationSubject .newInstance ("myIssuer:mySubject" )))
292
+ .build ();
293
+
294
+ final ThingCommandResponse <?> readResponse = getRetrieveThingResponse (responseHeaders );
295
+
296
+ // Second message right after the response for the first was sent, should have the same correlation-id (Not suffixed).
297
+ underTest .tell (readResponse , getRef ());
298
+ final RetrieveThingResponse retrieveThingResponse =
299
+ TestSetup .fishForMsgClass (this , RetrieveThingResponse .class );
300
+ assertThat (retrieveThingResponse .getDittoHeaders ().getCorrelationId ()).isEqualTo (
301
+ read .getDittoHeaders ().getCorrelationId ());
302
+
303
+ underTest .tell (read , getRef ());
304
+
305
+ underTest .tell (readResponse , getRef ());
306
+ final RetrieveThingResponse retrieveThingResponse2 =
307
+ TestSetup .fishForMsgClass (this , RetrieveThingResponse .class );
308
+ assertThat (retrieveThingResponse2 .getDittoHeaders ().getCorrelationId ()).isEqualTo (
309
+ read .getDittoHeaders ().getCorrelationId ());
310
+ }};
311
+ }
312
+
313
+ @ Test
314
+ public void correlationIdDifferentInCaseOfConflict () {
315
+ final PolicyId policyId = PolicyId .of ("policy:id" );
316
+ final JsonObject thingWithPolicy = newThingWithPolicyId (policyId );
317
+ final JsonObject policy = PoliciesModelFactory .newPolicyBuilder (policyId )
318
+ .setRevision (1L )
319
+ .forLabel ("authorize-self" )
320
+ .setSubject (GOOGLE , SUBJECT_ID )
321
+ .setGrantedPermissions (PoliciesResourceType .messageResource (JsonPointer .empty ()),
322
+ Permissions .newInstance (Permission .READ , Permission .WRITE ))
323
+ .build ()
324
+ .toJson (FieldType .all ());
325
+ final SudoRetrieveThingResponse sudoRetrieveThingResponse =
326
+ SudoRetrieveThingResponse .of (thingWithPolicy , DittoHeaders .empty ());
327
+ final SudoRetrievePolicyResponse sudoRetrievePolicyResponse =
328
+ SudoRetrievePolicyResponse .of (policyId , policy , DittoHeaders .empty ());
329
+
330
+ new TestKit (system ) {{
331
+ mockEntitiesActorInstance .setReply (TestSetup .THING_SUDO , sudoRetrieveThingResponse );
332
+ mockEntitiesActorInstance .setReply (TestSetup .POLICY_SUDO , sudoRetrievePolicyResponse );
333
+
334
+ final ActorRef underTest = newEnforcerActor (getRef ());
335
+
336
+ final MessageCommand <?, ?> message = thingMessageCommand ("abc" );
337
+
338
+ underTest .tell (message , getRef ());
339
+ final DistributedPubSubMediator .Publish firstPublishRead =
340
+ pubSubMediatorProbe .expectMsgClass (DistributedPubSubMediator .Publish .class );
341
+ assertThat (firstPublishRead .topic ()).isEqualTo (StreamingType .MESSAGES .getDistributedPubSubTopic ());
342
+ assertThat (firstPublishRead .msg ()).isInstanceOf (MessageCommand .class );
343
+ assertThat ((CharSequence ) ((WithEntityId ) firstPublishRead .msg ()).getEntityId ()).isEqualTo (
344
+ message .getEntityId ());
345
+ assertThat ((CharSequence ) ((WithDittoHeaders ) firstPublishRead .msg ()).getDittoHeaders ()
346
+ .getCorrelationId ()
347
+ .orElseThrow ()).isEqualTo (
348
+ message .getDittoHeaders ().getCorrelationId ().orElseThrow ());
349
+
350
+ underTest .tell (message , getRef ());
351
+ final DistributedPubSubMediator .Publish secondPublishRead =
352
+ pubSubMediatorProbe .expectMsgClass (DistributedPubSubMediator .Publish .class );
353
+ assertThat (secondPublishRead .topic ()).isEqualTo (StreamingType .MESSAGES .getDistributedPubSubTopic ());
354
+ assertThat (secondPublishRead .msg ()).isInstanceOf (MessageCommand .class );
355
+ assertThat ((CharSequence ) ((WithEntityId ) secondPublishRead .msg ()).getEntityId ()).isEqualTo (
356
+ message .getEntityId ());
357
+ // Assure second command has suffixed correlation-id, because of conflict with first command.
358
+ assertThat ((CharSequence ) ((WithDittoHeaders ) secondPublishRead .msg ()).getDittoHeaders ()
359
+ .getCorrelationId ()
360
+ .orElseThrow ()).startsWith (
361
+ message .getDittoHeaders ().getCorrelationId ().orElseThrow ());
362
+ assertThat ((CharSequence ) ((WithDittoHeaders ) secondPublishRead .msg ()).getDittoHeaders ()
363
+ .getCorrelationId ()
364
+ .orElseThrow ()).isNotEqualTo (
365
+ message .getDittoHeaders ().getCorrelationId ().orElseThrow ());
366
+
367
+ }};
368
+ }
369
+
256
370
@ Test
257
371
public void acceptMessageCommandByPolicy () {
258
372
final PolicyId policyId = PolicyId .of ("policy:id" );
@@ -276,7 +390,7 @@ public void acceptMessageCommandByPolicy() {
276
390
277
391
final ActorRef underTest = newEnforcerActor (getRef ());
278
392
279
- final MessageCommand <?, ?> msgCommand = thingMessageCommand ();
393
+ final MessageCommand <?, ?> msgCommand = thingMessageCommand ("abc" );
280
394
mockEntitiesActorInstance .setReply (msgCommand );
281
395
underTest .tell (msgCommand , getRef ());
282
396
final DistributedPubSubMediator .Publish publish =
@@ -419,11 +533,12 @@ private static ThingCommand<?> getModifyFeatureCommand(final DittoHeaders header
419
533
return ModifyFeature .of (TestSetup .THING_ID , TestSetup .FEATURE , headers );
420
534
}
421
535
422
- private static MessageCommand <?, ?> thingMessageCommand () {
536
+ private static MessageCommand <?, ?> thingMessageCommand (final String correlationId ) {
423
537
final Message <Object > message = Message .newBuilder (
424
- MessageBuilder .newHeadersBuilder (MessageDirection .TO , TestSetup .THING_ID , "my-subject" )
425
- .contentType ("text/plain" )
426
- .build ())
538
+ MessageBuilder .newHeadersBuilder (MessageDirection .TO , TestSetup .THING_ID , "my-subject" )
539
+ .contentType ("text/plain" )
540
+ .correlationId (correlationId )
541
+ .build ())
427
542
.payload ("Hello you!" )
428
543
.build ();
429
544
return SendThingMessage .of (TestSetup .THING_ID , message , headers ());
@@ -441,10 +556,10 @@ private static ThingEvent<?> liveEventRevoked() {
441
556
442
557
private static MessageCommand <?, ?> featureMessageCommand () {
443
558
final Message <?> message = Message .newBuilder (
444
- MessageBuilder .newHeadersBuilder (MessageDirection .TO , TestSetup .THING_ID , "my-subject" )
445
- .contentType ("text/plain" )
446
- .featureId ("foo" )
447
- .build ())
559
+ MessageBuilder .newHeadersBuilder (MessageDirection .TO , TestSetup .THING_ID , "my-subject" )
560
+ .contentType ("text/plain" )
561
+ .featureId ("foo" )
562
+ .build ())
448
563
.payload ("Hello you!" )
449
564
.build ();
450
565
return SendFeatureMessage .of (TestSetup .THING_ID , "foo" , message , headers ());
0 commit comments