From 45e302d354d663eed61de58325ac162182497046 Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Thu, 18 Jun 2015 01:59:48 +0530 Subject: 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 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 groups = new Vector(); + + /** + * 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 getGroups() { + return groups; + } + + /** + * @param groups, Not Null. + */ + public void setGroups(Vector 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 items_ = new Vector(); + + 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 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 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("delegate", "http://isode.com/iq_delegation", IsodeIQDelegationParser.class)); addFactory(new GenericPayloadParserFactory("storage", "storage:bookmarks", StorageParser.class)); addFactory(new RosterParserFactory()); + addFactory(new GenericPayloadParserFactory("x", "http://jabber.org/protocol/rosterx", RosterItemExchangeParser.class)); addFactory(new GenericPayloadParserFactory("data", "http://jabber.org/protocol/ibb", IBBParser.class)); addFactory(new GenericPayloadParserFactory("query", "http://jabber.org/protocol/disco#info", DiscoInfoParser.class)); addFactory(new GenericPayloadParserFactory("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 { + + 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 { @@ -17,7 +18,13 @@ public class RosterParser extends GenericPayloadParser { } 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 { 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 { 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 { 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 { 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 { + + 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 { @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 { 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("" + + "" + + "Group 1" + + "Group 2" + + "" + + "" + + "")); + + RosterItemExchangePayload payload = (RosterItemExchangePayload)parser.getPayload(); + Vector 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( + "" + + " " + + " Group 1" + + " Group 2" + + " " + + " " + + "")); + + RosterPayload payload = (RosterPayload)(parser.getPayload()); + + assertNull(payload.getVersion()); + List 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( + "" + + " " + + " Group 1" + + " Baz" + + " Group 2" + + " foo" + + " " + + "")); + + RosterPayload payload = (RosterPayload)(parser.getPayload()); + List 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( + "Baz" + + "foo" + , items.get(0).getUnknownContent()); + } + + @Test + public void testParse_WithVersion() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertNotNull(parser.parse("")); + + 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("")); + + 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 = + "" + + "" + + "Group 1" + + "Group 2" + + "" + + "" + + ""; + + 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 = + "" + + "" + + "Group 1" + + "Group 2" + + "" + + "" + + ""; + + 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( + "Baz" + + "foo"); + roster.addItem(item); + + String expectedResult = + "" + + "" + + "Group 1" + + "Group 2" + + "Baz" + + "foo" + + "" + + ""; + + assertEquals(expectedResult, testling.serialize(roster)); + } + + @Test + public void testSerialize_WithVersion() { + RosterSerializer testling = new RosterSerializer(); + RosterPayload roster = new RosterPayload(); + roster.setVersion("ver20"); + + String expectedResult = ""; + + assertEquals(expectedResult, testling.serialize(roster)); + } + + @Test + public void testSerialize_WithEmptyVersion() { + RosterSerializer testling = new RosterSerializer(); + RosterPayload roster = new RosterPayload(); + roster.setVersion(""); + + String expectedResult = ""; + + assertEquals(expectedResult, testling.serialize(roster)); + } +} \ No newline at end of file -- cgit v0.10.2-6-g49f6