/
EndpointTestBase.java
executable file
·212 lines (176 loc) · 7.4 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
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.gateway.endpoints;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
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.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.JsonSchemaVersion;
import org.eclipse.ditto.services.utils.health.PersistenceClusterHealth;
import org.eclipse.ditto.services.utils.health.cluster.ClusterStatus;
import org.eclipse.ditto.signals.commands.base.AbstractCommandResponse;
import org.eclipse.ditto.signals.commands.base.WithEntity;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.ResponseEntity;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.testkit.JUnitRouteTest;
import akka.http.javadsl.testkit.TestRouteResult;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.ActorMaterializer;
/**
* Abstract base class for Endpoint tests for the gateway.
*/
public abstract class EndpointTestBase extends JUnitRouteTest {
public static final JsonValue DEFAULT_DUMMY_ENTITY_JSON = JsonValue.of("dummy");
public static final String DEFAULT_DUMMY_ENTITY = DEFAULT_DUMMY_ENTITY_JSON.toString();
private static final Function<Object, Optional<Object>> DUMMY_RESPONSE_PROVIDER = m -> Optional.of(new
DummyCommandResponse("bumlux",
HttpStatusCode.forInt(EndpointTestConstants.DUMMY_COMMAND_SUCCESS.intValue())
.orElse(HttpStatusCode.INTERNAL_SERVER_ERROR),
DittoHeaders.empty()));
@Override
public Config additionalConfig() {
return createTestConfig();
}
/**
* Constructs a test config object.
*
* @return a Config object
*/
private static Config createTestConfig() {
return ConfigFactory.load("test.conf");
}
/**
* Returns the config used for testing.
*
* @return the config
*/
protected Config getConfig() {
return systemResource().config();
}
protected ActorMaterializer actorMaterializer() {
// materializer is always of type ActorMaterializer (for akka-http-testkit_${scala.version}-10.0.4)
return (ActorMaterializer) materializer();
}
/**
* 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_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 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(PersistenceClusterHealth.newInstance()));
}
protected HttpRequest withDevopsCredentials(final HttpRequest httpRequest) {
return httpRequest.addCredentials(EndpointTestConstants.DEVOPS_CREDENTIALS);
}
protected String entityToString(final ResponseEntity entity) {
try {
final int timeoutMillis = 10_000;
return entity.toStrict(timeoutMillis, materializer())
.toCompletableFuture()
.get(timeoutMillis, TimeUnit.MILLISECONDS)
.getData()
.utf8String();
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new IllegalStateException(e);
}
}
protected void assertWebsocketUpgradeExpectedResult(final TestRouteResult result) {
result.assertStatusCode(StatusCodes.BAD_REQUEST);
result.assertEntity("Expected WebSocket Upgrade request");
}
private static 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 class DummyCommandResponse extends AbstractCommandResponse<DummyCommandResponse> implements
WithEntity<DummyCommandResponse> {
private JsonValue dummyEntity = DEFAULT_DUMMY_ENTITY_JSON;
private DummyCommandResponse(final String responseType, final HttpStatusCode statusCode,
final DittoHeaders dittoHeaders) {
super(responseType, statusCode, dittoHeaders);
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> predicate) {
// do nothing
}
@Override
public DummyCommandResponse setDittoHeaders(final DittoHeaders dittoHeaders) {
return new DummyCommandResponse(getType(), getStatusCode(), dittoHeaders);
}
@Override
public JsonPointer getResourcePath() {
return null;
}
@Override
public String getResourceType() {
return null;
}
@Override
public DummyCommandResponse setEntity(final JsonValue entity) {
this.dummyEntity = entity;
return new DummyCommandResponse(getType(), getStatusCode(), getDittoHeaders());
}
@Override
public JsonValue getEntity(final JsonSchemaVersion schemaVersion) {
return dummyEntity;
}
@Override
public String getId() {
return null;
}
}
}