@Test
  public void testNotProvidingNodeAttributeReturnsErrorStanza() throws Exception {
    BaseElement element = new BaseElement("subscriptions");
    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.modify, error.getType());
    Assert.assertEquals("nodeid-required", error.getApplicationConditionName());
  }
  @Test
  public void testNodeStoreExceptionResultsInInternalServerErrorStanza() throws Exception {
    Mockito.when(dataStore.nodeExists(Mockito.anyString())).thenThrow(NodeStoreException.class);

    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.wait, error.getType());
    Assert.assertEquals(PacketError.Condition.internal_server_error, error.getCondition());
  }
  @Test
  public void testInvalidSinceAttributesReturnsErrorStanza() throws Exception {

    request.getChildElement().element("user-items").addAttribute("since", "a week ago");

    userItemsGet.process(element, jid, request, null);
    Packet response = queue.poll();

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.modify, error.getType());
    Assert.assertEquals("invalid-since-value-provided", error.getApplicationConditionName());
  }
  @Test
  public void testNonExistantNodeRetunsErrorStanza() throws Exception {

    Mockito.when(dataStore.nodeExists(node)).thenReturn(false);
    event.setChannelManager(dataStore);

    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.cancel, error.getType());
    Assert.assertEquals(PacketError.Condition.item_not_found, error.getCondition());
  }
  @Test
  public void testNotProvidingSubscriptionAttributeReturnsErrorStanza() throws Exception {
    IQ request =
        toIq(
            readStanzaAsString("/iq/pubsub/subscribe/authorizationPendingGrantReply.stanza")
                .replaceFirst("subscription='subscribed'", ""));
    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.modify, error.getType());
    Assert.assertEquals(PacketError.Condition.bad_request, error.getCondition());
  }
  @Test
  public void testNotProvidingJidAttributeReturnsErrorStanza() throws Exception {
    IQ request =
        toIq(
            readStanzaAsString("/iq/pubsub/affiliation/affiliationChange.stanza")
                .replaceFirst("jid='*****@*****.**'", ""));
    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.modify, error.getType());
    Assert.assertEquals(PacketError.Condition.bad_request, error.getCondition());
  }
  @Test
  public void testUserWithoutSubscriptionReturnsErrorStanza() throws Exception {

    Mockito.when(dataStore.nodeExists(node)).thenReturn(true);
    Mockito.when(dataStore.getUserSubscription(node, jid)).thenReturn(null);
    event.setChannelManager(dataStore);

    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.auth, error.getType());
    Assert.assertEquals(PacketError.Condition.not_authorized, error.getCondition());
  }
  @Test
  public void testUserWhoIsntOwnerOrModeratorCantUpdateSubscription() throws Exception {
    NodeAffiliation subscriptionMock = Mockito.mock(NodeAffiliation.class);
    Mockito.when(subscriptionMock.getAffiliation()).thenReturn(Affiliations.member);

    Mockito.when(dataStore.nodeExists(node)).thenReturn(true);
    Mockito.when(dataStore.getUserAffiliation(node, jid)).thenReturn(subscriptionMock);
    event.setChannelManager(dataStore);

    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.auth, error.getType());
    Assert.assertEquals(PacketError.Condition.not_authorized, error.getCondition());
  }
  @Test
  public void testSubscribingUserMustHaveExistingSubscriptionToUpdate() throws Exception {
    NodeAffiliation subscriptionMockActor = Mockito.mock(NodeAffiliation.class);
    Mockito.when(subscriptionMockActor.getAffiliation()).thenReturn(Affiliations.owner);

    Mockito.when(dataStore.nodeExists(node)).thenReturn(true);
    Mockito.when(dataStore.getUserAffiliation(node, jid)).thenReturn(subscriptionMockActor);
    Mockito.when(dataStore.getUserSubscription(node, new JID(subscriber))).thenReturn(null);
    event.setChannelManager(dataStore);

    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.modify, error.getType());
    Assert.assertEquals(PacketError.Condition.unexpected_request, error.getCondition());
  }
  @Test
  public void testItIsNotPossibleToChangeTheAffiliationOfNodeOwner() throws Exception {

    NodeAffiliation affiliationSubscriber = Mockito.mock(NodeAffiliation.class);
    Mockito.when(affiliationSubscriber.getAffiliation()).thenReturn(Affiliations.owner);

    Mockito.when(channelManager.nodeExists(node)).thenReturn(true);

    Mockito.when(channelManager.getUserAffiliation(Mockito.anyString(), Mockito.any(JID.class)))
        .thenReturn(affiliationSubscriber);
    event.setChannelManager(channelManager);

    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);
    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.modify, error.getType());
    Assert.assertEquals(PacketError.Condition.not_acceptable, error.getCondition());
  }
  @Test
  public void testNodeStoreExceptionGeneratesAnErrorStanza() throws Exception {

    Mockito.when(
            channelManager.getUserFeedItems(
                Mockito.any(JID.class),
                Mockito.any(Date.class),
                Mockito.anyInt(),
                Mockito.any(GlobalItemID.class),
                Mockito.anyBoolean()))
        .thenThrow(new NodeStoreException());

    userItemsGet.process(element, jid, request, null);
    Packet response = queue.poll();

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.wait, error.getType());
    Assert.assertEquals(PacketError.Condition.internal_server_error, error.getCondition());
  }
  @Test
  public void testUserMustHaveExistingAffiliationToUpdate() throws Exception {
    NodeAffiliation affiliationMock = Mockito.mock(NodeAffiliation.class);
    Mockito.when(affiliationMock.getAffiliation()).thenReturn(Affiliations.owner);

    Mockito.when(channelManager.nodeExists(node)).thenReturn(true);
    // actorHasPermission
    Mockito.when(channelManager.getUserAffiliation(node, jid)).thenReturn(affiliationMock);
    // subscriberHasCurrentAffiliation
    Mockito.when(channelManager.getUserAffiliation(node, new JID("*****@*****.**")))
        .thenReturn(null);
    event.setChannelManager(channelManager);

    event.process(element, jid, request, null);
    Packet response = queue.poll(100, TimeUnit.MILLISECONDS);

    PacketError error = response.getError();
    Assert.assertNotNull(error);
    Assert.assertEquals(PacketError.Type.modify, error.getType());
    Assert.assertEquals(PacketError.Condition.unexpected_request, error.getCondition());
  }