-
Notifications
You must be signed in to change notification settings - Fork 215
/
AbstractHttpRequestActorTest.java
265 lines (225 loc) · 12 KB
/
AbstractHttpRequestActorTest.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
/*
* 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.endpoints.actors;
import static org.assertj.core.api.Assertions.assertThat;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationModelFactory;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.correlationid.TestNameCorrelationId;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.translator.HeaderTranslator;
import org.eclipse.ditto.gateway.service.endpoints.routes.whoami.DefaultUserInformation;
import org.eclipse.ditto.gateway.service.endpoints.routes.whoami.Whoami;
import org.eclipse.ditto.gateway.service.util.config.DittoGatewayConfig;
import org.eclipse.ditto.gateway.service.util.config.GatewayConfig;
import org.eclipse.ditto.internal.utils.akka.ActorSystemResource;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.messages.model.Message;
import org.eclipse.ditto.messages.model.MessageDirection;
import org.eclipse.ditto.messages.model.MessageHeaderDefinition;
import org.eclipse.ditto.messages.model.MessageHeaders;
import org.eclipse.ditto.messages.model.signals.commands.SendThingMessage;
import org.eclipse.ditto.messages.model.signals.commands.SendThingMessageResponse;
import org.eclipse.ditto.policies.model.SubjectId;
import org.eclipse.ditto.policies.model.SubjectIssuer;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyAttribute;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.HttpHeader;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.ResponseEntity;
import akka.http.javadsl.model.StatusCode;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.model.headers.RawHeader;
import akka.testkit.TestProbe;
import akka.util.ByteString;
/**
* Abstract class to set up the infrastructure to test HttpRequestActor.
*/
public abstract class AbstractHttpRequestActorTest {
@ClassRule
public static final ActorSystemResource ACTOR_SYSTEM_RESOURCE = ActorSystemResource.newInstance();
static final HeaderTranslator HEADER_TRANSLATOR = HeaderTranslator.of(DittoHeaderDefinition.values(),
MessageHeaderDefinition.values());
protected static GatewayConfig gatewayConfig;
protected static TestProbe connectivityShardRegionProxy;
@Rule
public final TestNameCorrelationId testNameCorrelationId = TestNameCorrelationId.newInstance();
@BeforeClass
public static void beforeClass() {
gatewayConfig = DittoGatewayConfig.of(DefaultScopedConfig.dittoScoped(ConfigFactory.load("test.conf")));
connectivityShardRegionProxy = ACTOR_SYSTEM_RESOURCE.newTestProbe();
}
void testThingModifyCommand(final ThingId thingId,
final String attributeName,
final JsonPointer attributePointer,
final DittoHeaders dittoHeaders,
final DittoHeaders expectedHeaders,
@Nullable final Object probeResponse,
final StatusCode expectedHttpStatusCode,
final DittoHeaders expectedResponseHeaders) throws InterruptedException, ExecutionException {
final var proxyActorProbe = ACTOR_SYSTEM_RESOURCE.newTestProbe();
final var modifyAttribute = ModifyAttribute.of(thingId, attributePointer, JsonValue.of("bar"), dittoHeaders);
final var responseFuture = new CompletableFuture<HttpResponse>();
final var underTest =
createHttpRequestActor(proxyActorProbe.ref(), getHttpRequest(modifyAttribute), responseFuture);
underTest.tell(modifyAttribute, ActorRef.noSender());
final var expectedModifyAttribute = modifyAttribute.setDittoHeaders(expectedHeaders);
proxyActorProbe.expectMsg(expectedModifyAttribute);
if (null != probeResponse) {
proxyActorProbe.reply(probeResponse);
}
final var httpResponse = responseFuture.get();
assertThat(httpResponse.status()).isEqualTo(expectedHttpStatusCode);
if (expectedResponseHeaders != null) {
final var expectedHttpResponseHeaders = HEADER_TRANSLATOR.toExternalHeaders(expectedResponseHeaders)
.entrySet()
.stream()
.map(e -> RawHeader.create(e.getKey(), e.getValue()))
.collect(Collectors.toList());
assertThat(httpResponse.getHeaders()).isEqualTo(expectedHttpResponseHeaders);
}
}
protected static HttpRequest getHttpRequest(final ModifyAttribute modifyAttribute) {
return HttpRequest.PUT(MessageFormat.format("/api/2/things/{0}/attributes{1}",
modifyAttribute.getEntityId(),
modifyAttribute.getAttributePointer()));
}
void testMessageCommand(final ThingId thingId,
final String messageSubject,
final DittoHeaders dittoHeaders,
final DittoHeaders expectedHeaders,
@Nullable final SendThingMessageResponse<?> probeResponse,
final StatusCode expectedHttpStatusCode,
final DittoHeaders expectedResponseHeaders,
@Nullable final ResponseEntity expectedHttpResponseEntity) throws InterruptedException, ExecutionException {
var expectedHttpResponse = HttpResponse.create().withStatus(expectedHttpStatusCode);
if (expectedResponseHeaders != null) {
final List<HttpHeader> expectedHttpResponseHeaders =
HEADER_TRANSLATOR.toExternalHeaders(expectedResponseHeaders)
.entrySet()
.stream()
.map(e -> RawHeader.create(e.getKey(), e.getValue()))
.collect(Collectors.toList());
expectedHttpResponse = expectedHttpResponse.withHeaders(expectedHttpResponseHeaders);
}
if (null != expectedHttpResponseEntity) {
expectedHttpResponse = expectedHttpResponse.withEntity(expectedHttpResponseEntity);
}
assertThat(testMessageCommand(thingId, messageSubject, dittoHeaders, expectedHeaders, probeResponse))
.isEqualTo(expectedHttpResponse);
}
HttpResponse testMessageCommand(final ThingId thingId,
final String messageSubject,
final DittoHeaders dittoHeaders,
final DittoHeaders expectedHeaders,
@Nullable final SendThingMessageResponse<?> probeResponse) throws InterruptedException, ExecutionException {
final var proxyActorProbe = ACTOR_SYSTEM_RESOURCE.newTestProbe();
final var messageHeaders = MessageHeaders.newBuilder(MessageDirection.TO, thingId, messageSubject)
.contentType("application/json")
.build();
final Message<?> message = Message.newBuilder(messageHeaders)
.payload(JsonValue.of("ping"))
.build();
final var sendThingMessage = SendThingMessage.of(thingId, message, dittoHeaders);
final var httpRequest = HttpRequest.PUT("/api/2/things/" + thingId + "/inbox/messages/" + messageSubject);
final var responseFuture = new CompletableFuture<HttpResponse>();
final var underTest = createHttpRequestActor(proxyActorProbe.ref(), httpRequest, responseFuture);
underTest.tell(sendThingMessage, ActorRef.noSender());
final var expectedSendThingMessage = sendThingMessage.setDittoHeaders(expectedHeaders);
proxyActorProbe.expectMsg(expectedSendThingMessage);
if (null != probeResponse) {
proxyActorProbe.reply(probeResponse);
}
return responseFuture.get();
}
SendThingMessageResponse<?> buildSendThingMessageResponse(final ThingId thingId,
final String messageSubject,
final DittoHeaders expectedHeaders,
final String contentType,
final JsonValue responsePayload) {
final Message<?> responseMessage = Message.newBuilder(
MessageHeaders.newBuilder(MessageDirection.TO, thingId, messageSubject)
.httpStatus(HttpStatus.IM_USED)
.contentType(contentType)
.build()
)
.payload(responsePayload)
.build();
return SendThingMessageResponse.of(thingId, responseMessage, HttpStatus.IM_USED, expectedHeaders);
}
ActorRef createHttpRequestActor(final ActorRef proxyActorRef,
final HttpRequest request,
final CompletableFuture<HttpResponse> response) {
return ACTOR_SYSTEM_RESOURCE.newActor(HttpRequestActor.props(
proxyActorRef,
HEADER_TRANSLATOR,
request,
response,
gatewayConfig.getHttpConfig(),
gatewayConfig.getCommandConfig(),
connectivityShardRegionProxy.ref()
)
);
}
DittoHeaders createAuthorizedHeaders() {
final var context = AuthorizationContext.newInstance(DittoAuthorizationContextType.JWT,
AuthorizationSubject.newInstance(SubjectId.newInstance(SubjectIssuer.GOOGLE, "any-google-user")),
AuthorizationSubject.newInstance(SubjectId.newInstance(SubjectIssuer.INTEGRATION,
"any-integration-subject")));
return DittoHeaders.newBuilder()
.correlationId(testNameCorrelationId.getCorrelationId())
.authorizationContext(context)
.build();
}
HttpResponse createExpectedWhoamiResponse(final Whoami whoami) {
final var authContext = getAuthContextWithPrefixedSubjectsFromHeaders(whoami.getDittoHeaders());
final var userInformation = DefaultUserInformation.fromAuthorizationContext(authContext);
final List<HttpHeader> expectedHeaders = HEADER_TRANSLATOR.toExternalHeaders(whoami.getDittoHeaders()
.toBuilder()
.responseRequired(false)
.build())
.entrySet()
.stream()
.map(e -> RawHeader.create(e.getKey(), e.getValue()))
.collect(Collectors.toList());
return HttpResponse.create().withStatus(StatusCodes.OK)
.withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(userInformation.toJsonString()))
.withHeaders(expectedHeaders);
}
static AuthorizationContext getAuthContextWithPrefixedSubjectsFromHeaders(final DittoHeaders headers) {
final var authContextString = headers.get(DittoHeaderDefinition.AUTHORIZATION_CONTEXT.getKey());
final var authContextJson = authContextString == null ?
JsonObject.empty() :
JsonObject.of(authContextString);
return AuthorizationModelFactory.newAuthContext(authContextJson);
}
}