@Override public NotificationSyncRequest createNotificationRequest() { if (clientState != null) { NotificationSyncRequest request = new NotificationSyncRequest(); if (!acceptedUnicastNotificationIds.isEmpty()) { LOG.info("Accepted unicast Notifications: {}", acceptedUnicastNotificationIds.size()); request.setAcceptedUnicastNotifications(new ArrayList<>(acceptedUnicastNotificationIds)); } request.setSubscriptionCommands(sentNotificationCommands); request.setTopicListHash(clientState.getTopicListHash()); request.setTopicStates(getTopicStates()); return request; } return null; }
sync.setTopicListHash(source.getTopicListHash()); if (source.getAcceptedUnicastNotifications() != null) { sync.setAcceptedUnicastNotifications(new ArrayList<>( source.getAcceptedUnicastNotifications())); if (source.getSubscriptionCommands() != null) { List<SubscriptionCommand> commands = new ArrayList<>( source.getSubscriptionCommands().size()); : source.getSubscriptionCommands()) { SubscriptionCommand copy = new SubscriptionCommand(); copy.setTopicId(command.getTopicId()); if (source.getTopicStates() != null) { List<TopicState> states = new ArrayList<TopicState>(source.getTopicStates().size()); for (org.kaaproject.kaa.common.endpoint.gen.TopicState state : source.getTopicStates()) { states.add(new TopicState(state.getTopicId(), state.getSeqNumber()));
@Override public NotificationSyncRequest createEmptyNotificationRequest() { if (clientState != null) { NotificationSyncRequest request = new NotificationSyncRequest(); request.setTopicListHash(clientState.getTopicListHash()); request.setTopicStates(getTopicStates()); return request; } return null; }
@Test public void testCreateEmptyRequest() { NotificationTransport transport1 = new DefaultNotificationTransport(); Assert.assertNull(transport1.createEmptyNotificationRequest()); KaaClientState clientState = Mockito.mock(KaaClientState.class); NotificationTransport transport2 = new DefaultNotificationTransport(); Assert.assertNull(transport2.createEmptyNotificationRequest()); transport2.setClientState(clientState); NotificationSyncRequest request = transport2.createEmptyNotificationRequest(); Assert.assertNull(request.getAcceptedUnicastNotifications()); Assert.assertNull(request.getSubscriptionCommands()); Assert.assertEquals(TopicListHashCalculator.NULL_LIST_HASH, request.getTopicListHash()); }
Mockito.verify(clientState, Mockito.times(1)).updateTopicSubscriptionInfo(Mockito.eq(topicId1), Mockito.eq(6)); Assert.assertEquals("uid", transport.createNotificationRequest().getAcceptedUnicastNotifications().get(0));
@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 testTopicState() { KaaClientState clientState = Mockito.mock(KaaClientState.class); Map<Long, Integer> nfSubscriptions = new HashMap<>(); nfSubscriptions.put(1l, 10); nfSubscriptions.put(2l, 3); Mockito.when(clientState.getNfSubscriptions()).thenReturn(nfSubscriptions); NotificationTransport transport = new DefaultNotificationTransport(); Assert.assertNull(transport.createEmptyNotificationRequest()); transport.setClientState(clientState); NotificationSyncRequest request = transport.createEmptyNotificationRequest(); Assert.assertTrue(request.getTopicStates().size() == 2); } }
@Override public NotificationSyncRequest build() { try { NotificationSyncRequest record = new NotificationSyncRequest(); record.topicListHash = fieldSetFlags()[0] ? this.topicListHash : (java.lang.Integer) defaultValue(fields()[0]); record.topicStates = fieldSetFlags()[1] ? this.topicStates : (java.util.List<org.kaaproject.kaa.common.endpoint.gen.TopicState>) defaultValue(fields()[1]); record.acceptedUnicastNotifications = fieldSetFlags()[2] ? this.acceptedUnicastNotifications : (java.util.List<java.lang.String>) defaultValue(fields()[2]); record.subscriptionCommands = fieldSetFlags()[3] ? this.subscriptionCommands : (java.util.List<org.kaaproject.kaa.common.endpoint.gen.SubscriptionCommand>) defaultValue(fields()[3]); 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()); }