This is an automated email from the ASF dual-hosted git repository. jianbin pushed a commit to branch 2.x in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push: new 3bb8f73a41 test: add Apollo mock test case (#6764) 3bb8f73a41 is described below commit 3bb8f73a41a50c7424cc93fa79261e20d53cb96d Author: jimin <sliev...@163.com> AuthorDate: Wed Aug 28 16:36:00 2024 +0800 test: add Apollo mock test case (#6764) --- changes/en-us/2.x.md | 2 + changes/zh-cn/2.x.md | 1 + config/seata-config-apollo/pom.xml | 10 ++ .../config/apollo/ApolloConfigurationTest.java | 113 +++++++++++++++++++++ .../seata/config/apollo/ApolloMockServer.java | 107 +++++++++++++++++++ .../org.apache.seata.config.ConfigurationProvider | 17 ++++ .../src/test/resources/mock-application.properties | 17 ++++ .../src/test/resources/registry-test.conf | 84 +++++++++++++++ dependencies/pom.xml | 1 + 9 files changed, 352 insertions(+) diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md index d906ae6606..87ce714321 100644 --- a/changes/en-us/2.x.md +++ b/changes/en-us/2.x.md @@ -81,9 +81,11 @@ Add changes here for all PR submitted to the 2.x branch. - [[#6608](https://github.com/apache/incubator-seata/pull/6608)] add unit test for sql-parser-core - [[#6647](https://github.com/apache/incubator-seata/pull/6647)] improve the test case coverage of saga module to 70% - [[#6695](https://github.com/apache/incubator-seata/pull/6695)] old version(< 0.7.1) client test case for multi-version protocol +- [[#6764](https://github.com/apache/incubator-seata/pull/6764)] add Apollo mock test case - [[#6750](https://github.com/apache/incubator-seata/pull/6750)] increase spring autoconfigure module unit test converage - [[#6773](https://github.com/apache/incubator-seata/pull/6773)] fix the wrong code coverage from codecov icon in default branch + Thanks to these contributors for their code commits. Please report an unintended omission. <!-- Please make sure your Github ID is in the list below --> diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md index e5d0684c41..11ee91bcba 100644 --- a/changes/zh-cn/2.x.md +++ b/changes/zh-cn/2.x.md @@ -84,6 +84,7 @@ - [[#6608](https://github.com/apache/incubator-seata/pull/6608)] 添加sql-parser-core模块测试用例 - [[#6647](https://github.com/apache/incubator-seata/pull/6647)] 增加saga模块的测试用例覆盖率 - [[#6695](https://github.com/apache/incubator-seata/pull/6695)] 多版本协议的旧版本(< 0.7.1)客户端测试用例 +- [[#6764](https://github.com/apache/incubator-seata/pull/6764)] 增加 Apollo Mock 测试用例 - [[#6750](https://github.com/apache/incubator-seata/pull/6750)] 提升spring autoconfigure模块单测覆盖率 - [[#6773](https://github.com/apache/incubator-seata/pull/6773)] 修复codecov图标显示错误的代码覆盖率 diff --git a/config/seata-config-apollo/pom.xml b/config/seata-config-apollo/pom.xml index 16d8eb80be..2515fb9db2 100644 --- a/config/seata-config-apollo/pom.xml +++ b/config/seata-config-apollo/pom.xml @@ -39,6 +39,16 @@ <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> </dependency> + <dependency> + <groupId>com.squareup.okhttp3</groupId> + <artifactId>mockwebserver</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/config/seata-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloConfigurationTest.java b/config/seata-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloConfigurationTest.java new file mode 100644 index 0000000000..aff6785a55 --- /dev/null +++ b/config/seata-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloConfigurationTest.java @@ -0,0 +1,113 @@ +/* + * 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.seata.config.apollo; + +import java.io.IOException; + +import org.apache.seata.common.exception.NotSupportYetException; +import org.apache.seata.config.ConfigurationChangeEvent; +import org.apache.seata.config.ConfigurationChangeListener; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +/** + * The type Apollo configuration test. + */ +public class ApolloConfigurationTest { + + private static final int PORT = 8081; + private static ApolloMockServer apolloMockServer; + + private static ApolloConfiguration apolloConfiguration; + + /** + * Sets up. + * + * @throws IOException the io exception + */ + @BeforeAll + public static void setUp() throws IOException { + System.setProperty("seataEnv", "test"); + apolloMockServer = new ApolloMockServer(PORT); + apolloConfiguration = ApolloConfiguration.getInstance(); + } + + /** + * Test get config. + */ + @Test + public void testGetConfig() { + String value = apolloConfiguration.getConfig("seata.test"); + Assertions.assertEquals("mockdata", value); + value = apolloConfiguration.getConfig("seata.key"); + Assertions.assertNull(value); + value = apolloConfiguration.getConfig("seata.key.1", "default"); + Assertions.assertEquals("default", value); + value = apolloConfiguration.getLatestConfig("seata.key.2", "default", 3000); + Assertions.assertEquals("default", value); + } + + /** + * Test update config. + */ + @Test + public void testUpdateConfig() { + Assertions.assertThrows(NotSupportYetException.class, () -> { + apolloConfiguration.putConfig("seata.test", "mockdata"); + }); + Assertions.assertThrows(NotSupportYetException.class, () -> { + apolloConfiguration.putConfigIfAbsent("seata.test", "mockdata"); + }); + Assertions.assertThrows(NotSupportYetException.class, () -> { + apolloConfiguration.removeConfig("seata.test"); + }); + } + + /** + * Test listener. + */ + @Test + public void testListener() { + ConfigurationChangeListener listener = new ConfigurationChangeListener() { + @Override + public void onChangeEvent(ConfigurationChangeEvent event) { + + } + }; + apolloConfiguration.addConfigListener("seata.test", listener); + Assertions.assertEquals(1, apolloConfiguration.getConfigListeners("seata.test").size()); + apolloConfiguration.removeConfigListener("seata.test", null); + Assertions.assertEquals(1, apolloConfiguration.getConfigListeners("seata.test").size()); + apolloConfiguration.removeConfigListener("seata.test", listener); + Assertions.assertEquals(0, apolloConfiguration.getConfigListeners("seata.test").size()); + + } + + /** + * Tear down. + * + * @throws IOException the io exception + */ + @AfterAll + public static void tearDown() throws IOException { + System.clearProperty("seataEnv"); + apolloMockServer.stop(); + } + +} diff --git a/config/seata-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloMockServer.java b/config/seata-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloMockServer.java new file mode 100644 index 0000000000..aa08722177 --- /dev/null +++ b/config/seata-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloMockServer.java @@ -0,0 +1,107 @@ +/* + * 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.seata.config.apollo; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import com.ctrip.framework.apollo.core.dto.ApolloConfig; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; + +/** + * The type Apollo mock server. + */ +public class ApolloMockServer { + + private MockWebServer server; + private final ObjectMapper mapper = new ObjectMapper(); + + private final String CONFIG_PREFIX_PATH = "/configs"; + + /** + * Instantiates a new Apollo mock server. + * + * @param port the port + * @throws IOException the io exception + */ + public ApolloMockServer(int port) throws IOException { + + server = new MockWebServer(); + server.setDispatcher(new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) throws InterruptedException { + if (request.getPath().startsWith(CONFIG_PREFIX_PATH)) { + List<String> pathSegments = request.getRequestUrl().pathSegments(); + String appId = pathSegments.get(1); + String cluster = pathSegments.get(2); + String namespace = pathSegments.get(3); + String result; + try { + result = loadMockData(appId, cluster, namespace); + return new MockResponse().setResponseCode(200).setBody(result); + } catch (JsonProcessingException e) { + } + } + return new MockResponse().setResponseCode(404); + } + }); + server.start(port); + System.setProperty("apollo.configService", "http://localhost:" + port); + + } + + private String loadMockData(String appId, String Cluster, String namespace) throws JsonProcessingException { + String fileName = "mock-" + namespace + ".properties"; + ApolloConfig apolloConfig = new ApolloConfig(appId, Cluster, namespace, "releaseKey"); + Properties properties = new Properties(); + try (InputStream input = this.getClass().getClassLoader().getResourceAsStream(fileName)) { + if (null != input) { + properties.load(input); + } + } catch (Exception ignore) { + } + Map<String, String> configurations = new HashMap<>(); + for (Map.Entry<Object, Object> entry : properties.entrySet()) { + configurations.put(entry.getKey().toString(), entry.getValue().toString()); + } + apolloConfig.setConfigurations(configurations); + String json = mapper.writeValueAsString(apolloConfig); + return json; + + } + + /** + * Stop. + * + * @throws IOException the io exception + */ + public void stop() throws IOException { + if (null != server) { + server.shutdown(); + } + } + +} diff --git a/config/seata-config-apollo/src/test/resources/META-INF/services/org.apache.seata.config.ConfigurationProvider b/config/seata-config-apollo/src/test/resources/META-INF/services/org.apache.seata.config.ConfigurationProvider new file mode 100644 index 0000000000..444de0ac36 --- /dev/null +++ b/config/seata-config-apollo/src/test/resources/META-INF/services/org.apache.seata.config.ConfigurationProvider @@ -0,0 +1,17 @@ +# +# 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. +# +org.apache.seata.config.apollo.ApolloConfigurationProvider \ No newline at end of file diff --git a/config/seata-config-apollo/src/test/resources/mock-application.properties b/config/seata-config-apollo/src/test/resources/mock-application.properties new file mode 100644 index 0000000000..9beea77cc5 --- /dev/null +++ b/config/seata-config-apollo/src/test/resources/mock-application.properties @@ -0,0 +1,17 @@ +# +# 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. +# +seata.test=mockdata \ No newline at end of file diff --git a/config/seata-config-apollo/src/test/resources/registry-test.conf b/config/seata-config-apollo/src/test/resources/registry-test.conf new file mode 100644 index 0000000000..820f726853 --- /dev/null +++ b/config/seata-config-apollo/src/test/resources/registry-test.conf @@ -0,0 +1,84 @@ +# +# 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. +# + +registry { + # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa、custom + type = "file" + + nacos { + application = "seata-server" + serverAddr = "127.0.0.1:8848" + group = "SEATA_GROUP" + namespace = "" + username = "" + password = "" + contextPath = "/foo" + ##if use MSE Nacos with auth, mutex with username/password attribute + #accessKey = "" + #secretKey = "" + ##if use Nacos naming meta-data for SLB service registry, specify nacos address pattern rules here + #slbPattern = "" + } + eureka { + serviceUrl = "http://localhost:8761/eureka" + weight = "1" + } + redis { + serverAddr = "localhost:6379" + db = "0" + password = "" + timeout = "0" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + aclToken = "" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + sofa { + serverAddr = "127.0.0.1:9603" + region = "DEFAULT_ZONE" + datacenter = "DefaultDataCenter" + group = "SEATA_GROUP" + addressWaitTime = "3000" + } + file { + name = "file.conf" + } + custom { + name = "" + } +} + +config { + # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig、custom + type = "apollo" + + apollo { + appId = "seata-server" + apolloMeta = "http://localhost:8801" + namespace = "application" + } +} diff --git a/dependencies/pom.xml b/dependencies/pom.xml index a94309cc3f..d00ac503b7 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -133,6 +133,7 @@ <assertj-core.version>3.12.2</assertj-core.version> <jetty-version>9.4.38.v20210224</jetty-version> <janino-version>3.1.7</janino-version> + <mockwebserver-version>4.12.0</mockwebserver-version> <native-lib-loader.version>2.4.0</native-lib-loader.version> </properties> --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org