test:add junit test to polaris-discovery. (#205)
parent
b97f77a418
commit
6a90de6d1e
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
|
||||||
|
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||||
|
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
|
||||||
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||||
|
import com.tencent.polaris.api.core.ProviderAPI;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link DiscoveryPropertiesAutoConfiguration}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class DiscoveryPropertiesAutoConfigurationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultInitialization() {
|
||||||
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner().withConfiguration(
|
||||||
|
AutoConfigurations.of(PolarisContextAutoConfiguration.class,
|
||||||
|
DiscoveryPropertiesAutoConfiguration.class));
|
||||||
|
applicationContextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(DiscoveryPropertiesAutoConfiguration.class);
|
||||||
|
assertThat(context).hasSingleBean(PolarisDiscoveryProperties.class);
|
||||||
|
assertThat(context).hasSingleBean(ConsulContextProperties.class);
|
||||||
|
assertThat(context).hasSingleBean(ProviderAPI.class);
|
||||||
|
assertThat(context).hasSingleBean(ConsumerAPI.class);
|
||||||
|
assertThat(context).hasSingleBean(PolarisDiscoveryHandler.class);
|
||||||
|
assertThat(context).hasSingleBean(DiscoveryConfigModifier.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInit() {
|
||||||
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner().withConfiguration(
|
||||||
|
AutoConfigurations.of(PolarisContextAutoConfiguration.class,
|
||||||
|
TestConfiguration.class,
|
||||||
|
DiscoveryPropertiesAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.cloud.polaris.discovery.register=false")
|
||||||
|
.withPropertyValues("spring.cloud.consul.discovery.register=false")
|
||||||
|
.withPropertyValues("spring.cloud.consul.discovery.enabled=false");
|
||||||
|
applicationContextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(DiscoveryPropertiesAutoConfiguration.class);
|
||||||
|
DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration = context.getBean(DiscoveryPropertiesAutoConfiguration.class);
|
||||||
|
assertThat(discoveryPropertiesAutoConfiguration.isRegisterEnabled()).isFalse();
|
||||||
|
assertThat(discoveryPropertiesAutoConfiguration.isDiscoveryEnabled()).isFalse();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfiguration {
|
||||||
|
@Bean
|
||||||
|
public PolarisDiscoveryProperties polarisDiscoveryProperties() {
|
||||||
|
PolarisDiscoveryProperties polarisDiscoveryProperties = new PolarisDiscoveryProperties();
|
||||||
|
polarisDiscoveryProperties.setEnabled(false);
|
||||||
|
return polarisDiscoveryProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConsulContextProperties consulContextProperties() {
|
||||||
|
ConsulContextProperties consulContextProperties = new ConsulContextProperties();
|
||||||
|
consulContextProperties.setEnabled(true);
|
||||||
|
return consulContextProperties;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link DiscoveryPropertiesBootstrapAutoConfiguration}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class DiscoveryPropertiesBootstrapAutoConfigurationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultInitialization() {
|
||||||
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner().withConfiguration(
|
||||||
|
AutoConfigurations.of(PolarisContextAutoConfiguration.class,
|
||||||
|
DiscoveryPropertiesBootstrapAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.cloud.polaris.enabled=true");
|
||||||
|
applicationContextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(DiscoveryPropertiesBootstrapAutoConfiguration.class);
|
||||||
|
assertThat(context).hasSingleBean(DiscoveryPropertiesAutoConfiguration.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.PROVIDER_TOKEN;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisDiscoveryProperties}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class PolarisDiscoveryPropertiesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAndSet() {
|
||||||
|
PolarisDiscoveryProperties polarisDiscoveryProperties = new PolarisDiscoveryProperties();
|
||||||
|
|
||||||
|
// HeartbeatEnabled
|
||||||
|
polarisDiscoveryProperties.setHeartbeatEnabled(true);
|
||||||
|
assertThat(polarisDiscoveryProperties.isHeartbeatEnabled()).isTrue();
|
||||||
|
|
||||||
|
// Namespace
|
||||||
|
polarisDiscoveryProperties.setNamespace(NAMESPACE_TEST);
|
||||||
|
assertThat(polarisDiscoveryProperties.getNamespace()).isEqualTo(NAMESPACE_TEST);
|
||||||
|
|
||||||
|
// Weight
|
||||||
|
polarisDiscoveryProperties.setWeight(10);
|
||||||
|
assertThat(polarisDiscoveryProperties.getWeight()).isEqualTo(10);
|
||||||
|
|
||||||
|
// Service
|
||||||
|
polarisDiscoveryProperties.setService(SERVICE_PROVIDER);
|
||||||
|
assertThat(polarisDiscoveryProperties.getService()).isEqualTo(SERVICE_PROVIDER);
|
||||||
|
|
||||||
|
// Enabled
|
||||||
|
polarisDiscoveryProperties.setEnabled(true);
|
||||||
|
assertThat(polarisDiscoveryProperties.isEnabled()).isTrue();
|
||||||
|
|
||||||
|
// RegisterEnabled
|
||||||
|
polarisDiscoveryProperties.setRegisterEnabled(true);
|
||||||
|
assertThat(polarisDiscoveryProperties.isRegisterEnabled()).isTrue();
|
||||||
|
|
||||||
|
// Token
|
||||||
|
polarisDiscoveryProperties.setToken(PROVIDER_TOKEN);
|
||||||
|
assertThat(polarisDiscoveryProperties.getToken()).isEqualTo(PROVIDER_TOKEN);
|
||||||
|
|
||||||
|
// Version
|
||||||
|
polarisDiscoveryProperties.setVersion("1.0.0");
|
||||||
|
assertThat(polarisDiscoveryProperties.getVersion()).isEqualTo("1.0.0");
|
||||||
|
|
||||||
|
// HTTP
|
||||||
|
polarisDiscoveryProperties.setProtocol("HTTP");
|
||||||
|
assertThat(polarisDiscoveryProperties.getProtocol()).isEqualTo("HTTP");
|
||||||
|
|
||||||
|
// Port
|
||||||
|
polarisDiscoveryProperties.setPort(PORT);
|
||||||
|
assertThat(polarisDiscoveryProperties.getPort()).isEqualTo(PORT);
|
||||||
|
|
||||||
|
// HealthCheckUrl
|
||||||
|
polarisDiscoveryProperties.setHealthCheckUrl("/health");
|
||||||
|
assertThat(polarisDiscoveryProperties.getHealthCheckUrl()).isEqualTo("/health");
|
||||||
|
|
||||||
|
// ServiceListRefreshInterval
|
||||||
|
polarisDiscoveryProperties.setServiceListRefreshInterval(1000L);
|
||||||
|
assertThat(polarisDiscoveryProperties.getServiceListRefreshInterval()).isEqualTo(1000L);
|
||||||
|
|
||||||
|
assertThat(polarisDiscoveryProperties.toString())
|
||||||
|
.isEqualTo("PolarisDiscoveryProperties{"
|
||||||
|
+ "namespace='Test'"
|
||||||
|
+ ", service='java_provider_test'"
|
||||||
|
+ ", token='19485a7674294e3c88dba293373c1534'"
|
||||||
|
+ ", weight=10, version='1.0.0'"
|
||||||
|
+ ", protocol='HTTP'"
|
||||||
|
+ ", port=9091"
|
||||||
|
+ ", enabled=true"
|
||||||
|
+ ", registerEnabled=true"
|
||||||
|
+ ", heartbeatEnabled=true"
|
||||||
|
+ ", healthCheckUrl='/health'"
|
||||||
|
+ ", serviceListRefreshInterval=1000}");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.refresh;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import com.tencent.polaris.api.pojo.DefaultInstance;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceEventKey;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceInfo;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.client.pojo.ServiceInstancesByProto;
|
||||||
|
import com.tencent.polaris.client.pojo.ServicesByProto;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
|
||||||
|
import static com.tencent.polaris.test.common.Consts.HOST;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisServiceStatusChangeListener}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class PolarisServiceStatusChangeListenerTest {
|
||||||
|
|
||||||
|
private ApplicationEventPublisher publisher;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
publisher = mock(ApplicationEventPublisher.class);
|
||||||
|
doNothing().when(publisher).publishEvent(any(ApplicationEvent.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnResourceUpdated() {
|
||||||
|
PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener();
|
||||||
|
polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher);
|
||||||
|
|
||||||
|
// Service update event
|
||||||
|
ServiceEventKey serviceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.SERVICE);
|
||||||
|
ServiceInfo serviceInfo = new ServiceInfo();
|
||||||
|
serviceInfo.setNamespace(NAMESPACE_TEST);
|
||||||
|
serviceInfo.setService(SERVICE_PROVIDER);
|
||||||
|
// Need update
|
||||||
|
ServicesByProto oldServices = new ServicesByProto(Collections.emptyList());
|
||||||
|
ServicesByProto newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
|
||||||
|
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
|
||||||
|
verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
|
||||||
|
// No need update
|
||||||
|
oldServices = new ServicesByProto(Collections.singletonList(serviceInfo));
|
||||||
|
newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
|
||||||
|
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
|
||||||
|
verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
|
||||||
|
|
||||||
|
|
||||||
|
// Instance update event
|
||||||
|
ServiceEventKey instanceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.INSTANCE);
|
||||||
|
DefaultInstance instance = new DefaultInstance();
|
||||||
|
instance.setNamespace(NAMESPACE_TEST);
|
||||||
|
instance.setService(SERVICE_PROVIDER);
|
||||||
|
instance.setHost(HOST);
|
||||||
|
instance.setPort(PORT);
|
||||||
|
try {
|
||||||
|
Field instances = ServiceInstancesByProto.class.getDeclaredField("instances");
|
||||||
|
instances.setAccessible(true);
|
||||||
|
|
||||||
|
// Need update
|
||||||
|
ServiceInstancesByProto oldInstances = new ServiceInstancesByProto();
|
||||||
|
instances.set(oldInstances, Collections.emptyList());
|
||||||
|
ServiceInstancesByProto newInstances = new ServiceInstancesByProto();
|
||||||
|
instances.set(newInstances, Collections.singletonList(instance));
|
||||||
|
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
|
||||||
|
verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
|
||||||
|
|
||||||
|
// No need update
|
||||||
|
oldInstances = new ServiceInstancesByProto();
|
||||||
|
instances.set(oldInstances, Collections.singletonList(instance));
|
||||||
|
newInstances = new ServiceInstancesByProto();
|
||||||
|
instances.set(newInstances, Collections.singletonList(instance));
|
||||||
|
polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
|
||||||
|
verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
|
||||||
|
}
|
||||||
|
catch (NoSuchFieldException | IllegalAccessException e) {
|
||||||
|
Assertions.fail("Exception encountered.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.consul;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration;
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
|
||||||
|
import com.tencent.polaris.client.api.SDKContext;
|
||||||
|
import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import static com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY;
|
||||||
|
import static com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey.IP_ADDRESS_KEY;
|
||||||
|
import static com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey.PREFER_IP_ADDRESS_KEY;
|
||||||
|
import static com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey.SERVICE_NAME_KEY;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.HOST;
|
||||||
|
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link ConsulContextProperties}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class ConsulContextPropertiesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testModify() {
|
||||||
|
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner().withConfiguration(
|
||||||
|
AutoConfigurations.of(PolarisContextAutoConfiguration.class,
|
||||||
|
ConsulContextPropertiesTest.TestConfiguration.class,
|
||||||
|
DiscoveryPropertiesAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.cloud.consul.discovery.register=true")
|
||||||
|
.withPropertyValues("spring.cloud.consul.discovery.enabled=true")
|
||||||
|
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||||
|
.withPropertyValues("spring.cloud.consul.discovery.instance-id=ins-test")
|
||||||
|
.withPropertyValues("spring.cloud.consul.discovery.prefer-ip-address=true")
|
||||||
|
.withPropertyValues("spring.cloud.consul.discovery.ip-address=" + HOST);
|
||||||
|
applicationContextRunner.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(SDKContext.class);
|
||||||
|
SDKContext sdkContext = context.getBean(SDKContext.class);
|
||||||
|
com.tencent.polaris.api.config.Configuration configuration = sdkContext.getConfig();
|
||||||
|
List<ServerConnectorConfigImpl> serverConnectorConfigs = configuration.getGlobal().getServerConnectors();
|
||||||
|
Map<String, String> metadata = null;
|
||||||
|
for (ServerConnectorConfigImpl serverConnectorConfig : serverConnectorConfigs) {
|
||||||
|
if (serverConnectorConfig.getId().equals("consul")) {
|
||||||
|
metadata = serverConnectorConfig.getMetadata();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertThat(metadata).isNotNull();
|
||||||
|
assertThat(metadata.get(SERVICE_NAME_KEY)).isEqualTo(SERVICE_PROVIDER);
|
||||||
|
assertThat(metadata.get(INSTANCE_ID_KEY)).isEqualTo("ins-test");
|
||||||
|
assertThat(metadata.get(PREFER_IP_ADDRESS_KEY)).isEqualTo("true");
|
||||||
|
assertThat(metadata.get(IP_ADDRESS_KEY)).isEqualTo(HOST);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConsulContextProperties consulContextProperties() {
|
||||||
|
ConsulContextProperties consulContextProperties = new ConsulContextProperties();
|
||||||
|
consulContextProperties.setEnabled(true);
|
||||||
|
return consulContextProperties;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. 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.util;
|
||||||
|
|
||||||
|
import org.assertj.core.util.Maps;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link OkHttpUtil}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = OkHttpUtilTest.TestApplication.class)
|
||||||
|
public class OkHttpUtilTest {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGet() {
|
||||||
|
assertThat(OkHttpUtil.get("http://localhost:" + port + "/test", Maps.newHashMap("key", "value"))).isTrue();
|
||||||
|
assertThat(OkHttpUtil.get("http://localhost:" + port + "/error", Maps.newHashMap("key", "value"))).isFalse();
|
||||||
|
assertThat(OkHttpUtil.get("http://localhost:55555/error", Maps.newHashMap("key", "value"))).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@RestController
|
||||||
|
static class TestApplication {
|
||||||
|
@GetMapping("/test")
|
||||||
|
public String test() {
|
||||||
|
return "test";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue