Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-coming committed Dec 8, 2024
1 parent 44b70fa commit 6859fe1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.ctrip.framework.apollo.core.dto.ConfigurationChange;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.enums.ConfigSyncType;
import com.ctrip.framework.apollo.core.enums.ConfigurationChangeType;
import com.ctrip.framework.apollo.core.schedule.ExponentialSchedulePolicy;
import com.ctrip.framework.apollo.core.schedule.SchedulePolicy;
import com.ctrip.framework.apollo.core.signature.Signature;
Expand Down Expand Up @@ -254,23 +255,21 @@ private ApolloConfig loadApolloConfig() {
}

ApolloConfig result = response.getBody();

if(result!=null){
ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType());

if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){
ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType());

Map<String, String> previousConfigurations=null;
if (configSyncType == ConfigSyncType.INCREMENTALSYNC) {

ApolloConfig previousConfig = m_configCache.get();

if(previousConfig!=null){
previousConfigurations=previousConfig.getConfigurations();
}
Map<String, String> previousConfigurations =
(previousConfig != null) ? previousConfig.getConfigurations() : null;

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

}

}

logger.debug("Loaded config for {}: {}", m_namespace, result);
Expand Down Expand Up @@ -382,7 +381,8 @@ private List<ServiceDTO> getConfigServices() {
return services;
}

public Map<String, String> mergeConfigurations(Map<String, String> previousConfigurations,List<ConfigurationChange> configurationChanges) {
Map<String, String> mergeConfigurations(Map<String, String> previousConfigurations,
List<ConfigurationChange> configurationChanges) {
Map<String, String> newConfigurations = new HashMap<>();

if(previousConfigurations!=null){
Expand All @@ -394,7 +394,7 @@ public Map<String, String> mergeConfigurations(Map<String, String> previousConfi
}

for (ConfigurationChange change : configurationChanges) {
switch (change.getConfigurationChangeType()) {
switch (ConfigurationChangeType.fromString(change.getConfigurationChangeType())) {
case ADDED:
case MODIFIED:
newConfigurations.put(change.getKey(), change.getNewValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,18 @@ public void testLoadConfigWithIncrementalSync() throws Exception {
when(someResponse.getStatusCode()).thenReturn(200);
when(someResponse.getBody()).thenReturn(someApolloConfig);

RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId,someNamespace);

remoteConfigRepository.sync();


List<ConfigurationChange> configurationChanges=new ArrayList<>();
String someNewValue = "someNewValue";
configurationChanges.add(new ConfigurationChange(someKey, someNewValue, ConfigurationChangeType.MODIFIED));
configurationChanges.add(new ConfigurationChange(someKey1, null, ConfigurationChangeType.DELETED));
configurationChanges.add(new ConfigurationChange(someKey, someNewValue, "MODIFIED"));
configurationChanges.add(new ConfigurationChange(someKey1, null, "DELETED"));
String someKey2 = "someKey2";
String someValue2 = "someValue2";
configurationChanges.add(new ConfigurationChange(someKey2, someValue2, ConfigurationChangeType.ADDED));
configurationChanges.add(new ConfigurationChange(someKey2, someValue2,"ADDED"));
ApolloConfig someApolloConfigWithIncrementalSync = assembleApolloConfigWithIncrementalSync(configurationChanges);

when(someResponse.getStatusCode()).thenReturn(200);
Expand Down Expand Up @@ -208,13 +208,13 @@ public void testMergeConfigurations() throws Exception {
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);

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

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

assertEquals(2, result.size());
Expand All @@ -232,7 +232,7 @@ public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exce
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);


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

assertEquals(2, result.size());
Expand All @@ -250,13 +250,13 @@ public void testMergeConfigurationWithChangesIsNULL() throws Exception {
String value3 = "value3";

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

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

assertEquals(2, result.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
public class ConfigurationChange {
private final String key;
private final String newValue;
private final ConfigurationChangeType configurationChangeType;
private final String configurationChangeType;

/**
* Constructor.
* @param key the key whose value is changed
* @param newValue the value after change
* @param configurationChangeType the change type
*/
public ConfigurationChange(String key, String newValue, ConfigurationChangeType configurationChangeType) {
public ConfigurationChange(String key, String newValue, String configurationChangeType) {
this.key = key;
this.newValue = newValue;
this.configurationChangeType = configurationChangeType;
Expand All @@ -47,7 +47,7 @@ public String getNewValue() {
return newValue;
}

public ConfigurationChangeType getConfigurationChangeType() {
public String getConfigurationChangeType() {
return configurationChangeType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@
package com.ctrip.framework.apollo.core.enums;


import com.google.common.base.Preconditions;

/**
* @author jason
*/
public enum ConfigurationChangeType {
ADDED, MODIFIED, DELETED
ADDED, MODIFIED, DELETED, UNKNOWN;

public static ConfigurationChangeType fromString(String changeType) {
ConfigurationChangeType configurationChangeType = ConfigurationChangeTypeUtils.transformChangeType(
changeType);
Preconditions.checkArgument(configurationChangeType != UNKNOWN,
String.format("ConfigurationChangeType %s is invalid", changeType));
return configurationChangeType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed 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 com.ctrip.framework.apollo.core.enums;

import com.ctrip.framework.apollo.core.utils.StringUtils;

/**
* A utility class for the {@link ConfigurationChangeType} enum.
* <p>
* The class provides simple functionalities that extend the capabilities of {@link ConfigurationChangeType}
*
* @author json
*/
public final class ConfigurationChangeTypeUtils {

/**
* Transforms a given String to its matching {@link ConfigurationChangeType}
*
* @param changeType the String to convert
* @return the matching {@link ConfigurationChangeType} for the given String
*/
public static ConfigurationChangeType transformChangeType(String changeType) {
if (StringUtils.isBlank(changeType)) {
return ConfigurationChangeType.UNKNOWN;
}

String cleanedChangeType = changeType.trim().toUpperCase();

try {
return ConfigurationChangeType.valueOf(cleanedChangeType);
} catch (IllegalArgumentException e) {

return ConfigurationChangeType.UNKNOWN;
}
}
}

0 comments on commit 6859fe1

Please sign in to comment.