1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Add test case.

This commit is contained in:
xiaolongzuo
2019-02-13 17:16:52 +08:00
parent 13097fd32c
commit 910068c91f
25 changed files with 600 additions and 214 deletions

View File

@@ -0,0 +1,23 @@
/*
* 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 org.springframework.cloud.alibaba.nacos;
/**
* @author xiaolongzuo
*/
public class NacosConfigAutoConfiguration {
}

View File

@@ -16,36 +16,35 @@
package org.springframework.cloud.alicloud.context;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author xiaolongzuo
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AliCloudSpringApplicationTests.AliCloudDisabledApp.class, properties = {
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore("javax.management.*")
@SpringBootTest(classes = BaseAliCloudSpringApplication.AliCloudApplication.class, properties = {
"spring.application.name=myapp",
"spring.cloud.alicloud.edas.application.name=myapp",
"spring.cloud.alicloud.access-key=ak", "spring.cloud.alicloud.secret-key=sk",
"spring.cloud.alicloud.oss.endpoint=test",
"spring.cloud.alicloud.scx.group-id=1-2-3-4",
"spring.cloud.alicloud.edas.namespace=cn-test" }, webEnvironment = RANDOM_PORT)
@DirtiesContext
public class AliCloudSpringApplicationTests {
@Test
public void contextLoads() {
System.out.println("Context load...");
}
"spring.cloud.alicloud.edas.namespace=cn-test",
"spring.cloud.alicloud.ans.server-list=192.168.1.100",
"spring.cloud.alicloud.ans.server-port=8888",
"spring.cloud.alicloud.oss.enabled=false",
"spring.cloud.alicloud.scx.enabled=false" })
public abstract class BaseAliCloudSpringApplication {
@SpringBootApplication
public static class AliCloudDisabledApp {
public static class AliCloudApplication {
}

View File

@@ -0,0 +1,37 @@
/*
* 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 org.springframework.cloud.alicloud.context.ans;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import org.junit.Test;
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
/**
* @author xiaolongzuo
*/
public class AnsContextApplicationListenerTests extends BaseAliCloudSpringApplication {
@Test
public void testAnsContextApplicationListenerDefault() {
assertThat(System
.getProperty("com.alibaba.ans.shaded.com.taobao.vipserver.serverlist"))
.isEqualTo("192.168.1.100");
assertThat(System.getProperty("vipserver.server.port")).isEqualTo("8888");
}
}

View File

@@ -18,7 +18,17 @@ package org.springframework.cloud.alicloud.context.ans;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Vector;
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 org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration;
@@ -29,6 +39,8 @@ import com.alibaba.cloud.context.AliCloudServerMode;
/**
* @author xiaolongzuo
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ NetworkInterface.class, AnsProperties.class })
public class AnsPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
@@ -37,8 +49,7 @@ public class AnsPropertiesTests {
AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected()
throws ClassNotFoundException {
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.withPropertyValues().run(context -> {
AnsProperties ansProperties = context.getBean(AnsProperties.class);
assertThat(ansProperties.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL);
@@ -65,7 +76,7 @@ public class AnsPropertiesTests {
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded() {
public void testConfigurationValuesAreCorrectlyLoaded1() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.ans.server-mode=EDAS",
"spring.cloud.alicloud.ans.server-port=11111",
@@ -99,4 +110,37 @@ public class AnsPropertiesTests {
});
}
@Test(expected = RuntimeException.class)
public void testConfigurationValuesAreCorrectlyLoaded2() {
this.contextRunner.withPropertyValues(
"spring.cloud.alicloud.ans.client-interface-name=noneinterfacename")
.run(context -> {
AnsProperties ansProperties = context.getBean(AnsProperties.class);
assertThat(ansProperties.getClientInterfaceName())
.isEqualTo("noneinterfacename");
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded3() throws SocketException {
NetworkInterface networkInterface = PowerMockito.mock(NetworkInterface.class);
Vector<InetAddress> inetAddressList = new Vector<>();
Inet4Address inetAddress = PowerMockito.mock(Inet4Address.class);
PowerMockito.when(inetAddress.getHostAddress()).thenReturn("192.168.1.100");
inetAddressList.add(inetAddress);
PowerMockito.when(networkInterface.getInetAddresses())
.thenReturn(inetAddressList.elements());
PowerMockito.mockStatic(NetworkInterface.class);
PowerMockito.when(NetworkInterface.getByName("eth0"))
.thenReturn(networkInterface);
this.contextRunner
.withPropertyValues(
"spring.cloud.alicloud.ans.client-interface-name=eth0")
.run(context -> {
AnsProperties ansProperties = context.getBean(AnsProperties.class);
assertThat(ansProperties.getClientInterfaceName()).isEqualTo("eth0");
assertThat(ansProperties.getClientIp()).isEqualTo("192.168.1.100");
});
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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 org.springframework.cloud.alicloud.context.nacos;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
import org.springframework.cloud.alicloud.utils.ChangeOrderUtils;
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
/**
* @author xiaolongzuo
*/
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
NacosParameterInitListener.class, AliCloudAnsInitializer.class })
public class NacosParameterInitListenerTests extends BaseAliCloudSpringApplication {
@BeforeClass
public static void setUp() {
ChangeOrderUtils.mockChangeOrder();
}
@Test
public void testNacosParameterInitListener() {
assertThat(System.getProperty("spring.cloud.nacos.config.server-mode"))
.isEqualTo("EDAS");
assertThat(System.getProperty("spring.cloud.nacos.config.server-addr"))
.isEqualTo("");
assertThat(System.getProperty("spring.cloud.nacos.config.endpoint"))
.isEqualTo("testDomain");
assertThat(System.getProperty("spring.cloud.nacos.config.namespace"))
.isEqualTo("testTenantId");
assertThat(System.getProperty("spring.cloud.nacos.config.access-key"))
.isEqualTo("testAK");
assertThat(System.getProperty("spring.cloud.nacos.config.secret-key"))
.isEqualTo("testSK");
}
}

View File

@@ -35,11 +35,13 @@ public class OssAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(OssContextAutoConfiguration.class))
.withPropertyValues("spring.cloud.alicloud.accessKey=your-ak")
.withPropertyValues("spring.cloud.alicloud.secretKey=your-sk")
.withPropertyValues(
"spring.cloud.alicloud.oss.endpoint=http://oss-cn-beijing.aliyuncs.com")
.withPropertyValues("spring.cloud.alicloud.oss.config.userAgent=alibaba");
.withPropertyValues("spring.cloud.alicloud.accessKey=your-ak",
"spring.cloud.alicloud.secretKey=your-sk",
"spring.cloud.alicloud.oss.endpoint=http://oss-cn-beijing.aliyuncs.com",
"spring.cloud.alicloud.oss.config.userAgent=alibaba",
"spring.cloud.alicloud.oss.sts.access-key=your-sts-ak",
"spring.cloud.alicloud.oss.sts.secret-key=your-sts-sk",
"spring.cloud.alicloud.oss.sts.security-token=your-sts-token");
@Test
public void testOSSProperties() {
@@ -53,11 +55,15 @@ public class OssAutoConfigurationTests {
assertThat(ossProperties.getEndpoint())
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
assertThat(ossProperties.getConfig().getUserAgent()).isEqualTo("alibaba");
assertThat(ossProperties.getSts().getAccessKey()).isEqualTo("your-sts-ak");
assertThat(ossProperties.getSts().getSecretKey()).isEqualTo("your-sts-sk");
assertThat(ossProperties.getSts().getSecurityToken())
.isEqualTo("your-sts-token");
});
}
@Test
public void testOSSClient() {
public void testOSSClient1() {
this.contextRunner.run(context -> {
assertThat(context.getBeansOfType(OSS.class).size() == 1).isTrue();
assertThat(context.getBeanNamesForType(OSS.class)[0]).isEqualTo("ossClient");
@@ -74,4 +80,26 @@ public class OssAutoConfigurationTests {
});
}
@Test
public void testOSSClient2() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.oss.authorization-mode=STS")
.run(context -> {
assertThat(context.getBeansOfType(OSS.class).size() == 1).isTrue();
assertThat(context.getBeanNamesForType(OSS.class)[0])
.isEqualTo("ossClient");
OSSClient ossClient = (OSSClient) context.getBean(OSS.class);
assertThat(ossClient.getEndpoint().toString())
.isEqualTo("http://oss-cn-beijing.aliyuncs.com");
assertThat(ossClient.getClientConfiguration().getUserAgent())
.isEqualTo("alibaba");
assertThat(ossClient.getCredentialsProvider().getCredentials()
.getAccessKeyId()).isEqualTo("your-sts-ak");
assertThat(ossClient.getCredentialsProvider().getCredentials()
.getSecretAccessKey()).isEqualTo("your-sts-sk");
assertThat(ossClient.getCredentialsProvider().getCredentials()
.getSecurityToken()).isEqualTo("your-sts-token");
});
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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 org.springframework.cloud.alicloud.context.sentinel;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
import org.springframework.cloud.alicloud.context.Constants;
import org.springframework.cloud.alicloud.utils.ChangeOrderUtils;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
/**
* @author xiaolongzuo
*/
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
SentinelAliCloudListener.class })
public class SentinelAliCloudListenerTests extends BaseAliCloudSpringApplication {
@BeforeClass
public static void setUp() {
ChangeOrderUtils.mockChangeOrder();
}
@Test
public void testNacosParameterInitListener() {
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_ENDPOINT))
.isEqualTo("testDomain");
assertThat(System.getProperty(Constants.Sentinel.PROJECT_NAME))
.isEqualTo("testProjectName");
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_NAMESPACE))
.isEqualTo("testTenantId");
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_AK))
.isEqualTo("testAK");
assertThat(System.getProperty(Constants.Sentinel.NACOS_DATASOURCE_SK))
.isEqualTo("testSK");
}
}

View File

@@ -0,0 +1,69 @@
/*
* 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 org.springframework.cloud.alicloud.context.sms;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import org.assertj.core.api.AssertionsForClassTypes;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration;
import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration;
/**
* @author xiaolongzuo
*/
public class SmsPropertiesTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SmsContextAutoConfiguration.class,
EdasContextAutoConfiguration.class,
AliCloudContextAutoConfiguration.class));
@Test
public void testConfigurationValueDefaultsAreAsExpected() {
this.contextRunner.run(context -> {
SmsProperties config = context.getBean(SmsProperties.class);
assertThat(config.getReportQueueName()).isNull();
assertThat(config.getUpQueueName()).isNull();
assertThat(config.getConnectTimeout()).isEqualTo("10000");
assertThat(config.getReadTimeout()).isEqualTo("10000");
});
}
@Test
public void testConfigurationValuesAreCorrectlyLoaded() {
this.contextRunner
.withPropertyValues("spring.cloud.alicloud.sms.reportQueueName=q1",
"spring.cloud.alicloud.sms.upQueueName=q2",
"spring.cloud.alicloud.sms.connect-timeout=20",
"spring.cloud.alicloud.sms.read-timeout=30")
.run(context -> {
SmsProperties config = context.getBean(SmsProperties.class);
AssertionsForClassTypes.assertThat(config.getReportQueueName())
.isEqualTo("q1");
AssertionsForClassTypes.assertThat(config.getUpQueueName())
.isEqualTo("q2");
AssertionsForClassTypes.assertThat(config.getConnectTimeout())
.isEqualTo("20");
AssertionsForClassTypes.assertThat(config.getReadTimeout())
.isEqualTo("30");
});
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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 org.springframework.cloud.alicloud.utils;
import org.powermock.api.mockito.PowerMockito;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
/**
* @author xiaolongzuo
*/
public class ChangeOrderUtils {
private ChangeOrderUtils() {
}
public static void mockChangeOrder() {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = PowerMockito
.mock(EdasChangeOrderConfiguration.class);
PowerMockito.when(edasChangeOrderConfiguration.isEdasManaged()).thenReturn(true);
PowerMockito.when(edasChangeOrderConfiguration.getAddressServerDomain())
.thenReturn("testDomain");
PowerMockito.when(edasChangeOrderConfiguration.getTenantId())
.thenReturn("testTenantId");
PowerMockito.when(edasChangeOrderConfiguration.getDauthAccessKey())
.thenReturn("testAK");
PowerMockito.when(edasChangeOrderConfiguration.getDauthSecretKey())
.thenReturn("testSK");
PowerMockito.when(edasChangeOrderConfiguration.getProjectName())
.thenReturn("testProjectName");
PowerMockito.when(edasChangeOrderConfiguration.getAddressServerPort())
.thenReturn("8080");
PowerMockito.mockStatic(EdasChangeOrderConfigurationFactory.class);
PowerMockito
.when(EdasChangeOrderConfigurationFactory
.getEdasChangeOrderConfiguration())
.thenReturn(edasChangeOrderConfiguration);
}
}