|
34 | 34 | import com.ctrip.framework.apollo.core.dto.ApolloConfig; |
35 | 35 | import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; |
36 | 36 | import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages; |
| 37 | +import com.ctrip.framework.apollo.core.dto.ConfigurationChange; |
37 | 38 | import com.ctrip.framework.apollo.core.dto.ServiceDTO; |
| 39 | +import com.ctrip.framework.apollo.core.enums.ConfigSyncType; |
38 | 40 | import com.ctrip.framework.apollo.core.signature.Signature; |
39 | 41 | import com.ctrip.framework.apollo.enums.ConfigSourceType; |
40 | 42 | import com.ctrip.framework.apollo.exceptions.ApolloConfigException; |
|
53 | 55 | import com.google.common.util.concurrent.SettableFuture; |
54 | 56 | import com.google.gson.Gson; |
55 | 57 | import java.lang.reflect.Type; |
| 58 | +import java.util.ArrayList; |
56 | 59 | import java.util.List; |
57 | 60 | import java.util.Map; |
58 | 61 | import java.util.Properties; |
@@ -154,6 +157,141 @@ public void testLoadConfig() throws Exception { |
154 | 157 | assertEquals(ConfigSourceType.REMOTE, remoteConfigRepository.getSourceType()); |
155 | 158 | } |
156 | 159 |
|
| 160 | + @Test |
| 161 | + public void testLoadConfigWithIncrementalSync() throws Exception { |
| 162 | + |
| 163 | + String someKey = "someKey"; |
| 164 | + String someValue = "someValue"; |
| 165 | + String someKey1 = "someKey1"; |
| 166 | + String someValue1 = "someKey1"; |
| 167 | + Map<String, String> configurations = Maps.newHashMap(); |
| 168 | + configurations.put(someKey, someValue); |
| 169 | + configurations.put(someKey1, someValue1); |
| 170 | + ApolloConfig someApolloConfig = assembleApolloConfig(configurations); |
| 171 | + |
| 172 | + when(someResponse.getStatusCode()).thenReturn(200); |
| 173 | + when(someResponse.getBody()).thenReturn(someApolloConfig); |
| 174 | + |
| 175 | + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, |
| 176 | + someNamespace); |
| 177 | + |
| 178 | + remoteConfigRepository.sync(); |
| 179 | + |
| 180 | + List<ConfigurationChange> configurationChanges = new ArrayList<>(); |
| 181 | + String someNewValue = "someNewValue"; |
| 182 | + configurationChanges.add(new ConfigurationChange(someKey, someNewValue, "MODIFIED")); |
| 183 | + configurationChanges.add(new ConfigurationChange(someKey1, null, "DELETED")); |
| 184 | + String someKey2 = "someKey2"; |
| 185 | + String someValue2 = "someValue2"; |
| 186 | + configurationChanges.add(new ConfigurationChange(someKey2, someValue2, "ADDED")); |
| 187 | + ApolloConfig someApolloConfigWithIncrementalSync = assembleApolloConfigWithIncrementalSync( |
| 188 | + configurationChanges); |
| 189 | + |
| 190 | + when(someResponse.getStatusCode()).thenReturn(200); |
| 191 | + when(someResponse.getBody()).thenReturn(someApolloConfigWithIncrementalSync); |
| 192 | + |
| 193 | + remoteConfigRepository.sync(); |
| 194 | + |
| 195 | + Properties config = remoteConfigRepository.getConfig(); |
| 196 | + |
| 197 | + assertEquals(2, config.size()); |
| 198 | + assertEquals("someNewValue", config.getProperty("someKey")); |
| 199 | + assertEquals("someValue2", config.getProperty("someKey2")); |
| 200 | + assertEquals(ConfigSourceType.REMOTE, remoteConfigRepository.getSourceType()); |
| 201 | + remoteConfigLongPollService.stopLongPollingRefresh(); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void testMergeConfigurations() throws Exception { |
| 206 | + String key1 = "key1"; |
| 207 | + String value1 = "value1"; |
| 208 | + String anotherValue1 = "anotherValue1"; |
| 209 | + |
| 210 | + String key3 = "key3"; |
| 211 | + String value3 = "value3"; |
| 212 | + Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1, key3, value3); |
| 213 | + |
| 214 | + List<ConfigurationChange> configurationChanges = new ArrayList<>(); |
| 215 | + configurationChanges.add(new ConfigurationChange(key1, anotherValue1, "MODIFIED")); |
| 216 | + String key2 = "key2"; |
| 217 | + String value2 = "value2"; |
| 218 | + configurationChanges.add(new ConfigurationChange(key2, value2, "ADDED")); |
| 219 | + configurationChanges.add(new ConfigurationChange(key3, null, "DELETED")); |
| 220 | + |
| 221 | + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, |
| 222 | + someNamespace); |
| 223 | + Map<String, String> result = remoteConfigRepository.mergeConfigurations(previousConfigurations, |
| 224 | + configurationChanges); |
| 225 | + |
| 226 | + assertEquals(2, result.size()); |
| 227 | + assertEquals(anotherValue1, result.get(key1)); |
| 228 | + assertEquals(value2, result.get(key2)); |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exception { |
| 233 | + String key1 = "key1"; |
| 234 | + String value1 = "value1"; |
| 235 | + |
| 236 | + String key3 = "key3"; |
| 237 | + String value3 = "value3"; |
| 238 | + |
| 239 | + Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1, key3, value3); |
| 240 | + |
| 241 | + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, |
| 242 | + someNamespace); |
| 243 | + Map<String, String> result = remoteConfigRepository.mergeConfigurations(previousConfigurations, |
| 244 | + null); |
| 245 | + |
| 246 | + assertEquals(2, result.size()); |
| 247 | + assertEquals(value1, result.get(key1)); |
| 248 | + assertEquals(value3, result.get(key3)); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void testMergeConfigurationWithChangesIsNULL() throws Exception { |
| 253 | + String key1 = "key1"; |
| 254 | + String value1 = "value1"; |
| 255 | + String anotherValue1 = "anotherValue1"; |
| 256 | + |
| 257 | + String key3 = "key3"; |
| 258 | + String value3 = "value3"; |
| 259 | + |
| 260 | + List<ConfigurationChange> configurationChanges = new ArrayList<>(); |
| 261 | + configurationChanges.add(new ConfigurationChange(key1, anotherValue1, "MODIFIED")); |
| 262 | + String key2 = "key2"; |
| 263 | + String value2 = "value2"; |
| 264 | + configurationChanges.add(new ConfigurationChange(key2, value2, "ADDED")); |
| 265 | + configurationChanges.add(new ConfigurationChange(key3, null, "DELETED")); |
| 266 | + |
| 267 | + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, |
| 268 | + someNamespace); |
| 269 | + Map<String, String> result = remoteConfigRepository.mergeConfigurations(null, |
| 270 | + configurationChanges); |
| 271 | + |
| 272 | + assertEquals(2, result.size()); |
| 273 | + assertEquals(anotherValue1, result.get(key1)); |
| 274 | + assertEquals(value2, result.get(key2)); |
| 275 | + } |
| 276 | + |
| 277 | + @Test(expected = ApolloConfigException.class) |
| 278 | + public void testGetRemoteConfigWithUnknownSync() throws Exception { |
| 279 | + |
| 280 | + ApolloConfig someApolloConfigWithUnknownSync = assembleApolloConfigWithUnknownSync( |
| 281 | + new ArrayList<>()); |
| 282 | + |
| 283 | + when(someResponse.getStatusCode()).thenReturn(200); |
| 284 | + when(someResponse.getBody()).thenReturn(someApolloConfigWithUnknownSync); |
| 285 | + |
| 286 | + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, |
| 287 | + someNamespace); |
| 288 | + |
| 289 | + //must stop the long polling before exception occurred |
| 290 | + remoteConfigLongPollService.stopLongPollingRefresh(); |
| 291 | + |
| 292 | + remoteConfigRepository.getConfig(); |
| 293 | + } |
| 294 | + |
157 | 295 | @Test |
158 | 296 | public void testLoadConfigWithOrderedProperties() throws Exception { |
159 | 297 | String someKey = "someKey"; |
@@ -371,6 +509,32 @@ private ApolloConfig assembleApolloConfig(Map<String, String> configurations) { |
371 | 509 | return apolloConfig; |
372 | 510 | } |
373 | 511 |
|
| 512 | + private ApolloConfig assembleApolloConfigWithIncrementalSync( |
| 513 | + List<ConfigurationChange> configurationChanges) { |
| 514 | + String someAppId = "appId"; |
| 515 | + String someClusterName = "cluster"; |
| 516 | + String someReleaseKey = "1"; |
| 517 | + ApolloConfig apolloConfig = |
| 518 | + new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseKey); |
| 519 | + |
| 520 | + apolloConfig.setConfigSyncType(ConfigSyncType.INCREMENTAL_SYNC.getValue()); |
| 521 | + apolloConfig.setConfigurationChanges(configurationChanges); |
| 522 | + return apolloConfig; |
| 523 | + } |
| 524 | + |
| 525 | + private ApolloConfig assembleApolloConfigWithUnknownSync( |
| 526 | + List<ConfigurationChange> configurationChanges) { |
| 527 | + String someAppId = "appId"; |
| 528 | + String someClusterName = "cluster"; |
| 529 | + String someReleaseKey = "1"; |
| 530 | + ApolloConfig apolloConfig = |
| 531 | + new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseKey); |
| 532 | + |
| 533 | + apolloConfig.setConfigSyncType(ConfigSyncType.UNKNOWN.getValue()); |
| 534 | + apolloConfig.setConfigurationChanges(configurationChanges); |
| 535 | + return apolloConfig; |
| 536 | + } |
| 537 | + |
374 | 538 | public static class MockConfigUtil extends ConfigUtil { |
375 | 539 |
|
376 | 540 | @Override |
|
0 commit comments