Skip to content

Commit 0b05aac

Browse files
committed
test deprecated classes
1 parent e141184 commit 0b05aac

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.webflux.v5_3;
7+
8+
import io.opentelemetry.instrumentation.spring.webflux.client.AbstractSpringWebfluxClientInstrumentationTest;
9+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
10+
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
11+
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
12+
import java.util.Collections;
13+
import org.junit.jupiter.api.extension.RegisterExtension;
14+
import org.springframework.web.reactive.function.client.WebClient;
15+
16+
class SpringWebfluxClientInstrumentationTest
17+
extends AbstractSpringWebfluxClientInstrumentationTest {
18+
19+
@RegisterExtension
20+
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forLibrary();
21+
22+
@Override
23+
protected WebClient.Builder instrument(WebClient.Builder builder) {
24+
SpringWebfluxClientTelemetry instrumentation =
25+
SpringWebfluxClientTelemetry.builder(testing.getOpenTelemetry())
26+
.setCapturedRequestHeaders(
27+
Collections.singletonList(AbstractHttpClientTest.TEST_REQUEST_HEADER))
28+
.setCapturedResponseHeaders(
29+
Collections.singletonList(AbstractHttpClientTest.TEST_RESPONSE_HEADER))
30+
.build();
31+
return builder.filters(instrumentation::addTracingFilter);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.webflux.v5_3;
7+
8+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
12+
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
13+
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension;
14+
import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions;
15+
import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint;
16+
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpRequest;
17+
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.RegisterExtension;
20+
import org.springframework.context.ConfigurableApplicationContext;
21+
22+
public final class SpringWebfluxServerInstrumentationTest
23+
extends AbstractHttpServerTest<ConfigurableApplicationContext> {
24+
25+
private static final String CONTEXT_PATH = "/test";
26+
27+
@RegisterExtension
28+
static final InstrumentationExtension testing = HttpServerInstrumentationExtension.forLibrary();
29+
30+
@Override
31+
protected ConfigurableApplicationContext setupServer() {
32+
return TestWebfluxSpringBootApp.start(port, CONTEXT_PATH);
33+
}
34+
35+
@Override
36+
public void stopServer(ConfigurableApplicationContext applicationContext) {
37+
applicationContext.close();
38+
}
39+
40+
@Override
41+
protected void configure(HttpServerTestOptions options) {
42+
options.setContextPath(CONTEXT_PATH);
43+
options.setTestPathParam(true);
44+
options.setExpectedException(new RuntimeException(ServerEndpoint.EXCEPTION.getBody()));
45+
46+
options.setExpectedHttpRoute(
47+
(endpoint, method) -> {
48+
if (endpoint == ServerEndpoint.PATH_PARAM) {
49+
return CONTEXT_PATH + "/path/{id}/param";
50+
}
51+
return expectedHttpRoute(endpoint, method);
52+
});
53+
54+
options.disableTestNonStandardHttpMethod();
55+
}
56+
57+
@Test
58+
void noMono() {
59+
ServerEndpoint endpoint = new ServerEndpoint("NO_MONO", "no-mono", 200, "success");
60+
String method = "GET";
61+
AggregatedHttpRequest request = request(endpoint, method);
62+
AggregatedHttpResponse response = client.execute(request).aggregate().join();
63+
64+
assertThat(response.status().code()).isEqualTo(SUCCESS.getStatus());
65+
assertThat(response.contentUtf8()).isEqualTo(SUCCESS.getBody());
66+
67+
assertTheTraces(1, null, null, null, method, endpoint);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.webflux.v5_3;
7+
8+
import static io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest.controller;
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.PATH_PARAM;
14+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM;
15+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT;
16+
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS;
17+
import static java.util.Collections.singletonList;
18+
19+
import io.opentelemetry.api.GlobalOpenTelemetry;
20+
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest;
21+
import java.net.URI;
22+
import java.util.Properties;
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.context.ConfigurableApplicationContext;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.http.HttpStatus;
28+
import org.springframework.http.ResponseEntity;
29+
import org.springframework.http.server.reactive.ServerHttpResponse;
30+
import org.springframework.stereotype.Controller;
31+
import org.springframework.web.bind.annotation.PathVariable;
32+
import org.springframework.web.bind.annotation.RequestHeader;
33+
import org.springframework.web.bind.annotation.RequestMapping;
34+
import org.springframework.web.bind.annotation.RequestParam;
35+
import org.springframework.web.bind.annotation.ResponseBody;
36+
import org.springframework.web.server.WebFilter;
37+
import reactor.core.publisher.Flux;
38+
import reactor.core.publisher.Mono;
39+
40+
@SpringBootApplication
41+
class TestWebfluxSpringBootApp {
42+
43+
static ConfigurableApplicationContext start(int port, String contextPath) {
44+
Properties props = new Properties();
45+
props.put("server.port", port);
46+
props.put("spring.webflux.base-path", contextPath);
47+
48+
SpringApplication app = new SpringApplication(TestWebfluxSpringBootApp.class);
49+
app.setDefaultProperties(props);
50+
return app.run();
51+
}
52+
53+
@Bean
54+
WebFilter telemetryFilter() {
55+
return SpringWebfluxServerTelemetry.builder(GlobalOpenTelemetry.get())
56+
.setCapturedRequestHeaders(singletonList(AbstractHttpServerTest.TEST_REQUEST_HEADER))
57+
.setCapturedResponseHeaders(singletonList(AbstractHttpServerTest.TEST_RESPONSE_HEADER))
58+
.build()
59+
.createWebFilterAndRegisterReactorHook();
60+
}
61+
62+
@Controller
63+
static class TestController {
64+
65+
@RequestMapping("/success")
66+
@ResponseBody
67+
Flux<String> success() {
68+
return Flux.defer(() -> Flux.just(controller(SUCCESS, SUCCESS::getBody)));
69+
}
70+
71+
@RequestMapping("/no-mono")
72+
@ResponseBody
73+
String noMono() {
74+
return controller(SUCCESS, SUCCESS::getBody);
75+
}
76+
77+
@RequestMapping("/query")
78+
@ResponseBody
79+
Mono<String> query_param(@RequestParam("some") String param) {
80+
return Mono.just(controller(QUERY_PARAM, () -> "some=" + param));
81+
}
82+
83+
@RequestMapping("/redirect")
84+
@ResponseBody
85+
Mono<Void> redirect(ServerHttpResponse response) {
86+
response.setStatusCode(HttpStatus.FOUND);
87+
response.getHeaders().setLocation(URI.create("/redirected"));
88+
return controller(REDIRECT, response::setComplete);
89+
}
90+
91+
@RequestMapping("/error-status")
92+
Mono<ResponseEntity<String>> error() {
93+
return Mono.just(
94+
controller(
95+
ERROR,
96+
() -> new ResponseEntity<>(ERROR.getBody(), HttpStatus.valueOf(ERROR.getStatus()))));
97+
}
98+
99+
@RequestMapping("/exception")
100+
Flux<ResponseEntity<String>> exception() throws Exception {
101+
return Flux.just(
102+
controller(
103+
EXCEPTION,
104+
() -> {
105+
throw new RuntimeException(EXCEPTION.getBody());
106+
}));
107+
}
108+
109+
@RequestMapping("/captureHeaders")
110+
Mono<ResponseEntity<String>> capture_headers(
111+
@RequestHeader("X-Test-Request") String testRequestHeader) {
112+
return Mono.just(
113+
controller(
114+
CAPTURE_HEADERS,
115+
() ->
116+
ResponseEntity.ok()
117+
.header("X-Test-Response", testRequestHeader)
118+
.body(CAPTURE_HEADERS.getBody())));
119+
}
120+
121+
@RequestMapping("/path/{id}/param")
122+
@ResponseBody
123+
Mono<String> path_param(@PathVariable("id") int id) {
124+
return Mono.just(controller(PATH_PARAM, () -> String.valueOf(id)));
125+
}
126+
127+
@RequestMapping("/child")
128+
@ResponseBody
129+
Mono<String> indexed_child(@RequestParam("id") String id) {
130+
return Mono.just(
131+
controller(
132+
INDEXED_CHILD,
133+
() -> {
134+
INDEXED_CHILD.collectSpanAttributes(name -> name.equals("id") ? id : null);
135+
return INDEXED_CHILD.getBody();
136+
}));
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)