diff --git a/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/TracerAutoConfigurationTest.java b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/TracerAutoConfigurationTest.java new file mode 100644 index 0000000000..d7e7fbdb48 --- /dev/null +++ b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/TracerAutoConfigurationTest.java @@ -0,0 +1,85 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.trace.Tracer; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; + +/** Spring Boot auto configuration test for {@link TracerAutoConfiguration} */ +public class TracerAutoConfigurationTest { + @TestConfiguration + static class CustomTracerConfiguration { + @Bean + public Tracer customTestTracer() { + return OpenTelemetry.getTracer("customTestTracer"); + } + } + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); + + @Test + public void should_NOT_initalize_otelTracer_if_tracer_bean_exists() { + this.contextRunner + .withUserConfiguration(CustomTracerConfiguration.class) + .withConfiguration(AutoConfigurations.of(TracerAutoConfiguration.class)) + .run( + (context) -> { + assertTrue( + "Application Context contains customTestTracer bean", + context.containsBean("customTestTracer")); + + assertFalse( + "Application Context DOES NOT contain the otelTracer bean defined in TracerAutoConfiguration", + context.containsBean("otelTracer")); + }); + } + + @Test + public void should_contain_otelTracer_bean() { + this.contextRunner + .withConfiguration(AutoConfigurations.of(TracerAutoConfiguration.class)) + .run( + (context) -> { + assertTrue( + "Application Context contains otelTracer bean", + context.containsBean("otelTracer")); + }); + } + + @Test + public void should_set_tracer_name() { + this.contextRunner + .withPropertyValues("opentelemetry.trace.tracer.name=testTracer") + .withConfiguration(AutoConfigurations.of(TracerAutoConfiguration.class)) + .run( + (context) -> { + assertEquals( + "Application Context sets the Tracer with name to testTracer", + context.getBean("otelTracer", Tracer.class), + OpenTelemetry.getTracer("testTracer")); + }); + } +} diff --git a/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateAutoConfigurationTest.java b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateAutoConfigurationTest.java new file mode 100644 index 0000000000..bbec4f4250 --- /dev/null +++ b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateAutoConfigurationTest.java @@ -0,0 +1,72 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +/** Spring Boot auto configuration test for {@link RestTemplateAutoConfiguration} */ +public class RestTemplateAutoConfigurationTest { + + private final ApplicationContextRunner contextRunner = + new ApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of( + TracerAutoConfiguration.class, RestTemplateAutoConfiguration.class)); + + @Test + public void should_initialize_RestTemplateInterceptor_bean_when_httpclients_are_ENABLED() { + this.contextRunner + .withPropertyValues("opentelemetry.trace.httpclients.enabled=true") + .run( + (context) -> { + assertNotNull( + "Application Context contains RestTemplateBeanPostProcessor bean", + context.getBean( + "otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class)); + }); + } + + @Test + public void should_NOT_initialize_RestTemplateInterceptor_bean_when_httpclients_are_DISABLED() { + this.contextRunner + .withPropertyValues("opentelemetry.trace.httpclients.enabled=false") + .run( + (context) -> { + assertFalse( + "Application Context DOES NOT contain otelRestTemplateBeanPostProcessor bean", + context.containsBean("otelRestTemplateBeanPostProcessor")); + }); + } + + @Test + public void + should_initialize_RestTemplateInterceptor_bean_when_httpclients_enabled_property_is_MISSING() { + this.contextRunner.run( + (context) -> { + assertNotNull( + "Application Context contains RestTemplateBeanPostProcessor bean", + context.getBean( + "otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class)); + }); + } +} diff --git a/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateBeanPostProcessorTest.java b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateBeanPostProcessorTest.java new file mode 100644 index 0000000000..274c802842 --- /dev/null +++ b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/resttemplate/RestTemplateBeanPostProcessorTest.java @@ -0,0 +1,70 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import io.opentelemetry.instrumentation.spring.httpclients.RestTemplateInterceptor; +import io.opentelemetry.trace.Tracer; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.web.client.RestTemplate; + +/** Spring bean post processor test {@link RestTemplateBeanPostProcessor} */ +@RunWith(MockitoJUnitRunner.class) +public class RestTemplateBeanPostProcessorTest { + + @Mock Tracer tracer; + + RestTemplateBeanPostProcessor restTemplateBeanPostProcessor = + new RestTemplateBeanPostProcessor(tracer); + + @Test + public void should_return_object_if_processed_bean_is_not_of_type_RestTemplate() { + assertEquals( + restTemplateBeanPostProcessor + .postProcessAfterInitialization(new Object(), "testObject") + .getClass(), + Object.class); + } + + @Test + public void should_return_RestTemplate_if_processed_bean_is_of_type_RestTemplate() { + assertTrue( + restTemplateBeanPostProcessor.postProcessAfterInitialization( + new RestTemplate(), "testRestTemplate") + instanceof RestTemplate); + } + + @Test + public void should_add_ONE_RestTemplateInterceptor_if_processed_bean_is_of_type_RestTemplate() { + RestTemplate restTemplate = new RestTemplate(); + + restTemplateBeanPostProcessor.postProcessAfterInitialization(restTemplate, "testRestTemplate"); + restTemplateBeanPostProcessor.postProcessAfterInitialization(restTemplate, "testRestTemplate"); + restTemplateBeanPostProcessor.postProcessAfterInitialization(restTemplate, "testRestTemplate"); + + assertEquals( + restTemplate.getInterceptors().stream() + .filter(rti -> rti instanceof RestTemplateInterceptor) + .count(), + 1); + } +} diff --git a/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientAutoConfigurationTest.java b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientAutoConfigurationTest.java new file mode 100644 index 0000000000..8d9fa6d80d --- /dev/null +++ b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientAutoConfigurationTest.java @@ -0,0 +1,72 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +/** Spring Boot auto configuration test for {@link WebClientAutoConfiguration} */ +public class WebClientAutoConfigurationTest { + + private final ApplicationContextRunner contextRunner = + new ApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of( + TracerAutoConfiguration.class, WebClientAutoConfiguration.class)); + + @Test + public void should_initialize_WebClientBeanPostProccesor_bean_when_httpclients_are_ENABLED() { + this.contextRunner + .withPropertyValues("opentelemetry.trace.httpclients.enabled=true") + .run( + (context) -> { + assertNotNull( + "Application Context contains WebClientBeanPostProcessor bean", + context.getBean( + "otelWebClientBeanPostProcessor", WebClientBeanPostProcessor.class)); + }); + } + + @Test + public void + should_NOT_initialize_WebClientBeanPostProccesor_bean_when_httpclients_are_DISABLED() { + this.contextRunner + .withPropertyValues("opentelemetry.trace.httpclients.enabled=false") + .run( + (context) -> { + assertFalse( + "Application Context DOES NOT contain otelWebClientBeanPostProcessor bean", + context.containsBean("otelWebClientBeanPostProcessor")); + }); + } + + @Test + public void + should_initialize_WebClientBeanPostProccesor_bean_when_httpclients_enabled_property_is_MISSING() { + this.contextRunner.run( + (context) -> { + assertNotNull( + "Application Context contains WebClientBeanPostProcessor bean", + context.getBean("otelWebClientBeanPostProcessor", WebClientBeanPostProcessor.class)); + }); + } +} diff --git a/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientBeanPostProcessorTest.java b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientBeanPostProcessorTest.java new file mode 100644 index 0000000000..f93138a22c --- /dev/null +++ b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/httpclients/webclient/WebClientBeanPostProcessorTest.java @@ -0,0 +1,101 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import io.opentelemetry.instrumentation.springwebflux.client.WebClientTracingFilter; +import io.opentelemetry.trace.Tracer; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.web.reactive.function.client.WebClient; + +/** Spring bean post processor test {@link WebClientBeanPostProcessor} */ +@RunWith(MockitoJUnitRunner.class) +public class WebClientBeanPostProcessorTest { + + @Mock Tracer tracer; + + WebClientBeanPostProcessor webClientBeanPostProcessor = new WebClientBeanPostProcessor(tracer); + + @Test + public void + should_return_object_if_processed_bean_is_NOT_of_type_WebClient_or_WebClientBuilder() { + assertEquals( + webClientBeanPostProcessor + .postProcessAfterInitialization(new Object(), "testObject") + .getClass(), + Object.class); + } + + @Test + public void should_return_web_client_if_processed_bean_is_of_type_WebClient() { + WebClient webClient = WebClient.create(); + + assertTrue( + webClientBeanPostProcessor.postProcessAfterInitialization(webClient, "testWebClient") + instanceof WebClient); + } + + @Test + public void should_return_WebClientBuilder_if_processed_bean_is_of_type_WebClientBuilder() { + WebClient.Builder webClientBuilder = WebClient.builder(); + + assertTrue( + webClientBeanPostProcessor.postProcessAfterInitialization( + webClientBuilder, "testWebClientBuilder") + instanceof WebClient.Builder); + } + + @Test + public void should_add_exchange_filter_to_WebClient() { + WebClient webClient = WebClient.create(); + Object processedWebClient = + webClientBeanPostProcessor.postProcessAfterInitialization(webClient, "testWebClient"); + + assertTrue(processedWebClient instanceof WebClient); + ((WebClient) processedWebClient) + .mutate() + .filters( + functions -> { + assertEquals( + functions.stream().filter(wctf -> wctf instanceof WebClientTracingFilter).count(), + 1); + }); + } + + @Test + public void should_add_ONE_exchange_filter_to_WebClientBuilder() { + + WebClient.Builder webClientBuilder = WebClient.builder(); + webClientBeanPostProcessor.postProcessAfterInitialization( + webClientBuilder, "testWebClientBuilder"); + webClientBeanPostProcessor.postProcessAfterInitialization( + webClientBuilder, "testWebClientBuilder"); + webClientBeanPostProcessor.postProcessAfterInitialization( + webClientBuilder, "testWebClientBuilder"); + + webClientBuilder.filters( + functions -> { + assertEquals( + functions.stream().filter(wctf -> wctf instanceof WebClientTracingFilter).count(), 1); + }); + } +} diff --git a/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMVCFilterAutoConfigurationTest.java b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMVCFilterAutoConfigurationTest.java new file mode 100644 index 0000000000..1b1d6e53d3 --- /dev/null +++ b/instrumentation-core/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/webmvc/WebMVCFilterAutoConfigurationTest.java @@ -0,0 +1,69 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration; +import io.opentelemetry.instrumentation.springwebmvc.WebMVCTracingFilter; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +/** Spring Boot auto configuration test for {@link WebMVCFilterAutoConfiguration} */ +public class WebMVCFilterAutoConfigurationTest { + private final ApplicationContextRunner contextRunner = + new ApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of( + TracerAutoConfiguration.class, WebMVCFilterAutoConfiguration.class)); + + @Test + public void should_initialize_WebMvcTracingFilter_bean_when_web_is_enabled() { + this.contextRunner + .withPropertyValues("opentelemetry.trace.web.enabled=true") + .run( + (context) -> { + assertNotNull( + "Application Context contains WebMVCTracingFilter bean", + context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class)); + }); + } + + @Test + public void should_NOT_initialize_WebMvcTracingFilter_bean_when_web_is_NOT_enabled() { + this.contextRunner + .withPropertyValues("opentelemetry.trace.web.enabled=false") + .run( + (context) -> { + assertFalse( + "Application Context DOES NOT contain WebMVCTracingFilter bean", + context.containsBean("otelWebMVCTracingFilter")); + }); + } + + @Test + public void should_initialize_WebMvcTracingFilter_bean_when_web_enabled_property_is_MISSING() { + this.contextRunner.run( + (context) -> { + assertNotNull( + "Application Context contains WebMVCTracingFilter bean", + context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class)); + }); + } +}