Search in sources :

Example 1 with ApolloNotificationMessages

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

the class NotificationControllerV2Test method testPollNotificationWithMultipleNamespaceWithNotificationIdOutDated.

@Test
public void testPollNotificationWithMultipleNamespaceWithNotificationIdOutDated() throws Exception {
    String someWatchKey = "someKey";
    String anotherWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join(someAppId, someCluster, somePublicNamespace);
    String yetAnotherWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join(someAppId, defaultCluster, somePublicNamespace);
    long notificationId = someNotificationId + 1;
    long yetAnotherNotificationId = someNotificationId;
    Multimap<String, String> watchKeysMap = assembleMultiMap(defaultNamespace, Lists.newArrayList(someWatchKey));
    watchKeysMap.putAll(assembleMultiMap(somePublicNamespace, Lists.newArrayList(anotherWatchKey, yetAnotherWatchKey)));
    when(watchKeysUtil.assembleAllWatchKeys(someAppId, someCluster, Sets.newHashSet(defaultNamespace, somePublicNamespace), someDataCenter)).thenReturn(watchKeysMap);
    ReleaseMessage someReleaseMessage = mock(ReleaseMessage.class);
    when(someReleaseMessage.getId()).thenReturn(notificationId);
    when(someReleaseMessage.getMessage()).thenReturn(anotherWatchKey);
    ReleaseMessage yetAnotherReleaseMessage = mock(ReleaseMessage.class);
    when(yetAnotherReleaseMessage.getId()).thenReturn(yetAnotherNotificationId);
    when(yetAnotherReleaseMessage.getMessage()).thenReturn(yetAnotherWatchKey);
    when(releaseMessageService.findLatestReleaseMessagesGroupByMessages(Sets.newHashSet(watchKeysMap.values()))).thenReturn(Lists.newArrayList(someReleaseMessage, yetAnotherReleaseMessage));
    String notificationAsString = transformApolloConfigNotificationsToString(defaultNamespace, someNotificationId, somePublicNamespace, someNotificationId);
    DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> deferredResult = controller.pollNotification(someAppId, someCluster, notificationAsString, someDataCenter, someClientIp);
    ResponseEntity<List<ApolloConfigNotification>> result = (ResponseEntity<List<ApolloConfigNotification>>) deferredResult.getResult();
    assertEquals(HttpStatus.OK, result.getStatusCode());
    assertEquals(1, result.getBody().size());
    assertEquals(somePublicNamespace, result.getBody().get(0).getNamespaceName());
    assertEquals(notificationId, result.getBody().get(0).getNotificationId());
    ApolloNotificationMessages notificationMessages = result.getBody().get(0).getMessages();
    assertEquals(2, notificationMessages.getDetails().size());
    assertEquals(notificationId, notificationMessages.get(anotherWatchKey).longValue());
    assertEquals(yetAnotherNotificationId, notificationMessages.get(yetAnotherWatchKey).longValue());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) List(java.util.List) Test(org.junit.Test)

Example 2 with ApolloNotificationMessages

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

the class NotificationControllerV2Test method testPollNotificationWithMultipleNamespacesAndHandleMessage.

@Test
public void testPollNotificationWithMultipleNamespacesAndHandleMessage() throws Exception {
    String someWatchKey = "someKey";
    String anotherWatchKey = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join(someAppId, someCluster, somePublicNamespace);
    Multimap<String, String> watchKeysMap = assembleMultiMap(defaultNamespace, Lists.newArrayList(someWatchKey));
    watchKeysMap.putAll(assembleMultiMap(somePublicNamespace, Lists.newArrayList(anotherWatchKey)));
    when(watchKeysUtil.assembleAllWatchKeys(someAppId, someCluster, Sets.newHashSet(defaultNamespace, somePublicNamespace), someDataCenter)).thenReturn(watchKeysMap);
    String notificationAsString = transformApolloConfigNotificationsToString(defaultNamespace, someNotificationId, somePublicNamespace, someNotificationId);
    DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> deferredResult = controller.pollNotification(someAppId, someCluster, notificationAsString, someDataCenter, someClientIp);
    assertEquals(watchKeysMap.size(), deferredResults.size());
    long someId = 1;
    ReleaseMessage someReleaseMessage = new ReleaseMessage(anotherWatchKey);
    someReleaseMessage.setId(someId);
    controller.handleMessage(someReleaseMessage, Topics.APOLLO_RELEASE_TOPIC);
    ResponseEntity<List<ApolloConfigNotification>> response = (ResponseEntity<List<ApolloConfigNotification>>) deferredResult.getResult();
    assertEquals(1, response.getBody().size());
    ApolloConfigNotification notification = response.getBody().get(0);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(somePublicNamespace, notification.getNamespaceName());
    assertEquals(someId, notification.getNotificationId());
    ApolloNotificationMessages notificationMessages = response.getBody().get(0).getMessages();
    assertEquals(1, notificationMessages.getDetails().size());
    assertEquals(someId, notificationMessages.get(anotherWatchKey).longValue());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) List(java.util.List) Test(org.junit.Test)

Example 3 with ApolloNotificationMessages

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

the class RemoteConfigLongPollServiceTest method testSubmitLongPollNamespaceWithMessagesUpdated.

@Test
public void testSubmitLongPollNamespaceWithMessagesUpdated() throws Exception {
    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) {
            }
            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);
    // reset to 304
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED);
    final ArgumentCaptor<ApolloNotificationMessages> captor = ArgumentCaptor.forClass(ApolloNotificationMessages.class);
    verify(someRepository, times(1)).onLongPollNotified(any(ServiceDTO.class), captor.capture());
    ApolloNotificationMessages captured = captor.getValue();
    assertEquals(1, captured.getDetails().size());
    assertEquals(someNotificationId, captured.get(someKey).longValue());
    final SettableFuture<Boolean> anotherOnNotified = SettableFuture.create();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            anotherOnNotified.set(true);
            return null;
        }
    }).when(someRepository).onLongPollNotified(any(ServiceDTO.class), any(ApolloNotificationMessages.class));
    String anotherKey = "anotherKey";
    long anotherNotificationId = 2;
    notificationMessages.put(anotherKey, anotherNotificationId);
    // send notifications
    when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_OK);
    anotherOnNotified.get(5000, TimeUnit.MILLISECONDS);
    remoteConfigLongPollService.stopLongPollingRefresh();
    verify(someRepository, times(2)).onLongPollNotified(any(ServiceDTO.class), captor.capture());
    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 4 with ApolloNotificationMessages

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

the class RemoteConfigRepositoryTest method testAssembleQueryConfigUrl.

@Test
public void testAssembleQueryConfigUrl() throws Exception {
    Gson gson = new Gson();
    String someUri = "http://someServer";
    String someAppId = "someAppId";
    String someCluster = "someCluster+ &.-_someSign";
    String someReleaseKey = "20160705193346-583078ef5716c055+20160705193308-31c471ddf9087c3f";
    ApolloNotificationMessages notificationMessages = new ApolloNotificationMessages();
    String someKey = "someKey";
    long someNotificationId = 1;
    String anotherKey = "anotherKey";
    long anotherNotificationId = 2;
    notificationMessages.put(someKey, someNotificationId);
    notificationMessages.put(anotherKey, anotherNotificationId);
    RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
    ApolloConfig someApolloConfig = mock(ApolloConfig.class);
    when(someApolloConfig.getReleaseKey()).thenReturn(someReleaseKey);
    String queryConfigUrl = remoteConfigRepository.assembleQueryConfigUrl(someUri, someAppId, someCluster, someNamespace, null, notificationMessages, someApolloConfig);
    remoteConfigLongPollService.stopLongPollingRefresh();
    assertTrue(queryConfigUrl.contains("http://someServer/configs/someAppId/someCluster+%20&.-_someSign/" + someNamespace));
    assertTrue(queryConfigUrl.contains("releaseKey=20160705193346-583078ef5716c055%2B20160705193308-31c471ddf9087c3f"));
    assertTrue(queryConfigUrl.contains("messages=" + UrlEscapers.urlFormParameterEscaper().escape(gson.toJson(notificationMessages))));
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Gson(com.google.gson.Gson) ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) Test(org.junit.Test)

Example 5 with ApolloNotificationMessages

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

the class NotificationControllerV2IntegrationTest method testPollNotificationWithMultiplePublicNamespaceWithIncorrectCase4WithNotificationIdOutDated.

@Test(timeout = 5000L)
@Sql(scripts = "/integration-test/test-release.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/test-release-message.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/integration-test/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testPollNotificationWithMultiplePublicNamespaceWithIncorrectCase4WithNotificationIdOutDated() throws Exception {
    String publicAppId = "somePublicAppId";
    long someOutDatedNotificationId = 1;
    long newNotificationId = 20;
    String somePublicNameWithIncorrectCase = somePublicNamespace.toUpperCase();
    // the same namespace with difference character case, and difference notification id
    ResponseEntity<List<ApolloConfigNotification>> result = restTemplate.exchange("http://{baseurl}/notifications/v2?appId={appId}&cluster={clusterName}&notifications={notifications}", HttpMethod.GET, null, typeReference, getHostUrl(), someAppId, someCluster, transformApolloConfigNotificationsToString(somePublicNamespace, someOutDatedNotificationId, somePublicNameWithIncorrectCase, newNotificationId));
    List<ApolloConfigNotification> notifications = result.getBody();
    assertEquals(HttpStatus.OK, result.getStatusCode());
    assertEquals(1, notifications.size());
    assertEquals(somePublicNamespace, notifications.get(0).getNamespaceName());
    assertEquals(newNotificationId, notifications.get(0).getNotificationId());
    String key = assembleKey(publicAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, somePublicNamespace);
    ApolloNotificationMessages messages = result.getBody().get(0).getMessages();
    assertEquals(1, messages.getDetails().size());
    assertTrue(messages.has(key));
    assertEquals(newNotificationId, messages.get(key).longValue());
}
Also used : ApolloNotificationMessages(com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages) List(java.util.List) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Aggregations

ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)31 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)27 Test (org.junit.Test)27 List (java.util.List)20 Sql (org.springframework.test.context.jdbc.Sql)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)4 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)4 ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)3 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)3 ResponseEntity (org.springframework.http.ResponseEntity)3 Release (com.ctrip.framework.apollo.biz.entity.Release)1 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Gson (com.google.gson.Gson)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Before (org.junit.Before)1