diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/EndpointTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/EndpointTests.java deleted file mode 100644 index 5ef53f03..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/EndpointTests.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 NacosPowerMockitBaseTests { - - @Test - public void nacosConfigEndpoint() { - - NacosConfigEndpoint nacosConfigEndpoint = super.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); - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java deleted file mode 100644 index f8c29c46..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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; - -import org.junit.Test; -import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator; -import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties; -import org.springframework.core.env.CompositePropertySource; -import org.springframework.core.env.PropertySource; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author xiaojing - * @author pbting - */ -public class NacosConfigAutoConfigurationTests extends NacosPowerMockitBaseTests { - @Test - public void testNacosConfigProperties() { - - NacosConfigProperties nacosConfigProperties = context.getParent() - .getBean(NacosConfigProperties.class); - assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); - assertThat(nacosConfigProperties.getPrefix() == null).isEqualTo(true); - assertThat(nacosConfigProperties.getNamespace() == null).isEqualTo(true); - 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("common.properties"); - assertThat(nacosConfigProperties.getExtConfig().size()).isEqualTo(3); - assertThat(nacosConfigProperties.getExtConfig().get(0).getDataId()) - .isEqualTo("ext00.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); - } - - @Test - public void testNacosRefreshProperties() { - - NacosRefreshProperties nacosRefreshProperties = this.context - .getBean(NacosRefreshProperties.class); - assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true); - - } - -} diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigHealthIndicatorTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigHealthIndicatorTests.java deleted file mode 100644 index 1fd434b5..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigHealthIndicatorTests.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.actuate.health.Health; -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 NacosPowerMockitBaseTests { - - @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(); - } - - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationExtConfigTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationExtConfigTests.java new file mode 100644 index 00000000..024ddbc4 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationExtConfigTests.java @@ -0,0 +1,157 @@ +/* + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +import java.util.Map; + +import com.alibaba.nacos.client.config.NacosConfigService; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.api.support.MethodProxy; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; +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.alibaba.nacos.client.NacosPropertySourceLocator; +import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.management.*") +@PowerMockRunnerDelegate(SpringRunner.class) +@PrepareForTest({ NacosConfigService.class }) +@SpringBootTest(classes = NacosConfigurationExtConfigTests.TestConfig.class, properties = { + "spring.application.name=myTestService1", "spring.profiles.active=dev,test", + "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.config.encode=utf-8", + "spring.cloud.nacos.config.timeout=1000", + "spring.cloud.nacos.config.file-extension=properties", + + "spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties", + + "spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties", + "spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP", + + "spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties", + + "spring.cloud.nacos.config.accessKey=test-accessKey", + "spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE) +public class NacosConfigurationExtConfigTests { + + static { + + try { + // when(any(ConfigService.class).getConfig(eq("test-name.properties"), + // eq("test-group"), any())).thenReturn("user.name=hello"); + + Method method = PowerMockito.method(NacosConfigService.class, "getConfig", + String.class, String.class, long.class); + MethodProxy.proxy(method, new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + + if ("test-name.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "user.name=hello\nuser.age=12"; + } + + if ("test-name-dev.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "user.name=dev"; + } + + if ("ext-config-common01.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "test-ext-config1=config1\ntest-ext-config2=config1"; + } + if ("ext-config-common02.properties".equals(args[0]) + && "GLOBAL_GROUP".equals(args[1])) { + return "test-ext-config2=config2"; + } + + if ("common1.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "test-common1=common1\ntest-common2=common1"; + } + + if ("common2.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "test-common2=common2"; + } + + return ""; + } + }); + + } + catch (Exception ignore) { + ignore.printStackTrace(); + + } + } + + @Autowired + private Environment environment; + + @Autowired + private NacosPropertySourceLocator locator; + + @Autowired + private NacosConfigProperties properties; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosPropertySourceLocator was not created", locator); + assertNotNull("NacosConfigProperties was not created", properties); + + Assert.assertEquals(environment.getProperty("test-ext-config1"), "config1"); + Assert.assertEquals(environment.getProperty("test-ext-config2"), "config2"); + Assert.assertEquals(environment.getProperty("test-common1"), "common1"); + Assert.assertEquals(environment.getProperty("test-common2"), "common2"); + + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class, + NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationTests.java new file mode 100644 index 00000000..8a4bdda6 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigurationTests.java @@ -0,0 +1,268 @@ +/* + * 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +import java.util.Map; + +import com.alibaba.nacos.client.config.NacosConfigService; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.api.support.MethodProxy; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; +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.alibaba.nacos.client.NacosPropertySourceLocator; +import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpoint; +import org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.management.*") +@PowerMockRunnerDelegate(SpringRunner.class) +@PrepareForTest({ NacosConfigService.class }) +@SpringBootTest(classes = NacosConfigurationTests.TestConfig.class, properties = { + "spring.application.name=myTestService1", "spring.profiles.active=dev,test", + "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.config.endpoint=test-endpoint", + "spring.cloud.nacos.config.namespace=test-namespace", + "spring.cloud.nacos.config.encode=utf-8", + "spring.cloud.nacos.config.timeout=1000", + "spring.cloud.nacos.config.group=test-group", + "spring.cloud.nacos.config.name=test-name", + "spring.cloud.nacos.config.cluster-name=test-cluster", + "spring.cloud.nacos.config.file-extension=properties", + "spring.cloud.nacos.config.contextPath=test-contextpath", + + "spring.cloud.nacos.config.ext-config[0].data-id=ext-config-common01.properties", + + "spring.cloud.nacos.config.ext-config[1].data-id=ext-config-common02.properties", + "spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP", + + "spring.cloud.nacos.config.shared-dataids=common1.properties,common2.properties", + + "spring.cloud.nacos.config.accessKey=test-accessKey", + "spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE) +public class NacosConfigurationTests { + + static { + + try { + // when(any(ConfigService.class).getConfig(eq("test-name.properties"), + // eq("test-group"), any())).thenReturn("user.name=hello"); + + Method method = PowerMockito.method(NacosConfigService.class, "getConfig", + String.class, String.class, long.class); + MethodProxy.proxy(method, new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + + if ("test-name.properties".equals(args[0]) + && "test-group".equals(args[1])) { + return "user.name=hello\nuser.age=12"; + } + + if ("test-name-dev.properties".equals(args[0]) + && "test-group".equals(args[1])) { + return "user.name=dev"; + } + + if ("ext-config-common01.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "test-ext-config1=config1\ntest-ext-config2=config1"; + } + if ("ext-config-common02.properties".equals(args[0]) + && "GLOBAL_GROUP".equals(args[1])) { + return "test-ext-config2=config2"; + } + + if ("common1.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "test-common1=common1\ntest-common2=common1"; + } + + if ("common2.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "test-common2=common2"; + } + + return ""; + } + }); + + } + catch (Exception ignore) { + ignore.printStackTrace(); + + } + } + + @Autowired + private Environment environment; + + @Autowired + private NacosPropertySourceLocator locator; + + @Autowired + private NacosConfigProperties properties; + + @Autowired + private NacosRefreshHistory refreshHistory; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosPropertySourceLocator was not created", locator); + assertNotNull("NacosConfigProperties was not created", properties); + + checkoutNacosConfigServerAddr(); + checkoutNacosConfigEndpoint(); + checkoutNacosConfigNamespace(); + checkoutNacosConfigClusterName(); + checkoutNacosConfigAccessKey(); + checkoutNacosConfigSecrectKey(); + checkoutNacosConfigName(); + checkoutNacosConfigGroup(); + checkoutNacosConfigContextPath(); + checkoutNacosConfigFileExtension(); + checkoutNacosConfigTimeout(); + checkoutNacosConfigEncode(); + checkoutNacosConfigProfiles(); + checkoutNacosConfigExtConfig(); + + checkoutEndpoint(); + + Assert.assertEquals(environment.getProperty("user.name"), "dev"); + Assert.assertEquals(environment.getProperty("user.age"), "12"); + } + + private void checkoutNacosConfigServerAddr() { + assertEquals("NacosConfigProperties server address is wrong", "127.0.0.1:8848", + properties.getServerAddr()); + + } + + private void checkoutNacosConfigEndpoint() { + assertEquals("NacosConfigProperties endpoint is wrong", "test-endpoint", + properties.getEndpoint()); + + } + + private void checkoutNacosConfigNamespace() { + assertEquals("NacosConfigProperties namespace is wrong", "test-namespace", + properties.getNamespace()); + + } + + private void checkoutNacosConfigClusterName() { + assertEquals("NacosConfigProperties' cluster is wrong", "test-cluster", + properties.getClusterName()); + } + + private void checkoutNacosConfigAccessKey() { + assertEquals("NacosConfigProperties' is access key is wrong", "test-accessKey", + properties.getAccessKey()); + } + + private void checkoutNacosConfigSecrectKey() { + assertEquals("NacosConfigProperties' is secret key is wrong", "test-secretKey", + properties.getSecretKey()); + } + + private void checkoutNacosConfigContextPath() { + assertEquals("NacosConfigProperties' context path is wrong", "test-contextpath", + properties.getContextPath()); + } + + private void checkoutNacosConfigName() { + assertEquals("NacosConfigProperties' name is wrong", "test-name", + properties.getName()); + } + + private void checkoutNacosConfigGroup() { + assertEquals("NacosConfigProperties' group is wrong", "test-group", + properties.getGroup()); + } + + private void checkoutNacosConfigFileExtension() { + assertEquals("NacosConfigProperties' file extension is wrong", "properties", + properties.getFileExtension()); + } + + private void checkoutNacosConfigTimeout() { + assertEquals("NacosConfigProperties' timeout is wrong", 1000, + properties.getTimeout()); + } + + private void checkoutNacosConfigEncode() { + assertEquals("NacosConfigProperties' encode is wrong", "utf-8", + properties.getEncode()); + } + + private void checkoutNacosConfigExtConfig() { + assertEquals("NacosConfigProperties' ext config is wrong", + "ext-config-common01.properties", + properties.getExtConfig().get(0).getDataId()); + assertEquals("NacosConfigProperties' ext config is wrong", + "ext-config-common02.properties", + properties.getExtConfig().get(1).getDataId()); + assertEquals("NacosConfigProperties' ext config is wrong", "GLOBAL_GROUP", + properties.getExtConfig().get(1).getGroup()); + } + + private void checkoutNacosConfigProfiles() { + assertEquals("NacosConfigProperties' profiles is wrong", + new String[] { "dev", "test" }, properties.getActiveProfiles()); + } + + private void checkoutEndpoint() throws Exception { + NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties, + refreshHistory); + Map map = nacosConfigEndpoint.invoke(); + assertEquals(map.get("NacosConfigProperties"), properties); + assertEquals(map.get("RefreshHistory"), refreshHistory.getRecords()); + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class, + NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosFileExtensionTest.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosFileExtensionTest.java new file mode 100644 index 00000000..979cac3f --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosFileExtensionTest.java @@ -0,0 +1,98 @@ +/* + * 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 static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +import com.alibaba.nacos.client.config.NacosConfigService; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.api.support.MethodProxy; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; +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.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.management.*") +@PowerMockRunnerDelegate(SpringRunner.class) +@PrepareForTest({ NacosConfigService.class }) +@SpringBootTest(classes = NacosFileExtensionTest.TestConfig.class, properties = { + "spring.application.name=test-name", + "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.config.file-extension=yaml" }, webEnvironment = NONE) +public class NacosFileExtensionTest { + + static { + + try { + Method method = PowerMockito.method(NacosConfigService.class, "getConfig", + String.class, String.class, long.class); + MethodProxy.proxy(method, new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + if ("test-name.yaml".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "user:\n name: hello\n age: 12"; + } + return ""; + } + }); + + } + catch (Exception ignore) { + ignore.printStackTrace(); + + } + } + + @Autowired + private Environment environment; + + @Test + public void contextLoads() throws Exception { + + Assert.assertEquals(environment.getProperty("user.name"), "hello"); + Assert.assertEquals(environment.getProperty("user.age"), "12"); + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class, + NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPowerMockitBaseTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPowerMockitBaseTests.java deleted file mode 100644 index ada71a9c..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPowerMockitBaseTests.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * 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 com.alibaba.nacos.api.config.ConfigService; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.MethodProxy; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.modules.junit4.PowerMockRunnerDelegate; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource; -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.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.util.ReflectionUtils; - -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -/** - * @author pbting - * @date 2019-01-17 8:54 PM - */ -@RunWith(PowerMockRunner.class) -@PowerMockRunnerDelegate(SpringRunner.class) -@PowerMockIgnore({ "javax.management.*", "javax.net.ssl.*" }) -@PrepareForTest({ NacosPropertySourceBuilder.class }) -@SpringBootTest(classes = { NacosConfigBootstrapConfiguration.class, - NacosConfigEndpointAutoConfiguration.class, NacosConfigAutoConfiguration.class, - NacosPowerMockitBaseTests.TestConfiguration.class }, properties = { - "spring.application.name=sca-nacos-config", - "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", - "spring.cloud.nacos.config.name=sca-nacos-config", - // "spring.cloud.nacos.config.refresh.enabled=false", - "spring.cloud.nacos.config.encode=utf-8", - "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.yml", - "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", - "spring.profiles.active=develop", "server.port=19090" }) -public class NacosPowerMockitBaseTests { - - private final static List DATAIDS = Arrays.asList("common.properties", - "base-common.properties", "ext00.yaml", "ext01.yml", "ext02.yaml", - "sca-nacos-config.properties", "sca-nacos-config-develop.properties"); - - private final static HashMap VALUES = new HashMap<>(); - - @Autowired - protected ApplicationContext context; - - static { - initDataIds(); - try { - final Constructor constructor = ReflectionUtils.accessibleConstructor( - NacosPropertySource.class, String.class, String.class, Map.class, - Date.class, boolean.class); - Method method = PowerMockito.method(NacosPropertySourceBuilder.class, "build", - String.class, String.class, String.class, boolean.class); - MethodProxy.proxy(method, new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) - throws Throwable { - Properties properties = VALUES.get(args[0].toString()); - if (properties == null) { - properties = new Properties(); - properties.put("user.name", args[0].toString()); - } - Object instance = constructor.newInstance(args[1].toString(), - args[0].toString(), properties, new Date(), args[3]); - return instance; - } - }); - } - catch (NoSuchMethodException e) { - e.printStackTrace(); - } - } - - private static void initDataIds() { - DATAIDS.forEach(dataId -> { - String realpath = "/" + dataId; - ClassPathResource classPathResource = new ClassPathResource(realpath); - if (realpath.endsWith("properties")) { - Properties properties = new Properties(); - try { - properties.load(classPathResource.getInputStream()); - VALUES.put(dataId, properties); - } - catch (IOException e) { - e.printStackTrace(); - } - } - - if (realpath.endsWith("yaml") || realpath.endsWith("yml")) { - YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); - yamlFactory.setResources(classPathResource); - try { - VALUES.put(dataId, yamlFactory.getObject()); - } - catch (Exception e) { - e.printStackTrace(); - } - } - }); - } - - 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; - } - - @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); - } - } - - @Test - public void testAppContext() { - System.err.println(this.context); - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceBuilderTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceBuilderTests.java deleted file mode 100644 index 40248d25..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosPropertySourceBuilderTests.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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.powermock.api.support.MethodProxy; -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.Constructor; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author pbting - * @date 2019-01-17 11:49 AM - */ -public class NacosPropertySourceBuilderTests extends NacosPowerMockitBaseTests { - - @Test - public void nacosPropertySourceBuilder() { - - assertThat(nacosPropertySourceBuilderInstance() != null).isEqualTo(true); - } - - @Test - public void getConfigByProperties() { - try { - final HashMap value = new HashMap<>(); - value.put("dev.mode", "local-mock"); - - final Constructor constructor = ReflectionUtils.accessibleConstructor( - NacosPropertySource.class, String.class, String.class, Map.class, - Date.class, boolean.class); - - 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); - MethodProxy.proxy(method, new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) - throws Throwable { - Object instance = constructor.newInstance(args[1].toString(), - args[0].toString(), value, new Date(), args[3]); - return instance; - } - }); - - Object result = method.invoke(nacosPropertySourceBuilder, - "mock-nacos-config.properties", "DEFAULT_GROUP", "properties", true); - assertThat(result != null).isEqualTo(true); - assertThat(result instanceof NacosPropertySource).isEqualTo(true); - NacosPropertySource nacosPropertySource = (NacosPropertySource) result; - assertThat(nacosPropertySource.getProperty("dev.mode")) - .isEqualTo("local-mock"); - } - catch (Exception e) { - e.printStackTrace(); - } - } - - @Test - public void getConfigByYaml() { - - try { - // - final HashMap value = new HashMap<>(); - value.put("mock-ext-config", "mock-ext-config-value"); - - final Constructor constructor = ReflectionUtils.accessibleConstructor( - NacosPropertySource.class, String.class, String.class, Map.class, - Date.class, boolean.class); - - Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class, - "build", String.class, String.class, String.class, boolean.class); - ReflectionUtils.makeAccessible(method); - assertThat(method != null).isEqualTo(true); - - MethodProxy.proxy(method, new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) - throws Throwable { - Object instance = constructor.newInstance(args[1].toString(), - args[0].toString(), value, new Date(), args[3]); - return instance; - } - }); - - NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance(); - Object result = method.invoke(nacosPropertySourceBuilder, "ext-config.yaml", - "DEFAULT_GROUP", "yaml", true); - assertThat(result != null).isEqualTo(true); - assertThat(result instanceof NacosPropertySource).isEqualTo(true); - NacosPropertySource nacosPropertySource = (NacosPropertySource) result; - assertThat(nacosPropertySource.getProperty("mock-ext-config")) - .isEqualTo("mock-ext-config-value"); - } - catch (Exception e) { - e.printStackTrace(); - } - } - - @Test - public void getConfigByYml() { - try { - // - final HashMap value = new HashMap<>(); - value.put("mock-ext-config-yml", "mock-ext-config-yml-value"); - - final Constructor constructor = ReflectionUtils.accessibleConstructor( - NacosPropertySource.class, String.class, String.class, Map.class, - Date.class, boolean.class); - - Method method = ReflectionUtils.findMethod(NacosPropertySourceBuilder.class, - "build", String.class, String.class, String.class, boolean.class); - ReflectionUtils.makeAccessible(method); - assertThat(method != null).isEqualTo(true); - - MethodProxy.proxy(method, new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) - throws Throwable { - Object instance = constructor.newInstance(args[1].toString(), - args[0].toString(), value, new Date(), args[3]); - return instance; - } - }); - - NacosPropertySourceBuilder nacosPropertySourceBuilder = nacosPropertySourceBuilderInstance(); - Object result = method.invoke(nacosPropertySourceBuilder, "ext-config.yml", - "DEFAULT_GROUP", "yml", true); - assertThat(result != null).isEqualTo(true); - assertThat(result instanceof NacosPropertySource).isEqualTo(true); - NacosPropertySource nacosPropertySource = (NacosPropertySource) result; - assertThat(nacosPropertySource.getProperty("mock-ext-config-yml")) - .isEqualTo("mock-ext-config-yml-value"); - } - catch (Exception 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(); - } - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosSharedAndExtConfigTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosSharedAndExtConfigTests.java deleted file mode 100644 index 90fe3ca4..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosSharedAndExtConfigTests.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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 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 NacosPowerMockitBaseTests { - private final static Logger log = LoggerFactory - .getLogger(NacosSharedAndExtConfigTests.class); - - @Test - public void testSharedConfigPriority() { - String userName = this.context.getEnvironment().getProperty("user.address"); - assertThat(userName).isEqualTo("zhejiang-ningbo"); - } - - @Test - public void testSharedConfigRefresh() { - - while (true) { - // ContextRefresher contextRefresher = this.context - // .getBean(ContextRefresher.class); - // contextRefresher.refresh(); - String userName = this.context.getEnvironment().getProperty("user.address"); - try { - assertThat(userName).isEqualTo("zhejiang-ningbo"); - TimeUnit.SECONDS.sleep(1); - log.info("user name is {}", userName); - } - catch (InterruptedException e) { - e.printStackTrace(); - } - // 真实测试时将这里 注释掉 - break; - } - } - - @Test - public void testExtConfigPriority() { - String extKey = this.context.getEnvironment().getProperty("ext.key"); - assertThat(extKey).isEqualTo("ext.value02"); - } - - @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(); - } - break; - } - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointTests.java new file mode 100644 index 00000000..ef823960 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointTests.java @@ -0,0 +1,141 @@ +/* + * 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.endpoint; + +import static org.junit.Assert.assertEquals; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.alibaba.nacos.client.config.NacosConfigService; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.api.support.MethodProxy; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.health.Health.Builder; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosConfigBootstrapConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosConfigProperties; +import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.management.*") +@PowerMockRunnerDelegate(SpringRunner.class) +@PrepareForTest({ NacosConfigService.class }) +@SpringBootTest(classes = NacosConfigEndpointTests.TestConfig.class, properties = { + "spring.application.name=test-name", + "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.config.file-extension=properties" }, webEnvironment = NONE) +public class NacosConfigEndpointTests { + + static { + + try { + + Method method = PowerMockito.method(NacosConfigService.class, "getConfig", + String.class, String.class, long.class); + MethodProxy.proxy(method, new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + + if ("test-name.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "user.name=hello\nuser.age=12"; + } + return ""; + } + }); + + } + catch (Exception ignore) { + ignore.printStackTrace(); + + } + } + + @Autowired + private NacosConfigProperties properties; + + @Autowired + private NacosRefreshHistory refreshHistory; + + @Test + public void contextLoads() throws Exception { + + checkoutEndpoint(); + checkoutAcmHealthIndicator(); + + } + + private void checkoutAcmHealthIndicator() { + try { + Builder builder = new Builder(); + + NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator( + properties, properties.configServiceInstance()); + healthIndicator.doHealthCheck(builder); + + Builder builder1 = new Builder(); + List dataIds = new ArrayList<>(); + dataIds.add("test-name.properties"); + builder1.up().withDetail("dataIds", dataIds); + + Assert.assertTrue(builder.build().equals(builder1.build())); + + } + catch (Exception ignoreE) { + + } + + } + + private void checkoutEndpoint() throws Exception { + NacosConfigEndpoint endpoint = new NacosConfigEndpoint(properties, + refreshHistory); + Map map = endpoint.invoke(); + assertEquals(map.get("NacosConfigProperties"), properties); + assertEquals(map.get("RefreshHistory"), refreshHistory.getRecords()); + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class, + NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/base-common.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/base-common.properties deleted file mode 100644 index 71137acf..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/resources/base-common.properties +++ /dev/null @@ -1,2 +0,0 @@ -user.name=base-common-value -user.address=zhejiang-hangzhou \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/common.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/common.properties deleted file mode 100644 index d8f61776..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/resources/common.properties +++ /dev/null @@ -1,2 +0,0 @@ -user.name=common-value -user.address=zhejiang-ningbo \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/ext00.yaml b/spring-cloud-alibaba-nacos-config/src/test/resources/ext00.yaml deleted file mode 100644 index 4d70f674..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/resources/ext00.yaml +++ /dev/null @@ -1,4 +0,0 @@ -user: - name: ext-00-value -ext: - key: ext.value00 \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/ext01.yml b/spring-cloud-alibaba-nacos-config/src/test/resources/ext01.yml deleted file mode 100644 index c689c09e..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/resources/ext01.yml +++ /dev/null @@ -1,5 +0,0 @@ -user: - name: ext-01-value - ext: EXT01_GROUP-value -ext: - key: ext.value01 diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/ext02.yaml b/spring-cloud-alibaba-nacos-config/src/test/resources/ext02.yaml deleted file mode 100644 index 9cc47740..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/resources/ext02.yaml +++ /dev/null @@ -1,5 +0,0 @@ -user: - name: ext-02-value -ext: - key: ext.value02 -app-local-common: update app local shared cguration for Nacos \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config-develop.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config-develop.properties deleted file mode 100644 index 21a6ef58..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config-develop.properties +++ /dev/null @@ -1 +0,0 @@ -user.name=sca-nacos-config-value-develop \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config.properties b/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config.properties deleted file mode 100644 index db268f2c..00000000 --- a/spring-cloud-alibaba-nacos-config/src/test/resources/sca-nacos-config.properties +++ /dev/null @@ -1,2 +0,0 @@ -user.name=sca-nacos-config-value -dev.mode=local \ No newline at end of file