fix:fix PolarisRegistration cannot get metadata bug. (#218)
parent
58a82731fe
commit
f0b499a1c1
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* 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.registry;
|
||||
|
||||
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.nullable;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisAutoServiceRegistration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisAutoServiceRegistrationTest {
|
||||
|
||||
@Mock
|
||||
private ServiceRegistry<Registration> serviceRegistry;
|
||||
|
||||
@Mock
|
||||
private AutoServiceRegistrationProperties autoServiceRegistrationProperties;
|
||||
|
||||
@Mock
|
||||
private PolarisDiscoveryProperties polarisDiscoveryProperties;
|
||||
|
||||
@Mock
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Mock
|
||||
private Environment environment;
|
||||
|
||||
@Mock
|
||||
private PolarisRegistration registration;
|
||||
|
||||
private PolarisAutoServiceRegistration polarisAutoServiceRegistration;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
doReturn(polarisDiscoveryProperties).when(registration).getPolarisProperties();
|
||||
|
||||
doNothing().when(serviceRegistry).register(nullable(Registration.class));
|
||||
|
||||
polarisAutoServiceRegistration =
|
||||
new PolarisAutoServiceRegistration(serviceRegistry, autoServiceRegistrationProperties, registration);
|
||||
|
||||
doReturn(environment).when(applicationContext).getEnvironment();
|
||||
polarisAutoServiceRegistration.setApplicationContext(applicationContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegister() {
|
||||
doReturn(false).when(registration).isRegisterEnabled();
|
||||
try {
|
||||
polarisAutoServiceRegistration.register();
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
doReturn(true).when(registration).isRegisterEnabled();
|
||||
doReturn(-1).when(registration).getPort();
|
||||
try {
|
||||
polarisAutoServiceRegistration.register();
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
doReturn(PORT).when(registration).getPort();
|
||||
try {
|
||||
polarisAutoServiceRegistration.register();
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetManagementRegistration() {
|
||||
assertThat(polarisAutoServiceRegistration.getManagementRegistration()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterManagement() {
|
||||
doReturn(false).when(registration).isRegisterEnabled();
|
||||
try {
|
||||
polarisAutoServiceRegistration.registerManagement();
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
doReturn(true).when(registration).isRegisterEnabled();
|
||||
try {
|
||||
polarisAutoServiceRegistration.registerManagement();
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAppName() {
|
||||
doReturn("application").when(environment).getProperty(anyString(), anyString());
|
||||
assertThat(polarisAutoServiceRegistration.getAppName()).isEqualTo("application");
|
||||
|
||||
doReturn(SERVICE_PROVIDER).when(polarisDiscoveryProperties).getService();
|
||||
assertThat(polarisAutoServiceRegistration.getAppName()).isEqualTo(SERVICE_PROVIDER);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* 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.registry;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration;
|
||||
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
||||
import com.tencent.polaris.api.config.Configuration;
|
||||
import com.tencent.polaris.api.config.global.APIConfig;
|
||||
import com.tencent.polaris.api.config.global.GlobalConfig;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.HOST;
|
||||
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisRegistration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PolarisRegistrationTest {
|
||||
|
||||
private PolarisRegistration polarisRegistration;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// mock DiscoveryPropertiesAutoConfiguration
|
||||
DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration =
|
||||
mock(DiscoveryPropertiesAutoConfiguration.class);
|
||||
doReturn(true).when(discoveryPropertiesAutoConfiguration).isRegisterEnabled();
|
||||
|
||||
// mock PolarisDiscoveryProperties
|
||||
PolarisDiscoveryProperties polarisDiscoveryProperties = mock(PolarisDiscoveryProperties.class);
|
||||
doReturn(SERVICE_PROVIDER).when(polarisDiscoveryProperties).getService();
|
||||
doReturn(PORT).when(polarisDiscoveryProperties).getPort();
|
||||
doReturn("http").when(polarisDiscoveryProperties).getProtocol();
|
||||
|
||||
// mock SDKContext
|
||||
APIConfig apiConfig = mock(APIConfig.class);
|
||||
doReturn(HOST).when(apiConfig).getBindIP();
|
||||
GlobalConfig globalConfig = mock(GlobalConfig.class);
|
||||
doReturn(apiConfig).when(globalConfig).getAPI();
|
||||
Configuration configuration = mock(Configuration.class);
|
||||
doReturn(globalConfig).when(configuration).getGlobal();
|
||||
SDKContext polarisContext = mock(SDKContext.class);
|
||||
doReturn(configuration).when(polarisContext).getConfig();
|
||||
|
||||
// mock StaticMetadataManager
|
||||
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
|
||||
doReturn(Collections.singletonMap("key1", "value1")).when(staticMetadataManager).getMergedStaticMetadata();
|
||||
doReturn(Collections.singletonMap("key2", "value2")).when(staticMetadataManager).getLocationMetadata();
|
||||
|
||||
polarisRegistration = new PolarisRegistration(
|
||||
discoveryPropertiesAutoConfiguration, polarisDiscoveryProperties, polarisContext, staticMetadataManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetServiceId() {
|
||||
assertThat(polarisRegistration.getServiceId()).isEqualTo(SERVICE_PROVIDER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetHost() {
|
||||
assertThat(polarisRegistration.getHost()).isEqualTo(HOST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPort() {
|
||||
assertThat(polarisRegistration.getPort()).isEqualTo(PORT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSecure() {
|
||||
assertThat(polarisRegistration.isSecure()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUri() {
|
||||
assertThat(polarisRegistration.getUri().toString()).isEqualTo("http://" + HOST + ":" + PORT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMetadata() {
|
||||
Map<String, String> metadata = polarisRegistration.getMetadata();
|
||||
assertThat(metadata).isNotNull();
|
||||
assertThat(metadata).isNotEmpty();
|
||||
assertThat(metadata.size()).isEqualTo(2);
|
||||
assertThat(metadata.get("key1")).isEqualTo("value1");
|
||||
assertThat(metadata.get("key2")).isEqualTo("value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPolarisProperties() {
|
||||
assertThat(polarisRegistration.getPolarisProperties()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsRegisterEnabled() {
|
||||
assertThat(polarisRegistration.isRegisterEnabled()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
System.out.println(polarisRegistration);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue