summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-06-17 20:29:48 (GMT)
committerTarun Gupta <tarun1995gupta@gmail.com>2015-06-23 14:50:06 (GMT)
commit45e302d354d663eed61de58325ac162182497046 (patch)
treeb5115b957fc2d0d7ffecc43d6cf3c3cf64c94493
parentbb52f4e377fac262fe5b4f0ca2e1f6ed3669ee7d (diff)
downloadstroke-45e302d354d663eed61de58325ac162182497046.zip
stroke-45e302d354d663eed61de58325ac162182497046.tar.bz2
Add Roster Elements.
Adds RosterItemExchangePayload Element, its Parser and Serializer. Updates RosterItemPayload, its Parser and Serializer. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for: RosterItemPayload Parser and Serializer. RosterItemExchangePayload Parser and Serializer. All tests passes. Change-Id: I8d16a18290d9820cea6839af1f075da00a25db09
-rw-r--r--src/com/isode/stroke/elements/RosterItemExchangePayload.java137
-rw-r--r--src/com/isode/stroke/elements/RosterItemPayload.java10
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java1
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParser.java112
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/RosterParser.java43
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java1
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializer.java50
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java9
-rw-r--r--test/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParserTest.java60
-rw-r--r--test/com/isode/stroke/parser/payloadparsers/RosterParserTest.java117
-rw-r--r--test/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializerTest.java58
-rw-r--r--test/com/isode/stroke/serializer/payloadserializers/RosterSerializerTest.java105
12 files changed, 697 insertions, 6 deletions
diff --git a/src/com/isode/stroke/elements/RosterItemExchangePayload.java b/src/com/isode/stroke/elements/RosterItemExchangePayload.java
new file mode 100644
index 0000000..a94040f
--- /dev/null
+++ b/src/com/isode/stroke/elements/RosterItemExchangePayload.java
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2011 Jan Kaluza
+ * Licensed under the Simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.elements;
+
+import com.isode.stroke.elements.Payload;
+import com.isode.stroke.jid.JID;
+import com.isode.stroke.base.NotNull;
+import java.util.Vector;
+
+public class RosterItemExchangePayload extends Payload {
+
+ public static class Item {
+
+ public enum Action {
+ Add, Modify, Delete
+ };
+
+ private Action action;
+ private JID jid = new JID();
+ private String name = "";
+ private Vector<String> groups = new Vector<String>();
+
+ /**
+ * Default Constructor.
+ */
+ public Item() {
+ this(Item.Action.Add);
+ }
+
+ /**
+ * Parameterized Constructor.
+ * @param action, Not Null.
+ */
+ public Item(Action action) {
+ NotNull.exceptIfNull(action, "action");
+ this.action = action;
+ }
+
+ /**
+ * @return action, Not Null.
+ */
+ public Action getAction() {
+ return action;
+ }
+
+ /**
+ * @param action, Not Null.
+ */
+ public void setAction(Action action) {
+ NotNull.exceptIfNull(action, "action");
+ this.action = action;
+ }
+
+ /**
+ * @return jid, Not Null.
+ */
+ public JID getJID() {
+ return jid;
+ }
+
+ /**
+ * @param jid, Not Null.
+ */
+ public void setJID(JID jid) {
+ NotNull.exceptIfNull(jid, "jid");
+ this.jid = jid;
+ }
+
+ /**
+ * @return name, Not Null.
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @param name, Not Null.
+ */
+ public void setName(String name) {
+ NotNull.exceptIfNull(name, "name");
+ this.name = name;
+ }
+
+ /**
+ * @return groups, Not Null.
+ */
+ public Vector<String> getGroups() {
+ return groups;
+ }
+
+ /**
+ * @param groups, Not Null.
+ */
+ public void setGroups(Vector<String> groups) {
+ NotNull.exceptIfNull(groups, "groups");
+ this.groups = groups;
+ }
+
+ /**
+ * @param group, Not Null.
+ */
+ public void addGroup(String group) {
+ NotNull.exceptIfNull(group, "group");
+ groups.add(group);
+ }
+ }
+
+ private Vector<RosterItemExchangePayload.Item> items_ = new Vector<RosterItemExchangePayload.Item>();
+
+ public RosterItemExchangePayload() {
+
+ }
+
+ /**
+ * @param item, Not Null.
+ */
+ public void addItem(RosterItemExchangePayload.Item item) {
+ NotNull.exceptIfNull(item, "item");
+ items_.add(item);
+ }
+
+ /**
+ * @return items, Not Null.
+ */
+ public Vector<RosterItemExchangePayload.Item> getItems() {
+ return items_;
+ }
+}
diff --git a/src/com/isode/stroke/elements/RosterItemPayload.java b/src/com/isode/stroke/elements/RosterItemPayload.java
index c080915..1ede267 100644
--- a/src/com/isode/stroke/elements/RosterItemPayload.java
+++ b/src/com/isode/stroke/elements/RosterItemPayload.java
@@ -86,9 +86,19 @@ public class RosterItemPayload {
public boolean getSubscriptionRequested() {
return ask_;
}
+
+ public String getUnknownContent() {
+ return unknownContent_;
+ }
+
+ public void addUnknownContent(String c) {
+ unknownContent_ += c;
+ }
+
private JID jid_;
private String name_;
private Subscription subscription_;
private Collection<String> groups_;
private boolean ask_;
+ private String unknownContent_ = "";
}
diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java
index b591047..f5ae485 100644
--- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java
+++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java
@@ -35,6 +35,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl
addFactory(new GenericPayloadParserFactory<IsodeIQDelegationParser>("delegate", "http://isode.com/iq_delegation", IsodeIQDelegationParser.class));
addFactory(new GenericPayloadParserFactory<StorageParser>("storage", "storage:bookmarks", StorageParser.class));
addFactory(new RosterParserFactory());
+ addFactory(new GenericPayloadParserFactory<RosterItemExchangeParser>("x", "http://jabber.org/protocol/rosterx", RosterItemExchangeParser.class));
addFactory(new GenericPayloadParserFactory<IBBParser>("data", "http://jabber.org/protocol/ibb", IBBParser.class));
addFactory(new GenericPayloadParserFactory<DiscoInfoParser>("query", "http://jabber.org/protocol/disco#info", DiscoInfoParser.class));
addFactory(new GenericPayloadParserFactory<DiscoItemsParser>("query", "http://jabber.org/protocol/disco#items", DiscoItemsParser.class));
diff --git a/src/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParser.java b/src/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParser.java
new file mode 100644
index 0000000..7228fc3
--- /dev/null
+++ b/src/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParser.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2011 Jan Kaluza
+ * Licensed under the Simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+/*
+ * Copyright (c) 2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.parser.payloadparsers;
+
+import com.isode.stroke.parser.GenericPayloadParser;
+import com.isode.stroke.parser.AttributeMap;
+import com.isode.stroke.elements.RosterItemExchangePayload;
+import com.isode.stroke.base.NotNull;
+import com.isode.stroke.jid.JID;
+
+public class RosterItemExchangeParser extends GenericPayloadParser<RosterItemExchangePayload> {
+
+ private final int TopLevel = 0;
+ private final int PayloadLevel = 1;
+ private final int ItemLevel = 2;
+ private int level_ = 0;
+ private boolean inItem_;
+ private RosterItemExchangePayload.Item currentItem_;
+ private String currentText_ = "";
+
+ public RosterItemExchangeParser() {
+ super(new RosterItemExchangePayload());
+ this.level_ = TopLevel;
+ this.inItem_ = false;
+ }
+
+ /**
+ * @param element, NotNull.
+ * @param ns.
+ * @param attributes, NotNull.
+ */
+ @Override
+ public void handleStartElement(String element, String ns, AttributeMap attributes) {
+ NotNull.exceptIfNull(element, "element");
+ NotNull.exceptIfNull(attributes, "attributes");
+ if (level_ == PayloadLevel) {
+ if (element.equals("item")) {
+ inItem_ = true;
+
+ currentItem_ = new RosterItemExchangePayload.Item();
+
+ currentItem_.setJID(new JID(attributes.getAttribute("jid")));
+ currentItem_.setName(attributes.getAttribute("name"));
+
+ String action = attributes.getAttribute("action");
+ if (action.equals("add")) {
+ currentItem_.setAction(RosterItemExchangePayload.Item.Action.Add);
+ }
+ else if (action.equals("modify")) {
+ currentItem_.setAction(RosterItemExchangePayload.Item.Action.Modify);
+ }
+ else if (action.equals("delete")) {
+ currentItem_.setAction(RosterItemExchangePayload.Item.Action.Delete);
+ }
+ else {
+ // Add is default action according to XEP
+ currentItem_.setAction(RosterItemExchangePayload.Item.Action.Add);
+ }
+ }
+ }
+ else if (level_ == ItemLevel) {
+ if (element.equals("group")) {
+ currentText_ = "";
+ }
+ }
+ ++level_;
+ }
+
+ /**
+ * @param element, NotNull.
+ * @param ns.
+ */
+ @Override
+ public void handleEndElement(String element, String ns) {
+ NotNull.exceptIfNull(element, "element");
+ --level_;
+ if (level_ == PayloadLevel) {
+ if (inItem_) {
+ getPayloadInternal().addItem(currentItem_);
+ inItem_ = false;
+ }
+ }
+ else if (level_ == ItemLevel) {
+ if (element.equals("group")) {
+ currentItem_.addGroup(currentText_);
+ }
+ }
+ }
+
+ /**
+ * @param data, NotNull.
+ */
+ @Override
+ public void handleCharacterData(String data) {
+ NotNull.exceptIfNull(data, "data");
+ currentText_ += data;
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/parser/payloadparsers/RosterParser.java b/src/com/isode/stroke/parser/payloadparsers/RosterParser.java
index bb57c55..3d410d5 100644
--- a/src/com/isode/stroke/parser/payloadparsers/RosterParser.java
+++ b/src/com/isode/stroke/parser/payloadparsers/RosterParser.java
@@ -9,6 +9,7 @@ import com.isode.stroke.elements.RosterPayload;
import com.isode.stroke.jid.JID;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;
+import com.isode.stroke.parser.SerializingParser;
public class RosterParser extends GenericPayloadParser<RosterPayload> {
@@ -17,7 +18,13 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> {
}
public void handleStartElement(String element, String ns, AttributeMap attributes) {
- if (level_ == PayloadLevel) {
+ if (level_ == TopLevel) {
+ String ver = attributes.getAttributeValue("ver");
+ if (ver != null) {
+ getPayloadInternal().setVersion(ver);
+ }
+ }
+ else if (level_ == PayloadLevel) {
if (element.equals("item")) {
inItem_ = true;
currentItem_ = new RosterItemPayload();
@@ -30,7 +37,7 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> {
currentItem_.setSubscription(RosterItemPayload.Subscription.Both);
} else if ("to".equals(subscription)) {
currentItem_.setSubscription(RosterItemPayload.Subscription.To);
- } else if ("frome".equals(subscription)) {
+ } else if ("from".equals(subscription)) {
currentItem_.setSubscription(RosterItemPayload.Subscription.From);
} else if ("remove".equals(subscription)) {
currentItem_.setSubscription(RosterItemPayload.Subscription.Remove);
@@ -42,10 +49,19 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> {
currentItem_.setSubscriptionRequested();
}
}
- } else if (level_ == ItemLevel) {
+ }
+ else if (level_ == ItemLevel) {
if (element.equals("group")) {
currentText_ = "";
}
+ else {
+ assert(unknownContentParser_ == null);
+ unknownContentParser_ = new SerializingParser();
+ unknownContentParser_.handleStartElement(element, ns, attributes);
+ }
+ }
+ else if (unknownContentParser_ != null) {
+ unknownContentParser_.handleStartElement(element, ns, attributes);
}
++level_;
}
@@ -57,15 +73,29 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> {
getPayloadInternal().addItem(currentItem_);
inItem_ = false;
}
- } else if (level_ == ItemLevel) {
- if (element.equals("group")) {
+ }
+ else if (level_ == ItemLevel) {
+ if (unknownContentParser_ != null) {
+ unknownContentParser_.handleEndElement(element, ns);
+ currentItem_.addUnknownContent(unknownContentParser_.getResult());
+ unknownContentParser_ = null;
+ }
+ else if (element.equals("group")) {
currentItem_.addGroup(currentText_);
}
}
+ else if (unknownContentParser_ != null) {
+ unknownContentParser_.handleEndElement(element, ns);
+ }
}
public void handleCharacterData(String data) {
- currentText_ += data;
+ if (unknownContentParser_ != null) {
+ unknownContentParser_.handleCharacterData(data);
+ }
+ else {
+ currentText_ += data;
+ }
}
private final int TopLevel = 0;
private final int PayloadLevel = 1;
@@ -74,4 +104,5 @@ public class RosterParser extends GenericPayloadParser<RosterPayload> {
private boolean inItem_ = false;
private RosterItemPayload currentItem_;
private String currentText_;
+ private SerializingParser unknownContentParser_;
}
diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java
index 9b88d0c..9ad402c 100644
--- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java
+++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java
@@ -31,6 +31,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection
addSerializer(new JingleFileTransferHashSerializer());
addSerializer(new JingleContentPayloadSerializer());
addSerializer(new RosterSerializer());
+ addSerializer(new RosterItemExchangeSerializer());
addSerializer(new MUCPayloadSerializer());
addSerializer(new MUCDestroyPayloadSerializer());
addSerializer(new MUCAdminPayloadSerializer());
diff --git a/src/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializer.java
new file mode 100644
index 0000000..3aaab00
--- /dev/null
+++ b/src/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializer.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2011 Jan Kaluza
+ * Licensed under the Simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.serializer.payloadserializers;
+
+import com.isode.stroke.serializer.GenericPayloadSerializer;
+import com.isode.stroke.serializer.xml.XMLTextNode;
+import com.isode.stroke.serializer.xml.XMLElement;
+import com.isode.stroke.elements.RosterItemExchangePayload;
+import com.isode.stroke.base.NotNull;
+
+public class RosterItemExchangeSerializer extends GenericPayloadSerializer<RosterItemExchangePayload> {
+
+ public RosterItemExchangeSerializer() {
+ super(RosterItemExchangePayload.class);
+ }
+
+ public String serializePayload(RosterItemExchangePayload roster) {
+ XMLElement queryElement = new XMLElement("x", "http://jabber.org/protocol/rosterx");
+ for(RosterItemExchangePayload.Item item : roster.getItems()) {
+ XMLElement itemElement = new XMLElement("item");
+ itemElement.setAttribute("jid", item.getJID().toString());
+ itemElement.setAttribute("name", item.getName());
+
+ switch (item.getAction()) {
+ case Add: itemElement.setAttribute("action", "add"); break;
+ case Modify: itemElement.setAttribute("action", "modify"); break;
+ case Delete: itemElement.setAttribute("action", "delete"); break;
+ }
+
+ for(String group : item.getGroups()) {
+ XMLElement groupElement = new XMLElement("group");
+ groupElement.addNode(new XMLTextNode(group));
+ itemElement.addNode(groupElement);
+ }
+
+ queryElement.addNode(itemElement);
+ }
+
+ return queryElement.serialize();
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java
index b75d217..6ec1e58 100644
--- a/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java
+++ b/src/com/isode/stroke/serializer/payloadserializers/RosterSerializer.java
@@ -13,6 +13,7 @@ import com.isode.stroke.elements.RosterPayload;
import com.isode.stroke.serializer.GenericPayloadSerializer;
import com.isode.stroke.serializer.xml.XMLElement;
import com.isode.stroke.serializer.xml.XMLTextNode;
+import com.isode.stroke.serializer.xml.XMLRawTextNode;
/**
* Roster to string.
@@ -26,6 +27,9 @@ public class RosterSerializer extends GenericPayloadSerializer<RosterPayload> {
@Override
protected String serializePayload(RosterPayload roster) {
XMLElement queryElement = new XMLElement("query", "jabber:iq:roster");
+ if (roster.getVersion() != null) {
+ queryElement.setAttribute("ver", roster.getVersion());
+ }
for (RosterItemPayload item : roster.getItems()) {
XMLElement itemElement = new XMLElement("item");
itemElement.setAttribute("jid", item.getJID().toString());
@@ -53,6 +57,11 @@ public class RosterSerializer extends GenericPayloadSerializer<RosterPayload> {
itemElement.addNode(groupElement);
}
+
+ if (item.getUnknownContent().length() != 0) {
+ itemElement.addNode(new XMLRawTextNode(item.getUnknownContent()));
+ }
+
queryElement.addNode(itemElement);
}
diff --git a/test/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParserTest.java b/test/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParserTest.java
new file mode 100644
index 0000000..8bf2d52
--- /dev/null
+++ b/test/com/isode/stroke/parser/payloadparsers/RosterItemExchangeParserTest.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2011 Jan Kaluza
+ * Licensed under the Simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.parser.payloadparsers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import org.junit.Test;
+import com.isode.stroke.elements.RosterItemExchangePayload;
+import com.isode.stroke.parser.payloadparsers.RosterItemExchangeParser;
+import com.isode.stroke.parser.payloadparsers.PayloadsParserTester;
+import com.isode.stroke.eventloop.DummyEventLoop;
+import com.isode.stroke.jid.JID;
+import java.util.Vector;
+
+public class RosterItemExchangeParserTest {
+
+ public RosterItemExchangeParserTest() {
+
+ }
+
+ @Test
+ public void testParse() {
+ DummyEventLoop eventLoop = new DummyEventLoop();
+ PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
+ assertNotNull(parser.parse("<x xmlns=\"http://jabber.org/protocol/rosterx\">" +
+ "<item action=\"add\" jid=\"foo@bar.com\" name=\"Foo @ Bar\">" +
+ "<group>Group 1</group>" +
+ "<group>Group 2</group>" +
+ "</item>" +
+ "<item action=\"modify\" jid=\"baz@blo.com\" name=\"Baz\"/>" +
+ "</x>"));
+
+ RosterItemExchangePayload payload = (RosterItemExchangePayload)parser.getPayload();
+ Vector<RosterItemExchangePayload.Item> items = payload.getItems();
+
+ assertEquals(2, items.size());
+
+ assertEquals(new JID("foo@bar.com"), items.get(0).getJID());
+ assertEquals("Foo @ Bar", items.get(0).getName());
+ assertEquals(RosterItemExchangePayload.Item.Action.Add, items.get(0).getAction());
+ assertEquals(2, items.get(0).getGroups().size());
+ assertEquals("Group 1", items.get(0).getGroups().get(0));
+ assertEquals("Group 2", items.get(0).getGroups().get(1));
+
+ assertEquals(new JID("baz@blo.com"), items.get(1).getJID());
+ assertEquals("Baz", items.get(1).getName());
+ assertEquals(RosterItemExchangePayload.Item.Action.Modify, items.get(1).getAction());
+ assertEquals(0, items.get(1).getGroups().size());
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/parser/payloadparsers/RosterParserTest.java b/test/com/isode/stroke/parser/payloadparsers/RosterParserTest.java
new file mode 100644
index 0000000..45f1735
--- /dev/null
+++ b/test/com/isode/stroke/parser/payloadparsers/RosterParserTest.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.parser.payloadparsers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import com.isode.stroke.elements.RosterPayload;
+import com.isode.stroke.elements.RosterItemPayload;
+import com.isode.stroke.parser.payloadparsers.RosterParser;
+import com.isode.stroke.parser.payloadparsers.PayloadsParserTester;
+import com.isode.stroke.eventloop.DummyEventLoop;
+import com.isode.stroke.jid.JID;
+import java.util.ArrayList;
+import java.util.List;
+
+public class RosterParserTest {
+
+ public RosterParserTest() {
+
+ }
+
+ @Test
+ public void testParse() {
+ DummyEventLoop eventLoop = new DummyEventLoop();
+ PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
+ assertNotNull(parser.parse(
+ "<query xmlns='jabber:iq:roster'>" +
+ " <item jid='foo@bar.com' name='Foo @ Bar' subscription='from' ask='subscribe'>" +
+ " <group>Group 1</group>" +
+ " <group>Group 2</group>" +
+ " </item>" +
+ " <item jid='baz@blo.com' name='Baz'/>" +
+ "</query>"));
+
+ RosterPayload payload = (RosterPayload)(parser.getPayload());
+
+ assertNull(payload.getVersion());
+ List<RosterItemPayload> items = payload.getItems();
+
+ assertEquals(2, items.size());
+
+ assertEquals(new JID("foo@bar.com"), items.get(0).getJID());
+ assertEquals("Foo @ Bar", items.get(0).getName());
+ assertEquals(RosterItemPayload.Subscription.From, items.get(0).getSubscription());
+ assertTrue(items.get(0).getSubscriptionRequested());
+ assertEquals(2, items.get(0).getGroups().size());
+ assertEquals("Group 1", items.get(0).getGroups().toArray()[0]);
+ assertEquals("Group 2", items.get(0).getGroups().toArray()[1]);
+
+ assertEquals(new JID("baz@blo.com"), items.get(1).getJID());
+ assertEquals("Baz", items.get(1).getName());
+ assertEquals(RosterItemPayload.Subscription.None, items.get(1).getSubscription());
+ assertFalse(items.get(1).getSubscriptionRequested());
+ assertEquals(0, items.get(1).getGroups().size());
+ }
+
+ @Test
+ public void testParse_ItemWithUnknownContent() {
+ DummyEventLoop eventLoop = new DummyEventLoop();
+ PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
+ assertNotNull(parser.parse(
+ "<query xmlns='jabber:iq:roster'>" +
+ " <item jid='foo@bar.com' name='Foo @ Bar' subscription='from' ask='subscribe'>" +
+ " <group>Group 1</group>" +
+ " <foo xmlns=\"http://example.com\"><bar>Baz</bar></foo>" +
+ " <group>Group 2</group>" +
+ " <baz><fum>foo</fum></baz>" +
+ " </item>" +
+ "</query>"));
+
+ RosterPayload payload = (RosterPayload)(parser.getPayload());
+ List<RosterItemPayload> items = payload.getItems();
+
+ assertEquals(1, items.size());
+ assertEquals("Group 1", items.get(0).getGroups().toArray()[0]);
+ assertEquals("Group 2", items.get(0).getGroups().toArray()[1]);
+ assertEquals(
+ "<foo xmlns=\"http://example.com\"><bar xmlns=\"http://example.com\">Baz</bar></foo>" +
+ "<baz xmlns=\"jabber:iq:roster\"><fum xmlns=\"jabber:iq:roster\">foo</fum></baz>"
+ , items.get(0).getUnknownContent());
+ }
+
+ @Test
+ public void testParse_WithVersion() {
+ DummyEventLoop eventLoop = new DummyEventLoop();
+ PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
+ assertNotNull(parser.parse("<query xmlns='jabber:iq:roster' ver='ver10'/>"));
+
+ RosterPayload payload = (RosterPayload)(parser.getPayload());
+ assertNotNull(payload.getVersion());
+ assertEquals("ver10", payload.getVersion());
+ }
+
+ @Test
+ public void testParse_WithEmptyVersion() {
+ DummyEventLoop eventLoop = new DummyEventLoop();
+ PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
+ assertNotNull(parser.parse("<query xmlns='jabber:iq:roster' ver=''/>"));
+
+ RosterPayload payload = (RosterPayload)(parser.getPayload());
+ assertNotNull(payload.getVersion());
+ assertEquals("", payload.getVersion());
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializerTest.java
new file mode 100644
index 0000000..c732d94
--- /dev/null
+++ b/test/com/isode/stroke/serializer/payloadserializers/RosterItemExchangeSerializerTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2011 Jan Kaluza
+ * Licensed under the Simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.serializer.payloadserializers;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+import com.isode.stroke.serializer.payloadserializers.RosterItemExchangeSerializer;
+import com.isode.stroke.elements.RosterItemExchangePayload;
+import com.isode.stroke.jid.JID;
+
+public class RosterItemExchangeSerializerTest {
+
+ /**
+ * Default Constructor.
+ */
+ public RosterItemExchangeSerializerTest() {
+
+ }
+
+ @Test
+ public void testSerialize() {
+ RosterItemExchangeSerializer testling = new RosterItemExchangeSerializer();
+ RosterItemExchangePayload roster = new RosterItemExchangePayload();
+ RosterItemExchangePayload.Item item1 = new RosterItemExchangePayload.Item();
+ item1.setJID(new JID("foo@bar.com"));
+ item1.setName("Foo @ Bar");
+ item1.setAction(RosterItemExchangePayload.Item.Action.Add);
+ item1.addGroup("Group 1");
+ item1.addGroup("Group 2");
+ roster.addItem(item1);
+
+ RosterItemExchangePayload.Item item2 = new RosterItemExchangePayload.Item();
+ item2.setJID(new JID("baz@blo.com"));
+ item2.setName("Baz");
+ item2.setAction(RosterItemExchangePayload.Item.Action.Modify);
+ roster.addItem(item2);
+
+ String expectedResult =
+ "<x xmlns=\"http://jabber.org/protocol/rosterx\">" +
+ "<item action=\"add\" jid=\"foo@bar.com\" name=\"Foo @ Bar\">" +
+ "<group>Group 1</group>" +
+ "<group>Group 2</group>" +
+ "</item>" +
+ "<item action=\"modify\" jid=\"baz@blo.com\" name=\"Baz\"/>" +
+ "</x>";
+
+ assertEquals(expectedResult, testling.serialize(roster));
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/serializer/payloadserializers/RosterSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/RosterSerializerTest.java
new file mode 100644
index 0000000..ec74efe
--- /dev/null
+++ b/test/com/isode/stroke/serializer/payloadserializers/RosterSerializerTest.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.serializer.payloadserializers;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+import com.isode.stroke.serializer.payloadserializers.RosterSerializer;
+import com.isode.stroke.elements.RosterPayload;
+import com.isode.stroke.elements.RosterItemPayload;
+import com.isode.stroke.jid.JID;
+
+public class RosterSerializerTest {
+
+ /**
+ * Default Constructor.
+ */
+ public RosterSerializerTest() {
+
+ }
+
+ @Test
+ public void testSerialize() {
+ RosterSerializer testling = new RosterSerializer();
+ RosterPayload roster = new RosterPayload();
+
+ RosterItemPayload item1 = new RosterItemPayload();
+ item1.setJID(new JID("foo@bar.com"));
+ item1.setName("Foo @ Bar");
+ item1.setSubscription(RosterItemPayload.Subscription.From);
+ item1.addGroup("Group 1");
+ item1.addGroup("Group 2");
+ item1.setSubscriptionRequested();
+ roster.addItem(item1);
+
+ RosterItemPayload item2 = new RosterItemPayload();
+ item2.setJID(new JID("baz@blo.com"));
+ item2.setName("Baz");
+ roster.addItem(item2);
+
+ String expectedResult =
+ "<query xmlns=\"jabber:iq:roster\">" +
+ "<item ask=\"subscribe\" jid=\"foo@bar.com\" name=\"Foo @ Bar\" subscription=\"from\">" +
+ "<group>Group 1</group>" +
+ "<group>Group 2</group>" +
+ "</item>" +
+ "<item jid=\"baz@blo.com\" name=\"Baz\" subscription=\"none\"/>" +
+ "</query>";
+
+ assertEquals(expectedResult, testling.serialize(roster));
+ }
+
+ @Test
+ public void testSerialize_ItemWithUnknownContent() {
+ RosterSerializer testling = new RosterSerializer();
+ RosterPayload roster = new RosterPayload();
+
+ RosterItemPayload item = new RosterItemPayload();
+ item.setJID(new JID("baz@blo.com"));
+ item.setName("Baz");
+ item.addGroup("Group 1");
+ item.addGroup("Group 2");
+ item.addUnknownContent(
+ "<foo xmlns=\"http://example.com\"><bar xmlns=\"http://example.com\">Baz</bar></foo>" +
+ "<baz xmlns=\"jabber:iq:roster\"><fum xmlns=\"jabber:iq:roster\">foo</fum></baz>");
+ roster.addItem(item);
+
+ String expectedResult =
+ "<query xmlns=\"jabber:iq:roster\">" +
+ "<item jid=\"baz@blo.com\" name=\"Baz\" subscription=\"none\">" +
+ "<group>Group 1</group>" +
+ "<group>Group 2</group>" +
+ "<foo xmlns=\"http://example.com\"><bar xmlns=\"http://example.com\">Baz</bar></foo>" +
+ "<baz xmlns=\"jabber:iq:roster\"><fum xmlns=\"jabber:iq:roster\">foo</fum></baz>" +
+ "</item>" +
+ "</query>";
+
+ assertEquals(expectedResult, testling.serialize(roster));
+ }
+
+ @Test
+ public void testSerialize_WithVersion() {
+ RosterSerializer testling = new RosterSerializer();
+ RosterPayload roster = new RosterPayload();
+ roster.setVersion("ver20");
+
+ String expectedResult = "<query ver=\"ver20\" xmlns=\"jabber:iq:roster\"/>";
+
+ assertEquals(expectedResult, testling.serialize(roster));
+ }
+
+ @Test
+ public void testSerialize_WithEmptyVersion() {
+ RosterSerializer testling = new RosterSerializer();
+ RosterPayload roster = new RosterPayload();
+ roster.setVersion("");
+
+ String expectedResult = "<query ver=\"\" xmlns=\"jabber:iq:roster\"/>";
+
+ assertEquals(expectedResult, testling.serialize(roster));
+ }
+} \ No newline at end of file