Refactor RestTemplate tests to Java
DELETE
instrumentation/spring/spring-web-3.1/library/src/test/groovy/SpringWebInstrumentationTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import io.opentelemetry.api.common.AttributeKey
-import io.opentelemetry.instrumentation.spring.web.SpringWebTelemetry
-import io.opentelemetry.instrumentation.test.LibraryTestTrait
-import io.opentelemetry.instrumentation.test.base.HttpClientTest
-import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest
-import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
-import org.springframework.http.HttpEntity
-import org.springframework.http.HttpHeaders
-import org.springframework.http.HttpMethod
-import org.springframework.http.client.SimpleClientHttpRequestFactory
-import org.springframework.web.client.ResourceAccessException
-import org.springframework.web.client.RestTemplate
-import spock.lang.Shared
-
-class SpringWebInstrumentationTest extends HttpClientTest<HttpEntity<String>> implements LibraryTestTrait {
- @Shared
- RestTemplate restTemplate
-
- def setupSpec() {
- if (restTemplate == null) {
- def requestFactory = new SimpleClientHttpRequestFactory()
- requestFactory.setConnectTimeout(CONNECT_TIMEOUT_MS)
- restTemplate = new RestTemplate(requestFactory)
- restTemplate.getInterceptors().add(SpringWebTelemetry.create(getOpenTelemetry()).newInterceptor())
- }
- }
-
- @Override
- HttpEntity<String> buildRequest(String method, URI uri, Map<String, String> headers) {
- def httpHeaders = new HttpHeaders()
- headers.each { httpHeaders.put(it.key, [it.value]) }
- return new HttpEntity<String>(httpHeaders)
- }
-
- @Override
- int sendRequest(HttpEntity<String> request, String method, URI uri, Map<String, String> headers) {
- try {
- return restTemplate.exchange(uri, HttpMethod.valueOf(method), request, String)
- .statusCode
- .value()
- } catch (ResourceAccessException exception) {
- throw exception.getCause()
- }
- }
-
- @Override
- void sendRequestWithCallback(HttpEntity<String> request, String method, URI uri, Map<String, String> headers, AbstractHttpClientTest.RequestResult requestResult) {
- try {
- restTemplate.execute(uri, HttpMethod.valueOf(method), { req ->
- headers.forEach(req.getHeaders().&add)
- }, { response ->
- requestResult.complete(response.statusCode.value())
- })
- } catch (ResourceAccessException exception) {
- requestResult.complete(exception.getCause())
- }
- }
-
- @Override
- boolean testCircularRedirects() {
- false
- }
-
- @Override
- Set<AttributeKey<?>> httpAttributes(URI uri) {
- def attributes = super.httpAttributes(uri)
- attributes.remove(SemanticAttributes.HTTP_FLAVOR)
- attributes.add(SemanticAttributes.HTTP_SCHEME)
- attributes
- }
-}

ADD
instrumentation/spring/spring-web-3.1/library/src/test/java/io/opentelemetry/instrumentation/spring/web/SpringWebInstrumentationTest.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.instrumentation.spring.web;
+
+import static java.util.Collections.singletonList;
+
+import io.opentelemetry.api.common.AttributeKey;
+import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
+import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
+import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
+import java.net.URI;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.web.client.ResourceAccessException;
+import org.springframework.web.client.RestTemplate;
+
+public class SpringWebInstrumentationTest extends AbstractHttpClientTest<HttpEntity<String>> {
+
+ @RegisterExtension
+ static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forLibrary();
+
+ static RestTemplate restTemplate;
+
+ @BeforeAll
+ static void setUp() {
+ SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
+ requestFactory.setConnectTimeout((int) CONNECTION_TIMEOUT.toMillis());
+ restTemplate = new RestTemplate(requestFactory);
+ restTemplate
+ .getInterceptors()
+ .add(SpringWebTelemetry.create(testing.getOpenTelemetry()).newInterceptor());
+ }
+
+ @Override
+ public HttpEntity<String> buildRequest(String method, URI uri, Map<String, String> headers) {
+ HttpHeaders httpHeaders = new HttpHeaders();
+ headers.forEach((k, v) -> httpHeaders.put(k, singletonList(v)));
+ return new HttpEntity<>(httpHeaders);
+ }
+
+ @Override
+ public int sendRequest(
+ HttpEntity<String> request, String method, URI uri, Map<String, String> headers)
+ throws Exception {
+ try {
+ return restTemplate
+ .exchange(uri, HttpMethod.valueOf(method), request, String.class)
+ .getStatusCode()
+ .value();
+ } catch (ResourceAccessException exception) {
+ throw (Exception) exception.getCause();
+ }
+ }
+
+ @Override
+ public void sendRequestWithCallback(
+ HttpEntity<String> request,
+ String method,
+ URI uri,
+ Map<String, String> headers,
+ AbstractHttpClientTest.RequestResult requestResult) {
+ try {
+ restTemplate.execute(
+ uri,
+ HttpMethod.valueOf(method),
+ req -> headers.forEach(req.getHeaders()::add),
+ response -> {
+ requestResult.complete(response.getStatusCode().value());
+ return null;
+ });
+ } catch (ResourceAccessException exception) {
+ requestResult.complete(exception.getCause());
+ }
+ }
+
+ @Override
+ protected void configure(HttpClientTestOptions options) {
+ options.disableTestCircularRedirects();
+ options.setHttpAttributes(
+ uri -> {
+ Set<AttributeKey<?>> attributes =
+ new HashSet<>(HttpClientTestOptions.DEFAULT_HTTP_ATTRIBUTES);
+ attributes.remove(SemanticAttributes.HTTP_FLAVOR);
+ return attributes;
+ });
+ }
+}

DELETE
instrumentation/spring/spring-web-3.1/library/src/test/java/io/opentelemetry/instrumentation/spring/web/SpringWebTelemetryTest.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.instrumentation.spring.web;
-
-import static io.opentelemetry.sdk.testing.assertj.TracesAssert.assertThat;
-import static org.mockito.BDDMockito.then;
-
-import io.opentelemetry.api.trace.SpanKind;
-import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.jupiter.api.extension.RegisterExtension;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-import org.springframework.http.HttpRequest;
-import org.springframework.http.client.ClientHttpRequestExecution;
-import org.springframework.http.client.ClientHttpRequestInterceptor;
-
-@ExtendWith(MockitoExtension.class)
-class SpringWebTelemetryTest {
- @RegisterExtension
- static final LibraryInstrumentationExtension testing = LibraryInstrumentationExtension.create();
-
- @Mock HttpRequest httpRequestMock;
- @Mock ClientHttpRequestExecution requestExecutionMock;
- static final byte[] requestBody = new byte[0];
-
- @Test
- void shouldSkipWhenContextHasClientSpan() throws Exception {
- // given
- ClientHttpRequestInterceptor interceptor =
- SpringWebTelemetry.create(testing.getOpenTelemetry()).newInterceptor();
-
- // when
- testing.runWithHttpClientSpan(
- "parent",
- () -> {
- interceptor.intercept(httpRequestMock, requestBody, requestExecutionMock);
- });
-
- // then
- then(requestExecutionMock).should().execute(httpRequestMock, requestBody);
-
- assertThat(testing.waitForTraces(1))
- .hasTracesSatisfyingExactly(
- trace ->
- trace.hasSpansSatisfyingExactly(
- span -> span.hasName("parent").hasKind(SpanKind.CLIENT)));
- }
-}

DELETE
instrumentation/spring/spring-web-3.1/testing/src/test/groovy/SpringRestTemplateTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import io.opentelemetry.instrumentation.test.AgentTestTrait
-import io.opentelemetry.instrumentation.test.base.HttpClientTest
-import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest
-import org.springframework.http.HttpEntity
-import org.springframework.http.HttpHeaders
-import org.springframework.http.HttpMethod
-import org.springframework.http.client.ClientHttpRequestFactory
-import org.springframework.http.client.SimpleClientHttpRequestFactory
-import org.springframework.web.client.ResourceAccessException
-import org.springframework.web.client.RestTemplate
-import spock.lang.Shared
-
-class SpringRestTemplateTest extends HttpClientTest<HttpEntity<String>> implements AgentTestTrait {
-
- @Shared
- ClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory()
- @Shared
- RestTemplate restTemplate = new RestTemplate(factory)
-
- def setupSpec() {
- factory.connectTimeout = CONNECT_TIMEOUT_MS
- }
-
- @Override
- HttpEntity<String> buildRequest(String method, URI uri, Map<String, String> headers) {
- def httpHeaders = new HttpHeaders()
- headers.each { httpHeaders.put(it.key, [it.value]) }
- return new HttpEntity<String>(httpHeaders)
- }
-
- @Override
- int sendRequest(HttpEntity<String> request, String method, URI uri, Map<String, String> headers) {
- try {
- return restTemplate.exchange(uri, HttpMethod.valueOf(method), request, String)
- .statusCode
- .value()
- } catch (ResourceAccessException exception) {
- throw exception.getCause()
- }
- }
-
- @Override
- void sendRequestWithCallback(HttpEntity<String> request, String method, URI uri, Map<String, String> headers = [:], AbstractHttpClientTest.RequestResult requestResult) {
- try {
- restTemplate.execute(uri, HttpMethod.valueOf(method), { req ->
- req.getHeaders().putAll(request.getHeaders())
- }, { response ->
- // read request body to avoid broken pipe errors on the server side
- byte[] buffer = new byte[1024]
- try (InputStream inputStream = response.body) {
- while (inputStream.read(buffer) >= 0) {
- }
- }
- requestResult.complete(response.statusCode.value())
- })
- } catch (ResourceAccessException exception) {
- requestResult.complete(exception.getCause())
- }
- }
-
- @Override
- int maxRedirects() {
- 20
- }
-
- @Override
- Integer responseCodeOnRedirectError() {
- return 302
- }
-}

ADD
instrumentation/spring/spring-web-3.1/testing/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/web/SpringRestTemplateTest.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.spring.web;
+
+import static java.util.Collections.singletonList;
+
+import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
+import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Map;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.web.client.ResourceAccessException;
+import org.springframework.web.client.RestTemplate;
+
+public class SpringRestTemplateTest extends AbstractHttpClientTest<HttpEntity<String>> {
+
+ @RegisterExtension
+ static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
+
+ static RestTemplate restTemplate;
+
+ @BeforeAll
+ static void setUp() {
+ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
+ factory.setConnectTimeout((int) CONNECTION_TIMEOUT.toMillis());
+ restTemplate = new RestTemplate(factory);
+ }
+
+ @Override
+ public HttpEntity<String> buildRequest(String method, URI uri, Map<String, String> headers) {
+ HttpHeaders httpHeaders = new HttpHeaders();
+ headers.forEach((k, v) -> httpHeaders.put(k, singletonList(v)));
+ return new HttpEntity<>(httpHeaders);
+ }
+
+ @Override
+ public int sendRequest(
+ HttpEntity<String> request, String method, URI uri, Map<String, String> headers)
+ throws Exception {
+ try {
+ return restTemplate
+ .exchange(uri, HttpMethod.valueOf(method), request, String.class)
+ .getStatusCode()
+ .value();
+ } catch (ResourceAccessException exception) {
+ throw (Exception) exception.getCause();
+ }
+ }
+
+ @Override
+ public void sendRequestWithCallback(
+ HttpEntity<String> request,
+ String method,
+ URI uri,
+ Map<String, String> headers,
+ AbstractHttpClientTest.RequestResult requestResult) {
+ try {
+ restTemplate.execute(
+ uri,
+ HttpMethod.valueOf(method),
+ req -> headers.forEach(req.getHeaders()::add),
+ response -> {
+ byte[] buffer = new byte[1024];
+ try (InputStream inputStream = response.getBody()) {
+ while (inputStream.read(buffer) >= 0) {}
+ }
+ requestResult.complete(response.getStatusCode().value());
+ return null;
+ });
+ } catch (ResourceAccessException exception) {
+ requestResult.complete(exception.getCause());
+ }
+ }
+
+ @Override
+ protected void configure(HttpClientTestOptions options) {
+ options.setMaxRedirects(20);
+ options.setResponseCodeOnRedirectError(302);
+ }
+}

Refactor RestTemplate tests to Java (#6485)