@Test public void testPassingValidSubscriptionSendsOutExpectedNotifications() throws Exception { IQ request = toIq( readStanzaAsString("/iq/pubsub/subscribe/authorizationPendingGrantReply.stanza") .replaceFirst("subscription='subscribed'", "subscription='subscribed'")); NodeAffiliation subscriptionMockActor = Mockito.mock(NodeAffiliation.class); Mockito.when(subscriptionMockActor.getAffiliation()).thenReturn(Affiliations.owner); NodeSubscription subscriptionMockSubscriber = Mockito.mock(NodeSubscription.class); Mockito.when(subscriptionMockSubscriber.getSubscription()).thenReturn(Subscriptions.subscribed); Mockito.when(dataStore.nodeExists(node)).thenReturn(true); Mockito.when(dataStore.getUserAffiliation(node, jid)).thenReturn(subscriptionMockActor); Mockito.when(dataStore.getUserSubscription(node, new JID(subscriber))) .thenReturn(subscriptionMockSubscriber); event.setChannelManager(dataStore); ArrayList<NodeSubscription> subscribers = new ArrayList<NodeSubscription>(); subscribers.add(new NodeSubscriptionMock(new JID("*****@*****.**"))); subscribers.add(new NodeSubscriptionMock(new JID("*****@*****.**"))); Mockito.doReturn(new ResultSetImpl<NodeSubscription>(subscribers)) .when(dataStore) .getNodeSubscriptionListeners(Mockito.anyString()); event.setChannelManager(dataStore); event.process(element, jid, request, null); Assert.assertEquals(2, queue.size()); Packet notification = queue.poll(100, TimeUnit.MILLISECONDS); Assert.assertEquals("*****@*****.**", notification.getTo().toString()); notification = queue.poll(100, TimeUnit.MILLISECONDS); Assert.assertEquals("*****@*****.**", notification.getTo().toString()); Assert.assertEquals( node, notification.getElement().element("event").element("subscription").attributeValue("node")); Assert.assertTrue(notification.toXML().contains(JabberPubsub.NS_PUBSUB_EVENT)); Assert.assertEquals( "subscribed", notification .getElement() .element("event") .element("subscription") .attributeValue("subscription")); Assert.assertEquals( subscriber, notification.getElement().element("event").element("subscription").attributeValue("jid")); }
private void addDeleteItemNotifications(String node, String itemId, List<Packet> notifications) throws NodeStoreException { ResultSet<NodeSubscription> subscriptions = channelManager.getNodeSubscriptionListeners(node); Message notification = createItemDeleteNotificationMessage(node, itemId); if (subscriptions != null) { for (NodeSubscription subscription : subscriptions) { if (subscription.getSubscription().equals(Subscriptions.subscribed)) { notification.setTo(subscription.getListener().toString()); notifications.add(notification); } } } Collection<JID> adminUsers = Configuration.getInstance().getAdminUsers(); for (JID admin : adminUsers) { notification.setTo(admin); notifications.add(notification); } }
@Test @Ignore("Need to work out how to check set subscription values") public void testPassingInvalidSubscriptionTypeSetsSubscriptionToNone() throws Exception { IQ request = toIq( readStanzaAsString("/iq/pubsub/subscribe/authorizationPendingGrantReply.stanza") .replaceFirst( "subscription='subscribed'", "subscription='i-can-haz-all-the-items'")); NodeAffiliation subscriptionMockActor = Mockito.mock(NodeAffiliation.class); Mockito.when(subscriptionMockActor.getAffiliation()).thenReturn(Affiliations.owner); NodeSubscription subscriptionMockSubscriber = Mockito.mock(NodeSubscription.class); Mockito.when(subscriptionMockSubscriber.getSubscription()).thenReturn(Subscriptions.subscribed); Mockito.when(dataStore.nodeExists(node)).thenReturn(true); Mockito.when(dataStore.getUserAffiliation(node, jid)).thenReturn(subscriptionMockActor); Mockito.doCallRealMethod() .when(dataStore) .addUserSubscription(Mockito.any(NodeSubscription.class)); Mockito.when(dataStore.getUserSubscription(node, new JID(subscriber))) .thenReturn(subscriptionMockSubscriber); event.setChannelManager(dataStore); event.process(element, jid, request, null); NodeSubscription subscriptionMock = new NodeSubscriptionImpl( node, new JID("*****@*****.**"), new JID("*****@*****.**"), Subscriptions.none); /* * subscriptionMock Mockito.anyString(), Mockito.any(JID.class), * Mockito.any(JID.class), Mockito.eq(Subscriptions.none)); */ Mockito.verify(dataStore).addUserSubscription(subscriptionMock); }