diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-20 01:02:12 (GMT) |
---|---|---|
committer | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-27 07:12:55 (GMT) |
commit | a673d269487fd86efe7f9c5f9b4cd1c00cab556d (patch) | |
tree | 10145697d3153a8a170361c5355ad9bccee21776 /test/com | |
parent | 27212e007077418d18014286a46723fa26693864 (diff) | |
download | stroke-a673d269487fd86efe7f9c5f9b4cd1c00cab556d.zip stroke-a673d269487fd86efe7f9c5f9b4cd1c00cab556d.tar.bz2 |
Adds Form Elements and Version Element.
Adds FormPage, FormReportedRef, FormSection, FormText and Version Elements.
Updates Form Elements, its Parser And Serializer.
Updates SearchPayload Element, its Parser And Serializer.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Adds Search Payload Parser and Serializer Test.
Updates tests for Form Parser and Serializer.
Updates InBandRegistrationPayloadSerializer Test and MAMQuerySerializerTest.
All tests passes.
Change-Id: I8c620a3db39fe433bc9a5478b98d5caeaf9ed40b
Diffstat (limited to 'test/com')
6 files changed, 978 insertions, 373 deletions
diff --git a/test/com/isode/stroke/parser/payloadparsers/FormParserTest.java b/test/com/isode/stroke/parser/payloadparsers/FormParserTest.java index 4ab8190..10efcb9 100644 --- a/test/com/isode/stroke/parser/payloadparsers/FormParserTest.java +++ b/test/com/isode/stroke/parser/payloadparsers/FormParserTest.java @@ -10,151 +10,266 @@ package com.isode.stroke.parser.payloadparsers; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; - +import static org.junit.Assert.assertNotNull; import org.junit.BeforeClass; import org.junit.Test; import com.isode.stroke.elements.Form; import com.isode.stroke.elements.Form.Type; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.FormField.Option; +import com.isode.stroke.elements.FormItem; +import com.isode.stroke.elements.FormSection; +import com.isode.stroke.elements.FormText; +import com.isode.stroke.elements.FormPage; +import com.isode.stroke.elements.FormReportedRef; import com.isode.stroke.elements.Payload; import com.isode.stroke.eventloop.DummyEventLoop; import com.isode.stroke.jid.JID; +import java.util.List; +import java.util.ArrayList; public class FormParserTest { - @BeforeClass - public static void init() throws Exception { - } - - private static Form parse(String xmlString) { - DummyEventLoop eventLoop = new DummyEventLoop(); - PayloadsParserTester parser = new PayloadsParserTester(eventLoop); - assertTrue(parser.parse(xmlString)); - - Payload payload = null; - do { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - eventLoop.processEvents(); - payload = parser.getPayload(); - } while (payload == null); - - return (Form) payload; - } - - @Test - public void testParse_FormInformation() throws Exception { - Form payload = parse("<x type=\"submit\" xmlns=\"jabber:x:data\">" - + "<title>Bot Configuration</title>" - + "<instructions>Hello!</instructions>" - + "<instructions>Fill out this form to configure your new bot!</instructions>" - + "</x>"); - assertEquals("Bot Configuration", payload.getTitle()); - assertEquals("Hello!\nFill out this form to configure your new bot!", - payload.getInstructions()); - assertEquals(Type.SUBMIT_TYPE, payload.getType()); - } - - @Test - public void testParse() { - Form payload = parse("<x type=\"form\" xmlns=\"jabber:x:data\">" - + "<field type=\"hidden\" var=\"FORM_TYPE\">" - + "<value>jabber:bot</value>" - + "</field>" - + "<reported>" - + "<field var=\"field name\" label=\"description\" type=\"unknown\">" - + "<value>someText</value>" - + "</field>" - + "</reported>" - + "<item>" - + "<field label=\"itemField\">" - + "<value>itemValue</value>" - + "</field>" - + "</item>" - + "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>" - + "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>" - + "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>" - + "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">" - + "<required/>" - + "<value>1</value>" - + "</field>" - + "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>" - + "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">" - + "<value>news</value>" - + "<value>search</value>" - + "<option label=\"Contests\"><value>contests</value></option>" - + "<option label=\"News\"><value>news</value></option>" - + "<option label=\"Polls\"><value>polls</value></option>" - + "<option label=\"Reminders\"><value>reminders</value></option>" - + "<option label=\"Search\"><value>search</value></option>" - + "</field>" - + "<field label=\"Maximum number of subscribers\" type=\"list-single\" var=\"maxsubs\">" - + "<value>20</value>" - + "<option label=\"10\"><value>10</value></option>" - + "<option label=\"20\"><value>20</value></option>" - + "<option label=\"30\"><value>30</value></option>" - + "<option label=\"50\"><value>50</value></option>" - + "<option label=\"100\"><value>100</value></option>" - + "<option label=\"None\"><value>none</value></option>" - + "</field>" - + "<field label=\"People to invite\" type=\"jid-multi\" var=\"invitelist\">" - + "<desc>Tell all your friends about your new bot!</desc>" - + "<value>foo@bar.com</value>" + "<value>baz@fum.org</value>" - + "</field>" + "<field var=\"untyped\">" + "<value>foo</value>" - + "</field>" - + "</x>"); - - - - assertEquals(10, payload.getFields().size()); - - assertEquals("jabber:bot", payload.getFields().get(0).getValues().get(0)); - - assertEquals("FORM_TYPE", payload.getFields().get(0).getName()); - assertTrue(!payload.getFields().get(0).getRequired()); - - assertEquals("description", payload.getReportedFields().get(0).getLabel()); - assertEquals("someText", payload.getReportedFields().get(0).getValues().get(0)); - assertEquals("itemField", payload.getItems().get(0).getItemFields().get(0).getLabel()); - assertEquals("itemValue", payload.getItems().get(0).getItemFields().get(0).getValues().get(0)); - - assertEquals("Section 1: Bot Info", payload.getFields().get(1).getValues().get(0)); - - assertEquals("The name of your bot", payload.getFields().get(2) - .getLabel()); - - assertEquals("This is a bot.\nA quite good one actually", - payload.getFields().get(3).getTextMultiValue()); - - assertEquals(Boolean.TRUE, payload.getFields() - .get(4).getBoolValue()); - assertTrue(payload.getFields().get(4).getRequired()); - assertEquals("1", payload.getFields().get(4).getValues().get(0)); - - assertEquals("news", payload.getFields().get(6).getValues().get(0)); - assertEquals("search", payload.getFields().get(6).getValues().get(1)); - assertEquals(5, payload.getFields().get(6).getOptions().size()); - assertEquals("Contests", - payload.getFields().get(6).getOptions().get(0).label_); - assertEquals("contests", - payload.getFields().get(6).getOptions().get(0).value_); - assertEquals("News", - payload.getFields().get(6).getOptions().get(1).label_); - assertEquals("news", - payload.getFields().get(6).getOptions().get(1).value_); - - assertEquals("20", payload.getFields().get(7).getValues().get(0)); - - assertEquals(new JID("foo@bar.com"), payload - .getFields().get(8).getJIDMultiValue(0)); - assertEquals(new JID("baz@fum.org"), payload - .getFields().get(8).getJIDMultiValue(1)); - assertEquals("Tell all your friends about your new bot!", payload - .getFields().get(8).getDescription()); - - assertEquals("foo", payload.getFields().get(9).getValues().get(0)); - } + @BeforeClass + public static void init() throws Exception { + } + + private static Form parse(String xmlString) { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertTrue(parser.parse(xmlString)); + + Payload payload = null; + do { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + eventLoop.processEvents(); + payload = parser.getPayload(); + } while (payload == null); + + return (Form) payload; + } + + @Test + public void testParse_FormInformation() throws Exception { + Form payload = parse("<x type=\"submit\" xmlns=\"jabber:x:data\">" + + "<title>Bot Configuration</title>" + + "<instructions>Hello!</instructions>" + + "<instructions>Fill out this form to configure your new bot!</instructions>" + + "</x>"); + assertEquals("Bot Configuration", payload.getTitle()); + assertEquals("Hello!\nFill out this form to configure your new bot!", + payload.getInstructions()); + assertEquals(Type.SUBMIT_TYPE, payload.getType()); + } + + @Test + public void testParse() { + Form payload = parse("<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<field type=\"hidden\" var=\"FORM_TYPE\">" + + "<value>jabber:bot</value>" + + "</field>" + + "<reported>" + + "<field var=\"field name\" label=\"description\" type=\"unknown\">" + + "<value>someText</value>" + + "</field>" + + "</reported>" + + "<item>" + + "<field label=\"itemField\">" + + "<value>itemValue</value>" + + "</field>" + + "</item>" + + "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>" + + "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>" + + "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>" + + "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">" + + "<required/>" + + "<value>1</value>" + + "</field>" + + "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>" + + "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">" + + "<value>news</value>" + + "<value>search</value>" + + "<option label=\"Contests\"><value>contests</value></option>" + + "<option label=\"News\"><value>news</value></option>" + + "<option label=\"Polls\"><value>polls</value></option>" + + "<option label=\"Reminders\"><value>reminders</value></option>" + + "<option label=\"Search\"><value>search</value></option>" + + "</field>" + + "<field label=\"Maximum number of subscribers\" type=\"list-single\" var=\"maxsubs\">" + + "<value>20</value>" + + "<option label=\"10\"><value>10</value></option>" + + "<option label=\"20\"><value>20</value></option>" + + "<option label=\"30\"><value>30</value></option>" + + "<option label=\"50\"><value>50</value></option>" + + "<option label=\"100\"><value>100</value></option>" + + "<option label=\"None\"><value>none</value></option>" + + "</field>" + + "<field label=\"People to invite\" type=\"jid-multi\" var=\"invitelist\">" + + "<desc>Tell all your friends about your new bot!</desc>" + + "<value>foo@bar.com</value>" + "<value>baz@fum.org</value>" + + "</field>" + "<field var=\"untyped\">" + "<value>foo</value>" + + "</field>" + + "</x>"); + + + + assertEquals(10, payload.getFields().size()); + + assertEquals("jabber:bot", payload.getFields().get(0).getValues().get(0)); + + assertEquals("FORM_TYPE", payload.getFields().get(0).getName()); + assertTrue(!payload.getFields().get(0).getRequired()); + + assertEquals("description", payload.getReportedFields().get(0).getLabel()); + assertEquals("someText", payload.getReportedFields().get(0).getValues().get(0)); + assertEquals("itemField", payload.getItems().get(0).getItemFields().get(0).getLabel()); + assertEquals("itemValue", payload.getItems().get(0).getItemFields().get(0).getValues().get(0)); + + assertEquals("Section 1: Bot Info", payload.getFields().get(1).getValues().get(0)); + + assertEquals("The name of your bot", payload.getFields().get(2) + .getLabel()); + + assertEquals("This is a bot.\nA quite good one actually", + payload.getFields().get(3).getTextMultiValue()); + + assertEquals(Boolean.TRUE, payload.getFields() + .get(4).getBoolValue()); + assertTrue(payload.getFields().get(4).getRequired()); + assertEquals("1", payload.getFields().get(4).getValues().get(0)); + + assertEquals("news", payload.getFields().get(6).getValues().get(0)); + assertEquals("search", payload.getFields().get(6).getValues().get(1)); + assertEquals(5, payload.getFields().get(6).getOptions().size()); + assertEquals("Contests", + payload.getFields().get(6).getOptions().get(0).label_); + assertEquals("contests", + payload.getFields().get(6).getOptions().get(0).value_); + assertEquals("News", + payload.getFields().get(6).getOptions().get(1).label_); + assertEquals("news", + payload.getFields().get(6).getOptions().get(1).value_); + + assertEquals("20", payload.getFields().get(7).getValues().get(0)); + + assertEquals(new JID("foo@bar.com"), payload + .getFields().get(8).getJIDMultiValue(0)); + assertEquals(new JID("baz@fum.org"), payload + .getFields().get(8).getJIDMultiValue(1)); + assertEquals("Tell all your friends about your new bot!", payload + .getFields().get(8).getDescription()); + + assertEquals("foo", payload.getFields().get(9).getValues().get(0)); + } + + @Test + public void testParse_FormLayout() { + + // P1 = page one, S1 = section one, F1 = field one, T1 = text one + Form payload = parse( + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<page label=\"P1\" xmlns=\"http://jabber.org/protocol/xdata-layout\">" + + "<reportedref/>" + + "<text>P1T1</text>" + + "<fieldref var=\"P1F1\"/>" + + "<section label=\"P1S1\">" + + "<text>P1S1T1</text>" + + "<fieldref var=\"P1S1F1\"/>" + + "</section>" + + "</page>" + + "<page label=\"P2\" xmlns=\"http://jabber.org/protocol/xdata-layout\">" + + "<section label=\"P2S1\">" + + "<section label=\"P2S2\">" + + "<section label=\"P2S3\"/>" + + "</section>" + + "</section>" + + "</page>" + + "<field label=\"field one\" type=\"text-single\" var=\"P1F1\"/>" + + "<field label=\"field two\" type=\"text-single\" var=\"P1S1F1\"/>" + + "</x>"); + + assertEquals(0, payload.getFields().size()); + // PAGE ONE - parsing of element types + assertEquals("P1", payload.getPages().get(0).getLabel()); + assertNotNull(payload.getPages().get(0).getReportedRefs().get(0)); + assertEquals("P1T1", payload.getPages().get(0).getTextElements().get(0).getTextString()); + assertEquals("P1F1", payload.getPages().get(0).getFields().get(0).getName()); + assertEquals("P1S1", payload.getPages().get(0).getChildSections().get(0).getLabel()); + assertEquals("P1S1T1", payload.getPages().get(0).getChildSections().get(0).getTextElements().get(0).getTextString()); + assertEquals("P1S1F1", payload.getPages().get(0).getChildSections().get(0).getFields().get(0).getName()); + // PAGE TWO - parsing of nested elements + assertEquals("P2", payload.getPages().get(1).getLabel()); + assertEquals("P2S1", payload.getPages().get(1).getChildSections().get(0).getLabel()); + assertEquals("P2S2", payload.getPages().get(1).getChildSections().get(0).getChildSections().get(0).getLabel()); + assertEquals("P2S3", payload.getPages().get(1).getChildSections().get(0).getChildSections().get(0).getChildSections().get(0).getLabel()); + } + + @Test + public void testParse_FormItems() { + + Form dataForm = parse( + "<x xmlns='jabber:x:data' type='result'>" + + "<field type='hidden' var='FORM_TYPE'>" + + "<value>jabber:iq:search</value>" + + "</field>" + + "<reported>" + + "<field var='first' label='Given Name' type='text-single'/>" + + "<field var='last' label='Family Name' type='text-single'/>" + + "<field var='jid' label='Jabber ID' type='jid-single'/>" + + "<field var='x-gender' label='Gender' type='list-single'/>" + + "</reported>" + + "<item>" + + "<field var='first'><value>Benvolio</value></field>" + + "<field var='last'><value>Montague</value></field>" + + "<field var='jid'><value>benvolio@montague.net</value></field>" + + "<field var='x-gender'><value>male</value></field>" + + "</item>" + + "<item>" + + "<field var='first'><value>Romeo</value></field>" + + "<field var='last'><value>Montague</value></field>" + + "<field var='jid'><value>romeo@montague.net</value></field>" + + "<field var='x-gender'><value>male</value></field>" + + "</item>" + + "</x>"); + + assertNotNull(dataForm); + + List<FormField> reported = dataForm.getReportedFields(); + assertEquals(4, reported.size()); + + List<FormItem> items = dataForm.getItems(); + assertEquals(2, items.size()); + + FormItem item = items.get(0); + assertEquals(4, item.getItemFields().size()); + + assertEquals("Benvolio", item.getItemFields().get(0).getValues().get(0)); + assertEquals("first", item.getItemFields().get(0).getName()); + assertEquals("Montague", item.getItemFields().get(1).getValues().get(0)); + assertEquals("last", item.getItemFields().get(1).getName()); + FormField jidField = item.getItemFields().get(2); + assertEquals(new JID("benvolio@montague.net"), jidField.getJIDSingleValue()); + assertEquals("jid", item.getItemFields().get(2).getName()); + assertEquals("male", item.getItemFields().get(3).getValues().get(0)); + assertEquals("x-gender", item.getItemFields().get(3).getName()); + + item = items.get(1); + assertEquals(4, item.getItemFields().size()); + + assertEquals("Romeo", item.getItemFields().get(0).getValues().get(0)); + assertEquals("first", item.getItemFields().get(0).getName()); + assertEquals("Montague", item.getItemFields().get(1).getValues().get(0)); + assertEquals("last", item.getItemFields().get(1).getName()); + jidField = item.getItemFields().get(2); + assertNotNull(jidField); + assertEquals(new JID("romeo@montague.net"), jidField.getJIDSingleValue()); + assertEquals("jid", item.getItemFields().get(2).getName()); + assertEquals("male", item.getItemFields().get(3).getValues().get(0)); + assertEquals("x-gender", item.getItemFields().get(3).getName()); + } }
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/SearchPayloadParserTest.java b/test/com/isode/stroke/parser/payloadparsers/SearchPayloadParserTest.java new file mode 100644 index 0000000..f8fd209 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/SearchPayloadParserTest.java @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2010-2013 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 org.junit.Test; +import com.isode.stroke.elements.SearchPayload; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.FormItem; +import com.isode.stroke.elements.FormSection; +import com.isode.stroke.elements.FormText; +import com.isode.stroke.elements.FormPage; +import com.isode.stroke.elements.FormReportedRef; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.jid.JID; +import java.util.Vector; +import java.util.ArrayList; +import java.util.List; + +public class SearchPayloadParserTest { + + public SearchPayloadParserTest() { + + } + + @Test + public void testParse_FormRequestResponse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse( + "<query xmlns=\"jabber:iq:search\">" + + "<instructions>Foo</instructions>" + + "<first/>" + + "<last/>" + + "</query>" + )); + + SearchPayload payload = (SearchPayload)parser.getPayload(); + assertEquals("Foo", payload.getInstructions()); + assertNotNull(payload.getFirst()); + assertNotNull(payload.getLast()); + assertNull(payload.getNick()); + } + + @Test + public void testParse_Results() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse( + "<query xmlns=\"jabber:iq:search\">" + + "<item jid=\"juliet@capulet.com\">" + + "<first>Juliet</first>" + + "<last>Capulet</last>" + + "<nick>JuliC</nick>" + + "<email>juliet@shakespeare.lit</email>" + + "</item>" + + "<item jid=\"tybalt@shakespeare.lit\">" + + "<first>Tybalt</first>" + + "<last>Capulet</last>" + + "<nick>ty</nick>" + + "<email>tybalt@shakespeare.lit</email>" + + "</item>" + + "</query>" + )); + + SearchPayload payload = (SearchPayload)parser.getPayload(); + assertEquals(2, payload.getItems().size()); + assertEquals(new JID("juliet@capulet.com"), payload.getItems().get(0).jid); + assertEquals("Juliet", payload.getItems().get(0).first); + assertEquals("Capulet", payload.getItems().get(0).last); + assertEquals("JuliC", payload.getItems().get(0).nick); + assertEquals("juliet@shakespeare.lit", payload.getItems().get(0).email); + assertEquals(new JID("tybalt@shakespeare.lit"), payload.getItems().get(1).jid); + } + + @Test + public void testParse_FormRequestResponse_XDATA() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse( + "<query xmlns='jabber:iq:search'>" + + "<instructions>" + + "Use the enclosed form to search. If your Jabber client does not" + + " support Data Forms, visit http://shakespeare.lit/" + + "</instructions>" + + "<x xmlns='jabber:x:data' type='form'>" + + "<title>User Directory Search</title>" + + "<instructions>" + + "Please provide the following information" + + " to search for Shakespearean characters." + + "</instructions>" + + "<field type='hidden'" + + " var='FORM_TYPE'>" + + "<value>jabber:iq:search</value>" + + "</field>" + + "<field type='text-single'" + + " label='Given Name'" + + " var='first'/>" + + "<field type='text-single'" + + " label='Family Name'" + + " var='last'/>" + + "<field type='list-single'" + + " label='Gender'" + + " var='x-gender'>" + + "<option label='Male'><value>male</value></option>" + + "<option label='Female'><value>female</value></option>" + + "</field>" + + "</x>" + + "</query>" + )); + + SearchPayload payload = (SearchPayload)parser.getPayload(); + assertEquals("Use the enclosed form to search. If your Jabber client does not" + + " support Data Forms, visit http://shakespeare.lit/", payload.getInstructions()); + assertNotNull(payload.getForm()); + assertEquals("Please provide the following information" + + " to search for Shakespearean characters.", payload.getForm().getInstructions()); + } + + @Test + public void testParse_Results_XDATA() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse("<query xmlns='jabber:iq:search'>" + + " <x xmlns='jabber:x:data' type='result'>" + + " <field type='hidden' var='FORM_TYPE'>" + + " <value>jabber:iq:search</value>" + + " </field>" + + " <reported>" + + " <field var='first' label='Given Name' type='text-single'/>" + + " <field var='last' label='Family Name' type='text-single'/>" + + " <field var='jid' label='Jabber ID' type='jid-single'/>" + + " <field var='x-gender' label='Gender' type='list-single'/>" + + " </reported>" + + " <item>" + + " <field var='first'><value>Benvolio</value></field>" + + " <field var='last'><value>Montague</value></field>" + + " <field var='jid'><value>benvolio@montague.net</value></field>" + + " <field var='x-gender'><value>male</value></field>" + + " </item>" + + " <item>" + + " <field var='first'><value>Romeo</value></field>" + + " <field var='last'><value>Montague</value></field>" + + " <field var='jid'><value>romeo@montague.net</value></field>" + + " <field var='x-gender'><value>male</value></field>" + + " </item>" + + " </x>" + + "</query>")); + + SearchPayload payload = (SearchPayload)parser.getPayload(); + assertNotNull(payload); + + Form dataForm = payload.getForm(); + assertNotNull(dataForm); + + List<FormField> reported = dataForm.getReportedFields(); + assertEquals(4, reported.size()); + + List<FormItem> items = dataForm.getItems(); + assertEquals(2, items.size()); + + FormItem item = items.get(0); + assertEquals(4, item.getItemFields().size()); + + assertEquals("Benvolio", item.getItemFields().get(0).getValues().get(0)); + assertEquals("first", item.getItemFields().get(0).getName()); + assertEquals("Montague", item.getItemFields().get(1).getValues().get(0)); + assertEquals("last", item.getItemFields().get(1).getName()); + assertEquals("benvolio@montague.net", item.getItemFields().get(2).getValues().get(0)); + assertEquals("jid", item.getItemFields().get(2).getName()); + assertEquals("male", item.getItemFields().get(3).getValues().get(0)); + assertEquals("x-gender", item.getItemFields().get(3).getName()); + + item = items.get(1); + assertEquals(4, item.getItemFields().size()); + + assertEquals("Romeo", item.getItemFields().get(0).getValues().get(0)); + assertEquals("first", item.getItemFields().get(0).getName()); + assertEquals("Montague", item.getItemFields().get(1).getValues().get(0)); + assertEquals("last", item.getItemFields().get(1).getName()); + assertEquals("romeo@montague.net", item.getItemFields().get(2).getValues().get(0)); + assertEquals("jid", item.getItemFields().get(2).getName()); + assertEquals("male", item.getItemFields().get(3).getValues().get(0)); + assertEquals("x-gender", item.getItemFields().get(3).getName()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java index d7347cb..5190806 100644 --- a/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java +++ b/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java @@ -18,193 +18,293 @@ import com.isode.stroke.elements.Form.Type; import com.isode.stroke.elements.FormField; import com.isode.stroke.elements.FormField.Option; import com.isode.stroke.elements.FormItem; +import com.isode.stroke.elements.FormSection; +import com.isode.stroke.elements.FormText; +import com.isode.stroke.elements.FormPage; +import com.isode.stroke.elements.FormReportedRef; +import com.isode.stroke.jid.JID; public class FormSerializerTest { - @BeforeClass - public static void init() throws Exception { - } - - @Test - public void testSerializeFormInformation() { - FormSerializer testling = new FormSerializer(); - Form form = new Form(Type.FORM_TYPE); - form.setTitle("Bot Configuration"); - form - .setInstructions("Hello!\nFill out this form to configure your new bot!"); - - assertEquals( - "<x type=\"form\" xmlns=\"jabber:x:data\">" - + "<title>Bot Configuration</title>" - + "<instructions>Hello!</instructions>" - + "<instructions>Fill out this form to configure your new bot!</instructions>" - + "</x>", testling.serialize(form)); - } - - @Test - public void testSerializeFields() { - FormSerializer testling = new FormSerializer(); - Form form = new Form(Type.FORM_TYPE); - - FormField field = new FormField(FormField.Type.UNKNOWN_TYPE); - field.setName("field name"); - field.setLabel("description"); - field.addValue("someText"); - form.addReportedField(field); - - field = new FormField(FormField.Type.TEXT_MULTI_TYPE); - field.setLabel("text-multi-field"); - field.setTextMultiValue("This is some text\nthis is some more"); - form.addField(field); - - FormItem item = new FormItem(); - field = new FormField(FormField.Type.UNKNOWN_TYPE); - field.setName("itemField"); - field.addValue("itemValue"); - item.addItemField(field); - form.addItem(item); - - field = new FormField(FormField.Type.HIDDEN_TYPE, "jabber:bot"); - field.setName("FORM_TYPE"); - form.addField(field); - - form.addField(new FormField(FormField.Type.FIXED_TYPE, "Section 1: Bot Info")); - - field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); - field.setName("botname"); - field.setLabel("The name of your bot"); - form.addField(field); - - field = new FormField(FormField.Type.TEXT_MULTI_TYPE); - field.setTextMultiValue("This is a bot.\nA quite good one actually"); - field.setName("description"); - field.setLabel("Helpful description of your bot"); - form.addField(field); - - field = new FormField(FormField.Type.BOOLEAN_TYPE); - field.setBoolValue(true); - field.setName("public"); - field.setLabel("Public bot?"); - field.setRequired(true); - form.addField(field); - - field = new FormField(FormField.Type.TEXT_PRIVATE_TYPE); - field.setName("password"); - field.setLabel("Password for special access"); - form.addField(field); - - field = new FormField(FormField.Type.LIST_MULTI_TYPE); - field.addValue("news"); - field.addValue("search"); - field.setName("features"); - field.setLabel("What features will the bot support?"); - field.addOption(new Option("Contests", "contests")); - field.addOption(new Option("News", "news")); - field.addOption(new Option("Polls", "polls")); - field.addOption(new Option("Reminders", "reminders")); - field.addOption(new Option("Search", "search")); - form.addField(field); - - field = new FormField(FormField.Type.LIST_SINGLE_TYPE, "20"); - field.setName("maxsubs"); - field.setLabel("Maximum number of subscribers"); - field.addOption(new Option("10", "10")); - field.addOption(new Option("20", "20")); - field.addOption(new Option("30", "30")); - field.addOption(new Option("50", "50")); - field.addOption(new Option("100", "100")); - field.addOption(new Option("", "none")); - form.addField(field); - - String jid = "user@example.com"; - field = new FormField(FormField.Type.JID_SINGLE_TYPE); - field.addValue(jid); - field.setName("jidSingle"); - field.setLabel("jidSingleLabel"); - field.setDescription("jidSingleDescription"); - form.addField(field); - - field = new FormField(FormField.Type.JID_MULTI_TYPE); - field.addValue("foo@bar.com"); - field.addValue("baz@fum.org"); - field.setName("invitelist"); - field.setLabel("People to invite"); - field.setDescription("Tell all your friends about your new bot!"); - form.addField(field); - - assertEquals( - "<x type=\"form\" xmlns=\"jabber:x:data\">" - + "<reported>" - + "<field label=\"description\" var=\"field name\">" - + "<value>someText</value>" - + "</field>" - + "</reported>" - + "<item>" - + "<field var=\"itemField\">" - + "<value>itemValue</value>" - + "</field>" - + "</item>" - + "<field label=\"text-multi-field\" type=\"text-multi\">" - + "<value>This is some text</value><value>this is some more</value>" - + "</field>" - + "<field type=\"hidden\" var=\"FORM_TYPE\">" - + "<value>jabber:bot</value>" - + "</field>" - + "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>" - + "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>" - + "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>" - + "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">" - + "<required/>" - + "<value>1</value>" - + "</field>" - + "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>" - + "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">" - + "<value>news</value>" - + "<value>search</value>" - + "<option label=\"Contests\"><value>contests</value></option>" - + "<option label=\"News\"><value>news</value></option>" - + "<option label=\"Polls\"><value>polls</value></option>" - + "<option label=\"Reminders\"><value>reminders</value></option>" - + "<option label=\"Search\"><value>search</value></option>" - + "</field>" - + "<field label=\"Maximum number of subscribers\" type=\"list-single\" var=\"maxsubs\">" - + "<value>20</value>" - + "<option label=\"10\"><value>10</value></option>" - + "<option label=\"20\"><value>20</value></option>" - + "<option label=\"30\"><value>30</value></option>" - + "<option label=\"50\"><value>50</value></option>" - + "<option label=\"100\"><value>100</value></option>" - + "<option><value>none</value></option>" - + "</field>" - + "<field label=\"jidSingleLabel\" type=\"jid-single\" var=\"jidSingle\">" - + "<desc>jidSingleDescription</desc>" - + "<value>user@example.com</value>" - + "</field>" - + "<field label=\"People to invite\" type=\"jid-multi\" var=\"invitelist\">" - + "<desc>Tell all your friends about your new bot!</desc>" - + "<value>foo@bar.com</value>" - + "<value>baz@fum.org</value>" + "</field>" + "</x>", - - /* - + "<field label=\"booleanField\" type=\"boolean\"><value>0</value></field>" - + "<field label=\"fixedField\" type=\"fixed\"><value>Fixed></value></field>" - + "<field label=\"hiddenField\" type=\"hidden\"/>" - + "<field label=\"listSingleField\" type=\"list-single\">" - + "<option label=\"option1\"><value>listVal</value></option>" - + "<option label=\"option2\"><value>listVal</value></option>" - + "<option label=\"option3\"><value>listVal</value></option>" - + "</field>" - + "<field label=\"listMultiField\" type=\"list-multi\">" - + "<option label=\"option1\"><value>listVal</value></option>" - + "<option label=\"option1\"><value>listVal</value></option>" - + "<option label=\"option1\"><value>listVal</value></option>" - + "</field>" - + "<field label=\"textPrivateField\" type=\"text-private\"><value>textPrivateVal</value></field>" - + "<field label=\"textMultiField\" type=\"text-multi\">" - + "" - + "" - + "" - + "</field>" - */ - testling.serialize(form)); - } + @BeforeClass + public static void init() throws Exception { + } + + @Test + public void testSerializeFormInformation() { + FormSerializer testling = new FormSerializer(); + Form form = new Form(Type.FORM_TYPE); + form.setTitle("Bot Configuration"); + form + .setInstructions("Hello!\nFill out this form to configure your new bot!"); + + assertEquals( + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<title>Bot Configuration</title>" + + "<instructions>Hello!</instructions>" + + "<instructions>Fill out this form to configure your new bot!</instructions>" + + "</x>", testling.serialize(form)); + } + + @Test + public void testSerializeFields() { + FormSerializer testling = new FormSerializer(); + Form form = new Form(Form.Type.FORM_TYPE); + + FormField field = new FormField(FormField.Type.HIDDEN_TYPE, "jabber:bot"); + field.setName("FORM_TYPE"); + form.addField(field); + + form.addField(new FormField(FormField.Type.FIXED_TYPE, "Section 1: Bot Info")); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); + field.setName("botname"); + field.setLabel("The name of your bot"); + form.addField(field); + + field = new FormField(FormField.Type.TEXT_MULTI_TYPE); + field.setTextMultiValue("This is a bot.\nA quite good one actually"); + field.setName("description"); + field.setLabel("Helpful description of your bot"); + form.addField(field); + + field = new FormField(FormField.Type.BOOLEAN_TYPE, "1"); + field.setName("public"); + field.setLabel("Public bot?"); + field.setRequired(true); + form.addField(field); + + field = new FormField(FormField.Type.TEXT_PRIVATE_TYPE); + field.setName("password"); + field.setLabel("Password for special access"); + form.addField(field); + + field = new FormField(FormField.Type.LIST_MULTI_TYPE); + field.addValue("news"); + field.addValue("search"); + field.setName("features"); + field.setLabel("What features will the bot support?"); + field.addOption(new FormField.Option("Contests", "contests")); + field.addOption(new FormField.Option("News", "news")); + field.addOption(new FormField.Option("Polls", "polls")); + field.addOption(new FormField.Option("Reminders", "reminders")); + field.addOption(new FormField.Option("Search", "search")); + form.addField(field); + + field = new FormField(FormField.Type.LIST_SINGLE_TYPE, "20"); + field.setName("maxsubs"); + field.setLabel("Maximum number of subscribers"); + field.addOption(new FormField.Option("10", "10")); + field.addOption(new FormField.Option("20", "20")); + field.addOption(new FormField.Option("30", "30")); + field.addOption(new FormField.Option("50", "50")); + field.addOption(new FormField.Option("100", "100")); + field.addOption(new FormField.Option("", "none")); + form.addField(field); + + field = new FormField(FormField.Type.JID_MULTI_TYPE); + field.addValue("foo@bar.com"); + field.addValue("baz@fum.org"); + field.setName("invitelist"); + field.setLabel("People to invite"); + field.setDescription("Tell all your friends about your new bot!"); + form.addField(field); + + assertEquals( + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<field type=\"hidden\" var=\"FORM_TYPE\">" + + "<value>jabber:bot</value>" + + "</field>" + + "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>" + + "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>" + + "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>" + + "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">" + + "<required/>" + + "<value>1</value>" + + "</field>" + + "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>" + + "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">" + + "<value>news</value>" + + "<value>search</value>" + + "<option label=\"Contests\"><value>contests</value></option>" + + "<option label=\"News\"><value>news</value></option>" + + "<option label=\"Polls\"><value>polls</value></option>" + + "<option label=\"Reminders\"><value>reminders</value></option>" + + "<option label=\"Search\"><value>search</value></option>" + + "</field>" + + "<field label=\"Maximum number of subscribers\" type=\"list-single\" var=\"maxsubs\">" + + "<value>20</value>" + + "<option label=\"10\"><value>10</value></option>" + + "<option label=\"20\"><value>20</value></option>" + + "<option label=\"30\"><value>30</value></option>" + + "<option label=\"50\"><value>50</value></option>" + + "<option label=\"100\"><value>100</value></option>" + + "<option><value>none</value></option>" + + "</field>" + + "<field label=\"People to invite\" type=\"jid-multi\" var=\"invitelist\">" + + "<desc>Tell all your friends about your new bot!</desc>" + + "<value>foo@bar.com</value>" + + "<value>baz@fum.org</value>" + + "</field>" + + "</x>", + testling.serialize(form)); + } + + @Test + public void testSerializeLayout() { + FormSerializer testling = new FormSerializer(); + Form form = new Form(Type.FORM_TYPE); + + FormPage page = new FormPage(); + page.setLabel("P1"); + FormReportedRef reportedRef = new FormReportedRef(); + page.addReportedRef(reportedRef); + FormText formText = new FormText(); + formText.setTextString("P1T1"); + page.addTextElement(formText); + FormField field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); + field.setName("P1F1"); + field.setLabel("field one"); + page.addField(field); + + FormSection section = new FormSection(); + section.setLabel("P1S1"); + formText = new FormText(); + formText.setTextString("P1S1T1"); + section.addTextElement(formText); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); + field.setName("P1S1F1"); + field.setLabel("field two"); + section.addField(field); + page.addChildSection(section); + form.addPage(page); + + page = new FormPage(); + page.setLabel("P2"); + section = new FormSection(); + section.setLabel("P2S1"); + FormSection subSection = new FormSection(); + subSection.setLabel("P2S2"); + FormSection subSection2 = new FormSection(); + subSection2.setLabel("P2S3"); + subSection.addChildSection(subSection2); + section.addChildSection(subSection); + page.addChildSection(section); + form.addPage(page); + + // P1 = page one, S1 = section one, F1 = field one, T1 = text one + assertEquals( + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<page label=\"P1\" xmlns=\"http://jabber.org/protocol/xdata-layout\">" + + "<text>P1T1</text>" + + "<fieldref var=\"P1F1\"/>" + + "<reportedref/>" + + "<section label=\"P1S1\">" + + "<text>P1S1T1</text>" + + "<fieldref var=\"P1S1F1\"/>" + + "</section>" + + "</page>" + + "<page label=\"P2\" xmlns=\"http://jabber.org/protocol/xdata-layout\">" + + "<section label=\"P2S1\">" + + "<section label=\"P2S2\">" + + "<section label=\"P2S3\"/>" + + "</section>" + + "</section>" + + "</page>" + + "<field label=\"field one\" type=\"text-single\" var=\"P1F1\"/>" + + "<field label=\"field two\" type=\"text-single\" var=\"P1S1F1\"/>" + + "</x>", testling.serialize(form)); + } + + @Test + public void testSerializeFormItems() { + FormSerializer testling = new FormSerializer(); + Form form = new Form(Type.RESULT_TYPE); + + FormField field = new FormField(FormField.Type.HIDDEN_TYPE, "jabber:iq:search"); + field.setName("FORM_TYPE"); + form.addField(field); + + // reported fields + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); + field.setName("first"); + field.setLabel("Given Name"); + form.addReportedField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); + field.setName("last"); + field.setLabel("Family Name"); + form.addReportedField(field); + + field = new FormField(FormField.Type.JID_SINGLE_TYPE); + field.setName("jid"); + field.setLabel("Jabber ID"); + form.addReportedField(field); + + field = new FormField(FormField.Type.LIST_SINGLE_TYPE); + field.setName("x-gender"); + field.setLabel("Gender"); + form.addReportedField(field); + + FormItem firstItem = new FormItem(); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Benvolio"); + field.setName("first"); + firstItem.addItemField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Montague"); + field.setName("last"); + firstItem.addItemField(field); + + field = new FormField(FormField.Type.JID_SINGLE_TYPE, "benvolio@montague.net"); + field.setName("jid"); + firstItem.addItemField(field); + + field = new FormField(FormField.Type.LIST_SINGLE_TYPE, "male"); + field.setName("x-gender"); + firstItem.addItemField(field); + + FormItem secondItem = new FormItem(); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Romeo"); + field.setName("first"); + secondItem.addItemField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Montague"); + field.setName("last"); + secondItem.addItemField(field); + + field = new FormField(FormField.Type.JID_SINGLE_TYPE, "romeo@montague.net"); + field.setName("jid"); + secondItem.addItemField(field); + + field = new FormField(FormField.Type.LIST_SINGLE_TYPE, "male"); + field.setName("x-gender"); + secondItem.addItemField(field); + + form.addItem(firstItem); + form.addItem(secondItem); + + assertEquals( + "<x type=\"result\" xmlns=\"jabber:x:data\">" + + "<field type=\"hidden\" var=\"FORM_TYPE\">" + + "<value>jabber:iq:search</value>" + + "</field>" + + "<reported>" + + "<field label=\"Given Name\" type=\"text-single\" var=\"first\"/>" + + "<field label=\"Family Name\" type=\"text-single\" var=\"last\"/>" + + "<field label=\"Jabber ID\" type=\"jid-single\" var=\"jid\"/>" + + "<field label=\"Gender\" type=\"list-single\" var=\"x-gender\"/>" + + "</reported>" + + "<item>" + + "<field var=\"first\"><value>Benvolio</value></field>" + + "<field var=\"last\"><value>Montague</value></field>" + + "<field var=\"jid\"><value>benvolio@montague.net</value></field>" + + "<field var=\"x-gender\"><value>male</value></field>" + + "</item>" + + "<item>" + + "<field var=\"first\"><value>Romeo</value></field>" + + "<field var=\"last\"><value>Montague</value></field>" + + "<field var=\"jid\"><value>romeo@montague.net</value></field>" + + "<field var=\"x-gender\"><value>male</value></field>" + + "</item>" + + "</x>", testling.serialize(form)); + } } diff --git a/test/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializerTest.java index 6880f5d..1b49818 100644 --- a/test/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializerTest.java +++ b/test/com/isode/stroke/serializer/payloadserializers/InBandRegistrationPayloadSerializerTest.java @@ -56,15 +56,15 @@ public class InBandRegistrationPayloadSerializerTest { registration.setForm(form); String expectedResult = - "<query xmlns=\"jabber:iq:register\">" + - "<instructions>Use the enclosed form to register.</instructions>" + - "<x type=\"form\" xmlns=\"jabber:x:data\">" + - "<title>Contest Registration</title>" + - "<field type=\"hidden\" var=\"FORM_TYPE\">" + - "<value>jabber:iq:register</value>" + - "</field>" + - "</x>" + - "</query>"; + "<query xmlns=\"jabber:iq:register\">" + + "<instructions>Use the enclosed form to register.</instructions>" + + "<x type=\"form\" xmlns=\"jabber:x:data\">" + + "<title>Contest Registration</title>" + + "<field type=\"hidden\" var=\"FORM_TYPE\">" + + "<value>jabber:iq:register</value>" + + "</field>" + + "</x>" + + "</query>"; assertEquals(expectedResult, testling.serialize(registration)); } diff --git a/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java index 82083cc..8f92a70 100644 --- a/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java +++ b/test/com/isode/stroke/serializer/payloadserializers/MAMQuerySerializerTest.java @@ -20,44 +20,44 @@ import static org.junit.Assert.assertEquals; public class MAMQuerySerializerTest { - @Test - public void testSerialize() { - MAMQuerySerializer serializer = new MAMQuerySerializer(); - - Form parameters = new Form(); - - FormField fieldType = new FormField(Type.TEXT_SINGLE_TYPE, "urn:xmpp:mam:0"); - fieldType.setName("FORM_TYPE"); - parameters.addField(fieldType); - - FormField fieldStart = new FormField(Type.TEXT_SINGLE_TYPE, "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.setNode("node1"); - query.setForm(parameters); - query.setResultSet(set); - - String expectedResult = - "<query node=\"node1\" 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)); - } + @Test + public void testSerialize() { + MAMQuerySerializer serializer = new MAMQuerySerializer(); + + Form parameters = new Form(); + + FormField fieldType = new FormField(Type.TEXT_SINGLE_TYPE, "urn:xmpp:mam:0"); + fieldType.setName("FORM_TYPE"); + parameters.addField(fieldType); + + FormField fieldStart = new FormField(Type.TEXT_SINGLE_TYPE, "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.setNode("node1"); + query.setForm(parameters); + query.setResultSet(set); + + String expectedResult = + "<query node=\"node1\" 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/SearchPayloadSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializerTest.java new file mode 100644 index 0000000..9bd8eb9 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializerTest.java @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2010-2013 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.serializer.payloadserializers; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import com.isode.stroke.serializer.payloadserializers.SearchPayloadSerializer; +import com.isode.stroke.elements.SearchPayload; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.Form.Type; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.FormField.Option; +import com.isode.stroke.elements.FormItem; +import com.isode.stroke.elements.FormSection; +import com.isode.stroke.elements.FormText; +import com.isode.stroke.elements.FormPage; +import com.isode.stroke.elements.FormReportedRef; +import com.isode.stroke.jid.JID; +import java.util.List; + +public class SearchPayloadSerializerTest { + + /** + * Default Constructor. + */ + public SearchPayloadSerializerTest() { + + } + + @Test + public void testSerialize_Request() { + SearchPayloadSerializer testling = new SearchPayloadSerializer(); + + SearchPayload payload = new SearchPayload(); + payload.setFirst("Juliet"); + payload.setLast("Capulet"); + + assertEquals("<query xmlns=\"jabber:iq:search\">" + + "<first>Juliet</first>" + + "<last>Capulet</last>" + + "</query>", testling.serialize(payload)); + } + + @Test + public void testSerialize_Items() { + SearchPayloadSerializer testling = new SearchPayloadSerializer(); + + SearchPayload payload = new SearchPayload(); + SearchPayload.Item item1 = new SearchPayload.Item(); + item1.jid = new JID("juliet@capulet.com"); + item1.first = "Juliet"; + item1.last = "Capulet"; + item1.nick = "JuliC"; + item1.email = "juliet@shakespeare.lit"; + payload.addItem(item1); + + SearchPayload.Item item2 = new SearchPayload.Item(); + item2.jid = new JID("tybalt@shakespeare.lit"); + item2.first = "Tybalt"; + item2.last = "Capulet"; + item2.nick = "ty"; + item2.email = "tybalt@shakespeare.lit"; + payload.addItem(item2); + + assertEquals("<query xmlns=\"jabber:iq:search\">" + + "<item jid=\"juliet@capulet.com\">" + + "<first>Juliet</first>" + + "<last>Capulet</last>" + + "<nick>JuliC</nick>" + + "<email>juliet@shakespeare.lit</email>" + + "</item>" + + "<item jid=\"tybalt@shakespeare.lit\">" + + "<first>Tybalt</first>" + + "<last>Capulet</last>" + + "<nick>ty</nick>" + + "<email>tybalt@shakespeare.lit</email>" + + "</item>" + + "</query>", testling.serialize(payload)); + } + + @Test + public void testSerialize_DataForm() { + SearchPayloadSerializer testling = new SearchPayloadSerializer(); + + SearchPayload payload = new SearchPayload(); + Form form = new Form(Form.Type.RESULT_TYPE); + + FormField field = new FormField(FormField.Type.HIDDEN_TYPE, "jabber:iq:search"); + field.setName("FORM_TYPE"); + form.addField(field); + + // reported fields + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); + field.setName("first"); + field.setLabel("Given Name"); + form.addReportedField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE); + field.setName("last"); + field.setLabel("Family Name"); + form.addReportedField(field); + + field = new FormField(FormField.Type.JID_SINGLE_TYPE); + field.setName("jid"); + field.setLabel("Jabber ID"); + form.addReportedField(field); + + field = new FormField(FormField.Type.LIST_SINGLE_TYPE); + field.setName("x-gender"); + field.setLabel("Gender"); + form.addReportedField(field); + + FormItem firstItem = new FormItem(); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Benvolio"); + field.setName("first"); + firstItem.addItemField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Montague"); + field.setName("last"); + firstItem.addItemField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "benvolio@montague.net"); + field.setName("jid"); + firstItem.addItemField(field); + + field = new FormField(FormField.Type.LIST_SINGLE_TYPE, "male"); + field.setName("x-gender"); + firstItem.addItemField(field); + + FormItem secondItem = new FormItem(); + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Romeo"); + field.setName("first"); + secondItem.addItemField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "Montague"); + field.setName("last"); + secondItem.addItemField(field); + + field = new FormField(FormField.Type.TEXT_SINGLE_TYPE, "romeo@montague.net"); + field.setName("jid"); + secondItem.addItemField(field); + + field = new FormField(FormField.Type.LIST_SINGLE_TYPE, "male"); + field.setName("x-gender"); + secondItem.addItemField(field); + + + form.addItem(firstItem); + form.addItem(secondItem); + + payload.setForm(form); + assertEquals("<query xmlns=\"jabber:iq:search\">" + + "<x type=\"result\" xmlns=\"jabber:x:data\">" + + "<field type=\"hidden\" var=\"FORM_TYPE\">" + + "<value>jabber:iq:search</value>" + + "</field>" + + "<reported>" + + "<field label=\"Given Name\" type=\"text-single\" var=\"first\"/>" + + "<field label=\"Family Name\" type=\"text-single\" var=\"last\"/>" + + "<field label=\"Jabber ID\" type=\"jid-single\" var=\"jid\"/>" + + "<field label=\"Gender\" type=\"list-single\" var=\"x-gender\"/>" + + "</reported>" + + "<item>" + + "<field var=\"first\"><value>Benvolio</value></field>" + + "<field var=\"last\"><value>Montague</value></field>" + + "<field var=\"jid\"><value>benvolio@montague.net</value></field>" + + "<field var=\"x-gender\"><value>male</value></field>" + + "</item>" + + "<item>" + + "<field var=\"first\"><value>Romeo</value></field>" + + "<field var=\"last\"><value>Montague</value></field>" + + "<field var=\"jid\"><value>romeo@montague.net</value></field>" + + "<field var=\"x-gender\"><value>male</value></field>" + + "</item>" + + "</x>" + + "</query>", testling.serialize(payload)); + } +}
\ No newline at end of file |