improve tests for RestTemplate (#11053)
Co-authored-by: Lauri Tulmin <tulmin@gmail.com>
This commit is contained in:
parent
0026dda7b1
commit
272258290f
|
@ -1,67 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright The OpenTelemetry Authors
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.web;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
import io.opentelemetry.api.OpenTelemetry;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
||||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
class RestTemplateBeanPostProcessorTest {
|
|
||||||
private static final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
|
||||||
|
|
||||||
static {
|
|
||||||
beanFactory.registerSingleton("openTelemetry", OpenTelemetry.noop());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("when processed bean is not of type RestTemplate should return object")
|
|
||||||
void returnsObject() {
|
|
||||||
BeanPostProcessor underTest =
|
|
||||||
new RestTemplateBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));
|
|
||||||
|
|
||||||
assertThat(underTest.postProcessAfterInitialization(new Object(), "testObject"))
|
|
||||||
.isExactlyInstanceOf(Object.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("when processed bean is of type RestTemplate should return RestTemplate")
|
|
||||||
void returnsRestTemplate() {
|
|
||||||
BeanPostProcessor underTest =
|
|
||||||
new RestTemplateBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));
|
|
||||||
|
|
||||||
assertThat(underTest.postProcessAfterInitialization(new RestTemplate(), "testRestTemplate"))
|
|
||||||
.isInstanceOf(RestTemplate.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("when processed bean is of type RestTemplate should add ONE RestTemplateInterceptor")
|
|
||||||
void addsRestTemplateInterceptor() {
|
|
||||||
BeanPostProcessor underTest =
|
|
||||||
new RestTemplateBeanPostProcessor(beanFactory.getBeanProvider(OpenTelemetry.class));
|
|
||||||
|
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
|
||||||
|
|
||||||
underTest.postProcessAfterInitialization(restTemplate, "testRestTemplate");
|
|
||||||
underTest.postProcessAfterInitialization(restTemplate, "testRestTemplate");
|
|
||||||
underTest.postProcessAfterInitialization(restTemplate, "testRestTemplate");
|
|
||||||
|
|
||||||
assertThat(
|
|
||||||
restTemplate.getInterceptors().stream()
|
|
||||||
.filter(RestTemplateBeanPostProcessorTest::isOtelInstrumentationInterceptor)
|
|
||||||
.count())
|
|
||||||
.isEqualTo(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isOtelInstrumentationInterceptor(ClientHttpRequestInterceptor rti) {
|
|
||||||
return rti.getClass().getName().startsWith("io.opentelemetry.instrumentation");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,26 +11,47 @@ import io.opentelemetry.api.OpenTelemetry;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
class SpringWebInstrumentationAutoConfigurationTest {
|
class SpringWebInstrumentationAutoConfigurationTest {
|
||||||
|
|
||||||
private final ApplicationContextRunner contextRunner =
|
private final ApplicationContextRunner contextRunner =
|
||||||
new ApplicationContextRunner()
|
new ApplicationContextRunner()
|
||||||
.withBean(OpenTelemetry.class, OpenTelemetry::noop)
|
.withBean(OpenTelemetry.class, OpenTelemetry::noop)
|
||||||
|
.withBean(RestTemplate.class, RestTemplate::new)
|
||||||
.withConfiguration(
|
.withConfiguration(
|
||||||
AutoConfigurations.of(SpringWebInstrumentationAutoConfiguration.class));
|
AutoConfigurations.of(SpringWebInstrumentationAutoConfiguration.class));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that users create {@link RestTemplate} bean is instrumented.
|
||||||
|
*
|
||||||
|
* <pre>{@code
|
||||||
|
* @Bean public RestTemplate restTemplate() {
|
||||||
|
* return new RestTemplate();
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
void instrumentationEnabled() {
|
void instrumentationEnabled() {
|
||||||
contextRunner
|
contextRunner
|
||||||
.withPropertyValues("otel.instrumentation.spring-web.enabled=true")
|
.withPropertyValues("otel.instrumentation.spring-web.enabled=true")
|
||||||
.run(
|
.run(
|
||||||
context ->
|
context -> {
|
||||||
assertThat(
|
assertThat(
|
||||||
context.getBean(
|
context.getBean(
|
||||||
"otelRestTemplateBeanPostProcessor",
|
"otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class))
|
||||||
RestTemplateBeanPostProcessor.class))
|
.isNotNull();
|
||||||
.isNotNull());
|
|
||||||
|
assertThat(
|
||||||
|
context.getBean(RestTemplate.class).getInterceptors().stream()
|
||||||
|
.filter(
|
||||||
|
rti ->
|
||||||
|
rti.getClass()
|
||||||
|
.getName()
|
||||||
|
.startsWith("io.opentelemetry.instrumentation"))
|
||||||
|
.count())
|
||||||
|
.isEqualTo(1);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue