Skip to content

Commit 3bb8f73

Browse files
authored
test: add Apollo mock test case (#6764)
1 parent 9483430 commit 3bb8f73

File tree

9 files changed

+352
-0
lines changed

9 files changed

+352
-0
lines changed

changes/en-us/2.x.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ Add changes here for all PR submitted to the 2.x branch.
8181
- [[#6608](https://github.com/apache/incubator-seata/pull/6608)] add unit test for sql-parser-core
8282
- [[#6647](https://github.com/apache/incubator-seata/pull/6647)] improve the test case coverage of saga module to 70%
8383
- [[#6695](https://github.com/apache/incubator-seata/pull/6695)] old version(< 0.7.1) client test case for multi-version protocol
84+
- [[#6764](https://github.com/apache/incubator-seata/pull/6764)] add Apollo mock test case
8485
- [[#6750](https://github.com/apache/incubator-seata/pull/6750)] increase spring autoconfigure module unit test converage
8586
- [[#6773](https://github.com/apache/incubator-seata/pull/6773)] fix the wrong code coverage from codecov icon in default branch
8687

88+
8789
Thanks to these contributors for their code commits. Please report an unintended omission.
8890

8991
<!-- Please make sure your Github ID is in the list below -->

changes/zh-cn/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
- [[#6608](https://github.com/apache/incubator-seata/pull/6608)] 添加sql-parser-core模块测试用例
8585
- [[#6647](https://github.com/apache/incubator-seata/pull/6647)] 增加saga模块的测试用例覆盖率
8686
- [[#6695](https://github.com/apache/incubator-seata/pull/6695)] 多版本协议的旧版本(< 0.7.1)客户端测试用例
87+
- [[#6764](https://github.com/apache/incubator-seata/pull/6764)] 增加 Apollo Mock 测试用例
8788
- [[#6750](https://github.com/apache/incubator-seata/pull/6750)] 提升spring autoconfigure模块单测覆盖率
8889
- [[#6773](https://github.com/apache/incubator-seata/pull/6773)] 修复codecov图标显示错误的代码覆盖率
8990

config/seata-config-apollo/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
<groupId>com.ctrip.framework.apollo</groupId>
4040
<artifactId>apollo-client</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.squareup.okhttp3</groupId>
44+
<artifactId>mockwebserver</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.fasterxml.jackson.core</groupId>
49+
<artifactId>jackson-databind</artifactId>
50+
<scope>test</scope>
51+
</dependency>
4252
</dependencies>
4353

4454
</project>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.config.apollo;
18+
19+
import java.io.IOException;
20+
21+
import org.apache.seata.common.exception.NotSupportYetException;
22+
import org.apache.seata.config.ConfigurationChangeEvent;
23+
import org.apache.seata.config.ConfigurationChangeListener;
24+
import org.junit.jupiter.api.AfterAll;
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
* The type Apollo configuration test.
31+
*/
32+
public class ApolloConfigurationTest {
33+
34+
private static final int PORT = 8081;
35+
private static ApolloMockServer apolloMockServer;
36+
37+
private static ApolloConfiguration apolloConfiguration;
38+
39+
/**
40+
* Sets up.
41+
*
42+
* @throws IOException the io exception
43+
*/
44+
@BeforeAll
45+
public static void setUp() throws IOException {
46+
System.setProperty("seataEnv", "test");
47+
apolloMockServer = new ApolloMockServer(PORT);
48+
apolloConfiguration = ApolloConfiguration.getInstance();
49+
}
50+
51+
/**
52+
* Test get config.
53+
*/
54+
@Test
55+
public void testGetConfig() {
56+
String value = apolloConfiguration.getConfig("seata.test");
57+
Assertions.assertEquals("mockdata", value);
58+
value = apolloConfiguration.getConfig("seata.key");
59+
Assertions.assertNull(value);
60+
value = apolloConfiguration.getConfig("seata.key.1", "default");
61+
Assertions.assertEquals("default", value);
62+
value = apolloConfiguration.getLatestConfig("seata.key.2", "default", 3000);
63+
Assertions.assertEquals("default", value);
64+
}
65+
66+
/**
67+
* Test update config.
68+
*/
69+
@Test
70+
public void testUpdateConfig() {
71+
Assertions.assertThrows(NotSupportYetException.class, () -> {
72+
apolloConfiguration.putConfig("seata.test", "mockdata");
73+
});
74+
Assertions.assertThrows(NotSupportYetException.class, () -> {
75+
apolloConfiguration.putConfigIfAbsent("seata.test", "mockdata");
76+
});
77+
Assertions.assertThrows(NotSupportYetException.class, () -> {
78+
apolloConfiguration.removeConfig("seata.test");
79+
});
80+
}
81+
82+
/**
83+
* Test listener.
84+
*/
85+
@Test
86+
public void testListener() {
87+
ConfigurationChangeListener listener = new ConfigurationChangeListener() {
88+
@Override
89+
public void onChangeEvent(ConfigurationChangeEvent event) {
90+
91+
}
92+
};
93+
apolloConfiguration.addConfigListener("seata.test", listener);
94+
Assertions.assertEquals(1, apolloConfiguration.getConfigListeners("seata.test").size());
95+
apolloConfiguration.removeConfigListener("seata.test", null);
96+
Assertions.assertEquals(1, apolloConfiguration.getConfigListeners("seata.test").size());
97+
apolloConfiguration.removeConfigListener("seata.test", listener);
98+
Assertions.assertEquals(0, apolloConfiguration.getConfigListeners("seata.test").size());
99+
100+
}
101+
102+
/**
103+
* Tear down.
104+
*
105+
* @throws IOException the io exception
106+
*/
107+
@AfterAll
108+
public static void tearDown() throws IOException {
109+
System.clearProperty("seataEnv");
110+
apolloMockServer.stop();
111+
}
112+
113+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.config.apollo;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Properties;
25+
26+
import com.ctrip.framework.apollo.core.dto.ApolloConfig;
27+
import com.fasterxml.jackson.core.JsonProcessingException;
28+
import com.fasterxml.jackson.databind.ObjectMapper;
29+
import okhttp3.mockwebserver.Dispatcher;
30+
import okhttp3.mockwebserver.MockResponse;
31+
import okhttp3.mockwebserver.MockWebServer;
32+
import okhttp3.mockwebserver.RecordedRequest;
33+
34+
/**
35+
* The type Apollo mock server.
36+
*/
37+
public class ApolloMockServer {
38+
39+
private MockWebServer server;
40+
private final ObjectMapper mapper = new ObjectMapper();
41+
42+
private final String CONFIG_PREFIX_PATH = "/configs";
43+
44+
/**
45+
* Instantiates a new Apollo mock server.
46+
*
47+
* @param port the port
48+
* @throws IOException the io exception
49+
*/
50+
public ApolloMockServer(int port) throws IOException {
51+
52+
server = new MockWebServer();
53+
server.setDispatcher(new Dispatcher() {
54+
@Override
55+
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
56+
if (request.getPath().startsWith(CONFIG_PREFIX_PATH)) {
57+
List<String> pathSegments = request.getRequestUrl().pathSegments();
58+
String appId = pathSegments.get(1);
59+
String cluster = pathSegments.get(2);
60+
String namespace = pathSegments.get(3);
61+
String result;
62+
try {
63+
result = loadMockData(appId, cluster, namespace);
64+
return new MockResponse().setResponseCode(200).setBody(result);
65+
} catch (JsonProcessingException e) {
66+
}
67+
}
68+
return new MockResponse().setResponseCode(404);
69+
}
70+
});
71+
server.start(port);
72+
System.setProperty("apollo.configService", "http://localhost:" + port);
73+
74+
}
75+
76+
private String loadMockData(String appId, String Cluster, String namespace) throws JsonProcessingException {
77+
String fileName = "mock-" + namespace + ".properties";
78+
ApolloConfig apolloConfig = new ApolloConfig(appId, Cluster, namespace, "releaseKey");
79+
Properties properties = new Properties();
80+
try (InputStream input = this.getClass().getClassLoader().getResourceAsStream(fileName)) {
81+
if (null != input) {
82+
properties.load(input);
83+
}
84+
} catch (Exception ignore) {
85+
}
86+
Map<String, String> configurations = new HashMap<>();
87+
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
88+
configurations.put(entry.getKey().toString(), entry.getValue().toString());
89+
}
90+
apolloConfig.setConfigurations(configurations);
91+
String json = mapper.writeValueAsString(apolloConfig);
92+
return json;
93+
94+
}
95+
96+
/**
97+
* Stop.
98+
*
99+
* @throws IOException the io exception
100+
*/
101+
public void stop() throws IOException {
102+
if (null != server) {
103+
server.shutdown();
104+
}
105+
}
106+
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
org.apache.seata.config.apollo.ApolloConfigurationProvider
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
seata.test=mockdata
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
registry {
19+
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa、custom
20+
type = "file"
21+
22+
nacos {
23+
application = "seata-server"
24+
serverAddr = "127.0.0.1:8848"
25+
group = "SEATA_GROUP"
26+
namespace = ""
27+
username = ""
28+
password = ""
29+
contextPath = "/foo"
30+
##if use MSE Nacos with auth, mutex with username/password attribute
31+
#accessKey = ""
32+
#secretKey = ""
33+
##if use Nacos naming meta-data for SLB service registry, specify nacos address pattern rules here
34+
#slbPattern = ""
35+
}
36+
eureka {
37+
serviceUrl = "http://localhost:8761/eureka"
38+
weight = "1"
39+
}
40+
redis {
41+
serverAddr = "localhost:6379"
42+
db = "0"
43+
password = ""
44+
timeout = "0"
45+
}
46+
zk {
47+
serverAddr = "127.0.0.1:2181"
48+
sessionTimeout = 6000
49+
connectTimeout = 2000
50+
username = ""
51+
password = ""
52+
}
53+
consul {
54+
serverAddr = "127.0.0.1:8500"
55+
aclToken = ""
56+
}
57+
etcd3 {
58+
serverAddr = "http://localhost:2379"
59+
}
60+
sofa {
61+
serverAddr = "127.0.0.1:9603"
62+
region = "DEFAULT_ZONE"
63+
datacenter = "DefaultDataCenter"
64+
group = "SEATA_GROUP"
65+
addressWaitTime = "3000"
66+
}
67+
file {
68+
name = "file.conf"
69+
}
70+
custom {
71+
name = ""
72+
}
73+
}
74+
75+
config {
76+
# file、nacos 、apollo、zk、consul、etcd3、springCloudConfig、custom
77+
type = "apollo"
78+
79+
apollo {
80+
appId = "seata-server"
81+
apolloMeta = "http://localhost:8801"
82+
namespace = "application"
83+
}
84+
}

dependencies/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<assertj-core.version>3.12.2</assertj-core.version>
134134
<jetty-version>9.4.38.v20210224</jetty-version>
135135
<janino-version>3.1.7</janino-version>
136+
<mockwebserver-version>4.12.0</mockwebserver-version>
136137
<native-lib-loader.version>2.4.0</native-lib-loader.version>
137138
</properties>
138139

0 commit comments

Comments
 (0)