This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 58c0369830220f84208cc93084e9d036fd4cebc2 Author: liubao <bi...@qq.com> AuthorDate: Sat Oct 7 16:12:34 2023 +0800 [SCB-2008]remove archaius dependency: fix tests --- .../rest/RestProducerInvocationCreatorTest.java | 8 +-- .../common/rest/TestRestEngineSchemaListener.java | 6 +- .../common/rest/codec/TestRestCodec.java | 2 + .../common/rest/codec/param/TestBodyProcessor.java | 6 ++ .../rest/codec/param/TestBodyProcessorCreator.java | 13 +++++ .../rest/codec/param/TestHeaderProcessor.java | 15 ++++- .../rest/codec/query/QueryCodecMultiTest.java | 14 +++++ .../common/rest/locator/TestMicroservicePaths.java | 6 +- .../core/executor/TestExecutorManager.java | 14 ++--- .../servicecomb/demo/CodeFirstRestTemplate.java | 4 +- .../servicecomb/config/nacos/NacosClientTest.java | 4 ++ .../servicecomb/config/BootStrapProperties.java | 6 +- .../config/FoundationConfigConfiguration.java | 5 ++ .../config/InMemoryDynamicPropertiesSource.java | 25 ++++----- .../config/BootStrapPropertiesTest.java | 64 ++++++---------------- .../huaweicloud/servicestage/TestCasEnvConfig.java | 55 ------------------- .../model/MicroservicePropertiesLoader.java | 10 ++-- .../config/TestAbstractPropertiesLoader.java | 12 +++- .../registry/lightweight/StoreServiceTest.java | 39 ++++++++++++- .../servicecomb/registry/lightweight/TestBase.java | 59 -------------------- 20 files changed, 161 insertions(+), 206 deletions(-) diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java index 6e184dfc5..88f340566 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java @@ -26,7 +26,6 @@ import static org.assertj.core.api.Assertions.catchThrowable; import org.apache.servicecomb.common.rest.definition.RestOperationMeta; import org.apache.servicecomb.common.rest.locator.OperationLocator; import org.apache.servicecomb.common.rest.locator.ServicePathManager; -import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.core.Endpoint; import org.apache.servicecomb.core.Invocation; @@ -37,6 +36,7 @@ import org.apache.servicecomb.core.definition.InvocationRuntimeType; import org.apache.servicecomb.core.definition.MicroserviceMeta; import org.apache.servicecomb.core.definition.OperationMeta; import org.apache.servicecomb.core.definition.SchemaMeta; +import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx; import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx; @@ -83,12 +83,12 @@ public class RestProducerInvocationCreatorTest { @BeforeAll public static void beforeClass() { - ArchaiusUtils.resetConfig(); - ConfigUtil.installDynamicConfig(); - engine = SCBBootstrap.createSCBEngineForTest(); engine.setStatus(SCBStatus.UP); Environment environment = Mockito.mock(Environment.class); + LegacyPropertyFactory.setEnvironment(environment); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false)) + .thenReturn(false); Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC, long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC); engine.setEnvironment(environment); diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java index 4450e91dd..7efbbeee8 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java @@ -26,12 +26,12 @@ import java.util.List; import org.apache.servicecomb.common.rest.locator.OperationLocator; import org.apache.servicecomb.common.rest.locator.ServicePathManager; import org.apache.servicecomb.common.rest.locator.TestPathSchema; -import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.core.BootListener; import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.core.bootstrap.SCBBootstrap; import org.apache.servicecomb.core.executor.ExecutorManager; import org.apache.servicecomb.core.transport.TransportManager; +import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.junit.jupiter.api.AfterAll; @@ -48,7 +48,6 @@ public class TestRestEngineSchemaListener { @BeforeAll public static void setup() { - ConfigUtil.installDynamicConfig(); scbEngine = SCBBootstrap.createSCBEngineForTest(); ExecutorManager executorManager = Mockito.mock(ExecutorManager.class); TransportManager transportManager = Mockito.mock(TransportManager.class); @@ -56,6 +55,9 @@ public class TestRestEngineSchemaListener { scbEngine.setExecutorManager(executorManager); Environment environment = Mockito.mock(Environment.class); scbEngine.setEnvironment(environment); + LegacyPropertyFactory.setEnvironment(environment); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false)) + .thenReturn(false); Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC, long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC); List<BootListener> listeners = new ArrayList<>(); diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java index dafb99949..6939974ff 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java @@ -62,6 +62,8 @@ public class TestRestCodec { @BeforeAll public static void beforeClass() { LegacyPropertyFactory.setEnvironment(environment); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false)) + .thenReturn(false); Mockito.when(environment.getProperty("servicecomb.rest.parameter.query.emptyAsNull", boolean.class, false)) .thenReturn(false); Mockito.when(environment.getProperty("servicecomb.rest.parameter.query.ignoreDefaultValue", boolean.class, false)) diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java index 340b7f15c..3debd1645 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessor.java @@ -30,12 +30,14 @@ import org.apache.servicecomb.common.rest.codec.param.BodyProcessorCreator.BodyP import org.apache.servicecomb.common.rest.codec.param.BodyProcessorCreator.RawJsonBodyProcessor; import org.apache.servicecomb.core.definition.OperationMeta; import org.apache.servicecomb.core.definition.SchemaMeta; +import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.apache.servicecomb.foundation.vertx.stream.BufferInputStream; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.springframework.core.env.Environment; import com.fasterxml.jackson.databind.type.TypeFactory; @@ -54,6 +56,7 @@ import jakarta.ws.rs.core.MediaType; public class TestBodyProcessor { + Environment environment = Mockito.mock(Environment.class); final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); @@ -112,6 +115,9 @@ public class TestBodyProcessor { @BeforeEach public void before() { + LegacyPropertyFactory.setEnvironment(environment); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false)) + .thenReturn(false); headers = new HeadersMultiMap(); value = "value"; } diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessorCreator.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessorCreator.java index 0de4a04c4..22d7e8d39 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessorCreator.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestBodyProcessorCreator.java @@ -21,9 +21,13 @@ import java.util.HashMap; import org.apache.servicecomb.common.rest.codec.param.BodyProcessorCreator.BodyProcessor; import org.apache.servicecomb.common.rest.codec.param.BodyProcessorCreator.RawJsonBodyProcessor; +import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.apache.servicecomb.swagger.generator.SwaggerConst; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.core.env.Environment; import io.swagger.v3.oas.models.media.Content; import io.swagger.v3.oas.models.media.MediaType; @@ -32,6 +36,15 @@ import io.swagger.v3.oas.models.parameters.RequestBody; @SuppressWarnings({"rawtypes", "unchecked"}) public class TestBodyProcessorCreator { + Environment environment = Mockito.mock(Environment.class); + + @BeforeEach + public void before() { + LegacyPropertyFactory.setEnvironment(environment); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false)) + .thenReturn(false); + } + @Test public void testCreateNormal() { ParamValueProcessorCreator creator = diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java index 5195aa5cd..39bed2e80 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestHeaderProcessor.java @@ -26,16 +26,17 @@ import java.util.List; import java.util.Map; import java.util.Set; -import jakarta.servlet.http.HttpServletRequest; - import org.apache.servicecomb.common.rest.codec.RestClientRequest; import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory; import org.apache.servicecomb.common.rest.codec.param.HeaderProcessorCreator.HeaderProcessor; +import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.springframework.core.env.Environment; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.type.TypeFactory; @@ -44,6 +45,7 @@ import com.fasterxml.jackson.databind.util.StdDateFormat; import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.parameters.HeaderParameter; +import jakarta.servlet.http.HttpServletRequest; public class TestHeaderProcessor { final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); @@ -71,6 +73,15 @@ public class TestHeaderProcessor { return new HeaderProcessor(headerParameter, javaType); } + Environment environment = Mockito.mock(Environment.class); + + @BeforeEach + void setUp() { + LegacyPropertyFactory.setEnvironment(environment); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.header.ignoreRequiredCheck" + , boolean.class, false)).thenReturn(false); + } + @Test public void testGetValueNormal() throws Exception { Mockito.when(request.getHeader("h1")).thenReturn("h1v"); diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/query/QueryCodecMultiTest.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/query/QueryCodecMultiTest.java index 759b74d69..3c56d1b1c 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/query/QueryCodecMultiTest.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/query/QueryCodecMultiTest.java @@ -19,13 +19,27 @@ package org.apache.servicecomb.common.rest.codec.query; import java.util.Date; +import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.core.env.Environment; class QueryCodecMultiTest extends QueryCodecTestBase { + Environment environment = Mockito.mock(Environment.class); + @BeforeEach void setUp() { + LegacyPropertyFactory.setEnvironment(environment); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false)) + .thenReturn(false); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.query.emptyAsNull", boolean.class, false)) + .thenReturn(false); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.query.ignoreDefaultValue", boolean.class, false)) + .thenReturn(false); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.query.ignoreRequiredCheck", boolean.class, false)) + .thenReturn(false); codec = new QueryCodecMulti(); } diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/locator/TestMicroservicePaths.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/locator/TestMicroservicePaths.java index b4af7c80a..442368b0a 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/locator/TestMicroservicePaths.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/locator/TestMicroservicePaths.java @@ -25,12 +25,12 @@ import java.util.List; import org.apache.servicecomb.common.rest.RestEngineSchemaListener; import org.apache.servicecomb.common.rest.definition.RestOperationMeta; -import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.core.BootListener; import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.core.bootstrap.SCBBootstrap; import org.apache.servicecomb.core.executor.ExecutorManager; import org.apache.servicecomb.core.transport.TransportManager; +import org.apache.servicecomb.foundation.common.LegacyPropertyFactory; import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.junit.jupiter.api.AfterAll; @@ -47,12 +47,14 @@ public class TestMicroservicePaths { @BeforeAll public static void setup() { - ConfigUtil.installDynamicConfig(); scbEngine = SCBBootstrap.createSCBEngineForTest(); Environment environment = Mockito.mock(Environment.class); + LegacyPropertyFactory.setEnvironment(environment); scbEngine.setEnvironment(environment); Mockito.when(environment.getProperty(CFG_KEY_TURN_DOWN_STATUS_WAIT_SEC, long.class, DEFAULT_TURN_DOWN_STATUS_WAIT_SEC)).thenReturn(DEFAULT_TURN_DOWN_STATUS_WAIT_SEC); + Mockito.when(environment.getProperty("servicecomb.rest.parameter.decodeAsObject", boolean.class, false)) + .thenReturn(false); List<BootListener> listeners = new ArrayList<>(); listeners.add(new RestEngineSchemaListener()); ExecutorManager executorManager = Mockito.mock(ExecutorManager.class); diff --git a/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java b/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java index ea4d8662f..2fc689efb 100644 --- a/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java +++ b/core/src/test/java/org/apache/servicecomb/core/executor/TestExecutorManager.java @@ -21,7 +21,6 @@ import java.util.concurrent.Executor; import org.apache.servicecomb.core.definition.OperationMeta; import org.apache.servicecomb.core.definition.SchemaMeta; import org.apache.servicecomb.foundation.common.utils.BeanUtils; -import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -43,12 +42,10 @@ public class TestExecutorManager { @Before public void setup() { executorManager.setEnvironment(environment); - ArchaiusUtils.resetConfig(); } @After public void teardown() { - ArchaiusUtils.resetConfig(); } @Test @@ -70,7 +67,7 @@ public class TestExecutorManager { String microserviceQualifiedName = "microserviceName.schemaId.opId"; String opBeanId = "opBeanId"; Mockito.when(environment.getProperty( - ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName, String.class)).thenReturn(opBeanId); + ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName)).thenReturn(opBeanId); new Expectations(BeanUtils.class) { { operationMeta.getMicroserviceQualifiedName(); @@ -90,7 +87,7 @@ public class TestExecutorManager { String microserviceQualifiedName = "microserviceName.schemaId.opId"; String opBeanId = "opBeanId"; Mockito.when(environment.getProperty( - ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName, String.class)).thenReturn(opBeanId); + ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceQualifiedName)).thenReturn(opBeanId); new Expectations(BeanUtils.class) { { operationMeta.getMicroserviceQualifiedName(); @@ -115,8 +112,8 @@ public class TestExecutorManager { String microserviceName = "serviceName"; String schemaName = "schemaId"; String opBeanId = "opBeanId"; - Mockito.when(environment.getProperty(ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceName + "." + schemaName - , String.class)).thenReturn(opBeanId); + Mockito.when(environment.getProperty(ExecutorManager.KEY_EXECUTORS_PREFIX + microserviceName + "." + schemaName)) + .thenReturn(opBeanId); new Expectations(BeanUtils.class) { { operationMeta.getSchemaId(); @@ -135,8 +132,7 @@ public class TestExecutorManager { @Mocked SchemaMeta schemaMeta, @Mocked OperationMeta operationMeta) { String beanId = "beanId"; - Mockito.when(environment.getProperty(ExecutorManager.KEY_EXECUTORS_DEFAULT - , String.class)).thenReturn(beanId); + Mockito.when(environment.getProperty(ExecutorManager.KEY_EXECUTORS_DEFAULT)).thenReturn(beanId); new Expectations(BeanUtils.class) { { BeanUtils.getBean(beanId); diff --git a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CodeFirstRestTemplate.java b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CodeFirstRestTemplate.java index 25e118c01..dfdafb0d8 100644 --- a/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CodeFirstRestTemplate.java +++ b/demo/demo-schema/src/main/java/org/apache/servicecomb/demo/CodeFirstRestTemplate.java @@ -21,13 +21,13 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; +import org.apache.servicecomb.config.InMemoryDynamicPropertiesSource; import org.apache.servicecomb.config.MicroserviceProperties; import org.apache.servicecomb.core.CoreConst; import org.apache.servicecomb.demo.compute.Person; import org.apache.servicecomb.demo.ignore.InputModelForTestIgnore; import org.apache.servicecomb.demo.ignore.OutputModelForTestIgnore; import org.apache.servicecomb.demo.server.User; -import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.swagger.invocation.context.ContextUtils; import org.apache.servicecomb.swagger.invocation.context.InvocationContext; import org.springframework.beans.factory.annotation.Autowired; @@ -50,7 +50,7 @@ public class CodeFirstRestTemplate { } protected void changeTransport(String microserviceName, String transport) { - ArchaiusUtils.setProperty("servicecomb.references.transport." + microserviceName, transport); + InMemoryDynamicPropertiesSource.DYNAMIC.put("servicecomb.references.transport." + microserviceName, transport); TestMgr.setMsg(microserviceName, transport); } diff --git a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java b/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java index 125d80a30..119bc73ca 100644 --- a/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java +++ b/dynamic-config/config-nacos/src/test/java/org/apache/servicecomb/config/nacos/NacosClientTest.java @@ -23,6 +23,7 @@ import java.util.Map; import org.apache.servicecomb.config.nacos.NacosDynamicPropertiesSource.UpdateHandler; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; public class NacosClientTest { @@ -35,6 +36,9 @@ public class NacosClientTest { NacosDynamicPropertiesSource impl = new NacosDynamicPropertiesSource(); UpdateHandler updateHandler = impl.new UpdateHandler(); NacosClient nacosClient = new NacosClient(updateHandler); + NacosConfig nacosConfig = Mockito.mock(NacosConfig.class); + Mockito.when(nacosConfig.getContentType()).thenReturn("yaml"); + nacosClient.setNacosConfig(nacosConfig); NacosClient.ConfigRefresh cr = nacosClient.new ConfigRefresh(); diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java index 81ed8ed00..6e9d9abcd 100644 --- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/BootStrapProperties.java @@ -193,13 +193,15 @@ public class BootStrapProperties { } private static Map<String, String> readProperties(Environment environment, String newKey, String oldKey) { - Set<String> keys = ConfigUtil.propertiesWithPrefix(environment, newKey); + String prefix = newKey; + Set<String> keys = ConfigUtil.propertiesWithPrefix(environment, prefix); if (keys.isEmpty()) { + prefix = oldKey; keys = ConfigUtil.propertiesWithPrefix(environment, oldKey); } Map<String, String> result = new HashMap<>(keys.size()); for (String key : keys) { - result.put(key, environment.getProperty(key)); + result.put(key.substring(prefix.length() + 1), environment.getProperty(key)); } return result; } diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/FoundationConfigConfiguration.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/FoundationConfigConfiguration.java index 9e521b091..91f49c8a1 100644 --- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/FoundationConfigConfiguration.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/FoundationConfigConfiguration.java @@ -75,4 +75,9 @@ public class FoundationConfigConfiguration { public LegacyPropertyFactory legacyPropertyFactory(Environment environment) { return new LegacyPropertyFactory(environment); } + + @Bean + public InMemoryDynamicPropertiesSource inMemoryDynamicPropertiesSource() { + return new InMemoryDynamicPropertiesSource(); + } } diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroservicePropertiesLoader.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/InMemoryDynamicPropertiesSource.java similarity index 55% copy from service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroservicePropertiesLoader.java copy to foundations/foundation-config/src/main/java/org/apache/servicecomb/config/InMemoryDynamicPropertiesSource.java index 23adef899..515d5ae82 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroservicePropertiesLoader.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/InMemoryDynamicPropertiesSource.java @@ -14,28 +14,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.apache.servicecomb.config; -package org.apache.servicecomb.registry.lightweight.model; - +import java.util.HashMap; import java.util.Map; -import org.apache.commons.configuration.Configuration; -import org.apache.servicecomb.config.BootStrapProperties; - -public final class MicroservicePropertiesLoader extends AbstractPropertiesLoader { +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; - public static final MicroservicePropertiesLoader INSTANCE = new MicroservicePropertiesLoader(); +public class InMemoryDynamicPropertiesSource implements DynamicPropertiesSource<Map<String, Object>> { + public static final String SOURCE_NAME = "in-memory"; - private MicroservicePropertiesLoader() { - } + public static final Map<String, Object> DYNAMIC = new HashMap<>(); @Override - protected Map<String, String> readProperties(Configuration configuration) { - return BootStrapProperties.readServiceProperties(configuration); + public EnumerablePropertySource<Map<String, Object>> create(Environment environment) { + return new MapPropertySource(SOURCE_NAME, DYNAMIC); } @Override - protected String readPropertiesExtendedClass(Configuration configuration) { - return BootStrapProperties.readServiceExtendedClass(configuration); + public int getOrder() { + return -100; } } diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java index 1513ae6ad..996cbcdb8 100644 --- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java +++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/BootStrapPropertiesTest.java @@ -20,20 +20,33 @@ package org.apache.servicecomb.config; import static org.apache.servicecomb.foundation.test.scaffolding.AssertUtils.assertPrettyJson; import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; -class BootStrapPropertiesTest { +public class BootStrapPropertiesTest { private Map<String, String> readInstanceProperties(String yaml) { Map<String, Object> properties = YAMLUtil.yaml2Properties(yaml); ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class); + MutablePropertySources mutablePropertySources = new MutablePropertySources(); + EnumerablePropertySource propertySource = new MapPropertySource("yaml", properties); + mutablePropertySources.addLast(propertySource); + Mockito.when(environment.getPropertySources()).thenReturn(mutablePropertySources); + + List<String> keys = new ArrayList<>(); for (Entry<String, Object> entry : properties.entrySet()) { Mockito.when(environment.getProperty(entry.getKey())).thenReturn(entry.getValue().toString()); + keys.add(entry.getKey()); } + return BootStrapProperties.readServiceInstanceProperties(environment); } @@ -72,51 +85,6 @@ class BootStrapPropertiesTest { + "}"); } - @Test - void should_resolve_placeholder_for_instance_properties() { - Map<String, String> properties = readInstanceProperties("" - + "k1: new\n" - + "servicecomb:\n" - + " instance:\n" - + " properties:\n" - + " k: ${k1}"); - - assertPrettyJson(properties).isEqualTo("" - + "{\n" - + " \"k\" : \"new\"\n" - + "}"); - } - - @Test - void should_resolve_boolean_to_string() { - Map<String, String> properties = readInstanceProperties("" - + "k: true\n" - + "servicecomb:\n" - + " instance:\n" - + " properties:\n" - + " k: ${k}"); - - assertPrettyJson(properties).isEqualTo("" - + "{\n" - + " \"k\" : \"true\"\n" - + "}"); - } - - @Test - void should_resolve_number_to_string() { - Map<String, String> properties = readInstanceProperties("" - + "k: 1\n" - + "servicecomb:\n" - + " instance:\n" - + " properties:\n" - + " k: ${k}"); - - assertPrettyJson(properties).isEqualTo("" - + "{\n" - + " \"k\" : \"1\"\n" - + "}"); - } - @Test void should_read_by_old_prefix_when_new_prefix_not_exists() { Map<String, String> properties = readInstanceProperties("" @@ -127,8 +95,8 @@ class BootStrapPropertiesTest { assertPrettyJson(properties).isEqualTo("" + "{\n" - + " \"k\" : \"v\",\n" - + " \"k1\" : \"v1\"\n" + + " \"k1\" : \"v1\",\n" + + " \"k\" : \"v\"\n" + "}"); } diff --git a/huawei-cloud/servicestage/src/test/java/org/apache/servicecomb/huaweicloud/servicestage/TestCasEnvConfig.java b/huawei-cloud/servicestage/src/test/java/org/apache/servicecomb/huaweicloud/servicestage/TestCasEnvConfig.java deleted file mode 100644 index 10ebf6f39..000000000 --- a/huawei-cloud/servicestage/src/test/java/org/apache/servicecomb/huaweicloud/servicestage/TestCasEnvConfig.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicecomb.huaweicloud.servicestage; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -public class TestCasEnvConfig { - - @BeforeAll - public static void init() { - System.setProperty("CAS_APPLICATION_ID", "application-id"); - System.setProperty("CAS_ENVIRONMENT_ID", "env-id"); - System.setProperty("SERVICECOMB_SERVICE_PROPS", "component:ConsumerService,other:A"); - System.setProperty("SERVICECOMB_INSTANCE_PROPS", "route:gray"); - } - - @Test - public void testConfig() { - CasEnvConfig instance = CasEnvConfig.INSTANCE; - Assertions.assertEquals(3, instance.getNonEmptyInstanceProperties().size()); - Assertions.assertEquals("application-id", instance.getNonEmptyInstanceProperties().get("CAS_APPLICATION_ID")); - Assertions.assertEquals("env-id", instance.getNonEmptyInstanceProperties().get("CAS_ENVIRONMENT_ID")); - Assertions.assertEquals("gray", instance.getNonEmptyInstanceProperties().get("route")); - - - Assertions.assertEquals(2, instance.getNonEmptyServiceProperties().size()); - Assertions.assertEquals("ConsumerService", instance.getNonEmptyServiceProperties().get("component")); - Assertions.assertEquals("A", instance.getNonEmptyServiceProperties().get("other")); - } - - @AfterAll - public static void destroy() { - System.getProperties().remove("CAS_ENVIRONMENT_ID"); - System.getProperties().remove("CAS_APPLICATION_ID"); - System.getProperties().remove("SERVICECOMB_SERVICE_PROPS"); - System.getProperties().remove("SERVICECOMB_INSTANCE_PROPS"); - } -} diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroservicePropertiesLoader.java b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroservicePropertiesLoader.java index 23adef899..ed6d181fb 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroservicePropertiesLoader.java +++ b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/model/MicroservicePropertiesLoader.java @@ -19,8 +19,8 @@ package org.apache.servicecomb.registry.lightweight.model; import java.util.Map; -import org.apache.commons.configuration.Configuration; import org.apache.servicecomb.config.BootStrapProperties; +import org.springframework.core.env.Environment; public final class MicroservicePropertiesLoader extends AbstractPropertiesLoader { @@ -30,12 +30,12 @@ public final class MicroservicePropertiesLoader extends AbstractPropertiesLoader } @Override - protected Map<String, String> readProperties(Configuration configuration) { - return BootStrapProperties.readServiceProperties(configuration); + protected Map<String, String> readProperties(Environment environment) { + return BootStrapProperties.readServiceProperties(environment); } @Override - protected String readPropertiesExtendedClass(Configuration configuration) { - return BootStrapProperties.readServiceExtendedClass(configuration); + protected String readPropertiesExtendedClass(Environment environment) { + return BootStrapProperties.readServiceExtendedClass(environment); } } diff --git a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java index 9f7692e87..4e3ceafdc 100644 --- a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java +++ b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/config/TestAbstractPropertiesLoader.java @@ -23,12 +23,20 @@ import org.apache.servicecomb.registry.lightweight.model.MicroservicePropertiesL import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import org.springframework.core.env.Environment; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.MutablePropertySources; public class TestAbstractPropertiesLoader { @Test public void testExtendedClassCompatible() { - Environment environment = Mockito.mock(Environment.class); + ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class); + MutablePropertySources mutablePropertySources = new MutablePropertySources(); + EnumerablePropertySource<?> propertySource = Mockito.mock(EnumerablePropertySource.class); + mutablePropertySources.addLast(propertySource); + Mockito.when(environment.getPropertySources()).thenReturn(mutablePropertySources); + Mockito.when(propertySource.getPropertyNames()).thenReturn(new String[] {}); + Mockito.when(environment.getProperty(BootStrapProperties.CONFIG_SERVICE_EXTENDED_CLASS)) .thenReturn("invalidClass"); AbstractPropertiesLoader loader = MicroservicePropertiesLoader.INSTANCE; diff --git a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java index 05a49928d..bab02249c 100644 --- a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java +++ b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java @@ -21,6 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.concurrent.CompletableFuture; +import org.apache.servicecomb.core.Endpoint; import org.apache.servicecomb.foundation.test.scaffolding.time.MockTicker; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.lightweight.model.Microservice; @@ -30,12 +31,28 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.MutablePropertySources; import com.google.common.eventbus.EventBus; import io.vertx.core.json.Json; -class StoreServiceTest extends TestBase { +public class StoreServiceTest { + static Endpoint endpoint = Mockito.mock(Endpoint.class); + + static class MockRegisterRequest extends RegisterRequest { + @Override + public Endpoint selectFirstEndpoint() { + return endpoint; + } + } + + static ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class); + + Self self; + EventBus eventBus = new EventBus(); MockTicker ticker = new MockTicker(); @@ -48,6 +65,26 @@ class StoreServiceTest extends TestBase { @BeforeEach void setUp() { + MutablePropertySources mutablePropertySources = new MutablePropertySources(); + EnumerablePropertySource<?> propertySource = Mockito.mock(EnumerablePropertySource.class); + mutablePropertySources.addLast(propertySource); + Mockito.when(environment.getPropertySources()).thenReturn(mutablePropertySources); + Mockito.when(propertySource.getPropertyNames()).thenReturn(new String[] {}); + Mockito.when(environment.getProperty("servicecomb.service.application")).thenReturn("app"); + Mockito.when(environment.getProperty("servicecomb.service.name")).thenReturn("svc"); + Mockito.when(environment.getProperty("servicecomb.service.version")).thenReturn("1.0.0.0"); + Mockito.when(environment.getProperty("servicecomb.instance.initialStatus")).thenReturn("UP"); + + self = new Self() { + @Override + protected RegisterRequest createRegisterRequest() { + return new MockRegisterRequest(); + } + } + .init(environment) + .addSchema("schema-1", "s1") + .addEndpoint("rest://1.1.1.1:80"); + Mockito.when(discoveryClient.getInfoAsync(ArgumentMatchers.any(), ArgumentMatchers.any())) .thenReturn(CompletableFuture.completedFuture(self.getMicroserviceInfo())); Mockito.when(discoveryClient.getInstanceAsync(ArgumentMatchers.any(), ArgumentMatchers.any())) diff --git a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/TestBase.java b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/TestBase.java deleted file mode 100644 index bc96fa160..000000000 --- a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/TestBase.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicecomb.registry.lightweight; - -import java.util.Map; - -import org.apache.commons.configuration.AbstractConfiguration; -import org.apache.commons.configuration.MapConfiguration; -import org.apache.servicecomb.config.YAMLUtil; -import org.apache.servicecomb.core.Endpoint; -import org.mockito.Mockito; - -public class TestBase { - static Endpoint endpoint = Mockito.mock(Endpoint.class); - - static class MockRegisterRequest extends RegisterRequest { - @Override - public Endpoint selectFirstEndpoint() { - return endpoint; - } - } - - static AbstractConfiguration configuration = build(); - - static AbstractConfiguration build() { - Map<String, Object> map = YAMLUtil.yaml2Properties("" - + "servicecomb:\n" - + " service:\n" - + " application: app\n" - + " name: svc\n" - + " version: 1.0.0.0"); - return new MapConfiguration(map); - } - - Self self = new Self() { - @Override - protected RegisterRequest createRegisterRequest() { - return new MockRegisterRequest(); - } - } - .init(configuration) - .addSchema("schema-1", "s1") - .addEndpoint("rest://1.1.1.1:80"); -}