fix: add ut.

pull/1768/head
fishtailfu 2 weeks ago
parent e2f50b7080
commit a76e84c5f8

@ -46,7 +46,7 @@ public class RegisterEnabledCondition implements Condition {
conditionContext.getEnvironment()
.getProperty("polaris.agent.nacos.discovery.enabled", "false"));
boolean isNacosRegisterEnabled = isNacosDiscoveryEnabled && Boolean.parseBoolean(conditionContext.getEnvironment()
.getProperty("spring.cloud.nacos.discovery.register-enabled", "false"));
.getProperty("spring.cloud.nacos.discovery.register-enabled", "true"));
isRegisterEnabled |= isNacosRegisterEnabled;

@ -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();
}
}

@ -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();
}
}
}

@ -96,6 +96,12 @@ public class NacosContextPropertiesTest {
assertThat(metadata.get(NACOS_CLUSTER_KEY)).isEqualTo(nacosContextProperties.getClusterName());
}
@Test
public void testDiscoveryEnabledProperty() {
// Test that discoveryEnabled is true from spring.cloud.nacos.discovery.enabled
assertThat(nacosContextProperties.isDiscoveryEnabled()).isTrue();
}
@SpringBootApplication
protected static class TestApplication {

@ -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();
}
}

@ -23,7 +23,6 @@ spring:
ip-address: 127.0.0.1
prefer-ip-address: true
nacos:
enabled: true
context-path: /nacos
discovery:
enabled: true

Loading…
Cancel
Save