Skip to content

Commit 5bf09b1

Browse files
committed
fix tests
1 parent 1cfd3b9 commit 5bf09b1

File tree

6 files changed

+130
-58
lines changed

6 files changed

+130
-58
lines changed

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/gateway/v2_0/GatewayRouteMappingTest.java

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55

66
package io.opentelemetry.javaagent.instrumentation.spring.gateway.v2_0;
77

8-
import static org.assertj.core.api.Assertions.assertThat;
9-
10-
import io.opentelemetry.api.trace.SpanKind;
118
import io.opentelemetry.instrumentation.spring.gateway.common.AbstractRouteMappingTest;
12-
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse;
13-
import org.junit.jupiter.api.Test;
9+
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
10+
import java.util.List;
1411
import org.junit.jupiter.api.extension.ExtendWith;
1512
import org.springframework.boot.test.context.SpringBootTest;
1613
import org.springframework.test.context.junit.jupiter.SpringExtension;
@@ -21,54 +18,23 @@
2118
classes = {GatewayTestApplication.class})
2219
class GatewayRouteMappingTest extends AbstractRouteMappingTest {
2320

24-
@Test
25-
void gatewayRouteMappingTest() {
26-
String requestBody = "gateway";
27-
AggregatedHttpResponse response = client.post("/gateway/echo", requestBody).aggregate().join();
28-
assertThat(response.status().code()).isEqualTo(200);
29-
assertThat(response.contentUtf8()).isEqualTo(requestBody);
30-
testing.waitAndAssertTraces(
31-
trace ->
32-
trace.hasSpansSatisfyingExactly(
33-
span ->
34-
span.hasName("POST path_route")
35-
.hasKind(SpanKind.SERVER)
36-
.hasAttributesSatisfying(
37-
buildAttributeAssertions("path_route", "h1c://mock.response", 0, 1)),
38-
span -> span.hasName(WEBFLUX_SPAN_NAME).hasKind(SpanKind.INTERNAL)));
21+
@Override
22+
protected String getSpanName() {
23+
return "POST path_route";
24+
}
25+
26+
@Override
27+
protected List<AttributeAssertion> getExpectedAttributes() {
28+
return buildAttributeAssertions("path_route", "h1c://mock.response", 0, 1);
3929
}
4030

41-
@Test
42-
void gatewayRandomUuidRouteMappingTest() {
43-
String requestBody = "gateway";
44-
AggregatedHttpResponse response = client.post("/uuid/echo", requestBody).aggregate().join();
45-
assertThat(response.status().code()).isEqualTo(200);
46-
assertThat(response.contentUtf8()).isEqualTo(requestBody);
47-
testing.waitAndAssertTraces(
48-
trace ->
49-
trace.hasSpansSatisfyingExactly(
50-
span ->
51-
span.hasName("POST")
52-
.hasKind(SpanKind.SERVER)
53-
.hasAttributesSatisfying(buildAttributeAssertions("h1c://mock.uuid", 0, 1)),
54-
span -> span.hasName(WEBFLUX_SPAN_NAME).hasKind(SpanKind.INTERNAL)));
31+
@Override
32+
protected List<AttributeAssertion> getRandomUuidExpectedAttributes() {
33+
return buildAttributeAssertions("h1c://mock.uuid", 0, 1);
5534
}
5635

57-
@Test
58-
void gatewayFakeUuidRouteMappingTest() {
59-
String requestBody = "gateway";
60-
String routeId = "ffffffff-ffff-ffff-ffff-ffff";
61-
AggregatedHttpResponse response = client.post("/fake/echo", requestBody).aggregate().join();
62-
assertThat(response.status().code()).isEqualTo(200);
63-
assertThat(response.contentUtf8()).isEqualTo(requestBody);
64-
testing.waitAndAssertTraces(
65-
trace ->
66-
trace.hasSpansSatisfyingExactly(
67-
span ->
68-
span.hasName("POST " + routeId)
69-
.hasKind(SpanKind.SERVER)
70-
.hasAttributesSatisfying(
71-
buildAttributeAssertions(routeId, "h1c://mock.fake", 0, 1)),
72-
span -> span.hasName(WEBFLUX_SPAN_NAME).hasKind(SpanKind.INTERNAL)));
36+
@Override
37+
protected List<AttributeAssertion> getFakeUuidExpectedAttributes(String routeId) {
38+
return buildAttributeAssertions(routeId, "h1c://mock.fake", 0, 1);
7339
}
7440
}

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-2.2/testing/src/test/resources/application.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,14 @@ spring:
66
predicates:
77
- Path=/gateway/echo
88
order: 2023
9+
# Route without ID - will get random UUID (should be filtered out)
10+
- uri: h1c://mock.uuid
11+
predicates:
12+
- Path=/uuid/echo
13+
order: 0
14+
# Route with fake UUID ID (should NOT be filtered out)
15+
- id: ffffffff-ffff-ffff-ffff-ffff
16+
uri: h1c://mock.fake
17+
predicates:
18+
- Path=/fake/echo
19+
order: 0

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-common/testing/src/main/java/io/opentelemetry/instrumentation/spring/gateway/common/AbstractRouteMappingTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,55 @@ protected void testGatewayRouteMapping() {
7070
span -> span.hasName(getInternalSpanName()).hasKind(SpanKind.INTERNAL)));
7171
}
7272

73+
@Test
74+
protected void testRandomUuidRouteFiltering() {
75+
String requestBody = "gateway";
76+
AggregatedHttpResponse response = client.post("/uuid/echo", requestBody).aggregate().join();
77+
assertThat(response.status().code()).isEqualTo(200);
78+
assertThat(response.contentUtf8()).isEqualTo(requestBody);
79+
testing.waitAndAssertTraces(
80+
trace ->
81+
trace.hasSpansSatisfyingExactly(
82+
span ->
83+
span.hasName(getRandomUuidSpanName())
84+
.hasKind(SpanKind.SERVER)
85+
.hasAttributesSatisfying(getRandomUuidExpectedAttributes()),
86+
span -> span.hasName(getInternalSpanName()).hasKind(SpanKind.INTERNAL)));
87+
}
88+
89+
@Test
90+
protected void testFakeUuidRouteNotFiltered() {
91+
String requestBody = "gateway";
92+
String routeId = "ffffffff-ffff-ffff-ffff-ffff";
93+
AggregatedHttpResponse response = client.post("/fake/echo", requestBody).aggregate().join();
94+
assertThat(response.status().code()).isEqualTo(200);
95+
assertThat(response.contentUtf8()).isEqualTo(requestBody);
96+
testing.waitAndAssertTraces(
97+
trace ->
98+
trace.hasSpansSatisfyingExactly(
99+
span ->
100+
span.hasName(getFakeUuidSpanName(routeId))
101+
.hasKind(SpanKind.SERVER)
102+
.hasAttributesSatisfying(getFakeUuidExpectedAttributes(routeId)),
103+
span -> span.hasName(getInternalSpanName()).hasKind(SpanKind.INTERNAL)));
104+
}
105+
106+
protected String getRandomUuidSpanName() {
107+
return "POST";
108+
}
109+
110+
protected List<AttributeAssertion> getRandomUuidExpectedAttributes() {
111+
return buildAttributeAssertions("h1c://mock.uuid", 0, 0);
112+
}
113+
114+
protected String getFakeUuidSpanName(String routeId) {
115+
return "POST " + routeId;
116+
}
117+
118+
protected List<AttributeAssertion> getFakeUuidExpectedAttributes(String routeId) {
119+
return buildAttributeAssertions(routeId, "h1c://mock.fake", 0, 0);
120+
}
121+
73122
protected List<AttributeAssertion> buildAttributeAssertions(
74123
@Nullable String routeId, String uri, int order, int filterSize) {
75124
List<AttributeAssertion> assertions = new ArrayList<>();

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-webflux-4.3/testing/src/test/resources/application.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,15 @@ spring:
77
- uri: h1c://mock.response
88
predicates:
99
- Path=/gateway/echo
10-
order: 2023
10+
order: 2023
11+
# Route without ID - will get random UUID (should be filtered out)
12+
- uri: h1c://mock.uuid
13+
predicates:
14+
- Path=/uuid/echo
15+
order: 0
16+
# Route with fake UUID ID (should NOT be filtered out)
17+
- id: ffffffff-ffff-ffff-ffff-ffff
18+
uri: h1c://mock.fake
19+
predicates:
20+
- Path=/fake/echo
21+
order: 0

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-webmvc-4.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/gateway/webmvc/v5_0/Gateway43MvcRouteMappingTest.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
package io.opentelemetry.javaagent.instrumentation.spring.gateway.webmvc.v5_0;
77

8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
89
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
910

10-
import io.opentelemetry.api.common.AttributeKey;
1111
import io.opentelemetry.instrumentation.spring.gateway.common.AbstractRouteMappingTest;
1212
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
1313
import java.util.ArrayList;
@@ -37,8 +37,31 @@ protected String getInternalSpanName() {
3737
@Override
3838
protected List<AttributeAssertion> getExpectedAttributes() {
3939
List<AttributeAssertion> assertions = new ArrayList<>();
40-
assertions.add(
41-
equalTo(AttributeKey.stringKey("spring-cloud-gateway.route.id"), "test-route-id"));
40+
assertions.add(equalTo(stringKey("spring-cloud-gateway.route.id"), "test-route-id"));
41+
return assertions;
42+
}
43+
44+
@Override
45+
protected String getRandomUuidSpanName() {
46+
// WebMVC uses HTTP route in span name, not gateway route ID
47+
return "POST /uuid/echo";
48+
}
49+
50+
@Override
51+
protected List<AttributeAssertion> getRandomUuidExpectedAttributes() {
52+
return new ArrayList<>();
53+
}
54+
55+
@Override
56+
protected String getFakeUuidSpanName(String routeId) {
57+
// WebMVC uses HTTP route in span name, not gateway route ID
58+
return "POST /fake/echo";
59+
}
60+
61+
@Override
62+
protected List<AttributeAssertion> getFakeUuidExpectedAttributes(String routeId) {
63+
List<AttributeAssertion> assertions = new ArrayList<>();
64+
assertions.add(equalTo(stringKey("spring-cloud-gateway.route.id"), routeId));
4265
return assertions;
4366
}
4467
}

instrumentation/spring/spring-cloud-gateway/spring-cloud-gateway-webmvc-4.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/gateway/webmvc/v5_0/Gateway43MvcTestApplication.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,21 @@ public RouterFunction<ServerResponse> gatewayRouterFunction() {
3333
}
3434
};
3535

36-
return route("test-route-id")
37-
.POST("/gateway/echo", echoHandler)
38-
.before(uri("http://mock.response"))
39-
.build();
36+
RouterFunction<ServerResponse> mainRoute =
37+
route("test-route-id")
38+
.POST("/gateway/echo", echoHandler)
39+
.before(uri("http://mock.response"))
40+
.build();
41+
42+
RouterFunction<ServerResponse> uuidRoute =
43+
route().POST("/uuid/echo", echoHandler).before(uri("http://mock.uuid")).build();
44+
45+
RouterFunction<ServerResponse> fakeUuidRoute =
46+
route("ffffffff-ffff-ffff-ffff-ffff")
47+
.POST("/fake/echo", echoHandler)
48+
.before(uri("http://mock.fake"))
49+
.build();
50+
51+
return mainRoute.and(uuidRoute).and(fakeUuidRoute);
4052
}
4153
}

0 commit comments

Comments
 (0)