コード例 #1
0
  @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("[email protected]")));
    subscribers.add(new NodeSubscriptionMock(new JID("[email protected]")));

    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("[email protected]", notification.getTo().toString());
    notification = queue.poll(100, TimeUnit.MILLISECONDS);
    Assert.assertEquals("[email protected]", 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"));
  }
コード例 #2
0
 private void addDeleteNodeNotifications(String node, List<Packet> notifications)
     throws Exception {
   ResultSet<NodeSubscription> subscriptions = channelManager.getNodeSubscriptionListeners(node);
   Message notification = createNodeDeletedNotificationMessage(node);
   if (subscriptions != null) {
     for (NodeSubscription subscription : subscriptions) {
       notification.setTo(subscription.getListener().toString());
       notifications.add(notification.createCopy());
     }
   }
   Collection<JID> adminUsers = Configuration.getInstance().getAdminUsers();
   for (JID admin : adminUsers) {
     notification.setTo(admin);
     notifications.add(notification.createCopy());
   }
 }
コード例 #3
0
  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);
    }
  }
コード例 #4
0
  private void notifySubscribers(HashMap<String, String> configuration)
      throws NodeStoreException, InterruptedException {
    ResultSet<NodeSubscription> subscribers = channelManager.getNodeSubscriptionListeners(node);
    Document document = getDocumentHelper();
    Element message = document.addElement("message");
    message.addAttribute("remote-server-discover", "false");
    Element event = message.addElement("event");
    Element configurationElement = event.addElement("configuration");
    configurationElement.addAttribute("node", node);
    event.addNamespace("", Event.NAMESPACE);
    message.addAttribute("from", request.getTo().toString());
    message.addAttribute("type", "headline");
    Message rootElement = new Message(message);

    Element dataForm = configurationElement.addElement("x");
    dataForm.addAttribute("type", "result");
    dataForm.addNamespace("", DataForm.NAMESPACE);
    DataForm df = new DataForm(dataForm);

    FormField field;
    for (Map.Entry<String, String> entry : configuration.entrySet()) {
      String key = entry.getKey();
      Object value = entry.getValue();
      field = df.addField(key, null, null);
      field.addValue(value);
      // ...
    }

    for (NodeSubscription subscriber : subscribers) {
      Message notification = rootElement.createCopy();
      notification.setTo(subscriber.getListener());
      outQueue.put(notification);
    }

    Collection<JID> admins = getAdminUsers();
    for (JID admin : admins) {
      Message notification = rootElement.createCopy();
      notification.setTo(admin);
      outQueue.put(notification);
    }
  }
コード例 #5
0
  private void addUnsubscribeFromNodeNotifications(
      JID userJid, String node, List<Packet> notifications) throws NodeStoreException {

    ResultSet<NodeSubscription> subscribers = channelManager.getNodeSubscriptionListeners(node);

    Message messagePacket = createUnsubscribeNotification(userJid, node);
    if (subscribers != null) {
      for (NodeSubscription subscriber : subscribers) {
        Message notification = messagePacket.createCopy();
        notification.setTo(subscriber.getListener());
        notifications.add(notification);
      }
    }

    Collection<JID> adminUsers = Configuration.getInstance().getAdminUsers();
    for (JID admin : adminUsers) {
      Message notification = messagePacket.createCopy();
      notification.setTo(admin);
      notifications.add(notification);
    }
  }
コード例 #6
0
  @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("[email protected]"),
            new JID("[email protected]"),
            Subscriptions.none);

    /*
     * subscriptionMock Mockito.anyString(), Mockito.any(JID.class),
     * Mockito.any(JID.class), Mockito.eq(Subscriptions.none));
     */
    Mockito.verify(dataStore).addUserSubscription(subscriptionMock);
  }