Search in sources :

Example 51 with ApolloConfigNotification

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

the class RemoteConfigLongPollServiceTest method testSubmitLongPollNamespaceWith200Response.

@Test
public void testSubmitLongPollNamespaceWith200Response() throws Exception {
    RemoteConfigRepository someRepository = mock(RemoteConfigRepository.class);
    final String someNamespace = "someNamespace";
    ApolloNotificationMessages notificationMessages = new ApolloNotificationMessages();
    String someKey = "someKey";
    long someNotificationId = 1;
    String anotherKey = "anotherKey";
    long anotherNotificationId = 2;
    notificationMessages.put(someKey, someNotificationId);
    notificationMessages.put(anotherKey, anotherNotificationId);
    ApolloConfigNotification someNotification = mock(ApolloConfigNotification.class);
    when(someNotification.getNamespaceName()).thenReturn(someNamespace);
    when(someNotification.getMessages()).thenReturn(notificationMessages);
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification));
    doAnswer(new Answer<HttpResponse<List<ApolloConfigNotification>>>() {

        @Override
        public HttpResponse<List<ApolloConfigNotification>> answer(InvocationOnMock invocation) throws Throwable {
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
            }
            return pollResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), eq(responseType));
    final SettableFuture<Boolean> onNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            onNotified.set(true);
            return null;
        }
    }).when(someRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    remoteConfigLongPollService.submit(someNamespace, someRepository);
    onNotified.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    final ArgumentCaptor<ApolloNotificationMessages> captor = ArgumentCaptor.forClass(ApolloNotificationMessages.class);
    verify(someRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), captor.capture());
    ApolloNotificationMessages captured = captor.getValue();
    assertEquals(2, captured.getDetails().size());
    assertEquals(someNotificationId, captured.get(someKey).longValue());
    assertEquals(anotherNotificationId, captured.get(anotherKey).longValue());
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test)

Example 52 with ApolloConfigNotification

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

the class RemoteConfigLongPollServiceTest method testSubmitLongPollMultipleNamespacesWithMultipleNotificationsReturned.

@Test
public void testSubmitLongPollMultipleNamespacesWithMultipleNotificationsReturned() throws Exception {
    RemoteConfigRepository someRepository = mock(RemoteConfigRepository.class);
    RemoteConfigRepository anotherRepository = mock(RemoteConfigRepository.class);
    final String someNamespace = "someNamespace";
    final String anotherNamespace = "anotherNamespace";
    ApolloNotificationMessages notificationMessages = new ApolloNotificationMessages();
    String someKey = "someKey";
    long someNotificationId = 1;
    notificationMessages.put(someKey, someNotificationId);
    ApolloNotificationMessages anotherNotificationMessages = new ApolloNotificationMessages();
    String anotherKey = "anotherKey";
    long anotherNotificationId = 2;
    anotherNotificationMessages.put(anotherKey, anotherNotificationId);
    final ApolloConfigNotification someNotification = mock(ApolloConfigNotification.class);
    when(someNotification.getNamespaceName()).thenReturn(someNamespace);
    when(someNotification.getMessages()).thenReturn(notificationMessages);
    final ApolloConfigNotification anotherNotification = mock(ApolloConfigNotification.class);
    when(anotherNotification.getNamespaceName()).thenReturn(anotherNamespace);
    when(anotherNotification.getMessages()).thenReturn(anotherNotificationMessages);
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification, anotherNotification));
    doAnswer(new Answer<HttpResponse<List<ApolloConfigNotification>>>() {

        @Override
        public HttpResponse<List<ApolloConfigNotification>> answer(InvocationOnMock invocation) throws Throwable {
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
            }
            return pollResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), eq(responseType));
    final SettableFuture<Boolean> someRepositoryNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            someRepositoryNotified.set(true);
            return null;
        }
    }).when(someRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    final SettableFuture<Boolean> anotherRepositoryNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            anotherRepositoryNotified.set(true);
            return null;
        }
    }).when(anotherRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    remoteConfigLongPollService.submit(someNamespace, someRepository);
    remoteConfigLongPollService.submit(anotherNamespace, anotherRepository);
    someRepositoryNotified.get(5000, TimeUnit.MILLISECONDS);
    anotherRepositoryNotified.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    final ArgumentCaptor<ApolloNotificationMessages> captor = ArgumentCaptor.forClass(ApolloNotificationMessages.class);
    final ArgumentCaptor<ApolloNotificationMessages> anotherCaptor = ArgumentCaptor.forClass(ApolloNotificationMessages.class);
    verify(someRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), captor.capture());
    verify(anotherRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), anotherCaptor.capture());
    ApolloNotificationMessages result = captor.getValue();
    assertEquals(1, result.getDetails().size());
    assertEquals(someNotificationId, result.get(someKey).longValue());
    ApolloNotificationMessages anotherResult = anotherCaptor.getValue();
    assertEquals(1, anotherResult.getDetails().size());
    assertEquals(anotherNotificationId, anotherResult.get(anotherKey).longValue());
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test)

Example 53 with ApolloConfigNotification

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

the class RemoteConfigLongPollServiceTest method testSubmitLongPollNamespaceWithAccessKeySecret.

@Test
public void testSubmitLongPollNamespaceWithAccessKeySecret() throws Exception {
    someSecret = "someSecret";
    RemoteConfigRepository someRepository = mock(RemoteConfigRepository.class);
    final String someNamespace = "someNamespace";
    ApolloNotificationMessages notificationMessages = new ApolloNotificationMessages();
    String someKey = "someKey";
    long someNotificationId = 1;
    notificationMessages.put(someKey, someNotificationId);
    ApolloConfigNotification someNotification = mock(ApolloConfigNotification.class);
    when(someNotification.getNamespaceName()).thenReturn(someNamespace);
    when(someNotification.getMessages()).thenReturn(notificationMessages);
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    when(pollResponse.getBody()).thenReturn(Lists.newArrayList(someNotification));
    doAnswer(new Answer<HttpResponse<List<ApolloConfigNotification>>>() {

        @Override
        public HttpResponse<List<ApolloConfigNotification>> answer(InvocationOnMock invocation) throws Throwable {
            try {
                TimeUnit.MILLISECONDS.sleep(50);
            } catch (InterruptedException e) {
            }
            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 pollResponse;
        }
    }).when(httpClient).doGet(any(HttpRequest.class), eq(responseType));
    final SettableFuture<Boolean> onNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            onNotified.set(true);
            return null;
        }
    }).when(someRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    remoteConfigLongPollService.submit(someNamespace, someRepository);
    onNotified.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    verify(someRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)53 Test (org.junit.Test)41 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)28 Sql (org.springframework.test.context.jdbc.Sql)26 List (java.util.List)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)9 ResponseEntity (org.springframework.http.ResponseEntity)8 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)5 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)5 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)4 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)3 Config (com.ctrip.framework.apollo.Config)3 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)3 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)3 Map (java.util.Map)3 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)3 DeferredResultWrapper (com.ctrip.framework.apollo.configservice.wrapper.DeferredResultWrapper)2