mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
polish #761 update pkg name & maven coordinate for Greenwich
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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
|
||||
*
|
||||
* http://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 com.alibaba.alicloud.ans.ribbon;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.alibaba.alicloud.ans.AnsAutoConfiguration;
|
||||
import com.alibaba.alicloud.ans.AnsDiscoveryClientAutoConfiguration;
|
||||
|
||||
import com.netflix.client.config.DefaultClientConfigImpl;
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AnsRibbonClientConfigurationTests.TestConfig.class, properties = {
|
||||
"spring.application.name=myTestService1",
|
||||
"spring.cloud.alicloud.ans.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.ans.server-port=8080",
|
||||
"spring.cloud.alicloud.ans.endpoint=test-endpoint" }, webEnvironment = RANDOM_PORT)
|
||||
public class AnsRibbonClientConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private AnsServerList serverList;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertThat(serverList.getDom()).isEqualTo("myapp");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class AnsRibbonTestConfiguration {
|
||||
|
||||
@Bean
|
||||
IClientConfig iClientConfig() {
|
||||
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
|
||||
config.setClientName("myapp");
|
||||
return config;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
||||
AnsDiscoveryClientAutoConfiguration.class, AnsAutoConfiguration.class,
|
||||
AnsRibbonTestConfiguration.class, RibbonAnsAutoConfiguration.class,
|
||||
AnsRibbonClientConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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
|
||||
*
|
||||
* http://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 com.alibaba.alicloud.ans.ribbon;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import com.alibaba.alicloud.ans.test.AnsMockTest;
|
||||
import com.alibaba.ans.core.NamingService;
|
||||
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ NamingService.class, AnsServer.class })
|
||||
public class AnsServerListTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testEmptyInstancesReturnsEmptyList() throws Exception {
|
||||
|
||||
PowerMockito.mockStatic(NamingService.class);
|
||||
when(NamingService.getHosts(anyString())).thenReturn(Collections.EMPTY_LIST);
|
||||
|
||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||
when(clientConfig.getClientName()).thenReturn("test-service");
|
||||
AnsServerList serverList = new AnsServerList("test-service");
|
||||
serverList.initWithNiwsConfig(clientConfig);
|
||||
List<AnsServer> servers = serverList.getInitialListOfServers();
|
||||
Assertions.assertThat(servers).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetServers() throws Exception {
|
||||
|
||||
ArrayList<Host> hosts = new ArrayList<>();
|
||||
hosts.add(AnsMockTest.hostInstance("test-service", true, Collections.emptyMap()));
|
||||
|
||||
PowerMockito.mockStatic(NamingService.class);
|
||||
when(NamingService.getHosts(anyString())).thenReturn(hosts);
|
||||
|
||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||
when(clientConfig.getClientName()).thenReturn("test-service");
|
||||
AnsServerList serverList = new AnsServerList("test-service");
|
||||
serverList.initWithNiwsConfig(clientConfig);
|
||||
List<AnsServer> servers = serverList.getInitialListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
|
||||
servers = serverList.getUpdatedListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetServersWithInstanceStatus() throws Exception {
|
||||
ArrayList<Host> hosts = new ArrayList<>();
|
||||
|
||||
HashMap<String, String> map1 = new HashMap<>();
|
||||
map1.put("instanceNum", "1");
|
||||
HashMap<String, String> map2 = new HashMap<>();
|
||||
map2.put("instanceNum", "2");
|
||||
hosts.add(AnsMockTest.hostInstance("test-service", false, map1));
|
||||
hosts.add(AnsMockTest.hostInstance("test-service", true, map2));
|
||||
|
||||
PowerMockito.mockStatic(NamingService.class);
|
||||
when(NamingService.getHosts(eq("test-service"))).thenReturn(
|
||||
hosts.stream().filter(Host::isValid).collect(Collectors.toList()));
|
||||
|
||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||
when(clientConfig.getClientName()).thenReturn("test-service");
|
||||
AnsServerList serverList = new AnsServerList("test-service");
|
||||
serverList.initWithNiwsConfig(clientConfig);
|
||||
List<AnsServer> servers = serverList.getInitialListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
|
||||
AnsServer ansServer = servers.get(0);
|
||||
Host host = ansServer.getHealthService();
|
||||
|
||||
assertThat(ansServer.getMetaInfo().getInstanceId()).isEqualTo(
|
||||
host.getIp() + ":" + host.getHostname() + ":" + host.getPort());
|
||||
assertThat(ansServer.getHealthService().isValid()).isEqualTo(true);
|
||||
assertThat(ansServer.getHealthService().getHostname()).isEqualTo("test-service");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateServers() throws Exception {
|
||||
ArrayList<Host> hosts = new ArrayList<>();
|
||||
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("instanceNum", "1");
|
||||
hosts.add(AnsMockTest.hostInstance("test-service", true, map));
|
||||
|
||||
PowerMockito.mockStatic(NamingService.class);
|
||||
when(NamingService.getHosts(eq("test-service"))).thenReturn(
|
||||
hosts.stream().filter(Host::isValid).collect(Collectors.toList()));
|
||||
|
||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||
when(clientConfig.getClientName()).thenReturn("test-service");
|
||||
AnsServerList serverList = new AnsServerList("test-service");
|
||||
serverList.initWithNiwsConfig(clientConfig);
|
||||
|
||||
List<AnsServer> servers = serverList.getUpdatedListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
|
||||
assertThat(servers.get(0).getHealthService().isValid()).isEqualTo(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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
|
||||
*
|
||||
* http://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 com.alibaba.alicloud.ans.ribbon;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
|
||||
/**
|
||||
* @author xiaolongzuo
|
||||
*/
|
||||
public class AnsServiceListTests {
|
||||
|
||||
static final String IP_ADDR = "10.0.0.2";
|
||||
|
||||
static final int PORT = 8080;
|
||||
|
||||
@Test
|
||||
public void testAnsServer() {
|
||||
AnsServerList serverList = getAnsServerList();
|
||||
List<AnsServer> servers = serverList.getInitialListOfServers();
|
||||
assertNotNull("servers was null", servers);
|
||||
assertEquals("servers was not size 1", 1, servers.size());
|
||||
Server des = assertAnsServer(servers);
|
||||
assertEquals("hostPort was wrong", IP_ADDR + ":" + PORT, des.getHostPort());
|
||||
}
|
||||
|
||||
protected Server assertAnsServer(List<AnsServer> servers) {
|
||||
Server actualServer = servers.get(0);
|
||||
assertTrue("server was not a DomainExtractingServer",
|
||||
actualServer instanceof AnsServer);
|
||||
AnsServer des = AnsServer.class.cast(actualServer);
|
||||
assertNotNull("host is null", des.getHealthService());
|
||||
assertEquals("unit was wrong", "DEFAULT", des.getHealthService().getUnit());
|
||||
return des;
|
||||
}
|
||||
|
||||
protected AnsServerList getAnsServerList() {
|
||||
Host host = mock(Host.class);
|
||||
given(host.getIp()).willReturn(IP_ADDR);
|
||||
given(host.getDoubleWeight()).willReturn(1.0);
|
||||
given(host.getPort()).willReturn(PORT);
|
||||
given(host.getWeight()).willReturn(1);
|
||||
given(host.getUnit()).willReturn("DEFAULT");
|
||||
|
||||
AnsServer server = new AnsServer(host, "testDom");
|
||||
@SuppressWarnings("unchecked")
|
||||
AnsServerList originalServerList = mock(AnsServerList.class);
|
||||
given(originalServerList.getInitialListOfServers())
|
||||
.willReturn(Arrays.asList(server));
|
||||
return originalServerList;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user