Skip to content

Commit 28db801

Browse files
authored
Convert jaxrs tests to java (#12816)
1 parent 2201a6d commit 28db801

File tree

101 files changed

+2999
-2494
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+2999
-2494
lines changed

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-annotations/javaagent/src/test/groovy/JaxrsAnnotationsInstrumentationTest.groovy

-168
This file was deleted.

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-annotations/javaagent/src/test/java/JavaInterfaces.java instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-annotations/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JavaInterfaces.java

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;
7+
68
import javax.ws.rs.GET;
79
import javax.ws.rs.Path;
810

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;
7+
8+
import static io.opentelemetry.instrumentation.test.utils.ClassUtils.getClassName;
9+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
10+
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
11+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD;
12+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE;
13+
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION;
14+
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE;
15+
16+
import io.opentelemetry.api.trace.SpanKind;
17+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
18+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
19+
import io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0.JavaInterfaces.Jax;
20+
import io.opentelemetry.semconv.ErrorAttributes;
21+
import java.util.stream.Stream;
22+
import javax.ws.rs.DELETE;
23+
import javax.ws.rs.GET;
24+
import javax.ws.rs.HEAD;
25+
import javax.ws.rs.OPTIONS;
26+
import javax.ws.rs.POST;
27+
import javax.ws.rs.PUT;
28+
import javax.ws.rs.Path;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.RegisterExtension;
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.Arguments;
33+
import org.junit.jupiter.params.provider.MethodSource;
34+
35+
class JaxRs2AnnotationsInstrumentationTest {
36+
37+
@RegisterExtension
38+
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
39+
40+
private static Stream<Arguments> provideArguments() {
41+
return Stream.of(
42+
Arguments.of(
43+
"/a",
44+
new Jax() {
45+
@Path("/a")
46+
@Override
47+
public void call() {}
48+
}),
49+
Arguments.of(
50+
"/b",
51+
new Jax() {
52+
@GET
53+
@Path("/b")
54+
@Override
55+
public void call() {}
56+
}),
57+
Arguments.of(
58+
"/interface/c",
59+
new InterfaceWithPath() {
60+
@POST
61+
@Path("/c")
62+
@Override
63+
public void call() {}
64+
}),
65+
Arguments.of(
66+
"/interface",
67+
new InterfaceWithPath() {
68+
@HEAD
69+
@Override
70+
public void call() {}
71+
}),
72+
Arguments.of(
73+
"/abstract/d",
74+
new AbstractClassWithPath() {
75+
@POST
76+
@Path("/d")
77+
@Override
78+
public void call() {}
79+
}),
80+
Arguments.of(
81+
"/abstract",
82+
new AbstractClassWithPath() {
83+
@PUT
84+
@Override
85+
public void call() {}
86+
}),
87+
Arguments.of(
88+
"/child/e",
89+
new ChildClassWithPath() {
90+
@OPTIONS
91+
@Path("/e")
92+
@Override
93+
public void call() {}
94+
}),
95+
Arguments.of(
96+
"/child/call",
97+
new ChildClassWithPath() {
98+
@DELETE
99+
@Override
100+
public void call() {}
101+
}),
102+
Arguments.of("/child/call", new ChildClassWithPath()),
103+
Arguments.of("/child/call", new JavaInterfaces.ChildClassOnInterface()),
104+
Arguments.of("/child/call", new JavaInterfaces.DefaultChildClassOnInterface()));
105+
}
106+
107+
@ParameterizedTest
108+
@MethodSource("provideArguments")
109+
void createSpanForAnnotatedMethod(String path, Jax action) {
110+
String className = getClassName(action.getClass());
111+
testing.runWithHttpServerSpan(action::call);
112+
113+
testing.waitAndAssertTraces(
114+
trace ->
115+
trace.hasSpansSatisfyingExactly(
116+
span ->
117+
span.hasName("GET " + path)
118+
.hasKind(SpanKind.SERVER)
119+
.hasNoParent()
120+
.hasAttributesSatisfyingExactly(
121+
equalTo(HTTP_REQUEST_METHOD, "GET"),
122+
equalTo(HTTP_ROUTE, path),
123+
equalTo(ERROR_TYPE, ErrorAttributes.ErrorTypeValues.OTHER)),
124+
span ->
125+
span.hasName(className + ".call")
126+
.hasParent(trace.getSpan(0))
127+
.hasAttributesSatisfyingExactly(
128+
equalTo(CODE_NAMESPACE, action.getClass().getName()),
129+
equalTo(CODE_FUNCTION, "call"))));
130+
}
131+
132+
@Test
133+
void notAnnotatedMethod() {
134+
Jax action =
135+
new Jax() {
136+
@Override
137+
public void call() {}
138+
};
139+
testing.runWithHttpServerSpan(action::call);
140+
141+
testing.waitAndAssertTraces(
142+
trace ->
143+
trace.hasSpansSatisfyingExactly(
144+
span ->
145+
span.hasName("GET")
146+
.hasKind(SpanKind.SERVER)
147+
.hasNoParent()
148+
.hasAttributesSatisfyingExactly(
149+
equalTo(HTTP_REQUEST_METHOD, "GET"),
150+
equalTo(ERROR_TYPE, ErrorAttributes.ErrorTypeValues.OTHER))));
151+
}
152+
153+
@Path("/interface")
154+
interface InterfaceWithPath extends Jax {
155+
@GET
156+
@Override
157+
void call();
158+
}
159+
160+
@Path("/abstract")
161+
abstract static class AbstractClassWithPath implements Jax {
162+
@PUT
163+
@Override
164+
public abstract void call();
165+
}
166+
167+
@Path("child")
168+
static class ChildClassWithPath extends AbstractClassWithPath {
169+
@Path("call")
170+
@POST
171+
@Override
172+
public void call() {}
173+
}
174+
}

0 commit comments

Comments
 (0)