|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.observation.graphql; |
| 18 | + |
| 19 | +import io.micrometer.observation.ObservationRegistry; |
| 20 | +import io.micrometer.observation.tck.TestObservationRegistry; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 24 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.graphql.observation.GraphQlObservationInstrumentation; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link GraphQlObservationAutoConfiguration}. |
| 33 | + * |
| 34 | + * @author Brian Clozel |
| 35 | + */ |
| 36 | +class GraphQlObservationAutoConfigurationTests { |
| 37 | + |
| 38 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 39 | + .withBean(TestObservationRegistry.class, TestObservationRegistry::create) |
| 40 | + .withConfiguration(AutoConfigurations.of(GraphQlObservationAutoConfiguration.class)); |
| 41 | + |
| 42 | + @Test |
| 43 | + void backsOffWhenObservationRegistryIsMissing() { |
| 44 | + new ApplicationContextRunner() |
| 45 | + .withConfiguration(AutoConfigurations.of(GraphQlObservationAutoConfiguration.class)) |
| 46 | + .run((context) -> assertThat(context).doesNotHaveBean(GraphQlObservationInstrumentation.class)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void definesInstrumentationWhenObservationRegistryIsPresent() { |
| 51 | + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(GraphQlObservationInstrumentation.class)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void instrumentationBacksOffIfAlreadyPresent() { |
| 56 | + this.contextRunner.withUserConfiguration(InstrumentationConfiguration.class) |
| 57 | + .run((context) -> assertThat(context).hasSingleBean(GraphQlObservationInstrumentation.class) |
| 58 | + .hasBean("customInstrumentation")); |
| 59 | + } |
| 60 | + |
| 61 | + @Configuration(proxyBeanMethods = false) |
| 62 | + static class InstrumentationConfiguration { |
| 63 | + |
| 64 | + @Bean |
| 65 | + GraphQlObservationInstrumentation customInstrumentation(ObservationRegistry registry) { |
| 66 | + return new GraphQlObservationInstrumentation(registry); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments