diff options
author | Richard Maudsley <richard.maudsley@isode.com> | 2014-03-11 11:14:25 (GMT) |
---|---|---|
committer | Swift Review <review@swift.im> | 2014-05-26 17:16:33 (GMT) |
commit | db8568aa6a9bbfa8dca9cdae696e8428a0068d89 (patch) | |
tree | 38ab6981542192128711049cb551dc03cf6f97a7 /test | |
parent | cafd510a0efc9df985999ceded57efa1d411de2e (diff) | |
download | stroke-db8568aa6a9bbfa8dca9cdae696e8428a0068d89.zip stroke-db8568aa6a9bbfa8dca9cdae696e8428a0068d89.tar.bz2 |
Added MAM parsers, serializers and tests.
Change-Id: I4e5368f9ac86446b7ebf976e2cb63d64ebefe7b2
Diffstat (limited to 'test')
10 files changed, 738 insertions, 0 deletions
diff --git a/test/com/isode/stroke/parser/payloadparsers/ForwardedParserTest.java b/test/com/isode/stroke/parser/payloadparsers/ForwardedParserTest.java new file mode 100644 index 0000000..f2fee5b --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/ForwardedParserTest.java @@ -0,0 +1,127 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class ForwardedParserTest { + + @Test + public void testParseIQ() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>" + + "<iq type=\"get\" from=\"kindanormal@example.com/IM\" to=\"stupidnewbie@example.com\" id=\"id0\"/>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(payload.getDelay().getStamp())); + + assertTrue(payload.getStanza() instanceof IQ); + IQ iq = (IQ)payload.getStanza(); + assertTrue(iq != null); + assertEquals("stupidnewbie@example.com", iq.getTo().toString()); + assertEquals("kindanormal@example.com/IM", iq.getFrom().toString()); + assertEquals("id0", iq.getID()); + assertEquals(IQ.Type.Get, iq.getType()); + } + + @Test + public void testParseMessage() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>" + + "<message xmlns=\"jabber:client\" to=\"juliet@capulet.lit/balcony\" from=\"romeo@montague.lit/orchard\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(payload.getDelay().getStamp())); + + assertTrue(payload.getStanza() instanceof Message); + Message message = (Message)payload.getStanza(); + assertTrue(message != null); + String expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."; + assertEquals(expectedBody, message.getBody()); + assertEquals(Message.Type.Chat, message.getType()); + assertEquals("juliet@capulet.lit/balcony", message.getTo().toString()); + assertEquals("romeo@montague.lit/orchard", message.getFrom().toString()); + } + + @Test + public void testParseMessageNoDelay() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<message xmlns=\"jabber:client\" to=\"juliet@capulet.lit/balcony\" from=\"romeo@montague.lit/orchard\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() == null); + + assertTrue(payload.getStanza() instanceof Message); + Message message = (Message)payload.getStanza(); + assertTrue(message != null); + String expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."; + assertEquals(expectedBody, message.getBody()); + assertEquals(Message.Type.Chat, message.getType()); + assertEquals("juliet@capulet.lit/balcony", message.getTo().toString()); + assertEquals("romeo@montague.lit/orchard", message.getFrom().toString()); + } + + @Test + public void testParsePresence() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay xmlns=\"urn:xmpp:delay\" stamp=\"2010-07-10T23:08:25Z\"/>" + + "<presence from=\"alice@wonderland.lit/rabbithole\" to=\"madhatter@wonderland.lit\" type=\"unavailable\"/>" + + "</forwarded>")); + + assertTrue(parser.getPayload() instanceof Forwarded); + Forwarded payload = (Forwarded)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(payload.getDelay().getStamp())); + + assertTrue(payload.getStanza() instanceof Presence); + Presence presence = (Presence)payload.getStanza(); + assertTrue(presence != null); + assertEquals("madhatter@wonderland.lit", presence.getTo().toString()); + assertEquals("alice@wonderland.lit/rabbithole", presence.getFrom().toString()); + assertEquals(Presence.Type.Unavailable, presence.getType()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/MAMArchivedParserTest.java b/test/com/isode/stroke/parser/payloadparsers/MAMArchivedParserTest.java new file mode 100644 index 0000000..9a657b7 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/MAMArchivedParserTest.java @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import org.junit.Test; +import com.isode.stroke.elements.MAMArchived; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class MAMArchivedParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<archived by=\"juliet@capulet.lit\" id=\"28482-98726-73623\" xmlns=\"urn:xmpp:mam:0\"/>")); + + MAMArchived payload = (MAMArchived)parser.getPayload(); + assertTrue(payload != null); + assertEquals(JID.fromString("juliet@capulet.lit"), payload.getBy()); + assertEquals("28482-98726-73623", payload.getID()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/MAMQueryParserTest.java b/test/com/isode/stroke/parser/payloadparsers/MAMQueryParserTest.java new file mode 100644 index 0000000..c98562f --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/MAMQueryParserTest.java @@ -0,0 +1,76 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import org.junit.Test; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.MAMQuery; +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.eventloop.DummyEventLoop; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class MAMQueryParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<query queryid=\"id0\" xmlns=\"urn:xmpp:mam:0\">" + + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<field type=\"text-single\" var=\"FORM_TYPE\">" + + "<value>urn:xmpp:mam:0</value>" + + "</field>" + + "<field type=\"text-single\" var=\"start\">" + + "<value>2010-08-07T00:00:00Z</value>" + + "</field>" + + "</x>" + + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>10</max>" + + "</set>" + + "</query>")); + + MAMQuery payload = (MAMQuery)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getQueryID() != null); + assertEquals("id0", payload.getQueryID()); + + assertTrue(payload.getForm() != null); + FormField.TextSingleFormField fieldType = (FormField.TextSingleFormField)payload.getForm().getField("FORM_TYPE"); + assertTrue(fieldType != null); + assertEquals("urn:xmpp:mam:0", fieldType.getValue()); + FormField.TextSingleFormField fieldStart = (FormField.TextSingleFormField)payload.getForm().getField("start"); + assertTrue(fieldStart != null); + assertEquals("2010-08-07T00:00:00Z", fieldStart.getValue()); + + assertTrue(payload.getResultSet() != null); + ResultSet resultSet = payload.getResultSet(); + assertTrue(resultSet.getMaxItems() != null); + assertEquals(resultSet.getMaxItems(), new Long(10)); + } + + @Test + public void testParseEmpty() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<query queryid=\"id0\" xmlns=\"urn:xmpp:mam:0\">" + + "</query>")); + + MAMQuery payload = (MAMQuery)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getQueryID() != null); + assertEquals("id0", payload.getQueryID()); + assertTrue(payload.getForm() == null); + assertTrue(payload.getResultSet() == null); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/MAMResultParserTest.java b/test/com/isode/stroke/parser/payloadparsers/MAMResultParserTest.java new file mode 100644 index 0000000..9071158 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/MAMResultParserTest.java @@ -0,0 +1,58 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.MAMResult; +import com.isode.stroke.elements.Message; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class MAMResultParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<result id=\"28482-98726-73623\" queryid=\"f27\" xmlns=\"urn:xmpp:mam:0\">" + + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>" + + "</result>")); + + MAMResult payload = (MAMResult)parser.getPayload(); + assertTrue(payload != null); + assertTrue(payload.getID() != null); + assertEquals("28482-98726-73623", payload.getID()); + assertTrue(payload.getQueryID() != null); + assertEquals("f27", payload.getQueryID()); + + Forwarded forwarded = payload.getPayload(); + assertTrue(forwarded.getDelay() != null); + assertEquals("2010-07-10T23:08:25Z", DateTime.dateToString(forwarded.getDelay().getStamp())); + + assertTrue(forwarded.getStanza() instanceof Message); + Message message = (Message)forwarded.getStanza(); + assertTrue(message != null); + String expectedBody = "Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."; + assertEquals(expectedBody, message.getBody()); + assertEquals(Message.Type.Chat, message.getType()); + assertEquals("juliet@capulet.lit/balcony", message.getTo().toString()); + assertEquals("romeo@montague.lit/orchard", message.getFrom().toString()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/ResultSetParserTest.java b/test/com/isode/stroke/parser/payloadparsers/ResultSetParserTest.java new file mode 100644 index 0000000..d77ae95 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/ResultSetParserTest.java @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import com.isode.stroke.elements.ResultSet; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class ResultSetParserTest { + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>100</max>" + + "<count>800</count>" + + "<first index=\"123\">stpeter@jabber.org</first>" + + "<last>peterpan@neverland.lit</last>" + + "<after>09af3-cc343-b409f</after>" + + "<before>decaf-badba-dbad1</before>" + + "</set>")); + + assertTrue(parser.getPayload() instanceof ResultSet); + ResultSet payload = (ResultSet)parser.getPayload(); + assertTrue(payload.getMaxItems() != null); + assertEquals(new Long(100), payload.getMaxItems()); + assertTrue(payload.getCount() != null); + assertEquals(new Long(800), payload.getCount()); + assertTrue(payload.getFirstID() != null); + assertEquals("stpeter@jabber.org", payload.getFirstID()); + assertTrue(payload.getFirstIDIndex() != null); + assertEquals(new Long(123), payload.getFirstIDIndex()); + assertTrue(payload.getLastID() != null); + assertEquals("peterpan@neverland.lit", payload.getLastID()); + assertTrue(payload.getAfter() != null); + assertEquals("09af3-cc343-b409f", payload.getAfter()); + assertTrue(payload.getBefore() != null); + assertEquals("decaf-badba-dbad1", payload.getBefore()); + } + + @Test + public void testParseFirstNoIndex() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse( + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<first>stpeter@jabber.org</first>" + + "</set>")); + + assertTrue(parser.getPayload() instanceof ResultSet); + ResultSet payload = (ResultSet)parser.getPayload(); + assertTrue(payload.getFirstID() != null); + assertEquals("stpeter@jabber.org", payload.getFirstID()); + assertTrue(payload.getFirstIDIndex() == null); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/ForwardedSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/ForwardedSerializerTest.java new file mode 100644 index 0000000..281be2e --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/ForwardedSerializerTest.java @@ -0,0 +1,114 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Delay; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; + +public class ForwardedSerializerTest { + + @Test + public void testSerializeIQ() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + IQ iq = IQ.createResult(JID.fromString("juliet@capulet.lit/balcony"), JID.fromString("romeo@montague.lit/orchard"), "id0", new Payload()); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(iq); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<iq from=\"romeo@montague.lit/orchard\" id=\"id0\" to=\"juliet@capulet.lit/balcony\" type=\"result\"/>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + @Test + public void testSerializeMessage() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + Message message = new Message(); + message.setType(Message.Type.Chat); + message.setTo(JID.fromString("juliet@capulet.lit/balcony")); + message.setFrom(JID.fromString("romeo@montague.lit/orchard")); + message.setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(message); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + @Test + public void testSerializeMessageNoDelay() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + Message message = new Message(); + message.setType(Message.Type.Chat); + message.setTo(JID.fromString("juliet@capulet.lit/balcony")); + message.setFrom(JID.fromString("romeo@montague.lit/orchard")); + message.setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(message); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + @Test + public void testSerializePresence() { + ForwardedSerializer serializer = new ForwardedSerializer(serializers_); + + Presence presence = new Presence(); + presence.setType(Presence.Type.Subscribe); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(presence); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + String expectedResult = + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<presence type=\"subscribe\"/>" + + "</forwarded>"; + + assertEquals(expectedResult, serializer.serialize(forwarded)); + } + + FullPayloadSerializerCollection serializers_ = new FullPayloadSerializerCollection(); +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerilaizerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerilaizerTest.java new file mode 100644 index 0000000..865ad0c --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/MAMArchivedSerilaizerTest.java @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.elements.MAMArchived; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; + +public class MAMArchivedSerilaizerTest { + + @Test + public void testSerialize() { + MAMArchivedSerializer serializer = new MAMArchivedSerializer(); + + MAMArchived archived = new MAMArchived(); + archived.setBy(JID.fromString("juliet@capulet.lit")); + archived.setID("28482-98726-73623"); + + String expectedResult = + "<archived by=\"juliet@capulet.lit\" id=\"28482-98726-73623\" xmlns=\"urn:xmpp:mam:0\"/>"; + + assertEquals(expectedResult, serializer.serialize(archived)); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java new file mode 100644 index 0000000..ab4a3fa --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java @@ -0,0 +1,61 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.MAMQuery; +import com.isode.stroke.elements.ResultSet; +import static org.junit.Assert.assertEquals; + +public class MAMQuerySerializerTest { + + @Test + public void testSerialize() { + MAMQuerySerializer serializer = new MAMQuerySerializer(); + + Form parameters = new Form(); + + FormField.TextSingleFormField fieldType = FormField.TextSingleFormField.create("urn:xmpp:mam:0"); + fieldType.setName("FORM_TYPE"); + parameters.addField(fieldType); + + FormField.TextSingleFormField fieldStart = FormField.TextSingleFormField.create("2010-08-07T00:00:00Z"); + fieldStart.setName("start"); + parameters.addField(fieldStart); + + ResultSet set = new ResultSet(); + set.setMaxItems(new Long(10)); + + MAMQuery query = new MAMQuery(); + query.setQueryID("id0"); + query.setForm(parameters); + query.setResultSet(set); + + String expectedResult = + "<query queryid=\"id0\" xmlns=\"urn:xmpp:mam:0\">" + + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<field type=\"text-single\" var=\"FORM_TYPE\">" + + "<value>urn:xmpp:mam:0</value>" + + "</field>" + + "<field type=\"text-single\" var=\"start\">" + + "<value>2010-08-07T00:00:00Z</value>" + + "</field>" + + "</x>" + + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>10</max>" + + "</set>" + + "</query>"; + + assertEquals(expectedResult, serializer.serialize(query)); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/MAMResultSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MAMResultSerializerTest.java new file mode 100644 index 0000000..128fdc0 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/MAMResultSerializerTest.java @@ -0,0 +1,57 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import com.isode.stroke.base.DateTime; +import com.isode.stroke.elements.Delay; +import com.isode.stroke.elements.Forwarded; +import com.isode.stroke.elements.MAMResult; +import com.isode.stroke.elements.Message; +import com.isode.stroke.jid.JID; +import static org.junit.Assert.assertEquals; + +public class MAMResultSerializerTest { + + @Test + public void testSerialize() { + MAMResultSerializer serializer = new MAMResultSerializer(serializers_); + + Message message = new Message(); + message.setType(Message.Type.Chat); + message.setTo(JID.fromString("juliet@capulet.lit/balcony")); + message.setFrom(JID.fromString("romeo@montague.lit/orchard")); + message.setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo."); + + Forwarded forwarded = new Forwarded(); + forwarded.setStanza(message); + forwarded.setDelay(new Delay(DateTime.stringToDate("2010-07-10T23:08:25Z"), null)); + + MAMResult result = new MAMResult(); + result.setID("28482-98726-73623"); + result.setQueryID("f27"); + result.setPayload(forwarded); + + String expectedResult = + "<result id=\"28482-98726-73623\" queryid=\"f27\" xmlns=\"urn:xmpp:mam:0\">" + + "<forwarded xmlns=\"urn:xmpp:forward:0\">" + + "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>" + + "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\">" + + "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>" + + "</message>" + + "</forwarded>" + + "</result>"; + + assertEquals(expectedResult, serializer.serialize(result)); + } + + FullPayloadSerializerCollection serializers_ = new FullPayloadSerializerCollection(); +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/ResultSetSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/ResultSetSerializerTest.java new file mode 100644 index 0000000..f4c92cf --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/ResultSetSerializerTest.java @@ -0,0 +1,110 @@ +/* +* Copyright (c) 2014 Kevin Smith and Remko Tronçon +* All rights reserved. +*/ + +/* +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ + +package com.isode.stroke.serializer.payloadserializers; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import com.isode.stroke.elements.ResultSet; + +public class ResultSetSerializerTest { + + @Test + public void testSerializeFull() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setMaxItems(new Long(100)); + resultSet.setCount(new Long(800)); + resultSet.setFirstIDIndex(new Long(123)); + resultSet.setFirstID("stpeter@jabber.org"); + resultSet.setLastID("peterpan@neverland.lit"); + resultSet.setAfter("09af3-cc343-b409f"); + resultSet.setBefore("decaf-badba-dbad1"); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>100</max>" + + "<count>800</count>" + + "<first index=\"123\">stpeter@jabber.org</first>" + + "<last>peterpan@neverland.lit</last>" + + "<after>09af3-cc343-b409f</after>" + + "<before>decaf-badba-dbad1</before>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeMaxItems() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setMaxItems(new Long(100)); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<max>100</max>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeEmptyBefore() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setBefore(new String()); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<before/>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeFirst() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setFirstID("stpeter@jabber.org"); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<first>stpeter@jabber.org</first>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } + + @Test + public void testSerializeFirstWithIndex() { + ResultSetSerializer serializer = new ResultSetSerializer(); + + ResultSet resultSet = new ResultSet(); + + resultSet.setFirstID("stpeter@jabber.org"); + resultSet.setFirstIDIndex(new Long(123)); + + String expectedResult = + "<set xmlns=\"http://jabber.org/protocol/rsm\">" + + "<first index=\"123\">stpeter@jabber.org</first>" + + "</set>"; + + assertEquals(expectedResult, serializer.serialize(resultSet)); + } +} |