private static NotificationSyncResponse convert(NotificationServerSync source) { if (source == null) { return null; } NotificationSyncResponse sync = new NotificationSyncResponse(); sync.setResponseStatus(convert(source.getResponseStatus())); if (source.getAvailableTopics() != null) { List<Topic> topics = new ArrayList<>(source.getAvailableTopics().size()); for (org.kaaproject.kaa.server.sync.Topic topic : source.getAvailableTopics()) { topics.add(new Topic( topic.getIdAsLong(), topic.getName(), convert(topic.getSubscriptionType()))); } sync.setAvailableTopics(topics); } if (source.getNotifications() != null) { List<Notification> notifications = new ArrayList<>(source.getNotifications().size()); for (org.kaaproject.kaa.server.sync.Notification notification : source.getNotifications()) { notifications.add(new Notification( notification.getTopicIdAsLong(), convert(notification.getType()), notification.getUid(), notification.getSeqNumber(), notification.getBody())); } sync.setNotifications(notifications); } return sync; }
@Override public void onNotificationResponse(NotificationSyncResponse response) throws IOException { if (processor != null && clientState != null) { if (response.getResponseStatus() == SyncResponseStatus.NO_DELTA) { acceptedUnicastNotificationIds.clear(); } else { List<Topic> topics = response.getAvailableTopics(); if (topics != null) { clientState.setTopicListHash(TopicListHashCalculator.calculateTopicListHash(topics)); List<Notification> notifications = response.getNotifications(); if (notifications != null) { List<Notification> newNotifications = new ArrayList<>(notifications.size()); syncAck(response.getResponseStatus());
@Test public void testFilterStaleNotification() throws Exception { KaaClientState clientState = Mockito.mock(KaaClientState.class); NotificationProcessor notificationProcessor = Mockito.mock(NotificationProcessor.class); Mockito.when(clientState.updateTopicSubscriptionInfo(Mockito.anyLong(), Mockito.anyInt())).thenReturn(Boolean.FALSE); NotificationSyncResponse response = new NotificationSyncResponse(); response.setResponseStatus(SyncResponseStatus.DELTA); KaaChannelManager channelManagerMock = Mockito.mock(KaaChannelManager.class); NotificationTransport transport = new DefaultNotificationTransport(); transport.setChannelManager(channelManagerMock); transport.setNotificationProcessor(notificationProcessor); transport.setClientState(clientState); Notification nf1 = new Notification(1l, NotificationType.CUSTOM, null, 3, ByteBuffer.wrap(new byte[]{1, 2, 3})); Notification nf2 = new Notification(1l, NotificationType.CUSTOM, null, 3, ByteBuffer.wrap(new byte[]{1, 2, 3})); response.setNotifications(Arrays.asList(nf1, nf2)); transport.onNotificationResponse(response); List<Notification> expectedNotifications = Collections.emptyList(); Mockito.verify(notificationProcessor, Mockito.times(1)).notificationReceived(expectedNotifications); }
@Test public void testEmptyTopicListHash() throws Exception { final KaaClientState clientState = Mockito.mock(KaaClientState.class); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); Mockito.when(clientState.getTopicListHash()).thenReturn((Integer) args[0]); return null; } }).when(clientState).setTopicListHash(Mockito.anyInt()); NotificationProcessor notificationProcessor = Mockito.mock(NotificationProcessor.class); NotificationSyncResponse response = new NotificationSyncResponse(); response.setResponseStatus(SyncResponseStatus.DELTA); response.setAvailableTopics(Collections.<Topic>emptyList()); KaaChannelManager channelManagerMock = Mockito.mock(KaaChannelManager.class); NotificationTransport transport = new DefaultNotificationTransport(); transport.setChannelManager(channelManagerMock); transport.setNotificationProcessor(notificationProcessor); transport.setClientState(clientState); transport.onNotificationResponse(response); NotificationSyncRequest request = transport.createNotificationRequest(); Assert.assertEquals(TopicListHashCalculator.EMPTRY_LIST_HASH, request.getTopicListHash()); }
@Override public NotificationSyncResponse build() { try { NotificationSyncResponse record = new NotificationSyncResponse(); record.responseStatus = fieldSetFlags()[0] ? this.responseStatus : (org.kaaproject.kaa.common.endpoint.gen.SyncResponseStatus) defaultValue(fields()[0]); record.notifications = fieldSetFlags()[1] ? this.notifications : (java.util.List<org.kaaproject.kaa.common.endpoint.gen.Notification>) defaultValue(fields()[1]); record.availableTopics = fieldSetFlags()[2] ? this.availableTopics : (java.util.List<org.kaaproject.kaa.common.endpoint.gen.Topic>) defaultValue(fields()[2]); return record; } catch (Exception e) { throw new org.apache.avro.AvroRuntimeException(e); } } }
@Test public void testAcceptedUnicastNotification() throws Exception { KaaClientState clientState = Mockito.mock(KaaClientState.class); NotificationProcessor notificationProcessor = Mockito.mock(NotificationProcessor.class); NotificationSyncResponse response1 = new NotificationSyncResponse(); response1.setResponseStatus(SyncResponseStatus.DELTA); KaaChannelManager channelManagerMock = Mockito.mock(KaaChannelManager.class); NotificationTransport transport = new DefaultNotificationTransport(); transport.setChannelManager(channelManagerMock); transport.setNotificationProcessor(notificationProcessor); transport.setClientState(clientState); Notification nf1 = new Notification(1l, NotificationType.CUSTOM, "uid_1", 5, ByteBuffer.wrap(new byte[]{1, 2, 3})); Notification nf2 = new Notification(2l, NotificationType.CUSTOM, "uid_2", 3, ByteBuffer.wrap(new byte[]{1, 2, 3})); Notification nf3 = new Notification(3l, NotificationType.CUSTOM, "uid_2", 5, ByteBuffer.wrap(new byte[]{1, 2, 3})); response1.setNotifications(Arrays.asList(nf1, nf2, nf3)); transport.onNotificationResponse(response1); NotificationSyncRequest request1 = transport.createNotificationRequest(); Assert.assertTrue(request1.getAcceptedUnicastNotifications().size() == 2); NotificationSyncResponse response2 = new NotificationSyncResponse(); response2.setResponseStatus(SyncResponseStatus.NO_DELTA); transport.onNotificationResponse(response2); NotificationSyncRequest request2 = transport.createNotificationRequest(); Assert.assertNull(request2.getAcceptedUnicastNotifications()); }
@Test public void testTopicListHash() throws Exception { final KaaClientState clientState = Mockito.mock(KaaClientState.class); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); Mockito.when(clientState.getTopicListHash()).thenReturn((Integer) args[0]); return null; } }).when(clientState).setTopicListHash(Mockito.anyInt()); NotificationProcessor notificationProcessor = Mockito.mock(NotificationProcessor.class); NotificationSyncResponse response = new NotificationSyncResponse(); response.setResponseStatus(SyncResponseStatus.DELTA); List<Topic> topicList = new ArrayList<>(); topicList.add(new Topic(2l, null, SubscriptionType.MANDATORY_SUBSCRIPTION)); topicList.add(new Topic(1l, null, SubscriptionType.OPTIONAL_SUBSCRIPTION)); response.setAvailableTopics(topicList); KaaChannelManager channelManagerMock = Mockito.mock(KaaChannelManager.class); NotificationTransport transport = new DefaultNotificationTransport(); transport.setChannelManager(channelManagerMock); transport.setNotificationProcessor(notificationProcessor); transport.setClientState(clientState); transport.onNotificationResponse(response); NotificationSyncRequest request = transport.createNotificationRequest(); Assert.assertEquals(TopicListHashCalculator.calculateTopicListHash(topicList), request.getTopicListHash()); }
response.setConfigurationSyncResponse(new ConfigurationSyncResponse(SyncResponseStatus.DELTA, null, null)); response.setEventSyncResponse(new EventSyncResponse()); response.setNotificationSyncResponse(new NotificationSyncResponse(SyncResponseStatus.DELTA, null, null)); response.setProfileSyncResponse(new ProfileSyncResponse(SyncResponseStatus.DELTA)); response.setRedirectSyncResponse(new RedirectSyncResponse(1));
Mockito.when(clientState.updateTopicSubscriptionInfo(Mockito.anyLong(), Mockito.anyInt())).thenReturn(Boolean.TRUE); NotificationSyncResponse response = new NotificationSyncResponse(); response.setResponseStatus(SyncResponseStatus.DELTA); topicList.add(new Topic(topicId1, null, SubscriptionType.MANDATORY_SUBSCRIPTION)); topicList.add(new Topic(topicId2, null, SubscriptionType.OPTIONAL_SUBSCRIPTION)); response.setAvailableTopics(topicList); Notification nf3 = new Notification(topicId1, NotificationType.CUSTOM, null, 6, ByteBuffer.wrap(new byte[]{1, 2, 3})); response.setNotifications(Arrays.asList(nf3, nf1, nf2));
@Test public void testResponseWithNullTransports() throws Exception { KaaClientState state = Mockito.mock(KaaClientState.class); DefaultOperationDataProcessor operationsDataProcessor = new DefaultOperationDataProcessor(state); SyncResponse response = new SyncResponse(); response.setStatus(SyncResponseResultType.SUCCESS); response.setConfigurationSyncResponse(new ConfigurationSyncResponse(SyncResponseStatus.DELTA, null, null)); response.setEventSyncResponse(new EventSyncResponse()); response.setNotificationSyncResponse(new NotificationSyncResponse(SyncResponseStatus.DELTA, null, null)); response.setProfileSyncResponse(new ProfileSyncResponse(SyncResponseStatus.DELTA)); response.setRedirectSyncResponse(new RedirectSyncResponse(1)); response.setUserSyncResponse(new UserSyncResponse()); LogDeliveryStatus status = new LogDeliveryStatus(REQUEST_ID, SyncResponseResultType.SUCCESS, null); response.setLogSyncResponse(new LogSyncResponse(Collections.singletonList(status))); AvroByteArrayConverter<SyncResponse> converter = new AvroByteArrayConverter<>(SyncResponse.class); operationsDataProcessor.processResponse(converter.toByteArray(response)); }