-
Notifications
You must be signed in to change notification settings - Fork 215
/
StreamingSessionActorTest.java
449 lines (376 loc) · 20.4 KB
/
StreamingSessionActorTest.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
/*
* Copyright (c) 2020 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.gateway.service.streaming.actors;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.pekko.actor.Actor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.PoisonPill;
import org.apache.pekko.actor.Props;
import org.apache.pekko.stream.KillSwitch;
import org.apache.pekko.stream.KillSwitches;
import org.apache.pekko.stream.OverflowStrategy;
import org.apache.pekko.stream.javadsl.Keep;
import org.apache.pekko.stream.javadsl.Sink;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.stream.javadsl.SourceQueueWithComplete;
import org.apache.pekko.stream.testkit.TestSubscriber;
import org.apache.pekko.stream.testkit.javadsl.TestSink;
import org.apache.pekko.testkit.TestActor;
import org.apache.pekko.testkit.TestProbe;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabelNotDeclaredException;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabelNotUniqueException;
import org.eclipse.ditto.base.model.acks.AcknowledgementRequest;
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.common.BinaryValidationResult;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.translator.HeaderTranslator;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.acks.AcknowledgementCorrelationIdMissingException;
import org.eclipse.ditto.gateway.api.GatewayWebsocketSessionAbortedException;
import org.eclipse.ditto.gateway.api.GatewayWebsocketSessionClosedException;
import org.eclipse.ditto.gateway.api.GatewayWebsocketSessionExpiredException;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthenticationResult;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthenticationResultProvider;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtValidator;
import org.eclipse.ditto.gateway.service.streaming.signals.Connect;
import org.eclipse.ditto.gateway.service.streaming.signals.IncomingSignal;
import org.eclipse.ditto.gateway.service.streaming.signals.Jwt;
import org.eclipse.ditto.gateway.service.streaming.signals.StartStreaming;
import org.eclipse.ditto.gateway.service.streaming.signals.StreamingAck;
import org.eclipse.ditto.gateway.service.util.config.streaming.DefaultStreamingConfig;
import org.eclipse.ditto.internal.utils.pekko.ActorSystemResource;
import org.eclipse.ditto.internal.utils.pubsub.StreamingType;
import org.eclipse.ditto.internal.utils.pubsubthings.DittoProtocolSub;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.jwt.model.JsonWebToken;
import org.eclipse.ditto.jwt.model.JwtInvalidException;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.things.model.signals.events.ThingDeleted;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.typesafe.config.ConfigFactory;
/**
* Tests {@link StreamingSessionActor}.
*/
@RunWith(MockitoJUnitRunner.class)
public final class StreamingSessionActorTest {
@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();
private static AuthorizationContext authorizationContext;
@Rule
public final ActorSystemResource actorSystemResource = ActorSystemResource.newInstance(ConfigFactory.load("test"));
@Rule
public final TestName testName = new TestName();
@Mock private DittoProtocolSub mockSub;
@Mock private JwtValidator mockValidator;
@Mock private JwtAuthenticationResultProvider mockAuthenticationResultProvider;
private TestProbe commandRouterProbe;
private SourceQueueWithComplete<SessionedJsonifiable> sourceQueue;
private TestSubscriber.Probe<SessionedJsonifiable> sinkProbe;
private KillSwitch killSwitch;
@BeforeClass
public static void beforeClass() {
authorizationContext = AuthorizationContext.newInstance(DittoAuthorizationContextType.JWT,
AuthorizationSubject.newInstance("test-subject-1"),
AuthorizationSubject.newInstance("test-subject-2"),
AuthorizationSubject.newInstance("test-subject-3"));
}
@Before
public void before() {
commandRouterProbe = actorSystemResource.newTestProbe("commandRouter");
Mockito.when(mockSub.declareAcknowledgementLabels(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(null));
final Sink<SessionedJsonifiable, TestSubscriber.Probe<SessionedJsonifiable>> sink =
TestSink.probe(actorSystemResource.getActorSystem());
final Source<SessionedJsonifiable, SourceQueueWithComplete<SessionedJsonifiable>> source =
Source.queue(100, OverflowStrategy.fail());
final var pair =
source.viaMat(KillSwitches.single(), Keep.both())
.toMat(sink, Keep.both())
.run(actorSystemResource.getActorSystem());
sourceQueue = pair.first().first();
sinkProbe = pair.second();
killSwitch = pair.first().second();
}
@Test
public void terminateOnStreamFailure() {
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
killSwitch.abort(new IllegalStateException("expected exception"));
testKit.expectTerminated(underTest);
}
@Test
public void completeStreamWhenStopped() {
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
underTest.tell(PoisonPill.getInstance(), testKit.getRef());
testKit.expectTerminated(underTest);
sinkProbe.ensureSubscription();
sinkProbe.expectComplete();
}
@Test
public void terminateOnAckLabelDeclarationFailure() {
onDeclareAckLabels(CompletableFuture.failedStage(AcknowledgementLabelNotUniqueException.getInstance()));
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps("ack")));
testKit.expectTerminated(underTest);
}
@Test
public void sendDeclaredAckForGlobalDispatching() {
onDeclareAckLabels(CompletableFuture.completedFuture(null));
final var acknowledgement = Acknowledgement.of(AcknowledgementLabel.of("ack"),
ThingId.of("thing:id"),
HttpStatus.OK,
DittoHeaders.newBuilder().correlationId("corr:" + testName.getMethodName()).build());
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps("ack")));
underTest.tell(IncomingSignal.of(acknowledgement), ActorRef.noSender());
commandRouterProbe.expectMsg(acknowledgement);
}
@Test
public void sendMalformedAck() {
onDeclareAckLabels(CompletableFuture.completedFuture(null));
final var acknowledgement = Acknowledgement.of(AcknowledgementLabel.of("ack"),
ThingId.of("thing:id"),
HttpStatus.OK,
DittoHeaders.empty());
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps("ack")));
underTest.tell(IncomingSignal.of(acknowledgement), ActorRef.noSender());
final var sessionedJsonifiable = sinkProbe.requestNext();
assertThat(sessionedJsonifiable.getJsonifiable())
.isInstanceOf(AcknowledgementCorrelationIdMissingException.class);
}
@Test
public void sendNonDeclaredAck() {
onDeclareAckLabels(CompletableFuture.completedFuture(null));
final var acknowledgement = Acknowledgement.of(AcknowledgementLabel.of("ack2"),
ThingId.of("thing:id"),
HttpStatus.OK,
DittoHeaders.empty());
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps("ack")));
underTest.tell(IncomingSignal.of(acknowledgement), ActorRef.noSender());
final var sessionedJsonifiable = sinkProbe.requestNext();
assertThat(sessionedJsonifiable.getJsonifiable())
.isInstanceOf(AcknowledgementLabelNotDeclaredException.class);
}
@Test
public void acknowledgementRequestsAreRestrictedToDeclaredAcks() {
onDeclareAckLabels(CompletableFuture.completedFuture(null));
setUpMockForTwinEventsSubscription();
final var dittoHeaders = DittoHeaders.newBuilder()
.correlationId("corr:" + testName.getMethodName())
.readGrantedSubjects(List.of(AuthorizationSubject.newInstance("ditto:ditto")))
.acknowledgementRequests(getAcknowledgementRequests("ack", "ack2"))
.build();
final var signal = ThingDeleted.of(ThingId.of("thing:id"), 2L, null, dittoHeaders, null);
final var expectedSignal = signal.setDittoHeaders(DittoHeaders.newBuilder(signal.getDittoHeaders())
.acknowledgementRequests(getAcknowledgementRequests("ack"))
.build());
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps("ack")));
subscribeForTwinEvents(underTest);
underTest.tell(signal, ActorRef.noSender());
final var sessionedJsonifiable = sinkProbe.requestNext();
assertThat(sessionedJsonifiable.getJsonifiable()).isEqualTo(expectedSignal);
}
@Test
public void invalidJwtClosesStream() {
Mockito.when(mockValidator.validate(Mockito.any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(
BinaryValidationResult.invalid(JwtInvalidException.newBuilder().build())));
final var jwt = Jwt.newInstance(getTokenString(), testName.getMethodName());
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
underTest.tell(jwt, testKit.getRef());
assertThat(sinkProbe.expectSubscriptionAndError())
.isInstanceOf(GatewayWebsocketSessionClosedException.class)
.hasMessageContaining("invalid");
}
@Test
public void changingAuthorizationContextClosesStream() {
Mockito.when(mockValidator.validate(Mockito.any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid()));
Mockito.when(mockAuthenticationResultProvider.getAuthenticationResult(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(JwtAuthenticationResult.successful(DittoHeaders.empty(),
AuthorizationContext.newInstance(DittoAuthorizationContextType.UNSPECIFIED,
AuthorizationSubject.newInstance("new:auth-subject")),
Mockito.mock(JsonWebToken.class))));
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
final var jwt = Jwt.newInstance(getTokenString(), testName.getMethodName());
underTest.tell(jwt, testKit.getRef());
assertThat(sinkProbe.expectSubscriptionAndError())
.isInstanceOf(GatewayWebsocketSessionClosedException.class)
.hasMessageContaining("authorization context");
}
@Test
public void keepingAuthorizationContextDoesNotCloseStream() {
Mockito.when(mockValidator.validate(Mockito.any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid()));
Mockito.when(mockAuthenticationResultProvider.getAuthenticationResult(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(JwtAuthenticationResult.successful(DittoHeaders.empty(),
authorizationContext,
Mockito.mock(JsonWebToken.class))));
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
final var jwt = Jwt.newInstance(getTokenString(), testName.getMethodName());
underTest.tell(jwt, testKit.getRef());
sinkProbe.expectSubscription();
sinkProbe.expectNoMessage();
}
@Test
public void hugeJwtExpirationTimeDoesNotCloseStream() {
Mockito.when(mockValidator.validate(Mockito.any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid()));
Mockito.when(mockAuthenticationResultProvider.getAuthenticationResult(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(JwtAuthenticationResult.successful(DittoHeaders.empty(),
authorizationContext,
Mockito.mock(JsonWebToken.class))));
// maximum expiration is too far in the future
final var jwt =
Jwt.newInstance(getTokenString(Instant.now().plus(Duration.ofDays(999))), testName.getMethodName());
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
underTest.tell(jwt, testKit.getRef());
sinkProbe.expectSubscription();
sinkProbe.expectNoMessage();
}
@Test
public void jwtExpirationTimeClosesStream() {
Mockito.when(mockValidator.validate(Mockito.any(JsonWebToken.class)))
.thenReturn(CompletableFuture.completedFuture(BinaryValidationResult.valid()));
Mockito.when(mockAuthenticationResultProvider.getAuthenticationResult(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(JwtAuthenticationResult.successful(DittoHeaders.empty(),
authorizationContext,
Mockito.mock(JsonWebToken.class))));
final var jwt = Jwt.newInstance(getTokenString(Instant.now()), testName.getMethodName());
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
underTest.tell(jwt, testKit.getRef());
sinkProbe.expectSubscription();
assertThat(sinkProbe.expectError())
.isInstanceOf(GatewayWebsocketSessionExpiredException.class)
.hasMessageContaining("expired");
}
@Test
public void sendLiveCommandResponseAndEnsureForwarding() {
final var dittoHeaders = DittoHeaders.newBuilder()
.channel(TopicPath.Channel.LIVE.getName())
.correlationId("corr:" + testName.getMethodName())
.build();
final var retrieveThingResponse =
RetrieveThingResponse.of(ThingId.generateRandom(), JsonObject.empty(), dittoHeaders);
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
underTest.tell(IncomingSignal.of(retrieveThingResponse), ActorRef.noSender());
commandRouterProbe.expectMsg(retrieveThingResponse);
}
@Test
public void testGatewayWebsocketSessionAbortedExceptionIsThrownWhenServiceRequestDneIsReceived() {
final var testKit = actorSystemResource.newTestKit();
final var underTest = testKit.watch(actorSystemResource.newActor(getProps()));
underTest.tell(StreamingSessionActor.Control.SERVICE_REQUESTS_DONE, ActorRef.noSender());
sourceQueue.watchCompletion().whenComplete((done, throwable) -> {
assertThat(done).isNull();
assertThat(throwable).isInstanceOf(GatewayWebsocketSessionAbortedException.class);
});
}
private static String getTokenString() {
return getTokenString(Instant.now().plusSeconds(60L));
}
private static String getTokenString(final Instant expiration) {
final var header = "{\"header\":\"foo\"}";
final var payload = "{\"payload\":\"bar\",\"exp\":" + expiration.getEpochSecond() + "}";
final var signature = "{\"signature\":\"baz\"}";
return base64(header) + "." + base64(payload) + "." + base64(signature);
}
private static String base64(final String value) {
return new String(Base64.getEncoder().encode(value.getBytes()));
}
private Props getProps(final String... declaredAcks) {
return StreamingSessionActor.props(getConnect(getAcknowledgementLabels(declaredAcks)),
mockSub,
commandRouterProbe.ref(),
DefaultStreamingConfig.of(ConfigFactory.empty()),
HeaderTranslator.empty(),
Props.create(Actor.class, () -> new TestActor(new LinkedBlockingDeque<>())),
Props.create(Actor.class, () -> new TestActor(new LinkedBlockingDeque<>())),
mockValidator,
mockAuthenticationResultProvider);
}
private void onDeclareAckLabels(final CompletionStage<Void> answer) {
Mockito.when(mockSub.declareAcknowledgementLabels(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(answer);
}
private void setUpMockForTwinEventsSubscription() {
Mockito.when(mockSub.subscribe(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(null));
}
private void subscribeForTwinEvents(final ActorRef underTest) {
final var authorizationContext =
AuthorizationContext.newInstance(DittoAuthorizationContextType.PRE_AUTHENTICATED_HTTP,
AuthorizationSubject.newInstance("ditto:ditto"));
final var startStreaming =
StartStreaming.getBuilder(StreamingType.EVENTS, testName.getMethodName(), authorizationContext).build();
underTest.tell(startStreaming, ActorRef.noSender());
final var sessionedJsonifiable = sinkProbe.requestNext();
assertThat(sessionedJsonifiable.getJsonifiable()).isEqualTo(new StreamingAck(StreamingType.EVENTS, true));
}
private Connect getConnect(final Set<AcknowledgementLabel> declaredAcks) {
return new Connect(sourceQueue,
testName.getMethodName(),
"WS",
JsonSchemaVersion.LATEST,
null,
declaredAcks,
authorizationContext,
killSwitch);
}
private static Set<AcknowledgementLabel> getAcknowledgementLabels(final String... ackLabelNames) {
return Stream.of(ackLabelNames).map(AcknowledgementLabel::of).collect(Collectors.toSet());
}
private static Set<AcknowledgementRequest> getAcknowledgementRequests(final String... ackLabelNames) {
return Stream.of(ackLabelNames)
.map(AcknowledgementRequest::parseAcknowledgementRequest)
.collect(Collectors.toSet());
}
}