-
Notifications
You must be signed in to change notification settings - Fork 215
/
EndpointTestBase.java
executable file
·358 lines (306 loc) · 15.6 KB
/
EndpointTestBase.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
/*
* Copyright (c) 2017 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;
import static akka.http.javadsl.model.ContentTypes.APPLICATION_JSON;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.json.Jsonifiable;
import org.eclipse.ditto.base.model.signals.WithOptionalEntity;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommandResponse;
import org.eclipse.ditto.base.model.signals.commands.Command;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.base.service.config.http.DefaultHttpProxyConfig;
import org.eclipse.ditto.gateway.service.endpoints.routes.RootRouteExceptionHandler;
import org.eclipse.ditto.gateway.service.endpoints.routes.RouteBaseProperties;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthenticationFactory;
import org.eclipse.ditto.gateway.service.security.authentication.jwt.JwtAuthorizationSubjectsProvider;
import org.eclipse.ditto.gateway.service.util.config.endpoints.CloudEventsConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.CommandConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.DefaultClaimMessageConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.DefaultCloudEventsConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.DefaultCommandConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.DefaultMessageConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.DefaultPublicHealthConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.GatewayHttpConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.HttpConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.MessageConfig;
import org.eclipse.ditto.gateway.service.util.config.endpoints.PublicHealthConfig;
import org.eclipse.ditto.gateway.service.util.config.health.DefaultHealthCheckConfig;
import org.eclipse.ditto.gateway.service.util.config.health.HealthCheckConfig;
import org.eclipse.ditto.gateway.service.util.config.security.AuthenticationConfig;
import org.eclipse.ditto.gateway.service.util.config.security.DefaultAuthenticationConfig;
import org.eclipse.ditto.gateway.service.util.config.streaming.DefaultStreamingConfig;
import org.eclipse.ditto.gateway.service.util.config.streaming.StreamingConfig;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.internal.utils.cache.config.DefaultCacheConfig;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.internal.utils.health.StatusInfo;
import org.eclipse.ditto.internal.utils.health.cluster.ClusterStatus;
import org.eclipse.ditto.internal.utils.http.DefaultHttpClientFacade;
import org.eclipse.ditto.internal.utils.http.HttpClientFacade;
import org.eclipse.ditto.internal.utils.protocol.ProtocolAdapterProvider;
import org.eclipse.ditto.internal.utils.protocol.config.DefaultProtocolConfig;
import org.eclipse.ditto.internal.utils.protocol.config.ProtocolConfig;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.protocol.HeaderTranslator;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.ThingCommandResponse;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.http.javadsl.model.HttpEntities;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.Route;
import akka.http.javadsl.testkit.JUnitRouteTest;
import akka.http.javadsl.testkit.TestRouteResult;
import akka.japi.pf.ReceiveBuilder;
import akka.testkit.TestProbe;
/**
* Abstract base class for Endpoint tests for the gateway.
*/
public abstract class EndpointTestBase extends JUnitRouteTest {
private static final JsonValue DEFAULT_DUMMY_ENTITY_JSON = JsonValue.of("dummy");
private static final Function<Object, Optional<Object>> DUMMY_THING_MODIFY_RESPONSE_PROVIDER =
DummyThingModifyCommandResponse::echo;
protected static HttpConfig httpConfig;
protected static HealthCheckConfig healthCheckConfig;
protected static CommandConfig commandConfig;
protected static MessageConfig messageConfig;
protected static MessageConfig claimMessageConfig;
protected static AuthenticationConfig authConfig;
protected static CacheConfig cacheConfig;
protected static StreamingConfig streamingConfig;
protected static PublicHealthConfig publicHealthConfig;
protected static ProtocolConfig protocolConfig;
protected static CloudEventsConfig cloudEventsConfig;
protected static JwtAuthenticationFactory jwtAuthenticationFactory;
protected static HttpClientFacade httpClientFacade;
protected static JwtAuthorizationSubjectsProvider authorizationSubjectsProvider;
@Rule
public final TestNameCorrelationId testNameCorrelationId = TestNameCorrelationId.newInstance();
protected TestProbe connectivityShardRegionProxyActor;
protected HeaderTranslator httpHeaderTranslator;
protected RouteBaseProperties routeBaseProperties;
protected DittoHeaders dittoHeaders;
@BeforeClass
public static void initTestFixture() {
final var dittoScopedConfig = DefaultScopedConfig.dittoScoped(createTestConfig());
final var gatewayScopedConfig = DefaultScopedConfig.newInstance(dittoScopedConfig, "gateway");
final var actorSystem = ActorSystem.create(EndpointTestBase.class.getSimpleName(), createTestConfig());
httpConfig = GatewayHttpConfig.of(gatewayScopedConfig);
healthCheckConfig = DefaultHealthCheckConfig.of(gatewayScopedConfig);
commandConfig = DefaultCommandConfig.of(gatewayScopedConfig);
messageConfig = DefaultMessageConfig.of(gatewayScopedConfig);
claimMessageConfig = DefaultClaimMessageConfig.of(gatewayScopedConfig);
authConfig = DefaultAuthenticationConfig.of(gatewayScopedConfig);
cacheConfig = DefaultCacheConfig.of(gatewayScopedConfig, "cache.publickeys");
streamingConfig = DefaultStreamingConfig.of(gatewayScopedConfig);
publicHealthConfig = DefaultPublicHealthConfig.of(gatewayScopedConfig);
protocolConfig = DefaultProtocolConfig.of(dittoScopedConfig);
cloudEventsConfig = DefaultCloudEventsConfig.of(gatewayScopedConfig);
httpClientFacade =
DefaultHttpClientFacade.getInstance(actorSystem,
DefaultHttpProxyConfig.ofProxy(DefaultScopedConfig.empty("/")));
authorizationSubjectsProvider = JwtAuthorizationSubjectsProvider.get(actorSystem);
jwtAuthenticationFactory = JwtAuthenticationFactory.newInstance(authConfig.getOAuthConfig(),
cacheConfig,
httpClientFacade,
authorizationSubjectsProvider);
}
@Before
public void before() {
connectivityShardRegionProxyActor = new TestProbe(system());
final var adapterProvider = ProtocolAdapterProvider.load(protocolConfig, system());
httpHeaderTranslator = adapterProvider.getHttpHeaderTranslator();
routeBaseProperties = RouteBaseProperties.newBuilder()
.proxyActor(createDummyResponseActor())
.actorSystem(system())
.httpConfig(httpConfig)
.commandConfig(commandConfig)
.headerTranslator(httpHeaderTranslator)
.connectivityShardRegionProxy(connectivityShardRegionProxyActor.ref())
.build();
dittoHeaders = DittoHeaders.newBuilder().correlationId(testNameCorrelationId.getCorrelationId()).build();
}
@Override
public Config additionalConfig() {
return createTestConfig();
}
/**
* Constructs a test config object.
*
* @return a Config object
*/
protected static Config createTestConfig() {
return ConfigFactory.load("test.conf");
}
/**
* Creates a actor which creates a dummy response message as response to all received messages.
*
* @return the actor
*/
protected ActorRef createDummyResponseActor() {
return system().actorOf(DummyResponseAnswer.props(DUMMY_THING_MODIFY_RESPONSE_PROVIDER));
}
/**
* Creates a actor which creates a dummy response message as response to all received messages.
*
* @return the actor
*/
protected ActorRef createDummyResponseActor(final Function<Object, Optional<Object>> responseProvider) {
return system().actorOf(DummyResponseAnswer.props(responseProvider));
}
protected <T extends Command<?>> ActorRef startEchoActor(
final Class<? extends T> commandClass,
final Function<T, ? extends CommandResponse<?>> responseFactory
) {
final var actorSystem = system();
return actorSystem.actorOf(EchoActor.props(commandClass, responseFactory));
}
protected Supplier<ClusterStatus> createClusterStatusSupplierMock() {
return () -> ClusterStatus.of(Collections.emptySet(), Collections.emptySet(),
Collections.emptySet(), "leader", Collections.emptySet(), Collections.emptySet());
}
protected ActorRef createHealthCheckingActorMock() {
return createDummyResponseActor(message -> Optional.of(StatusInfo.fromStatus(StatusInfo.Status.UP)));
}
protected HttpRequest withDevopsCredentials(final HttpRequest httpRequest) {
return httpRequest.addCredentials(EndpointTestConstants.DEVOPS_CREDENTIALS);
}
protected HttpRequest withStatusCredentials(final HttpRequest httpRequest) {
return httpRequest.addCredentials(EndpointTestConstants.STATUS_CREDENTIALS);
}
protected Route handleExceptions(final Supplier<Route> inner) {
return handleExceptions(
RootRouteExceptionHandler.getInstance(exception ->
HttpResponse.create().withStatus(exception.getHttpStatus().getCode())
.withEntity(HttpEntities.create(APPLICATION_JSON, exception.toJsonString()))
),
inner);
}
protected static void assertWebsocketUpgradeExpectedResult(final TestRouteResult result) {
result.assertStatusCode(StatusCodes.BAD_REQUEST);
result.assertEntity("Expected WebSocket Upgrade request");
}
private static final class DummyResponseAnswer extends AbstractActor {
private final Function<Object, Optional<Object>> responseProvider;
private DummyResponseAnswer(final Function<Object, Optional<Object>> responseProvider) {
this.responseProvider = requireNonNull(responseProvider);
}
private static Props props(final Function<Object, Optional<Object>> responseProvider) {
return Props.create(DummyResponseAnswer.class, responseProvider);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.matchAny(o -> responseProvider.apply(o)
.ifPresent(response -> getSender().tell(response, getSelf())))
.build();
}
}
private static final class DummyThingModifyCommandResponse
extends AbstractCommandResponse<DummyThingModifyCommandResponse>
implements ThingCommandResponse<DummyThingModifyCommandResponse>, WithOptionalEntity {
private JsonValue dummyEntity = DEFAULT_DUMMY_ENTITY_JSON;
private DummyThingModifyCommandResponse(final String responseType,
final HttpStatus httpStatus,
final DittoHeaders dittoHeaders,
@Nullable final JsonValue dummyEntity) {
super(responseType, httpStatus, dittoHeaders);
if (null != dummyEntity) {
this.dummyEntity = dummyEntity;
}
}
private static Optional<Object> echo(final Object m) {
final DittoHeaders dittoHeaders;
if (m instanceof WithDittoHeaders) {
dittoHeaders = ((WithDittoHeaders) m).getDittoHeaders();
} else {
dittoHeaders = DittoHeaders.empty();
}
final DummyThingModifyCommandResponse response =
new DummyThingModifyCommandResponse("testonly.response.type",
HttpStatus.tryGetInstance(EndpointTestConstants.DUMMY_COMMAND_SUCCESS.intValue())
.orElse(HttpStatus.INTERNAL_SERVER_ERROR),
dittoHeaders, m instanceof Jsonifiable ? ((Jsonifiable<?>) m).toJson() : null);
return Optional.of(response);
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> predicate) {
// do nothing
}
@Override
public DummyThingModifyCommandResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return new DummyThingModifyCommandResponse(getType(), getHttpStatus(), dittoHeaders, dummyEntity);
}
@Override
public JsonPointer getResourcePath() {
return null;
}
@Override
public String getResourceType() {
return null;
}
@Override
public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) {
return Optional.of(dummyEntity);
}
@Override
public ThingId getEntityId() {
return EndpointTestConstants.KNOWN_THING_ID;
}
}
private static final class EchoActor<T> extends AbstractActor {
private final Class<T> handledMessageType;
private final Function<T, ?> responseFactory;
private EchoActor(final Class<T> handledMessageType, final Function<T, ?> responseFactory) {
this.handledMessageType = handledMessageType;
this.responseFactory = responseFactory;
}
private static <T> Props props(final Class<? extends T> handledMessageType,
final Function<? extends T, ?> responseFunction) {
return Props.create(EchoActor.class, handledMessageType, responseFunction);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(handledMessageType, message -> {
final var sender = getSender();
sender.tell(responseFactory.apply(message), getSelf());
})
.build();
}
}
}