|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 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.configuration.watcher; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.mockito.Mockito; |
| 21 | + |
| 22 | +import org.springframework.boot.test.context.TestConfiguration; |
| 23 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 24 | +import org.springframework.cloud.bus.BusProperties; |
| 25 | +import org.springframework.cloud.kubernetes.client.discovery.reactive.KubernetesInformerReactiveDiscoveryClient; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Primary; |
| 28 | +import org.springframework.web.reactive.function.client.WebClient; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.AMQP; |
| 32 | +import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.KAFKA; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author wind57 |
| 36 | + */ |
| 37 | +class RefreshTriggerAutoConfigurationTests { |
| 38 | + |
| 39 | + private ApplicationContextRunner applicationContextRunner; |
| 40 | + |
| 41 | + @Test |
| 42 | + void amqpOnly() { |
| 43 | + setup(AMQP); |
| 44 | + applicationContextRunner.run(context -> { |
| 45 | + assertThat(context).hasSingleBean(BusRefreshTrigger.class); |
| 46 | + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void kafkaOnly() { |
| 52 | + setup(KAFKA); |
| 53 | + applicationContextRunner.run(context -> { |
| 54 | + assertThat(context).hasSingleBean(BusRefreshTrigger.class); |
| 55 | + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void kafkaAndAmqp() { |
| 61 | + setup(KAFKA + " , " + AMQP); |
| 62 | + applicationContextRunner.run(context -> { |
| 63 | + assertThat(context).hasSingleBean(BusRefreshTrigger.class); |
| 64 | + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void notAmqp() { |
| 70 | + setup("not-" + AMQP); |
| 71 | + applicationContextRunner.run(context -> { |
| 72 | + assertThat(context).doesNotHaveBean(BusRefreshTrigger.class); |
| 73 | + assertThat(context).hasSingleBean(HttpRefreshTrigger.class); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void notKafka() { |
| 79 | + setup("not-" + KAFKA); |
| 80 | + applicationContextRunner.run(context -> { |
| 81 | + assertThat(context).doesNotHaveBean(BusRefreshTrigger.class); |
| 82 | + assertThat(context).hasSingleBean(HttpRefreshTrigger.class); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void amqpNotKafka() { |
| 88 | + setup(AMQP + "," + "not-" + KAFKA); |
| 89 | + applicationContextRunner.run(context -> { |
| 90 | + assertThat(context).hasSingleBean(BusRefreshTrigger.class); |
| 91 | + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void kafkaNotAmqp() { |
| 97 | + setup(KAFKA + "," + "not-" + AMQP); |
| 98 | + applicationContextRunner.run(context -> { |
| 99 | + assertThat(context).hasSingleBean(BusRefreshTrigger.class); |
| 100 | + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + private void setup(String activeProfiles) { |
| 105 | + applicationContextRunner = new ApplicationContextRunner() |
| 106 | + .withUserConfiguration(TestConfig.class, RefreshTriggerAutoConfiguration.class) |
| 107 | + .withPropertyValues("spring.main.cloud-platform=kubernetes", "spring.profiles.active=" + activeProfiles); |
| 108 | + } |
| 109 | + |
| 110 | + @TestConfiguration |
| 111 | + static class TestConfig { |
| 112 | + |
| 113 | + @Bean |
| 114 | + @Primary |
| 115 | + BusProperties busProperties() { |
| 116 | + return new BusProperties(); |
| 117 | + } |
| 118 | + |
| 119 | + @Bean |
| 120 | + @Primary |
| 121 | + KubernetesInformerReactiveDiscoveryClient client() { |
| 122 | + return Mockito.mock(KubernetesInformerReactiveDiscoveryClient.class); |
| 123 | + } |
| 124 | + |
| 125 | + @Bean |
| 126 | + @Primary |
| 127 | + ConfigurationWatcherConfigurationProperties configurationWatcherConfigurationProperties() { |
| 128 | + return new ConfigurationWatcherConfigurationProperties(); |
| 129 | + } |
| 130 | + |
| 131 | + @Primary |
| 132 | + @Bean |
| 133 | + WebClient webClient() { |
| 134 | + return Mockito.mock(WebClient.class); |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments