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; }
topicList.add(new Topic(topicId1, null, SubscriptionType.MANDATORY_SUBSCRIPTION)); topicList.add(new Topic(topicId2, null, SubscriptionType.OPTIONAL_SUBSCRIPTION)); response.setAvailableTopics(topicList);
@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 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()); }