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