use of com.ctrip.framework.apollo.util.OrderedProperties in project apollo by ctripcorp.
the class RemoteConfigRepositoryTest method testLoadConfigWithOrderedProperties.
@Test
public void testLoadConfigWithOrderedProperties() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
Map<String, String> configurations = Maps.newLinkedHashMap();
configurations.put(someKey, someValue);
configurations.put("someKey2", "someValue2");
ApolloConfig someApolloConfig = assembleApolloConfig(configurations);
when(someResponse.getStatusCode()).thenReturn(200);
when(someResponse.getBody()).thenReturn(someApolloConfig);
when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
@Override
public Properties answer(InvocationOnMock invocation) {
return new OrderedProperties();
}
});
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
Properties config = remoteConfigRepository.getConfig();
assertTrue(config instanceof OrderedProperties);
assertEquals(configurations, config);
assertEquals(ConfigSourceType.REMOTE, remoteConfigRepository.getSourceType());
remoteConfigLongPollService.stopLongPollingRefresh();
String[] actualArrays = config.keySet().toArray(new String[] {});
String[] expectedArrays = { "someKey", "someKey2" };
assertArrayEquals(expectedArrays, actualArrays);
}
use of com.ctrip.framework.apollo.util.OrderedProperties in project apollo by ctripcorp.
the class YamlParserTest method testOrderProperties.
@Test
public void testOrderProperties() throws IOException {
String yamlContent = loadYaml("orderedcase.yaml");
Properties nonOrderedProperties = parser.yamlToProperties(yamlContent);
PropertiesFactory propertiesFactory = mock(PropertiesFactory.class);
when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
@Override
public Properties answer(InvocationOnMock invocation) {
return new OrderedProperties();
}
});
MockInjector.setInstance(PropertiesFactory.class, propertiesFactory);
parser = new YamlParser();
Properties orderedProperties = parser.yamlToProperties(yamlContent);
assertTrue(orderedProperties instanceof OrderedProperties);
checkPropertiesEquals(nonOrderedProperties, orderedProperties);
String[] propertyNames = orderedProperties.stringPropertyNames().toArray(new String[0]);
assertEquals("k2", propertyNames[0]);
assertEquals("k4", propertyNames[1]);
assertEquals("k1", propertyNames[2]);
}
use of com.ctrip.framework.apollo.util.OrderedProperties in project apollo by ctripcorp.
the class YamlConfigFileTest method testWhenHasContentWithOrder.
@Test
public void testWhenHasContentWithOrder() throws Exception {
when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
@Override
public Properties answer(InvocationOnMock invocation) {
return new OrderedProperties();
}
});
Properties someProperties = new Properties();
String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
String someContent = "someKey: 'someValue'\nsomeKey2: 'someValue2'";
someProperties.setProperty(key, someContent);
someSourceType = ConfigSourceType.LOCAL;
Properties yamlProperties = new YamlParser().yamlToProperties(someContent);
when(configRepository.getConfig()).thenReturn(someProperties);
when(configRepository.getSourceType()).thenReturn(someSourceType);
when(yamlParser.yamlToProperties(someContent)).thenReturn(yamlProperties);
YamlConfigFile configFile = new YamlConfigFile(someNamespace, configRepository);
assertSame(someContent, configFile.getContent());
assertSame(yamlProperties, configFile.asProperties());
String[] actualArrays = configFile.asProperties().keySet().toArray(new String[] {});
String[] expectedArrays = { "someKey", "someKey2" };
assertArrayEquals(expectedArrays, actualArrays);
}
use of com.ctrip.framework.apollo.util.OrderedProperties in project apollo by ctripcorp.
the class ConfigIntegrationTest method testOrderGetConfigWithLocalFileAndRemoteConfigError.
@Test
public void testOrderGetConfigWithLocalFileAndRemoteConfigError() throws Exception {
String someKey1 = "someKey1";
String someValue1 = "someValue1";
String someKey2 = "someKey2";
String someValue2 = "someValue2";
setPropertiesOrderEnabled(true);
Properties properties = new OrderedProperties();
properties.put(someKey1, someValue1);
properties.put(someKey2, someValue2);
createLocalCachePropertyFile(properties);
ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
startServerWithHandlers(handler);
Config config = ConfigService.getAppConfig();
assertEquals(someValue1, config.getProperty(someKey1, null));
assertEquals(someValue2, config.getProperty(someKey2, null));
Set<String> propertyNames = config.getPropertyNames();
Iterator<String> it = propertyNames.iterator();
assertEquals(someKey1, it.next());
assertEquals(someKey2, it.next());
}
use of com.ctrip.framework.apollo.util.OrderedProperties in project apollo by ctripcorp.
the class ConfigIntegrationTest method testOrderGetConfigWithLocalFileAndWithRemoteConfig.
@Test
public void testOrderGetConfigWithLocalFileAndWithRemoteConfig() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
String anotherValue = "anotherValue";
String someKey1 = "someKey1";
String someValue1 = "someValue1";
String anotherValue1 = "anotherValue1";
String someKey2 = "someKey2";
String someValue2 = "someValue2";
setPropertiesOrderEnabled(true);
Properties properties = new OrderedProperties();
properties.put(someKey, someValue);
properties.put(someKey1, someValue1);
properties.put(someKey2, someValue2);
createLocalCachePropertyFile(properties);
Map<String, String> configurations = new LinkedHashMap<>();
configurations.put(someKey, anotherValue);
configurations.put(someKey1, anotherValue1);
configurations.put(someKey2, someValue2);
ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.copyOf(configurations));
ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
startServerWithHandlers(handler);
Config config = ConfigService.getAppConfig();
assertEquals(anotherValue, config.getProperty(someKey, null));
Set<String> propertyNames = config.getPropertyNames();
Iterator<String> it = propertyNames.iterator();
assertEquals(someKey, it.next());
assertEquals(someKey1, it.next());
assertEquals(someKey2, it.next());
assertEquals(anotherValue1, config.getProperty(someKey1, ""));
}
Aggregations