|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 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.metrics.export.stackdriver; |
| 18 | + |
| 19 | +import io.micrometer.core.instrument.Clock; |
| 20 | +import io.micrometer.stackdriver.StackdriverConfig; |
| 21 | +import io.micrometer.stackdriver.StackdriverMeterRegistry; |
| 22 | +import org.junit.jupiter.api.Test; |
| 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.context.annotation.Import; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link StackdriverMetricsExportAutoConfiguration}. |
| 33 | + * |
| 34 | + * @author Johannes Graf |
| 35 | + */ |
| 36 | +class StackdriverMetricsExportAutoConfigurationTest { |
| 37 | + |
| 38 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 39 | + .withConfiguration(AutoConfigurations.of(StackdriverMetricsExportAutoConfiguration.class)); |
| 40 | + |
| 41 | + @Test |
| 42 | + void backsOffWithoutAClock() { |
| 43 | + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(StackdriverMeterRegistry.class)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void failsWithoutAnProjectId() { |
| 48 | + this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
| 49 | + .run((context) -> assertThat(context).hasFailed()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void autoConfiguresConfigAndMeterRegistry() { |
| 54 | + this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
| 55 | + .withPropertyValues("management.metrics.export.stackdriver.project-id=qwert") |
| 56 | + .run((context) -> assertThat(context).hasSingleBean(StackdriverMeterRegistry.class) |
| 57 | + .hasSingleBean(StackdriverConfig.class)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void autoConfigurationCanBeDisabled() { |
| 62 | + this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
| 63 | + .withPropertyValues("management.metrics.export.stackdriver.enabled=false") |
| 64 | + .run((context) -> assertThat(context).doesNotHaveBean(StackdriverMeterRegistry.class) |
| 65 | + .doesNotHaveBean(StackdriverConfig.class)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void allowsCustomConfigToBeUsed() { |
| 70 | + this.contextRunner.withUserConfiguration(CustomConfigConfiguration.class).run((context) -> assertThat(context) |
| 71 | + .hasSingleBean(StackdriverMeterRegistry.class).hasSingleBean(StackdriverConfig.class).hasBean("customConfig")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void allowsCustomRegistryToBeUsed() { |
| 76 | + this.contextRunner.withUserConfiguration(CustomRegistryConfiguration.class) |
| 77 | + .withPropertyValues("management.metrics.export.stackdriver.project-id=qwert") |
| 78 | + .run((context) -> assertThat(context).hasSingleBean(StackdriverMeterRegistry.class) |
| 79 | + .hasBean("customRegistry").hasSingleBean(StackdriverConfig.class)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void stopsMeterRegistryWhenContextIsClosed() { |
| 84 | + this.contextRunner.withUserConfiguration(BaseConfiguration.class) |
| 85 | + .withPropertyValues("management.metrics.export.stackdriver.project-id=qwert").run((context) -> { |
| 86 | + StackdriverMeterRegistry registry = context.getBean(StackdriverMeterRegistry.class); |
| 87 | + assertThat(registry.isClosed()).isFalse(); |
| 88 | + context.close(); |
| 89 | + assertThat(registry.isClosed()).isTrue(); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + @Configuration(proxyBeanMethods = false) |
| 94 | + static class BaseConfiguration { |
| 95 | + |
| 96 | + @Bean |
| 97 | + Clock clock() { |
| 98 | + return Clock.SYSTEM; |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Configuration(proxyBeanMethods = false) |
| 104 | + @Import(BaseConfiguration.class) |
| 105 | + static class CustomConfigConfiguration { |
| 106 | + |
| 107 | + @Bean |
| 108 | + StackdriverConfig customConfig() { |
| 109 | + return (key) -> { |
| 110 | + if ("stackdriver.projectId".equals(key)) { |
| 111 | + return "qwert"; |
| 112 | + } |
| 113 | + return null; |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + @Configuration(proxyBeanMethods = false) |
| 120 | + @Import(BaseConfiguration.class) |
| 121 | + static class CustomRegistryConfiguration { |
| 122 | + |
| 123 | + @Bean |
| 124 | + StackdriverMeterRegistry customRegistry(StackdriverConfig config, Clock clock) { |
| 125 | + return new StackdriverMeterRegistry(config, clock); |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | +} |
0 commit comments