2018-03-20 11:49:32 +01:00
|
|
|
/**
|
|
|
|
*
|
2018-04-19 11:05:43 +02:00
|
|
|
* Copyright 2018 Timothy Pitt, Florian Schmaus
|
2018-03-20 11:49:32 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.pubsub;
|
|
|
|
|
2020-04-06 13:25:07 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2018-03-20 11:49:32 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.ThreadedDummyConnection;
|
2018-04-19 11:05:43 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2018-03-20 11:49:32 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2018-04-19 11:05:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.IQ.Type;
|
|
|
|
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
2018-03-20 11:49:32 +01:00
|
|
|
import org.jivesoftware.smack.test.util.TestUtils;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2019-05-06 22:06:13 +02:00
|
|
|
import org.jivesoftware.smack.xml.XmlPullParser;
|
2018-04-19 11:05:43 +02:00
|
|
|
|
|
|
|
import org.jivesoftware.smackx.pubsub.Affiliation.AffiliationNamespace;
|
2018-03-20 11:49:32 +01:00
|
|
|
import org.jivesoftware.smackx.pubsub.packet.PubSub;
|
2018-04-19 11:05:43 +02:00
|
|
|
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
|
2018-03-20 11:49:32 +01:00
|
|
|
|
2018-04-19 11:05:43 +02:00
|
|
|
import org.jivesoftware.util.ConnectionUtils;
|
|
|
|
import org.jivesoftware.util.Protocol;
|
2020-04-06 13:25:07 +02:00
|
|
|
import org.junit.jupiter.api.Test;
|
2018-03-20 11:49:32 +01:00
|
|
|
import org.jxmpp.jid.JidTestUtil;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
|
2018-04-19 11:05:43 +02:00
|
|
|
public class PubSubNodeTest extends SmackTestSuite {
|
2018-03-20 11:49:32 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void modifySubscriptionsAsOwnerTest() throws InterruptedException, SmackException, IOException, XMPPException, Exception {
|
|
|
|
ThreadedDummyConnection con = ThreadedDummyConnection.newInstance();
|
|
|
|
PubSubManager mgr = new PubSubManager(con, JidTestUtil.PUBSUB_EXAMPLE_ORG);
|
|
|
|
Node testNode = new LeafNode(mgr, "princely_musings");
|
|
|
|
|
|
|
|
List<Subscription> ChangeSubs = Arrays.asList(
|
|
|
|
new Subscription(JidCreate.from("romeo@montague.org"), Subscription.State.subscribed),
|
|
|
|
new Subscription(JidCreate.from("juliet@capulet.org"), Subscription.State.none)
|
|
|
|
);
|
|
|
|
testNode.modifySubscriptionsAsOwner(ChangeSubs);
|
|
|
|
|
|
|
|
PubSub request = con.getSentPacket();
|
|
|
|
|
|
|
|
assertEquals("http://jabber.org/protocol/pubsub#owner", request.getChildElementNamespace());
|
|
|
|
assertEquals("pubsub", request.getChildElementName());
|
|
|
|
|
2019-02-04 13:27:41 +01:00
|
|
|
XmlPullParser parser = TestUtils.getIQParser(request.toXML().toString());
|
2018-03-20 11:49:32 +01:00
|
|
|
PubSub pubsubResult = (PubSub) PacketParserUtils.parseIQ(parser);
|
2018-04-19 11:05:43 +02:00
|
|
|
SubscriptionsExtension subElem = pubsubResult.getExtension(PubSubElementType.SUBSCRIPTIONS_OWNER);
|
2018-03-20 11:49:32 +01:00
|
|
|
List<Subscription> subscriptions = subElem.getSubscriptions();
|
|
|
|
assertEquals(2, subscriptions.size());
|
|
|
|
|
|
|
|
Subscription sub1 = subscriptions.get(0);
|
|
|
|
assertEquals("romeo@montague.org", sub1.getJid().toString());
|
|
|
|
assertEquals(Subscription.State.subscribed, sub1.getState());
|
|
|
|
|
|
|
|
Subscription sub2 = subscriptions.get(1);
|
|
|
|
assertEquals("juliet@capulet.org", sub2.getJid().toString());
|
|
|
|
assertEquals(Subscription.State.none, sub2.getState());
|
|
|
|
}
|
2018-04-19 11:05:43 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getAffiliationsAsOwnerTest() throws InterruptedException, SmackException, IOException, XMPPException, Exception {
|
|
|
|
Protocol protocol = new Protocol();
|
|
|
|
XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, JidTestUtil.FULL_JID_1_RESOURCE_1);
|
|
|
|
|
|
|
|
PubSubManager mgr = new PubSubManager(connection, JidTestUtil.PUBSUB_EXAMPLE_ORG);
|
|
|
|
Node testNode = new LeafNode(mgr, "princely_musings");
|
|
|
|
|
|
|
|
List<Affiliation> affiliations = Arrays.asList(
|
|
|
|
new Affiliation(JidTestUtil.BARE_JID_1, Affiliation.Type.member),
|
|
|
|
new Affiliation(JidTestUtil.BARE_JID_2, Affiliation.Type.publisher)
|
|
|
|
);
|
|
|
|
AffiliationsExtension affiliationsExtension = new AffiliationsExtension(AffiliationNamespace.owner, affiliations);
|
|
|
|
PubSub response = new PubSub(JidTestUtil.PUBSUB_EXAMPLE_ORG, Type.result, PubSubNamespace.owner);
|
|
|
|
response.addExtension(affiliationsExtension);
|
|
|
|
protocol.addResponse(response);
|
|
|
|
|
|
|
|
List<Affiliation> returnedAffiliations = testNode.getAffiliationsAsOwner();
|
|
|
|
|
|
|
|
PubSub request = (PubSub) protocol.getRequests().get(0);
|
|
|
|
assertEquals("http://jabber.org/protocol/pubsub#owner", request.getChildElementNamespace());
|
|
|
|
assertEquals("pubsub", request.getChildElementName());
|
|
|
|
|
|
|
|
Affiliation affiliationOne = returnedAffiliations.get(0);
|
|
|
|
assertEquals(affiliationOne.getJid(), JidTestUtil.BARE_JID_1);
|
|
|
|
assertEquals(affiliationOne.getAffiliation(), Affiliation.Type.member);
|
|
|
|
|
|
|
|
Affiliation affiliationTwo = returnedAffiliations.get(1);
|
|
|
|
assertEquals(affiliationTwo.getJid(), JidTestUtil.BARE_JID_2);
|
|
|
|
assertEquals(affiliationTwo.getAffiliation(), Affiliation.Type.publisher);
|
|
|
|
}
|
2018-03-20 11:49:32 +01:00
|
|
|
}
|