-
Notifications
You must be signed in to change notification settings - Fork 214
/
DevOpsRouteTest.java
92 lines (79 loc) · 4 KB
/
DevOpsRouteTest.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
/*
* 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.devops;
import java.util.Collections;
import org.eclipse.ditto.base.api.devops.signals.commands.ExecutePiggybackCommand;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.gateway.service.endpoints.EndpointTestBase;
import org.eclipse.ditto.gateway.service.endpoints.directives.auth.DevopsAuthenticationDirectiveFactory;
import org.eclipse.ditto.gateway.service.util.config.security.DefaultDevOpsConfig;
import org.eclipse.ditto.gateway.service.util.config.security.DevOpsConfig;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing;
import org.junit.Before;
import org.junit.Test;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.http.javadsl.model.ContentTypes;
import org.apache.pekko.http.javadsl.model.HttpEntities;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.RequestEntity;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.http.javadsl.server.Route;
import org.apache.pekko.http.javadsl.testkit.TestRoute;
/**
* Unit test for {@link DevOpsRoute}.
*/
public final class DevOpsRouteTest extends EndpointTestBase {
private DevOpsRoute devOpsRoute;
private TestRoute underTest;
@Before
public void setUp() {
final var devopsAuthenticationDirectiveFactory =
DevopsAuthenticationDirectiveFactory.newInstance(jwtAuthenticationFactory, getInsecureDevopsConfig());
final var authenticationDirective = devopsAuthenticationDirectiveFactory.devops();
devOpsRoute = new DevOpsRoute(routeBaseProperties, authenticationDirective);
final Route route = extractRequestContext(ctx -> devOpsRoute.buildDevOpsRoute(ctx, Collections.emptyMap()));
underTest = testRoute(route);
}
@Test
public void testPiggyback() {
final var retrieveThing = RetrieveThing.of(ThingId.of("thing:id"), DittoHeaders.empty());
final var body = ExecutePiggybackCommand.of("things", "1", retrieveThing.toJson(), DittoHeaders.empty());
final RequestEntity requestEntity = HttpEntities.create(ContentTypes.APPLICATION_JSON, body.toJsonString());
final var result = underTest.run(HttpRequest.POST("/devops/piggyback")
.withEntity(requestEntity));
result.assertStatusCode(StatusCodes.OK);
}
@Test
public void testPiggybackWithJsonException() {
final var tooLongNumber = "89314404000484999942";
final var modifyAttribute = String.format("{\"type\":\"things.commands:modifyAttribute\"," +
"\"thingId\":\"thing:id\"," +
"\"attribute\":\"/attribute\"," +
"\"value\":%s}",
tooLongNumber);
final var executePiggyBack = String.format("{\"type\":\"devops.commands:executePiggybackCommand\"," +
"\"serviceName\":\"things\"," +
"\"instance\":null," +
"\"targetActorSelection\":\"1\"," +
"\"piggybackCommand\":%s}",
modifyAttribute);
final RequestEntity requestEntity = HttpEntities.create(ContentTypes.APPLICATION_JSON, executePiggyBack);
final var result = underTest.run(HttpRequest.POST("/devops/piggyback")
.withEntity(requestEntity));
result.assertStatusCode(StatusCodes.BAD_REQUEST);
}
private static DevOpsConfig getInsecureDevopsConfig() {
return DefaultDevOpsConfig.of(ConfigFactory.parseString("devops.secured=false"));
}
}