From d6f75709db4cbcc0125e60ea8fba4486d00c49a5 Mon Sep 17 00:00:00 2001 From: wind57 Date: Sat, 22 Feb 2025 22:37:54 +0200 Subject: [PATCH] fix Signed-off-by: wind57 --- ...urationWatcherConfigurationProperties.java | 7 +- .../RefreshTriggerAutoConfiguration.java | 4 +- .../RefreshTriggerAutoConfigurationTests.java | 139 ++++++++++++++++++ 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfigurationTests.java diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java index 47e9474d5b..9fb36744cb 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/ConfigurationWatcherConfigurationProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,11 @@ public class ConfigurationWatcherConfigurationProperties { */ public static final String KAFKA = "bus-kafka"; + /** + * not AMQP or KAFKA profile name. + */ + static final String NOT_AMQP_NOT_KAFKA = "!" + AMQP + " & !" + KAFKA; + /** * label to enable refresh/restart when using configmaps. */ diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfiguration.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfiguration.java index 40c6477f50..34904b5d61 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfiguration.java +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/main/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2024 the original author or authors. + * Copyright 2013-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.AMQP; import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.KAFKA; +import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.NOT_AMQP_NOT_KAFKA; /** * @author wind57 @@ -47,6 +48,7 @@ BusRefreshTrigger busRefreshTrigger(ApplicationEventPublisher applicationEventPu @Bean @ConditionalOnMissingBean + @Profile({ NOT_AMQP_NOT_KAFKA }) HttpRefreshTrigger httpRefreshTrigger(KubernetesInformerReactiveDiscoveryClient client, ConfigurationWatcherConfigurationProperties properties, WebClient webClient) { return new HttpRefreshTrigger(client, properties, webClient); diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfigurationTests.java b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfigurationTests.java new file mode 100644 index 0000000000..3d7d8abf7f --- /dev/null +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/src/test/java/org/springframework/cloud/kubernetes/configuration/watcher/RefreshTriggerAutoConfigurationTests.java @@ -0,0 +1,139 @@ +/* + * Copyright 2013-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.configuration.watcher; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.bus.BusProperties; +import org.springframework.cloud.kubernetes.client.discovery.reactive.KubernetesInformerReactiveDiscoveryClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.springframework.web.reactive.function.client.WebClient; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.AMQP; +import static org.springframework.cloud.kubernetes.configuration.watcher.ConfigurationWatcherConfigurationProperties.KAFKA; + +/** + * @author wind57 + */ +class RefreshTriggerAutoConfigurationTests { + + private ApplicationContextRunner applicationContextRunner; + + @Test + void amqpOnly() { + setup(AMQP); + applicationContextRunner.run(context -> { + assertThat(context).hasSingleBean(BusRefreshTrigger.class); + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); + }); + } + + @Test + void kafkaOnly() { + setup(KAFKA); + applicationContextRunner.run(context -> { + assertThat(context).hasSingleBean(BusRefreshTrigger.class); + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); + }); + } + + @Test + void kafkaAndAmqp() { + setup(KAFKA + " , " + AMQP); + applicationContextRunner.run(context -> { + assertThat(context).hasSingleBean(BusRefreshTrigger.class); + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); + }); + } + + @Test + void notAmqp() { + setup("not-" + AMQP); + applicationContextRunner.run(context -> { + assertThat(context).doesNotHaveBean(BusRefreshTrigger.class); + assertThat(context).hasSingleBean(HttpRefreshTrigger.class); + }); + } + + @Test + void notKafka() { + setup("not-" + KAFKA); + applicationContextRunner.run(context -> { + assertThat(context).doesNotHaveBean(BusRefreshTrigger.class); + assertThat(context).hasSingleBean(HttpRefreshTrigger.class); + }); + } + + @Test + void amqpNotKafka() { + setup(AMQP + "," + "not-" + KAFKA); + applicationContextRunner.run(context -> { + assertThat(context).hasSingleBean(BusRefreshTrigger.class); + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); + }); + } + + @Test + void kafkaNotAmqp() { + setup(KAFKA + "," + "not-" + AMQP); + applicationContextRunner.run(context -> { + assertThat(context).hasSingleBean(BusRefreshTrigger.class); + assertThat(context).doesNotHaveBean(HttpRefreshTrigger.class); + }); + } + + private void setup(String activeProfiles) { + applicationContextRunner = new ApplicationContextRunner() + .withUserConfiguration(TestConfig.class, RefreshTriggerAutoConfiguration.class) + .withPropertyValues("spring.main.cloud-platform=kubernetes", "spring.profiles.active=" + activeProfiles); + } + + @TestConfiguration + static class TestConfig { + + @Bean + @Primary + BusProperties busProperties() { + return new BusProperties(); + } + + @Bean + @Primary + KubernetesInformerReactiveDiscoveryClient client() { + return Mockito.mock(KubernetesInformerReactiveDiscoveryClient.class); + } + + @Bean + @Primary + ConfigurationWatcherConfigurationProperties configurationWatcherConfigurationProperties() { + return new ConfigurationWatcherConfigurationProperties(); + } + + @Primary + @Bean + WebClient webClient() { + return Mockito.mock(WebClient.class); + } + + } + +}