diff options
author | Mili Verma <mili.verma@isode.com> | 2012-01-06 15:00:55 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2012-01-09 15:54:52 (GMT) |
commit | cc760bfd15caadb56bfef477cb54dc94c25f7fa7 (patch) | |
tree | 05f5918b97488b05ca1266c0644a3874adb98129 /test | |
parent | 12b1d667965556002ea0fd300a71bcdf57634e90 (diff) | |
download | stroke-cc760bfd15caadb56bfef477cb54dc94c25f7fa7.zip stroke-cc760bfd15caadb56bfef477cb54dc94c25f7fa7.tar.bz2 |
Port Adhoc commands to Stroke
This patch ports the Adhoc commands from Swiften to Stroke.
It also ports their unit tests.
Test-information:
Unit tests pass.
MLC able to use the ad-hoc command fine.
Diffstat (limited to 'test')
5 files changed, 548 insertions, 0 deletions
diff --git a/test/com/isode/stroke/elements/FormTest.java b/test/com/isode/stroke/elements/FormTest.java new file mode 100644 index 0000000..1d37c86 --- /dev/null +++ b/test/com/isode/stroke/elements/FormTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ + +package com.isode.stroke.elements; + +import static org.junit.Assert.assertEquals; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.isode.stroke.elements.FormField.FixedFormField; +import com.isode.stroke.elements.FormField.HiddenFormField; + +public class FormTest { + @BeforeClass + public static void init() throws Exception { + } + + @Test + public void testGetFormType() { + Form form = new Form(); + + form.addField(FixedFormField.create("Foo")); + + FormField field = HiddenFormField.create("jabber:bot"); + field.setName("FORM_TYPE"); + form.addField(field); + + form.addField(FixedFormField.create("Bar")); + + assertEquals("jabber:bot", form.getFormType()); + } + + @Test + public void testGetFormType_InvalidFormType() { + Form form = new Form(); + + FormField field = FixedFormField.create("jabber:bot"); + field.setName("FORM_TYPE"); + form.addField(field); + + assertEquals("", form.getFormType()); + } + + @Test + public void testGetFormType_NoFormType() { + Form form = new Form(); + + form.addField(FixedFormField.create("Foo")); + + assertEquals("", form.getFormType()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/CommandParserTest.java b/test/com/isode/stroke/parser/payloadparsers/CommandParserTest.java new file mode 100644 index 0000000..4c9854b --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/CommandParserTest.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.isode.stroke.elements.Command; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.Command.Action; +import com.isode.stroke.elements.Command.Note; +import com.isode.stroke.elements.Command.Status; +import com.isode.stroke.elements.Command.Note.Type; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class CommandParserTest { + private static DummyEventLoop eventLoop; + + @BeforeClass + public static void init() throws Exception { + eventLoop = new DummyEventLoop(); + } + + private static Command parse(String xmlString) { + 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 (Command) payload; + } + + @Test + public void testParse() { + Command payload = parse("<command xmlns='http://jabber.org/protocol/commands' node='list' action='prev' sessionid='myid'/>"); + + assertEquals(Action.PREV, payload.getAction()); + assertEquals("list", payload.getNode()); + assertEquals("myid", payload.getSessionID()); + } + + @Test + public void testParse_Result() { + Command payload = parse("<command xmlns='http://jabber.org/protocol/commands' node='config' status='completed' sessionid='myid'>" + + "<note type='warn'>Service 'httpd' has been configured.</note>" + + "<note type='error'>I lied.</note>" + + "<actions execute='next'>" + + "<prev/>" + + "<next/>" + + "</actions>" + "</command>"); + + assertEquals(Status.COMPLETED, payload.getStatus()); + + List<Note> notes = payload.getNotes(); + assertEquals(2, notes.size()); + assertEquals(Type.WARN, notes.get(0).type); + assertEquals("Service 'httpd' has been configured.", notes.get(0).note); + assertEquals(Type.ERROR, notes.get(1).type); + assertEquals("I lied.", notes.get(1).note); + List<Action> actions = payload.getAvailableActions(); + assertEquals(2, actions.size()); + assertEquals(Action.PREV, actions.get(0)); + assertEquals(Action.NEXT, actions.get(1)); + assertEquals(Action.NEXT, payload.getExecuteAction()); + } + + @Test + public void testParse_Form() { + Command payload = parse("<command xmlns='http://jabber.org/protocol/commands' node='config' status='completed'>" + + "<x type=\"result\" xmlns=\"jabber:x:data\">" + + "<title>Bot Configuration</title>" + + "<instructions>Hello!</instructions>" + + "<instructions>Fill out this form to configure your new bot!</instructions>" + + "</x>" + "</command>"); + + Form form = payload.getForm(); + assertEquals("Bot Configuration", form.getTitle()); + assertEquals("Hello!\nFill out this form to configure your new bot!", + form.getInstructions()); + assertEquals(Form.Type.RESULT_TYPE, form.getType()); + } +} diff --git a/test/com/isode/stroke/parser/payloadparsers/FormParserTest.java b/test/com/isode/stroke/parser/payloadparsers/FormParserTest.java new file mode 100644 index 0000000..b57e7d8 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/FormParserTest.java @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.Form.Type; +import com.isode.stroke.elements.FormField.BooleanFormField; +import com.isode.stroke.elements.FormField.FixedFormField; +import com.isode.stroke.elements.FormField.HiddenFormField; +import com.isode.stroke.elements.FormField.JIDMultiFormField; +import com.isode.stroke.elements.FormField.ListMultiFormField; +import com.isode.stroke.elements.FormField.ListSingleFormField; +import com.isode.stroke.elements.FormField.TextMultiFormField; +import com.isode.stroke.elements.FormField.TextSingleFormField; +import com.isode.stroke.eventloop.DummyEventLoop; +import com.isode.stroke.jid.JID; + +public class FormParserTest { + private static DummyEventLoop eventLoop; + + @BeforeClass + public static void init() throws Exception { + eventLoop = new DummyEventLoop(); + } + + private static Form parse(String xmlString) { + 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>" + + "<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", ((HiddenFormField) (payload.getFields() + .get(0))).getValue()); + assertEquals("FORM_TYPE", payload.getFields().get(0).getName()); + assertTrue(!payload.getFields().get(0).getRequired()); + + assertEquals("Section 1: Bot Info", ((FixedFormField) (payload + .getFields().get(1))).getValue()); + + assertEquals("The name of your bot", payload.getFields().get(2) + .getLabel()); + + assertEquals("This is a bot.\nA quite good one actually", + ((TextMultiFormField) (payload.getFields().get(3))).getValue()); + + assertEquals(Boolean.TRUE, ((BooleanFormField) (payload.getFields() + .get(4))).getValue()); + assertTrue(payload.getFields().get(4).getRequired()); + assertEquals("1", ((BooleanFormField) (payload.getFields().get(4))) + .getRawValues().get(0)); + + assertEquals("news", + ((ListMultiFormField) (payload.getFields().get(6))).getValue() + .get(0)); + assertEquals("news", payload.getFields().get(6).getRawValues().get(0)); + assertEquals("search", ((ListMultiFormField) (payload.getFields() + .get(6))).getValue().get(1)); + assertEquals("search", payload.getFields().get(6).getRawValues().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", ((ListSingleFormField) (payload.getFields().get(7))) + .getValue()); + + assertEquals(new JID("foo@bar.com"), ((JIDMultiFormField) (payload + .getFields().get(8))).getValue().get(0)); + assertEquals(new JID("baz@fum.org"), ((JIDMultiFormField) (payload + .getFields().get(8))).getValue().get(1)); + assertEquals("Tell all your friends about your new bot!", payload + .getFields().get(8).getDescription()); + + assertEquals("foo", + ((TextSingleFormField) (payload.getFields().get(9))).getValue()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/PayloadsParserTester.java b/test/com/isode/stroke/parser/payloadparsers/PayloadsParserTester.java new file mode 100644 index 0000000..d5614fb --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/PayloadsParserTester.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.Payload; +import com.isode.stroke.eventloop.EventLoop; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PayloadParserFactory; +import com.isode.stroke.parser.PlatformXMLParserFactory; +import com.isode.stroke.parser.XMLParser; +import com.isode.stroke.parser.XMLParserClient; + +public class PayloadsParserTester implements XMLParserClient { + private XMLParser xmlParser; + private FullPayloadParserFactoryCollection factories = new FullPayloadParserFactoryCollection(); + private PayloadParser payloadParser; + private int level; + + public PayloadsParserTester(EventLoop eventLoop) { + level = 0; + xmlParser = PlatformXMLParserFactory.createXMLParser(this, eventLoop); + } + + public boolean parse(String data) { + return xmlParser.parse(data); + } + + public void handleStartElement(String element, String ns, + AttributeMap attributes) { + if (level == 0) { + assert (payloadParser == null); + PayloadParserFactory payloadParserFactory = factories + .getPayloadParserFactory(element, ns, attributes); + assert (payloadParserFactory != null); + payloadParser = payloadParserFactory.createPayloadParser(); + } + payloadParser.handleStartElement(element, ns, attributes); + level++; + } + + public void handleEndElement(String element, String ns) { + level--; + payloadParser.handleEndElement(element, ns); + } + + public void handleCharacterData(String data) { + payloadParser.handleCharacterData(data); + } + + public Payload getPayload() { + if (payloadParser == null) { + return null; + } + return payloadParser.getPayload(); + } +} diff --git a/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java new file mode 100644 index 0000000..4a1ccc0 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2012 Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010 Remko Tronçon + * All rights reserved. + */ + +package com.isode.stroke.serializer.payloadserializers; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.Form.Type; +import com.isode.stroke.elements.FormField.BooleanFormField; +import com.isode.stroke.elements.FormField.FixedFormField; +import com.isode.stroke.elements.FormField.HiddenFormField; +import com.isode.stroke.elements.FormField.JIDMultiFormField; +import com.isode.stroke.elements.FormField.ListMultiFormField; +import com.isode.stroke.elements.FormField.ListSingleFormField; +import com.isode.stroke.elements.FormField.Option; +import com.isode.stroke.elements.FormField.TextMultiFormField; +import com.isode.stroke.elements.FormField.TextPrivateFormField; +import com.isode.stroke.elements.FormField.TextSingleFormField; +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 = HiddenFormField.create("jabber:bot"); + field.setName("FORM_TYPE"); + form.addField(field); + + form.addField(FixedFormField.create("Section 1: Bot Info")); + + field = TextSingleFormField.create(); + field.setName("botname"); + field.setLabel("The name of your bot"); + form.addField(field); + + field = TextMultiFormField + .create("This is a bot.\nA quite good one actually"); + field.setName("description"); + field.setLabel("Helpful description of your bot"); + form.addField(field); + + field = BooleanFormField.create(true); + field.setName("public"); + field.setLabel("Public bot?"); + field.setRequired(true); + form.addField(field); + + field = TextPrivateFormField.create(); + field.setName("password"); + field.setLabel("Password for special access"); + form.addField(field); + + List<String> values = new ArrayList<String>(); + values.add("news"); + values.add("search"); + field = ListMultiFormField.create(values); + 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 = ListSingleFormField.create("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); + + List<JID> jids = new ArrayList<JID>(); + jids.add(new JID("foo@bar.com")); + jids.add(new JID("baz@fum.org")); + field = JIDMultiFormField.create(jids); + 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)); + } +} |