Skip to content

Commit 6c6be3d

Browse files
committed
Convert play v2.6 test to java
1 parent f204246 commit 6c6be3d

File tree

2 files changed

+239
-0
lines changed

2 files changed

+239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.play.v2_6.server;
7+
8+
import play.Mode;
9+
import play.libs.concurrent.HttpExecution;
10+
import play.mvc.Controller;
11+
import play.mvc.Http;
12+
import play.mvc.Results;
13+
import play.routing.RoutingDsl;
14+
import play.server.Server;
15+
import scala.concurrent.ExecutionContextExecutor;
16+
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.ExecutorService;
18+
import java.util.concurrent.Executors;
19+
20+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS;
21+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR;
22+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION;
23+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD;
24+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
25+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
26+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
27+
28+
class PlayAsyncServerTest extends PlayServerTest {
29+
30+
private static final ExecutorService executor = Executors.newCachedThreadPool();
31+
32+
@Override
33+
public Server setupServer(int port) {
34+
ExecutionContextExecutor executionContextExecutor = HttpExecution.fromThread(executor);
35+
return Server.forRouter(Mode.TEST, port, components ->
36+
RoutingDsl.fromComponents(components)
37+
.GET(SUCCESS.getPath()).routeAsync(() ->
38+
CompletableFuture.supplyAsync(() ->
39+
controller(SUCCESS, () ->
40+
Results.status(SUCCESS.getStatus(), SUCCESS.getBody())
41+
), executionContextExecutor
42+
)
43+
)
44+
.GET(QUERY_PARAM.getPath()).routeAsync(() ->
45+
CompletableFuture.supplyAsync(() ->
46+
controller(QUERY_PARAM, () ->
47+
Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody())
48+
), executionContextExecutor
49+
)
50+
)
51+
.GET(REDIRECT.getPath()).routeAsync(() ->
52+
CompletableFuture.supplyAsync(() ->
53+
controller(REDIRECT, () ->
54+
Results.found(REDIRECT.getBody())
55+
), executionContextExecutor
56+
)
57+
)
58+
.GET(ERROR.getPath()).routeAsync(() ->
59+
CompletableFuture.supplyAsync(() ->
60+
controller(ERROR, () ->
61+
Results.status(ERROR.getStatus(), ERROR.getBody())
62+
), executionContextExecutor
63+
)
64+
)
65+
.GET(EXCEPTION.getPath()).routeAsync(() ->
66+
CompletableFuture.supplyAsync(() ->
67+
controller(EXCEPTION, () -> {
68+
throw new IllegalArgumentException(EXCEPTION.getBody());
69+
}), executionContextExecutor
70+
)
71+
)
72+
.GET(CAPTURE_HEADERS.getPath()).routeAsync(() -> {
73+
Http.Request request = Controller.request();
74+
Http.Response response = Controller.response();
75+
return CompletableFuture.supplyAsync(() ->
76+
controller(CAPTURE_HEADERS, () -> {
77+
request.header("X-Test-Request").ifPresent(value ->
78+
response.setHeader("X-Test-Response", value)
79+
);
80+
return Results.status(CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody());
81+
}), executionContextExecutor
82+
);
83+
})
84+
.GET(INDEXED_CHILD.getPath()).routeAsync(() -> {
85+
String id = Controller.request().getQueryString("id");
86+
return CompletableFuture.supplyAsync(() ->
87+
controller(INDEXED_CHILD, () -> {
88+
INDEXED_CHILD.collectSpanAttributes(name -> "id".equals(name) ? id : null);
89+
return Results.status(INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody());
90+
}), executionContextExecutor
91+
);
92+
})
93+
.build()
94+
);
95+
}
96+
97+
@Override
98+
protected void stopServer(Server server) {
99+
server.stop();
100+
executor.shutdown();
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.play.v2_6.server;
7+
8+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
9+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS;
10+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR;
11+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION;
12+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD;
13+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
14+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
15+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
16+
17+
import io.opentelemetry.api.common.AttributeKey;
18+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
19+
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
20+
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension;
21+
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
22+
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
23+
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
24+
import io.opentelemetry.sdk.trace.data.StatusData;
25+
import io.opentelemetry.semconv.HttpAttributes;
26+
import java.util.HashSet;
27+
import java.util.Set;
28+
import org.junit.jupiter.api.extension.RegisterExtension;
29+
import play.Mode;
30+
import play.mvc.Controller;
31+
import play.mvc.Results;
32+
import play.routing.RoutingDsl;
33+
import play.server.Server;
34+
35+
abstract class PlayServerTest extends AbstractHttpServerTest<Server> {
36+
37+
@RegisterExtension
38+
static final InstrumentationExtension testing = HttpServerInstrumentationExtension.forAgent();
39+
40+
@Override
41+
protected Server setupServer() {
42+
return Server.forRouter(
43+
Mode.TEST,
44+
port,
45+
components ->
46+
RoutingDsl.fromComponents(components)
47+
.GET(SUCCESS.getPath())
48+
.routeTo(
49+
() ->
50+
controller(
51+
SUCCESS, () -> Results.status(SUCCESS.getStatus(), SUCCESS.getBody())))
52+
.GET(QUERY_PARAM.getPath())
53+
.routeTo(
54+
() ->
55+
controller(
56+
QUERY_PARAM,
57+
() -> Results.status(QUERY_PARAM.getStatus(), QUERY_PARAM.getBody())))
58+
.GET(REDIRECT.getPath())
59+
.routeTo(() -> controller(REDIRECT, () -> Results.found(REDIRECT.getBody())))
60+
.GET(ERROR.getPath())
61+
.routeTo(
62+
() ->
63+
controller(ERROR, () -> Results.status(ERROR.getStatus(), ERROR.getBody())))
64+
.GET(EXCEPTION.getPath())
65+
.routeTo(
66+
() ->
67+
controller(
68+
EXCEPTION,
69+
() -> {
70+
throw new IllegalArgumentException(EXCEPTION.getBody());
71+
}))
72+
.GET(CAPTURE_HEADERS.getPath())
73+
.routeTo(
74+
() ->
75+
controller(
76+
CAPTURE_HEADERS,
77+
() -> {
78+
Controller.request()
79+
.header("X-Test-Request")
80+
.ifPresent(
81+
value ->
82+
Controller.response()
83+
.setHeader("X-Test-Response", value));
84+
return Results.status(
85+
CAPTURE_HEADERS.getStatus(), CAPTURE_HEADERS.getBody());
86+
}))
87+
.GET(INDEXED_CHILD.getPath())
88+
.routeTo(
89+
() ->
90+
controller(
91+
INDEXED_CHILD,
92+
() -> {
93+
INDEXED_CHILD.collectSpanAttributes(
94+
name -> Controller.request().getQueryString(name));
95+
return Results.status(
96+
INDEXED_CHILD.getStatus(), INDEXED_CHILD.getBody());
97+
}))
98+
.build());
99+
}
100+
101+
@Override
102+
protected void stopServer(Server server) throws Exception {
103+
server.stop();
104+
}
105+
106+
@Override
107+
protected void configure(HttpServerTestOptions options) {
108+
options.setHasHandlerSpan(unused -> true);
109+
options.setTestHttpPipelining(false);
110+
options.setResponseCodeOnNonStandardHttpMethod(404);
111+
// server spans are ended inside of the controller spans
112+
options.setVerifyServerSpanEndTime(false);
113+
options.setHttpAttributes(
114+
serverEndpoint -> {
115+
Set<AttributeKey<?>> attributes =
116+
new HashSet<>(HttpServerTestOptions.DEFAULT_HTTP_ATTRIBUTES);
117+
attributes.remove(HttpAttributes.HTTP_ROUTE);
118+
return attributes;
119+
});
120+
121+
options.setExpectedException(new IllegalArgumentException(EXCEPTION.getBody()));
122+
options.disableTestNonStandardHttpMethod();
123+
}
124+
125+
@Override
126+
public SpanDataAssert assertHandlerSpan(
127+
SpanDataAssert span, String method, ServerEndpoint endpoint) {
128+
span.hasName("play.request").hasKind(INTERNAL);
129+
if (endpoint == EXCEPTION) {
130+
span.hasStatus(StatusData.error());
131+
span.hasException(new IllegalArgumentException(EXCEPTION.getBody()));
132+
}
133+
return span;
134+
}
135+
136+
public abstract Server setupServer(int port);
137+
}

0 commit comments

Comments
 (0)