Skip to content

Commit ebb31a8

Browse files
committed
add test
1 parent 99cf8fd commit ebb31a8

2 files changed

Lines changed: 84 additions & 5 deletions

File tree

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,21 @@ private ApolloConfig loadApolloConfig() {
249249
ApolloConfig result = response.getBody();
250250
if(result!=null){
251251
ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType());
252+
252253
if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){
253-
ApolloConfig apolloConfig = m_configCache.get()==null?new ApolloConfig():m_configCache.get();
254-
result.setConfigurations(mergeConfigurations(apolloConfig.getConfigurations(),result.getConfigurationChanges()));
254+
255+
Map<String, String> previousConfigurations=null;
256+
257+
ApolloConfig previousConfig = m_configCache.get();
258+
259+
if(previousConfig!=null){
260+
previousConfigurations=previousConfig.getConfigurations();
261+
}
262+
263+
result.setConfigurations(mergeConfigurations(previousConfigurations,result.getConfigurationChanges()));
264+
255265
}
266+
256267
}
257268

258269
logger.debug("Loaded config for {}: {}", m_namespace, result);
@@ -362,10 +373,11 @@ private List<ServiceDTO> getConfigServices() {
362373
return services;
363374
}
364375

365-
private Map<String, String> mergeConfigurations(Map<String, String> configurations,List<ConfigurationChange> configurationChanges) {
376+
public Map<String, String> mergeConfigurations(Map<String, String> previousConfigurations,List<ConfigurationChange> configurationChanges) {
366377
Map<String, String> newConfigurations = new HashMap<>();
367-
if(configurations!=null){
368-
Maps.newHashMap(configurations);
378+
379+
if(previousConfigurations!=null){
380+
newConfigurations=Maps.newHashMap(previousConfigurations);
369381
}
370382

371383
if (configurationChanges == null) {

apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,73 @@ public void testLoadConfigWithIncrementalSync() throws Exception {
196196
remoteConfigLongPollService.stopLongPollingRefresh();
197197
}
198198

199+
@Test
200+
public void testMergeConfigurations() throws Exception {
201+
String key1 = "key1";
202+
String value1 = "value1";
203+
String anotherValue1 = "anotherValue1";
204+
205+
String key3 = "key3";
206+
String value3 = "value3";
207+
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);
208+
209+
List<ConfigurationChange> configurationChanges=new ArrayList<>();
210+
configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED));
211+
String key2 = "key2";
212+
String value2 = "value2";
213+
configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED));
214+
configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED));
215+
216+
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
217+
Map<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, configurationChanges);
218+
219+
assertEquals(2, result.size());
220+
assertEquals(anotherValue1, result.get(key1));
221+
assertEquals(value2, result.get(key2));
222+
}
223+
@Test
224+
public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exception {
225+
String key1 = "key1";
226+
String value1 = "value1";
227+
228+
String key3 = "key3";
229+
String value3 = "value3";
230+
231+
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);
232+
233+
234+
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
235+
Map<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, null);
236+
237+
assertEquals(2, result.size());
238+
assertEquals(value1, result.get(key1));
239+
assertEquals(value3, result.get(key3));
240+
}
241+
242+
@Test
243+
public void testMergeConfigurationWithChangesIsNULL() throws Exception {
244+
String key1 = "key1";
245+
String value1 = "value1";
246+
String anotherValue1 = "anotherValue1";
247+
248+
String key3 = "key3";
249+
String value3 = "value3";
250+
251+
List<ConfigurationChange> configurationChanges=new ArrayList<>();
252+
configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED));
253+
String key2 = "key2";
254+
String value2 = "value2";
255+
configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED));
256+
configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED));
257+
258+
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
259+
Map<String, String> result=remoteConfigRepository.mergeConfigurations(null, configurationChanges);
260+
261+
assertEquals(2, result.size());
262+
assertEquals(anotherValue1, result.get(key1));
263+
assertEquals(value2, result.get(key2));
264+
}
265+
199266
@Test
200267
public void testLoadConfigWithOrderedProperties() throws Exception {
201268
String someKey = "someKey";

0 commit comments

Comments
 (0)