Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-coming committed Nov 28, 2024
1 parent 99cf8fd commit ebb31a8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,21 @@ private ApolloConfig loadApolloConfig() {
ApolloConfig result = response.getBody();
if(result!=null){
ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType());

if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){
ApolloConfig apolloConfig = m_configCache.get()==null?new ApolloConfig():m_configCache.get();
result.setConfigurations(mergeConfigurations(apolloConfig.getConfigurations(),result.getConfigurationChanges()));

Map<String, String> previousConfigurations=null;

ApolloConfig previousConfig = m_configCache.get();

if(previousConfig!=null){
previousConfigurations=previousConfig.getConfigurations();
}

result.setConfigurations(mergeConfigurations(previousConfigurations,result.getConfigurationChanges()));

}

}

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

private Map<String, String> mergeConfigurations(Map<String, String> configurations,List<ConfigurationChange> configurationChanges) {
public Map<String, String> mergeConfigurations(Map<String, String> previousConfigurations,List<ConfigurationChange> configurationChanges) {
Map<String, String> newConfigurations = new HashMap<>();
if(configurations!=null){
Maps.newHashMap(configurations);

if(previousConfigurations!=null){
newConfigurations=Maps.newHashMap(previousConfigurations);
}

if (configurationChanges == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,73 @@ public void testLoadConfigWithIncrementalSync() throws Exception {
remoteConfigLongPollService.stopLongPollingRefresh();
}

@Test
public void testMergeConfigurations() throws Exception {
String key1 = "key1";
String value1 = "value1";
String anotherValue1 = "anotherValue1";

String key3 = "key3";
String value3 = "value3";
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);

List<ConfigurationChange> configurationChanges=new ArrayList<>();
configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED));
String key2 = "key2";
String value2 = "value2";
configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED));
configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED));

RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
Map<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, configurationChanges);

assertEquals(2, result.size());
assertEquals(anotherValue1, result.get(key1));
assertEquals(value2, result.get(key2));
}
@Test
public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exception {
String key1 = "key1";
String value1 = "value1";

String key3 = "key3";
String value3 = "value3";

Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);


RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
Map<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, null);

assertEquals(2, result.size());
assertEquals(value1, result.get(key1));
assertEquals(value3, result.get(key3));
}

@Test
public void testMergeConfigurationWithChangesIsNULL() throws Exception {
String key1 = "key1";
String value1 = "value1";
String anotherValue1 = "anotherValue1";

String key3 = "key3";
String value3 = "value3";

List<ConfigurationChange> configurationChanges=new ArrayList<>();
configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED));
String key2 = "key2";
String value2 = "value2";
configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED));
configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED));

RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
Map<String, String> result=remoteConfigRepository.mergeConfigurations(null, configurationChanges);

assertEquals(2, result.size());
assertEquals(anotherValue1, result.get(key1));
assertEquals(value2, result.get(key2));
}

@Test
public void testLoadConfigWithOrderedProperties() throws Exception {
String someKey = "someKey";
Expand Down

0 comments on commit ebb31a8

Please sign in to comment.