@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); }
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; }
response.setResponseStatus(SyncResponseStatus.DELTA);
@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()); }
@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()); }