|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 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.cloud.kubernetes.fabric8.config.reload; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.boot.test.context.SpringBootTest; |
| 21 | +import org.springframework.boot.test.context.TestConfiguration; |
| 22 | +import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties; |
| 23 | +import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy; |
| 24 | +import org.springframework.cloud.kubernetes.commons.config.reload.PollingConfigMapChangeDetector; |
| 25 | +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySource; |
| 26 | +import org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Primary; |
| 29 | +import org.springframework.core.env.AbstractEnvironment; |
| 30 | +import org.springframework.mock.env.MockEnvironment; |
| 31 | + |
| 32 | +import java.time.Duration; |
| 33 | +import java.util.Set; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author wind57 |
| 39 | + */ |
| 40 | +@SpringBootTest(properties = { "spring.main.cloud-platform=kubernetes", "spring.cloud.kubernetes.reload.enabled=true", |
| 41 | + "spring.main.allow-bean-definition-overriding=true" }, |
| 42 | + classes = { ReloadConfigMapTest.TestConfig.class }) |
| 43 | +class ReloadConfigMapTest { |
| 44 | + |
| 45 | + private static boolean [] strategyCalled = new boolean[] { false }; |
| 46 | + |
| 47 | + @Test |
| 48 | + void test() { |
| 49 | + assertThat("1").isEqualTo("1"); |
| 50 | + } |
| 51 | + |
| 52 | + @TestConfiguration |
| 53 | + static class TestConfig { |
| 54 | + |
| 55 | + @Bean |
| 56 | + @Primary |
| 57 | + PollingConfigMapChangeDetector pollingConfigMapChangeDetector(AbstractEnvironment environment, |
| 58 | + ConfigReloadProperties configReloadProperties, ConfigurationUpdateStrategy configurationUpdateStrategy) { |
| 59 | + return new PollingConfigMapChangeDetector(environment, configReloadProperties, configurationUpdateStrategy, |
| 60 | + Fabric8ConfigMapPropertySource.class, null, null); |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + @Primary |
| 65 | + AbstractEnvironment environment() { |
| 66 | + return new MockEnvironment(); |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + @Primary |
| 71 | + ConfigReloadProperties configReloadProperties() { |
| 72 | + return new ConfigReloadProperties(true, true, false, |
| 73 | + ConfigReloadProperties.ReloadStrategy.REFRESH, ConfigReloadProperties.ReloadDetectionMode.POLLING, |
| 74 | + Duration.ofMillis(15000), Set.of("non-default"), false, Duration.ofSeconds(2)); |
| 75 | + } |
| 76 | + |
| 77 | + @Bean |
| 78 | + @Primary |
| 79 | + ConfigurationUpdateStrategy configurationUpdateStrategy() { |
| 80 | + return new ConfigurationUpdateStrategy("to-console", () -> { |
| 81 | + |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @Bean |
| 86 | + @Primary |
| 87 | + Fabric8ConfigMapPropertySourceLocator fabric8ConfigMapPropertySourceLocator() { |
| 88 | + return new Fabric8ConfigMapPropertySourceLocator(); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments