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

add nacos config unit test

This commit is contained in:
pbting 2019-01-17 15:43:35 +08:00
parent 5b2ddacac8
commit de394f352c
9 changed files with 514 additions and 57 deletions

View File

@ -1,2 +1,4 @@
spring.application.name=nacos-config-example
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=sca-nacos-config
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties
spring.cloud.nacos.config.refreshable-dataids=common.properties

View File

@ -65,6 +65,11 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -182,19 +182,19 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
}
}
private static void checkDataIdFileExtension(String[] sharedDataIdArry) {
private static void checkDataIdFileExtension(String[] dataIdArray) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < sharedDataIdArry.length; i++) {
for (int i = 0; i < dataIdArray.length; i++) {
boolean isLegal = false;
for (String fileExtension : SUPPORT_FILE_EXTENSION) {
if (sharedDataIdArry[i].indexOf(fileExtension) > 0) {
if (dataIdArray[i].indexOf(fileExtension) > 0) {
isLegal = true;
break;
}
}
// add tips
if (!isLegal) {
stringBuilder.append(sharedDataIdArry[i] + ",");
stringBuilder.append(dataIdArray[i] + ",");
}
}

View File

@ -0,0 +1,101 @@
/*
* Copyright (C) 2019 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;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.nacos.api.config.ConfigService;
/**
* @author pbting
* @date 2019-01-17 11:45 AM
*/
public abstract class BaseNacosConfigTests {
protected ConfigurableApplicationContext context;
@Before
public void setUp() throws Exception {
this.context = new SpringApplicationBuilder(
NacosConfigBootstrapConfiguration.class,
NacosConfigEndpointAutoConfiguration.class,
NacosConfigAutoConfiguration.class, TestConfiguration.class)
.web(WebApplicationType.SERVLET)
.run("--spring.cloud.nacos.config.name=sca-nacos-config",
"--spring.cloud.config.enabled=true",
"--server.port=18080",
"--spring.application.name=sca-nacos-config",
"--spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
// "--spring.cloud.nacos.config.prefix=test",
"--spring.cloud.nacos.config.encode=utf-8",
// "--spring.cloud.nacos.config.file-extension=yaml",
"--spring.profiles.active=develop",
"--spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties",
"--spring.cloud.nacos.config.refreshable-dataids=common.properties",
"--spring.cloud.nacos.config.ext-config[0].data-id=ext00.yaml",
"--spring.cloud.nacos.config.ext-config[1].data-id=ext01.yaml",
"--spring.cloud.nacos.config.ext-config[1].group=EXT01_GROUP",
"--spring.cloud.nacos.config.ext-config[1].refresh=true",
"--spring.cloud.nacos.config.ext-config[2].data-id=ext02.yaml");
}
public NacosPropertySourceBuilder nacosPropertySourceBuilderInstance() {
NacosConfigProperties nacosConfigProperties = this.context
.getBean(NacosConfigProperties.class);
ConfigService configService = nacosConfigProperties.configServiceInstance();
long timeout = nacosConfigProperties.getTimeout();
NacosPropertySourceBuilder nacosPropertySourceBuilder = new NacosPropertySourceBuilder(
configService, timeout);
return nacosPropertySourceBuilder;
}
@After
public void tearDown() throws Exception {
if (this.context != null) {
this.context.close();
}
}
@EnableAutoConfiguration
@Configuration
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
static class TestConfiguration {
@Autowired
ConfigurableApplicationContext context;
@Bean
ContextRefresher contextRefresher() {
RefreshScope refreshScope = new RefreshScope();
refreshScope.setApplicationContext(context);
return new ContextRefresher(context, refreshScope);
}
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2019 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;
import org.junit.Test;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpoint;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author pbting
* @date 2019-01-17 2:25 PM
*/
public class EndpointTests extends BaseNacosConfigTests {
@Test
public void nacosConfigEndpoint() {
NacosConfigEndpoint nacosConfigEndpoint = this.context
.getBean(NacosConfigEndpoint.class);
assertThat(nacosConfigEndpoint != null).isEqualTo(true);
}
@Test
public void endpointInvoke() {
NacosConfigEndpoint nacosConfigEndpoint = this.context
.getBean(NacosConfigEndpoint.class);
assertThat(nacosConfigEndpoint.invoke() != null).isEqualTo(true);
}
}

View File

@ -16,59 +16,60 @@
package org.springframework.cloud.alibaba.nacos;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator;
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.PropertySource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author xiaojing
*/
public class NacosConfigAutoConfigurationTests {
private ConfigurableApplicationContext context;
@Before
public void setUp() throws Exception {
this.context = new SpringApplicationBuilder(
NacosConfigBootstrapConfiguration.class,
NacosConfigAutoConfiguration.class, TestConfiguration.class)
.web(WebApplicationType.NONE)
.run("--spring.cloud.nacos.config.name=myapp",
"--spring.cloud.config.enabled=true",
"--spring.cloud.nacos.config.server-addr=127.0.0.1:8848",
"--spring.cloud.nacos.config.prefix=test");
}
@After
public void tearDown() throws Exception {
if (this.context != null) {
this.context.close();
}
}
public class NacosConfigAutoConfigurationTests extends BaseNacosConfigTests {
private final static Logger log = LoggerFactory
.getLogger(NacosConfigAutoConfigurationTests.class);
@Test
public void testNacosConfigProperties() {
NacosConfigProperties nacosConfigProperties = this.context.getParent()
NacosConfigProperties nacosConfigProperties = context.getParent()
.getBean(NacosConfigProperties.class);
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("test");
assertThat(nacosConfigProperties.getName()).isEqualTo("myapp");
// assertThat(nacosConfigProperties.getPrefix()).isEqualTo("test");
assertThat(nacosConfigProperties.getName()).isEqualTo("sca-nacos-config");
assertThat(nacosConfigProperties.getServerAddr()).isEqualTo("127.0.0.1:8848");
assertThat(nacosConfigProperties.getEncode()).isEqualTo("utf-8");
assertThat(nacosConfigProperties.getActiveProfiles())
.isEqualTo(new String[] { "develop" });
assertThat(nacosConfigProperties.getSharedDataids())
.isEqualTo("base-common.properties,common.properties");
assertThat(nacosConfigProperties.getRefreshableDataids())
.isEqualTo("base-common.properties");
assertThat(nacosConfigProperties.getExtConfig().size()).isEqualTo(3);
assertThat(nacosConfigProperties.getExtConfig().get(0).getDataId())
.isEqualTo("ext01.yaml");
assertThat(nacosConfigProperties.getExtConfig().get(1).getGroup())
.isEqualTo("EXT01_GROUP");
assertThat(nacosConfigProperties.getExtConfig().get(1).isRefresh())
.isEqualTo(true);
}
@Test
public void nacosPropertySourceLocator() {
NacosPropertySourceLocator nacosPropertySourceLocator = this.context
.getBean(NacosPropertySourceLocator.class);
PropertySource propertySource = nacosPropertySourceLocator
.locate(this.context.getEnvironment());
assertThat(propertySource instanceof CompositePropertySource).isEqualTo(true);
CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
assertThat(compositePropertySource.containsProperty("user.name")).isEqualTo(true);
assertThat(compositePropertySource.getProperty("user.name"))
.isEqualTo("sca-nacos-config-test-case");
}
@Test
@ -80,18 +81,4 @@ public class NacosConfigAutoConfigurationTests {
}
@Configuration
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
static class TestConfiguration {
@Autowired
ConfigurableApplicationContext context;
@Bean
ContextRefresher contextRefresher() {
return new ContextRefresher(context, new RefreshScope());
}
}
}

View File

@ -0,0 +1,82 @@
/*
* Copyright (C) 2019 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;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigHealthIndicator;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author pbting
* @date 2019-01-17 2:58 PM
*/
public class NacosConfigHealthIndicatorTests extends BaseNacosConfigTests {
@Override
public void setUp() throws Exception {
this.context = new SpringApplicationBuilder(
NacosConfigBootstrapConfiguration.class,
NacosConfigEndpointAutoConfiguration.class,
NacosConfigAutoConfiguration.class, TestConfiguration.class)
.web(WebApplicationType.SERVLET)
.run("--spring.cloud.config.enabled=true", "--server.port=18080",
"--spring.application.name=sca-nacos-config",
"--spring.cloud.nacos.config.server-addr=127.0.0.1:8848");
}
@Test
public void nacosConfigHealthIndicatorInstance() {
NacosConfigHealthIndicator nacosConfigHealthIndicator = this.context
.getBean(NacosConfigHealthIndicator.class);
assertThat(nacosConfigHealthIndicator != null).isEqualTo(true);
}
@Test
public void testHealthCheck() {
NacosConfigHealthIndicator nacosConfigHealthIndicator = this.context
.getBean(NacosConfigHealthIndicator.class);
Health.Builder builder = Health.up();
Method method = ReflectionUtils.findMethod(NacosConfigHealthIndicator.class,
"doHealthCheck", Health.Builder.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
method.invoke(nacosConfigHealthIndicator, builder);
assertThat(builder != null).isEqualTo(true);
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,148 @@
/*
* Copyright (C) 2019 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;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author pbting
* @date 2019-01-17 11:49 AM
*/
public class NacosPropertySourceBuilderTests extends BaseNacosConfigTests {
private final static Logger log = LoggerFactory
.getLogger(NacosPropertySourceBuilderTests.class);
@Test
public void nacosPropertySourceBuilder() {
assertThat(nacosPropertySourceBuilderInstance() != null).isEqualTo(true);
}
@Test
public void getConfigByProperties() {
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
Object result = method.invoke(nacosPropertySourceBuilder,
"ext-config-common01.properties", "DEFAULT_GROUP", "properties",
true);
assertThat(result != null).isEqualTo(true);
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
assertThat(nacosPropertySource.getProperty("ext.key"))
.isEqualTo("ext.value01");
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@Test
public void getConfigByYaml() {
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
Object result = method.invoke(nacosPropertySourceBuilder,
"app-local-common.yaml", "DEFAULT_GROUP", "yaml", true);
assertThat(result != null).isEqualTo(true);
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
assertThat(nacosPropertySource.getProperty("app-local-common"))
.isEqualTo("update app local shared cguration for Nacos");
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@Test
public void getConfigByYml() {
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
Object result = method.invoke(nacosPropertySourceBuilder, "nacos.yml",
"DEFAULT_GROUP", "yml", true);
assertThat(result != null).isEqualTo(true);
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
assertThat(nacosPropertySource.getProperty("address"))
.isEqualTo("zhejiang-hangzhou");
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
@Test
public void getEmpty() {
NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance();
Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class,
"build", String.class, String.class, String.class, boolean.class);
ReflectionUtils.makeAccessible(method);
assertThat(method != null).isEqualTo(true);
try {
Object result = method.invoke(nacosPropertySourceBuilder, "nacos-empty.yml",
"DEFAULT_GROUP", "yml", true);
assertThat(result != null).isEqualTo(true);
assertThat(result instanceof NacosPropertySource).isEqualTo(true);
NacosPropertySource nacosPropertySource = (NacosPropertySource) result;
assertThat(nacosPropertySource.getProperty("address")).isEqualTo(null);
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright (C) 2019 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;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.refresh.ContextRefresher;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author pbting
* @date 2019-01-17 11:46 AM
*/
public class NacosSharedAndExtConfigTests extends BaseNacosConfigTests {
private final static Logger log = LoggerFactory
.getLogger(NacosSharedAndExtConfigTests.class);
@Test
public void testSharedConfigPriority() {
String userName = this.context.getEnvironment().getProperty("user.name");
assertThat(userName).isEqualTo("common-value");
}
@Test
public void testSharedConfigRefresh() {
while (true) {
ContextRefresher contextRefresher = this.context
.getBean(ContextRefresher.class);
contextRefresher.refresh();
String userName = this.context.getEnvironment().getProperty("user.name");
try {
assertThat(userName).isEqualTo("common-value-update");
TimeUnit.SECONDS.sleep(1);
log.info("user name is {}", userName);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Test
public void testExtConfigPriority() {
String userName = this.context.getEnvironment().getProperty("user.name");
assertThat(userName).isEqualTo("ext-02-value");
}
@Test
public void testExtOtherGroup() {
String userExt = this.context.getEnvironment().getProperty("user.ext");
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
}
@Test
public void testExtRefresh() {
while (true) {
ContextRefresher contextRefresher = this.context
.getBean(ContextRefresher.class);
contextRefresher.refresh();
String userExt = this.context.getEnvironment().getProperty("user.ext");
try {
assertThat(userExt).isEqualTo("EXT01_GROUP-value");
TimeUnit.SECONDS.sleep(1);
log.info("user name is {}", userExt);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}