Search in sources :

Example 16 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class ConfigIntegrationTest method testRefreshConfig.

@Test
public void testRefreshConfig() throws Exception {
    final String someKey = "someKey";
    final String someValue = "someValue";
    final String anotherValue = "anotherValue";
    int someRefreshInterval = 500;
    TimeUnit someRefreshTimeUnit = TimeUnit.MILLISECONDS;
    setRefreshInterval(someRefreshInterval);
    setRefreshTimeUnit(someRefreshTimeUnit);
    Map<String, String> configurations = Maps.newHashMap();
    configurations.put(someKey, someValue);
    ApolloConfig apolloConfig = assembleApolloConfig(configurations);
    ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
    startServerWithHandlers(handler);
    Config config = ConfigService.getAppConfig();
    final List<ConfigChangeEvent> changeEvents = Lists.newArrayList();
    final SettableFuture<Boolean> refreshFinished = SettableFuture.create();
    config.addChangeListener(new ConfigChangeListener() {

        AtomicInteger counter = new AtomicInteger(0);

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            // only need to assert once
            if (counter.incrementAndGet() > 1) {
                return;
            }
            assertEquals(1, changeEvent.changedKeys().size());
            assertTrue(changeEvent.isChanged(someKey));
            assertEquals(someValue, changeEvent.getChange(someKey).getOldValue());
            assertEquals(anotherValue, changeEvent.getChange(someKey).getNewValue());
            // if there is any assertion failed above, this line won't be executed
            changeEvents.add(changeEvent);
            refreshFinished.set(true);
        }
    });
    apolloConfig.getConfigurations().put(someKey, anotherValue);
    refreshFinished.get(someRefreshInterval * 5, someRefreshTimeUnit);
    assertThat("Change event's size should equal to one or there must be some assertion failed in change listener", 1, equalTo(changeEvents.size()));
    assertEquals(anotherValue, config.getProperty(someKey, null));
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) TimeUnit(java.util.concurrent.TimeUnit) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 17 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class ConfigIntegrationTest method testLongPollRefreshWithMultipleNamespacesAndOnlyOneNamespaceNotified.

@Test
public void testLongPollRefreshWithMultipleNamespacesAndOnlyOneNamespaceNotified() throws Exception {
    final String someKey = "someKey";
    final String someValue = "someValue";
    final String anotherValue = "anotherValue";
    long someNotificationId = 1;
    long pollTimeoutInMS = 50;
    Map<String, String> configurations = Maps.newHashMap();
    configurations.put(someKey, someValue);
    ApolloConfig apolloConfig = assembleApolloConfig(configurations);
    ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
    ContextHandler pollHandler = mockPollNotificationHandler(pollTimeoutInMS, HttpServletResponse.SC_OK, Lists.newArrayList(new ApolloConfigNotification(apolloConfig.getNamespaceName(), someNotificationId)), false);
    startServerWithHandlers(configHandler, pollHandler);
    Config someOtherConfig = ConfigService.getConfig(someOtherNamespace);
    Config config = ConfigService.getAppConfig();
    assertEquals(someValue, config.getProperty(someKey, null));
    assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
    final SettableFuture<Boolean> longPollFinished = SettableFuture.create();
    config.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            longPollFinished.set(true);
        }
    });
    apolloConfig.getConfigurations().put(someKey, anotherValue);
    longPollFinished.get(5000, TimeUnit.MILLISECONDS);
    assertEquals(anotherValue, config.getProperty(someKey, null));
    TimeUnit.MILLISECONDS.sleep(pollTimeoutInMS * 10);
    assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 18 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class RemoteConfigRepositoryTest method testLoadConfigWithAccessKeySecret.

@Test
public void testLoadConfigWithAccessKeySecret() throws Exception {
    someSecret = "someSecret";
    String someKey = "someKey";
    String someValue = "someValue";
    Map<String, String> configurations = Maps.newHashMap();
    configurations.put(someKey, someValue);
    ApolloConfig someApolloConfig = assembleApolloConfig(configurations);
    when(someResponse.getStatusCode()).thenReturn(200);
    when(someResponse.getBody()).thenReturn(someApolloConfig);
    doAnswer(new Answer<HttpResponse<ApolloConfig>>() {

        @Override
        public HttpResponse<ApolloConfig> answer(InvocationOnMock invocation) throws Throwable {
            HttpRequest request = invocation.getArgument(0, HttpRequest.class);
            Map<String, String> headers = request.getHeaders();
            assertNotNull(headers);
            assertTrue(headers.containsKey(Signature.HTTP_HEADER_TIMESTAMP));
            assertTrue(headers.containsKey(HttpHeaders.AUTHORIZATION));
            return someResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), any(Class.class));
    RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
    Properties config = remoteConfigRepository.getConfig();
    assertEquals(configurations, config);
    assertEquals(ConfigSourceType.REMOTE, remoteConfigRepository.getSourceType());
    remoteConfigLongPollService.stopLongPollingRefresh();
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 19 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig in project apollo by ctripcorp.

the class RemoteConfigRepositoryTest method assembleApolloConfig.

private ApolloConfig assembleApolloConfig(Map<String, String> configurations) {
    String someAppId = "appId";
    String someClusterName = "cluster";
    String someReleaseKey = "1";
    ApolloConfig apolloConfig = new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseKey);
    apolloConfig.setConfigurations(configurations);
    return apolloConfig;
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig)

Example 20 with ApolloConfig

use of com.ctrip.framework.apollo.core.dto.ApolloConfig 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)

Aggregations

ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)57 Test (org.junit.Test)49 Sql (org.springframework.test.context.jdbc.Sql)17 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)10 Config (com.ctrip.framework.apollo.Config)10 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)10 Properties (java.util.Properties)8 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)7 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)4 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)4 Map (java.util.Map)4 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)3 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3