-
Notifications
You must be signed in to change notification settings - Fork 214
/
ThingsSseRouteBuilderTest.java
363 lines (312 loc) · 17.2 KB
/
ThingsSseRouteBuilderTest.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
/*
* Copyright (c) 2019 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.endpoints.routes.sse;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
import org.assertj.core.util.Lists;
import org.eclipse.ditto.base.model.auth.AuthorizationModelFactory;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.translator.HeaderTranslator;
import org.eclipse.ditto.gateway.api.GatewayServiceUnavailableException;
import org.eclipse.ditto.gateway.service.endpoints.EndpointTestBase;
import org.eclipse.ditto.gateway.service.streaming.actors.SessionedJsonifiable;
import org.eclipse.ditto.gateway.service.streaming.signals.Connect;
import org.eclipse.ditto.gateway.service.streaming.signals.StartStreaming;
import org.eclipse.ditto.internal.utils.pubsub.StreamingType;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.things.model.ThingFieldSelector;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThingResponse;
import org.eclipse.ditto.thingsearch.api.commands.sudo.StreamThings;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.http.javadsl.model.HttpHeader;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.MediaTypes;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.http.javadsl.server.Route;
import org.apache.pekko.http.javadsl.testkit.TestRoute;
import org.apache.pekko.http.javadsl.testkit.TestRouteResult;
import org.apache.pekko.stream.javadsl.Keep;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.stream.javadsl.SourceQueueWithComplete;
import org.apache.pekko.stream.javadsl.StreamRefs;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Unit test for the route built with {@link ThingsSseRouteBuilder}.
*/
public final class ThingsSseRouteBuilderTest extends EndpointTestBase {
private static final String THINGS_ROUTE = "/things";
private static final String SEARCH_ROUTE = "/search/things";
private static ActorSystem actorSystem;
private static HttpHeader acceptHeader;
private CharSequence connectionCorrelationId;
private TestProbe streamingActor;
private TestRoute underTest;
private TestProbe proxyActor;
@BeforeClass
public static void setUpClass() {
actorSystem = ActorSystem.create(ThingsSseRouteBuilderTest.class.getSimpleName(), ConfigFactory.load("test"));
acceptHeader = HttpHeader.parse("Accept", "text/event-stream");
}
@AfterClass
public static void tearDownClass() {
TestKit.shutdownActorSystem(actorSystem);
}
@Before
public void setUp() {
streamingActor = TestProbe.apply("streaming", actorSystem);
proxyActor = TestProbe.apply("proxy", actorSystem);
connectionCorrelationId = testNameCorrelationId.getCorrelationId();
final Supplier<CompletionStage<DittoHeaders>> dittoHeadersSupplier =
() -> CompletableFuture.completedFuture(dittoHeaders);
final var sseRouteBuilder =
ThingsSseRouteBuilder.getInstance(actorSystem, streamingActor.ref(), streamingConfig, proxyActor.ref(),
HeaderTranslator.empty());
sseRouteBuilder.withProxyActor(proxyActor.ref());
final Route sseRoute = extractRequestContext(ctx -> sseRouteBuilder.build(ctx, dittoHeadersSupplier));
underTest = testRoute(sseRoute);
}
@Test
public void getWithoutAcceptHeaderFails() {
underTest.run(HttpRequest.GET(THINGS_ROUTE)).assertStatusCode(StatusCodes.NOT_FOUND);
underTest.run(HttpRequest.GET(SEARCH_ROUTE)).assertStatusCode(StatusCodes.NOT_FOUND);
}
@Test
public void putWithAcceptHeaderFails() {
underTest.run(HttpRequest.PUT(THINGS_ROUTE).addHeader(acceptHeader))
.assertStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
underTest.run(HttpRequest.PUT(SEARCH_ROUTE).addHeader(acceptHeader))
.assertStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
}
@Test
public void postWithAcceptHeaderFails() {
underTest.run(HttpRequest.POST(THINGS_ROUTE).addHeader(acceptHeader))
.assertStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
underTest.run(HttpRequest.POST(SEARCH_ROUTE).addHeader(acceptHeader))
.assertStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
}
@Test
public void deleteWithAcceptHeaderFails() {
underTest.run(HttpRequest.DELETE(THINGS_ROUTE).addHeader(acceptHeader))
.assertStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
underTest.run(HttpRequest.DELETE(SEARCH_ROUTE).addHeader(acceptHeader))
.assertStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
}
@Test
public void getWithAcceptHeaderAndNoQueryParametersOpensSseConnection() {
executeThingsRouteTest(HttpRequest.GET(THINGS_ROUTE).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.EVENTS, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet())).build());
}
@Test
public void getWithAcceptHeaderAndNoQueryParametersOnSpecificThingOpensSseConnection() {
executeThingsRouteTest(HttpRequest.GET(THINGS_ROUTE + "/org.eclipse.ditto:my-thing-1").addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.EVENTS, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet())).build());
}
@Test
public void searchWithoutQueryParameters() {
final TestRouteResult routeResult = underTest.run(HttpRequest.GET(SEARCH_ROUTE).addHeader(acceptHeader));
final CompletableFuture<Void> assertions =
CompletableFuture.runAsync(() -> {
routeResult.assertMediaType(MediaTypes.TEXT_EVENT_STREAM);
routeResult.assertStatusCode(StatusCodes.OK);
});
proxyActor.expectMsgClass(StreamThings.class);
replySourceRef(proxyActor, Source.lazily(Source::empty));
assertions.join();
}
@Test
public void searchWithQueryParameters() {
final String filter = "not(exists(thingId))";
final String option = "sort(-policyId)";
final String namespaces = "a,b,c";
final String url = SEARCH_ROUTE + "?filter=" + filter + "&option=" + option + "&namespaces=" + namespaces;
final TestRouteResult routeResult = underTest.run(HttpRequest.GET(url).addHeader(acceptHeader));
final CompletableFuture<Void> assertions =
CompletableFuture.runAsync(() -> {
routeResult.assertMediaType(MediaTypes.TEXT_EVENT_STREAM);
routeResult.assertStatusCode(StatusCodes.OK);
});
final StreamThings streamThings = proxyActor.expectMsgClass(StreamThings.class);
replySourceRef(proxyActor, Source.lazily(Source::empty));
assertions.join();
assertThat(streamThings.getFilter()).contains(filter);
assertThat(streamThings.getNamespaces()).contains(Set.of("a", "b", "c"));
// sort options are parsed and appended with +/thingId
assertThat(streamThings.getSort()).contains("sort(-/policyId,+/thingId)");
}
@Test
public void searchWithResumption() {
final String lastEventId = "my:lastEventId";
final HttpHeader lastEventIdHeader = HttpHeader.parse("Last-Event-ID", lastEventId);
final TestRouteResult routeResult = underTest.run(
HttpRequest.GET(SEARCH_ROUTE)
.addHeader(acceptHeader)
.addHeader(lastEventIdHeader)
);
final CompletableFuture<Void> assertions =
CompletableFuture.runAsync(() -> {
routeResult.assertMediaType(MediaTypes.TEXT_EVENT_STREAM);
routeResult.assertStatusCode(StatusCodes.OK);
});
final RetrieveThing retrieveThing = proxyActor.expectMsgClass(RetrieveThing.class);
final Thing thing = Thing.newBuilder().setId(ThingId.of(lastEventId)).build();
proxyActor.reply(RetrieveThingResponse.of(ThingId.of(lastEventId),
thing,
null,
null,
retrieveThing.getDittoHeaders()
));
final StreamThings streamThings = proxyActor.expectMsgClass(StreamThings.class);
replySourceRef(proxyActor, Source.lazily(Source::empty));
assertions.join();
assertThat(streamThings.getSortValues()).contains(JsonArray.of(JsonValue.of(lastEventId)));
}
@Test
public void getWithAcceptHeaderAndFilterParameterOpensSseConnection() {
final String filter = "eq(attributes/manufacturer,\"ACME\")";
final String requestUrl = THINGS_ROUTE + "?filter=" + filter;
executeThingsRouteTest(HttpRequest.GET(requestUrl).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.EVENTS, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet()))
.withFilter(filter)
.build());
}
@Test
public void getWithAcceptHeaderAndSpecificThingAndFilterOpensSseConnection() {
final String filter = "eq(attributes/manufacturer,\"ACME\")";
final String requestUrl = THINGS_ROUTE + "/org.eclipse.ditto:my-thing-1?filter=" + filter;
executeThingsRouteTest(HttpRequest.GET(requestUrl).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.EVENTS, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet()))
.withFilter(filter)
.build());
}
@Test
public void getWithAcceptHeaderAndSpecificThingAndSubpathSseConnection() {
final String requestUrl = THINGS_ROUTE + "/org.eclipse.ditto:my-thing-1/attributes/manufacturer";
executeThingsRouteTest(HttpRequest.GET(requestUrl).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.EVENTS, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet()))
.build());
}
@Test
public void getWithAcceptHeaderAndSpecificThingForMessageInboxOpensSseConnection() {
final String thingId = "org.eclipse.ditto:my-thing-1";
final String requestUrl = THINGS_ROUTE + "/" + thingId + "/inbox/messages";
executeThingsRouteTest(HttpRequest.GET(requestUrl).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.MESSAGES, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet()))
.withFilter(String.format("and(eq(entity:id,'%s'),like(resource:path,'%s*'))", thingId,
"/inbox/messages"))
.build());
}
@Test
public void getWithAcceptHeaderAndSpecificThingForMessageInboxSpecifiedSubjectOpensSseConnection() {
final String thingId = "org.eclipse.ditto:my-thing-1";
final String requestUrl = THINGS_ROUTE + "/" + thingId + "/inbox/messages/hello-world";
executeThingsRouteTest(HttpRequest.GET(requestUrl).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.MESSAGES, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet()))
.withFilter(String.format("and(eq(entity:id,'%s'),eq(resource:path,'%s'))", thingId,
"/inbox/messages/hello-world"))
.build());
}
@Test
public void getWithAcceptHeaderAndNamespacesParameterOpensSseConnection() {
final Collection<String> namespaces = Lists.list("john", "frum", "tanna");
final String requestUrl = THINGS_ROUTE + "?namespaces=" + String.join(",", namespaces);
executeThingsRouteTest(HttpRequest.GET(requestUrl).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.EVENTS, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet()))
.withNamespaces(namespaces)
.build());
}
@Test
public void getWithAcceptHeaderAndExtraFieldsParameterOpensSseConnection() {
final ThingFieldSelector extraFields = ThingFieldSelector.fromJsonFieldSelector(
JsonFieldSelector.newInstance("attributes", "features/location"));
final String requestUrl = THINGS_ROUTE + "?extraFields=" + extraFields;
executeThingsRouteTest(HttpRequest.GET(requestUrl).addHeader(acceptHeader),
StartStreaming.getBuilder(StreamingType.EVENTS, connectionCorrelationId,
AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.UNSPECIFIED,
Collections.emptySet()))
.withExtraFields(extraFields)
.build());
}
/*
* Please change this method only if you know exactly what you do.
* The order of statements and everything else is crucial as SSE route testing is not provided by Akka HTTP
* route test kit.
*/
private void executeThingsRouteTest(final HttpRequest httpRequest, final StartStreaming expectedStartStreaming) {
new TestKit(actorSystem) {{
final TestRouteResult routeResult = underTest.run(httpRequest);
final CompletableFuture<Void> routeTestAssertions = CompletableFuture.runAsync(() -> {
routeResult.assertMediaType(MediaTypes.TEXT_EVENT_STREAM);
routeResult.assertStatusCode(StatusCodes.OK);
});
final Connect receivedConnect = streamingActor.expectMsgClass(Connect.class);
streamingActor.reply(streamingActor.ref());
final SourceQueueWithComplete<SessionedJsonifiable> publisherQueue =
receivedConnect.getEventAndResponsePublisher();
final StartStreaming receivedStartStreaming = streamingActor.expectMsgClass(StartStreaming.class);
// exclude connectionCorrelationId from being equal as the backend adds a random UUID to it:
assertThat(receivedStartStreaming.getAuthorizationContext())
.isEqualTo(expectedStartStreaming.getAuthorizationContext());
assertThat(receivedStartStreaming.getExtraFields())
.isEqualTo(expectedStartStreaming.getExtraFields());
assertThat(receivedStartStreaming.getFilter())
.isEqualTo(expectedStartStreaming.getFilter());
assertThat(receivedStartStreaming.getNamespaces())
.isEqualTo(expectedStartStreaming.getNamespaces());
assertThat(receivedStartStreaming.getStreamingType())
.isEqualTo(expectedStartStreaming.getStreamingType());
publisherQueue.fail(GatewayServiceUnavailableException.newBuilder().build());
try {
routeTestAssertions.join();
} catch (final CompletionException e) {
// Ignore as the Future is expected to fail because of exceptionally closing the SSE stream.
}
}};
}
private static void replySourceRef(final TestProbe testProbe, final Source<?, ?> source) {
testProbe.reply(source.toMat(StreamRefs.sourceRef(), Keep.right()).run(actorSystem));
}
}