From d417e9d0e0dde5aeffe56242651e43541cfbf1f2 Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Wed, 17 Dec 2025 17:15:20 +0800 Subject: [PATCH] feat: add new key for java agent nacos discovery. (#1769) --- CHANGELOG.md | 1 + .../discovery/DiscoveryEnabledCondition.java | 6 +- .../extend/nacos/NacosContextProperties.java | 2 +- .../registry/RegisterEnabledCondition.java | 10 +- .../DiscoveryEnabledConditionTest.java | 152 ++++++++++++++ .../NacosContextPropertiesAgentTest.java | 81 ++++++++ .../nacos/NacosContextPropertiesTest.java | 1 + .../RegisterEnabledConditionTest.java | 190 ++++++++++++++++++ .../src/test/resources/application-test.yml | 1 - 9 files changed, 435 insertions(+), 9 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledConditionTest.java create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesAgentTest.java create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/RegisterEnabledConditionTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index fe87055cc..233ed106a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,3 +43,4 @@ - [fix:replace with string inside @ConditionalOnClass.](https://github.com/Tencent/spring-cloud-tencent/pull/1750) - [feat: support TagUtils, ContextToHeaderInterceptor in TSF.](https://github.com/Tencent/spring-cloud-tencent/pull/1754) - [fix: send unit header in tsf gw.](https://github.com/Tencent/spring-cloud-tencent/pull/1758) +- [feat: add new key for java agent nacos discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/1769) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java index ed4af8299..fe89c8b3f 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledCondition.java @@ -42,9 +42,9 @@ public class DiscoveryEnabledCondition implements Condition { isDiscoveryEnabled |= isConsulDiscoveryEnabled; boolean isNacosDiscoveryEnabled = Boolean.parseBoolean( - conditionContext.getEnvironment().getProperty("spring.cloud.nacos.enabled", "false")) - && Boolean.parseBoolean( - conditionContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.enabled", "true")); + conditionContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.enabled", "false")) + || Boolean.parseBoolean( + conditionContext.getEnvironment().getProperty("polaris.agent.nacos.discovery.enabled", "false")); isDiscoveryEnabled |= isNacosDiscoveryEnabled; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java index 7986f6550..01f2e566b 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/nacos/NacosContextProperties.java @@ -32,7 +32,7 @@ import static shade.polaris.com.alibaba.nacos.api.common.Constants.DEFAULT_NAMES @ConfigurationProperties("spring.cloud.nacos") public class NacosContextProperties { - @Value("${spring.cloud.nacos.discovery.enabled:#{'false'}}") + @Value("${polaris.agent.nacos.discovery.enabled:${spring.cloud.nacos.discovery.enabled:#{'false'}}}") private boolean discoveryEnabled; /** diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/RegisterEnabledCondition.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/RegisterEnabledCondition.java index ce5febb58..2da96d036 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/RegisterEnabledCondition.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/RegisterEnabledCondition.java @@ -40,10 +40,12 @@ public class RegisterEnabledCondition implements Condition { isRegisterEnabled |= isConsulRegisterEnabled; - boolean isNacosRegisterEnabled = Boolean - .parseBoolean(conditionContext.getEnvironment() - .getProperty("spring.cloud.nacos.enabled", "false")) - && Boolean.parseBoolean(conditionContext.getEnvironment() + boolean isNacosDiscoveryEnabled = Boolean.parseBoolean( + conditionContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.enabled", "false")) || + Boolean.parseBoolean( + conditionContext.getEnvironment() + .getProperty("polaris.agent.nacos.discovery.enabled", "false")); + boolean isNacosRegisterEnabled = isNacosDiscoveryEnabled && Boolean.parseBoolean(conditionContext.getEnvironment() .getProperty("spring.cloud.nacos.discovery.register-enabled", "true")); isRegisterEnabled |= isNacosRegisterEnabled; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledConditionTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledConditionTest.java new file mode 100644 index 000000000..2f37cdef6 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/DiscoveryEnabledConditionTest.java @@ -0,0 +1,152 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 Tencent. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.discovery; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotatedTypeMetadata; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +/** + * Test for {@link DiscoveryEnabledCondition}. + * + * @author fishtailfu + */ +public class DiscoveryEnabledConditionTest { + + private final DiscoveryEnabledCondition condition = new DiscoveryEnabledCondition(); + + @Test + public void testPolarisDiscoveryEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("true"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testPolarisDiscoveryDisabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isFalse(); + } + + @Test + public void testConsulDiscoveryEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("true"); + when(environment.getProperty("spring.cloud.consul.discovery.enabled", "true")).thenReturn("true"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testNacosDiscoveryEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testPolarisAgentNacosDiscoveryEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testBothNacosPropertiesEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testAllDiscoveryDisabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.enabled", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.discovery.enabled", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isFalse(); + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesAgentTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesAgentTest.java new file mode 100644 index 000000000..5cf79c310 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesAgentTest.java @@ -0,0 +1,81 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 Tencent. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.extend.nacos; + +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for {@link NacosContextProperties} with polaris agent property. + * + * @author fishtailfu + */ +@ExtendWith(SpringExtension.class) +@SpringBootTest( + classes = NacosContextPropertiesAgentTest.TestApplication.class, + properties = { + "spring.cloud.polaris.address=grpc://127.0.0.1:8091", + "spring.cloud.polaris.discovery.enabled=false", + "polaris.agent.nacos.discovery.enabled=true", + "spring.cloud.nacos.discovery.enabled=false", + "spring.cloud.nacos.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.discovery.register-enabled=true", + "spring.cloud.nacos.discovery.group=polaris", + "spring.cloud.nacos.username=nacos", + "spring.cloud.nacos.password=nacos", + "spring.cloud.nacos.discovery.cluster-name=polaris", + "spring.cloud.nacos.context-path=/nacos" + } +) +@ActiveProfiles("agent-test") +public class NacosContextPropertiesAgentTest { + + @Autowired + private NacosContextProperties nacosContextProperties; + + @Test + public void testPolarisAgentNacosDiscoveryEnabled() { + assertThat(nacosContextProperties).isNotNull(); + // Test that polaris.agent.nacos.discovery.enabled takes precedence + assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue(); + assertThat(nacosContextProperties.getServerAddr()).isEqualTo("127.0.0.1:8848"); + assertThat(nacosContextProperties.isRegisterEnabled()).isTrue(); + assertThat(nacosContextProperties.getGroup()).isEqualTo("polaris"); + assertThat(nacosContextProperties.getUsername()).isEqualTo("nacos"); + assertThat(nacosContextProperties.getPassword()).isEqualTo("nacos"); + assertThat(nacosContextProperties.getClusterName()).isEqualTo("polaris"); + assertThat(nacosContextProperties.getContextPath()).isEqualTo("/nacos"); + } + + @SpringBootApplication + protected static class TestApplication { + + static { + PolarisSDKContextManager.innerDestroy(); + } + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java index f4cd2a160..ff33f26af 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/extend/nacos/NacosContextPropertiesTest.java @@ -61,6 +61,7 @@ public class NacosContextPropertiesTest { @Test public void testDefaultInitialization() { + assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue(); assertThat(nacosContextProperties).isNotNull(); assertThat(nacosContextProperties.getServerAddr()).isEqualTo("127.0.0.1:8848"); assertThat(nacosContextProperties.isRegisterEnabled()).isTrue(); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/RegisterEnabledConditionTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/RegisterEnabledConditionTest.java new file mode 100644 index 000000000..a25213d31 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/RegisterEnabledConditionTest.java @@ -0,0 +1,190 @@ +/* + * Tencent is pleased to support the open source community by making spring-cloud-tencent available. + * + * Copyright (C) 2021 Tencent. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.registry; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotatedTypeMetadata; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +/** + * Test for {@link RegisterEnabledCondition}. + * + * @author fishtailfu + */ +public class RegisterEnabledConditionTest { + + private final RegisterEnabledCondition condition = new RegisterEnabledCondition(); + + @Test + public void testPolarisRegisterEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("true"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testPolarisRegisterDisabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isFalse(); + } + + @Test + public void testConsulRegisterEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("true"); + when(environment.getProperty("spring.cloud.consul.discovery.register", "true")).thenReturn("true"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testNacosRegisterEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("true"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testNacosDiscoveryEnabledButRegisterDisabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isFalse(); + } + + @Test + public void testPolarisAgentNacosRegisterEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("true"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testPolarisAgentNacosDiscoveryEnabledButRegisterDisabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isFalse(); + } + + @Test + public void testBothNacosPropertiesEnabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("true"); + when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("true"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isTrue(); + } + + @Test + public void testAllRegisterDisabled() { + ConditionContext context = Mockito.mock(ConditionContext.class); + Environment environment = Mockito.mock(Environment.class); + AnnotatedTypeMetadata metadata = Mockito.mock(AnnotatedTypeMetadata.class); + + when(context.getEnvironment()).thenReturn(environment); + when(environment.getProperty("spring.cloud.polaris.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.consul.discovery.register", "true")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("polaris.agent.nacos.discovery.enabled", "false")).thenReturn("false"); + when(environment.getProperty("spring.cloud.nacos.discovery.register-enabled", "true")).thenReturn("false"); + + boolean result = condition.matches(context, metadata); + assertThat(result).isFalse(); + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml b/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml index 52752ef59..81ab48154 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/resources/application-test.yml @@ -23,7 +23,6 @@ spring: ip-address: 127.0.0.1 prefer-ip-address: true nacos: - enabled: true context-path: /nacos discovery: enabled: true