Search in sources :

Example 1 with OrderedProperties

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);
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) Test(org.junit.Test)

Example 2 with OrderedProperties

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]);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) PropertiesFactory(com.ctrip.framework.apollo.util.factory.PropertiesFactory) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Test(org.junit.Test)

Example 3 with OrderedProperties

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);
}
Also used : YamlParser(com.ctrip.framework.apollo.util.yaml.YamlParser) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Test(org.junit.Test)

Example 4 with OrderedProperties

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());
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 5 with OrderedProperties

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, ""));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Aggregations

OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)6 Test (org.junit.Test)6 Properties (java.util.Properties)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)3 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)2 Config (com.ctrip.framework.apollo.Config)2 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)2 PropertiesFactory (com.ctrip.framework.apollo.util.factory.PropertiesFactory)1 YamlParser (com.ctrip.framework.apollo.util.yaml.YamlParser)1