-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'apache:2.x' into feature_6760
- Loading branch information
Showing
19 changed files
with
792 additions
and
1,342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...a-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
...ig/seata-config-apollo/src/test/java/org/apache/seata/config/apollo/ApolloMockServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...apollo/src/test/resources/META-INF/services/org.apache.seata.config.ConfigurationProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
17 changes: 17 additions & 0 deletions
17
config/seata-config-apollo/src/test/resources/mock-application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.