From a673d269487fd86efe7f9c5f9b4cd1c00cab556d Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Sat, 20 Jun 2015 06:32:12 +0530 Subject: 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 diff --git a/src/com/isode/stroke/elements/Form.java b/src/com/isode/stroke/elements/Form.java index 8068f28..a6ff0c2 100644 --- a/src/com/isode/stroke/elements/Form.java +++ b/src/com/isode/stroke/elements/Form.java @@ -108,6 +108,10 @@ public class Form extends Payload { } } + private List reportedRefs_ = new ArrayList(); + private List textElements_ = new ArrayList(); + private List pages_ = new ArrayList(); + private FormReportedRef reportedRef_; private List fields_ = new ArrayList(); private List reportedFields_ = new ArrayList(); private List items_ = new ArrayList(); @@ -132,6 +136,51 @@ public class Form extends Payload { } /** + * @param reportedRef, Not Null. + */ + public void addReportedRef(FormReportedRef reportedRef) { + assert(reportedRef != null); + reportedRefs_.add(reportedRef); + } + + /** + * @return reportedRef, Not Null. + */ + public List getReportedRefs() { + return reportedRefs_; + } + + /** + * @param text, Not Null. + */ + public void addTextElement(FormText text) { + assert(text != null); + textElements_.add(text); + } + + /** + * @return text, Not Null. + */ + public List getTextElements() { + return textElements_; + } + + /** + * @return page, Not Null. + */ + public void addPage(FormPage page) { + assert(page != null); + pages_.add(page); + } + + /** + * @return pages, Not Null. + */ + public List getPages() { + return pages_; + } + + /** * Add to the list of fields for the form. * * @param field Field to add, must not be null. The instance of the form @@ -153,7 +202,11 @@ public class Form extends Payload { public List getFields() { return new ArrayList(fields_); } - + + public void clearFields() { + fields_.clear(); + } + /** * Add a reported element to this Form. * @param reportedField should not be null @@ -183,7 +236,11 @@ public class Form extends Payload { } items_.add(item); } - + + public void clearItems() { + items_.clear(); + } + /** * Get the list of FormItem elements for the form. * @return itemsCopy ArrayList>, list of items for the Form, @@ -295,6 +352,26 @@ public class Form extends Payload { return null; } + public void clearEmptyTextFields() { + List populatedFields = new ArrayList(); + for (FormField field : fields_) { + if (field.getType() == FormField.Type.TEXT_SINGLE_TYPE) { + if (!field.getTextSingleValue().isEmpty()) { + populatedFields.add(field); + } + } + else if (field.getType() == FormField.Type.TEXT_MULTI_TYPE) { + if (!field.getTextMultiValue().isEmpty()) { + populatedFields.add(field); + } + } + else { + populatedFields.add(field); + } + } + fields_ = populatedFields; + } + @Override public String toString() { return Form.class.getSimpleName() + "\ntitle: " + title_ diff --git a/src/com/isode/stroke/elements/FormField.java b/src/com/isode/stroke/elements/FormField.java index 84b6af1..c38772f 100644 --- a/src/com/isode/stroke/elements/FormField.java +++ b/src/com/isode/stroke/elements/FormField.java @@ -88,9 +88,6 @@ public class FormField { public FormField(Type type) { type_ = type; required_ = false; - if (type == Type.BOOLEAN_TYPE) { - setBoolValue(false); - } } /** diff --git a/src/com/isode/stroke/elements/FormPage.java b/src/com/isode/stroke/elements/FormPage.java new file mode 100644 index 0000000..b23a6e2 --- /dev/null +++ b/src/com/isode/stroke/elements/FormPage.java @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.elements; + +import com.isode.stroke.base.NotNull; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.FormReportedRef; +import com.isode.stroke.elements.FormText; +import com.isode.stroke.elements.FormSection; +import java.util.Vector; + +public class FormPage { + + private String label_ = ""; + private String xmlns_ = ""; + private Vector textElements_ = new Vector(); + private Vector reportedRefs_ = new Vector(); + private Vector childSections_ = new Vector(); + private Vector fields_ = new Vector(); + private Vector fieldRefs_ = new Vector(); + + /** + * Default Constructor. + */ + public FormPage() { + this.xmlns_ = "http://jabber.org/protocol/xdata-layout"; + } + + /** + * @param label, Not Null. + */ + public void setLabel(String label) { + NotNull.exceptIfNull(label, "label"); + label_ = label; + } + + /** + * @return label, Not Null. + */ + public String getLabel() { + return label_; + } + + /** + * @return xmlns, Not Null. + */ + public String getXMLNS() { + return xmlns_; + } + + /** + * @param textElement, Not Null. + */ + public void addTextElement(FormText textElement) { + NotNull.exceptIfNull(textElement, "textElement"); + textElements_.add(textElement); + } + + /** + * @return textElement, Not Null. + */ + public Vector getTextElements() { + return textElements_; + } + + /** + * @param reportedRef, Not Null. + */ + public void addReportedRef(FormReportedRef reportedRef) { + NotNull.exceptIfNull(reportedRef, "reportedRef"); + reportedRefs_.add(reportedRef); + } + + /** + * @return reportedRef, Not Null. + */ + public Vector getReportedRefs() { + return reportedRefs_; + } + + /** + * @param childSection, Not Null. + */ + public void addChildSection(FormSection childSection) { + NotNull.exceptIfNull(childSection, "childSection"); + childSections_.add(childSection); + } + + /** + * @return childSection, Not Null. + */ + public Vector getChildSections() { + return childSections_; + } + + /** + * @param field, Not Null. + */ + public void addField(FormField field) { + NotNull.exceptIfNull(field, "field"); + fields_.add(field); + } + + /** + * @return field, Not Null. + */ + public Vector getFields() { + return fields_; + } + + /** + * @param ref, Not Null. + */ + public void addFieldRef(String ref) { + NotNull.exceptIfNull(ref, "ref"); + fieldRefs_.add(ref); + } + + /** + * @return ref, Not Null. + */ + public Vector getFieldRefs() { + return fieldRefs_; + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/elements/FormReportedRef.java b/src/com/isode/stroke/elements/FormReportedRef.java new file mode 100644 index 0000000..1785ce1 --- /dev/null +++ b/src/com/isode/stroke/elements/FormReportedRef.java @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.elements; + +public class FormReportedRef { + +} \ No newline at end of file diff --git a/src/com/isode/stroke/elements/FormSection.java b/src/com/isode/stroke/elements/FormSection.java new file mode 100644 index 0000000..8cc42b3 --- /dev/null +++ b/src/com/isode/stroke/elements/FormSection.java @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.elements; + +import com.isode.stroke.base.NotNull; +import com.isode.stroke.elements.FormField; +import com.isode.stroke.elements.FormReportedRef; +import com.isode.stroke.elements.FormText; +import java.util.Vector; + +public class FormSection { + + private String label_ = ""; + private Vector textElements_ = new Vector(); + private Vector reportedRefs_ = new Vector(); + private Vector childSections_ = new Vector(); + private Vector fields_ = new Vector(); + private Vector fieldRefs_ = new Vector(); + + /** + * Default Constructor. + */ + public FormSection() { + + } + + /** + * @param label, Not Null. + */ + public void setLabel(String label) { + NotNull.exceptIfNull(label, "label"); + label_ = label; + } + + /** + * @return label, Not Null. + */ + public String getLabel() { + return label_; + } + + /** + * @param textElement, Not Null. + */ + public void addTextElement(FormText textElement) { + NotNull.exceptIfNull(textElement, "textElement"); + textElements_.add(textElement); + } + + /** + * @return textElement, Not Null. + */ + public Vector getTextElements() { + return textElements_; + } + + /** + * @param reportedRef, Not Null. + */ + public void addReportedRef(FormReportedRef reportedRef) { + NotNull.exceptIfNull(reportedRef, "reportedRef"); + reportedRefs_.add(reportedRef); + } + + /** + * @return reportedRef, Not Null. + */ + public Vector getReportedRefs() { + return reportedRefs_; + } + + /** + * @param childSection, Not Null. + */ + public void addChildSection(FormSection childSection) { + NotNull.exceptIfNull(childSection, "childSection"); + childSections_.add(childSection); + } + + /** + * @return childSection, Not Null. + */ + public Vector getChildSections() { + return childSections_; + } + + /** + * @param field, Not Null. + */ + public void addField(FormField field) { + NotNull.exceptIfNull(field, "field"); + fields_.add(field); + } + + /** + * @return field, Not Null. + */ + public Vector getFields() { + return fields_; + } + + /** + * @param ref, Not Null. + */ + public void addFieldRef(String ref) { + NotNull.exceptIfNull(ref, "ref"); + fieldRefs_.add(ref); + } + + /** + * @return ref, Not Null. + */ + public Vector getFieldRefs() { + return fieldRefs_; + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/elements/FormText.java b/src/com/isode/stroke/elements/FormText.java new file mode 100644 index 0000000..4a400b3 --- /dev/null +++ b/src/com/isode/stroke/elements/FormText.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.elements; + +import com.isode.stroke.base.NotNull; + +public class FormText { + + private String text_ = ""; + + /** + * Default Constructor. + */ + public FormText() { + + } + + /** + * @param text, Not Null. + */ + public void setTextString(String text) { + NotNull.exceptIfNull(text, "text"); + text_ = text; + } + + /** + * @return text, Not Null. + */ + public String getTextString() { + return text_; + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/elements/SearchPayload.java b/src/com/isode/stroke/elements/SearchPayload.java index 1568961..8b85841 100644 --- a/src/com/isode/stroke/elements/SearchPayload.java +++ b/src/com/isode/stroke/elements/SearchPayload.java @@ -9,6 +9,7 @@ package com.isode.stroke.elements; import com.isode.stroke.jid.JID; +import com.isode.stroke.elements.Form; import java.util.ArrayList; import java.util.List; @@ -26,8 +27,20 @@ public class SearchPayload extends Payload { public SearchPayload() { } - //Form::ref getForm() const { return form; } /* Not ported yet */ - //void setForm(Form::ref f) { form = f; } /* Not ported yet */ + /** + * @return Can be null + */ + public Form getForm() { + return form; + } + + /** + * @param v Null means no value. + */ + public void setForm(Form f) { + form = f; + } + /** * @return Can be null */ @@ -121,4 +134,5 @@ public class SearchPayload extends Payload { private String last; private String email; private ArrayList items = new ArrayList(); + private Form form; } diff --git a/src/com/isode/stroke/elements/Version.java b/src/com/isode/stroke/elements/Version.java new file mode 100644 index 0000000..02c6507 --- /dev/null +++ b/src/com/isode/stroke/elements/Version.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2010-2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.elements; + +import com.isode.stroke.elements.Payload; +import com.isode.stroke.base.NotNull; + +public class Version extends Payload { + + private String name_; + private String version_; + private String os_; + + /** + * Default Constructor. + */ + public Version() { + this("", "", ""); + } + + /** + * Parameterized Constructor. + * @param name, Not Null. + */ + public Version(String name) { + this(name, "", ""); + } + + /** + * Parameterized Constructor. + * @param name, Not Null. + * @param version, NotNull. + */ + public Version(String name, String version) { + this(name, version, ""); + } + + /** + * Parameterized Constructor. + * @param name, Not Null. + * @param version, NotNull. + * @param os , Not Null. + */ + public Version(String name, String version, String os) { + NotNull.exceptIfNull(name, "name"); + NotNull.exceptIfNull(version, "version"); + NotNull.exceptIfNull(os, "os"); + this.name_ = name; + this.version_ = version; + this.os_ = os; + } + + /** + * @return name, Not Null. + */ + public String getName() { + return name_; + } + + /** + * @return version, Not Null. + */ + public String getVersion() { + return version_; + } + + /** + * @return os, Not Null. + */ + public String getOS() { + return os_; + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/FormParser.java b/src/com/isode/stroke/parser/payloadparsers/FormParser.java index 2aa398c..ab14a4e 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FormParser.java +++ b/src/com/isode/stroke/parser/payloadparsers/FormParser.java @@ -17,6 +17,10 @@ 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.FormText; +import com.isode.stroke.elements.FormReportedRef; +import com.isode.stroke.elements.FormPage; +import com.isode.stroke.elements.FormSection; import com.isode.stroke.parser.AttributeMap; import com.isode.stroke.parser.GenericPayloadParser; @@ -32,7 +36,17 @@ public class FormParser extends GenericPayloadParser
{ private boolean parsingOption_ = false; private String currentOptionValue_ = ""; private String currentText_ = ""; - + private String currentFieldRef_ = ""; + private boolean parsingItem_ = false; + private boolean parseStarted_ = false; + private boolean hasReportedRef_ = false; + private FormText currentTextElement_; + private FormReportedRef currentReportedRef_; + private FormPage currentPage_; + private FormSection currentSection_; + private List currentPages_ = new ArrayList(); + private List sectionStack_ = new ArrayList(); + private List currentSections_ = new ArrayList(); private static final int TopLevel = 0; private static final int PayloadLevel = 1; private static final int FieldLevel = 2; @@ -81,12 +95,16 @@ public class FormParser extends GenericPayloadParser { } else if (element.equals(Form.FORM_ELEMENT_ITEM)) { + parsingItem_ = true; currentItem_ = new FormItem(); } - + else if (element == "page") { + currentPage_ = new FormPage(); + currentPage_.setLabel(attributes.getAttribute("label")); + } } - if (level_ == FieldLevel && currentField_ != null) { + else if (level_ == FieldLevel && currentField_ != null) { currentText_ = ""; if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) { currentOptionLabel_ = attributes @@ -115,7 +133,30 @@ public class FormParser extends GenericPayloadParser { currentText_ = ""; } } - + if (level_ > PayloadLevel) { + if (element.equals("section")) { + currentSection_ = new FormSection(); + currentSection_.setLabel(attributes.getAttribute("label")); + sectionStack_.add(currentSection_); + currentSections_.add(currentSection_); + } + if (element.equals("reportedref")) { + currentReportedRef_ = new FormReportedRef(); + } + if (element.equals("fieldref")) { + currentText_ = ""; + currentFieldRef_ = attributes.getAttribute("var"); + if (sectionStack_.size() > 0) { + sectionStack_.get(sectionStack_.size()-1).addFieldRef(currentFieldRef_); + } else if (currentPage_ != null) { + currentPage_.addFieldRef(currentFieldRef_); + } + } + if (element.equals("text")) { + currentText_ = ""; + currentTextElement_ = new FormText(); + } + } ++level_; } @@ -156,44 +197,104 @@ public class FormParser extends GenericPayloadParser { } else if (element.equals(Form.FORM_ELEMENT_ITEM)) { + parsingItem_ = false; currentItem_.addItemFields(currentFields_); form.addItem(currentItem_); currentFields_.clear(); currentItem_ = null; } + else if (element.equals("page")) { + getPayloadInternal().addPage(currentPage_); + currentPages_.add(currentPage_); + } } - - if (element.equals(FormField.FORM_FIELD_ELEMENT_REQUIRED)) { - currentField_.setRequired(true); - } - else if (element.equals(FormField.FORM_FIELD_ELEMENT_DESC)) { - currentField_.setDescription(currentText_); - } - else if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) { - currentField_.addOption( - new Option(currentOptionLabel_, currentOptionValue_)); - parsingOption_ = false; - } - else if (element.equals(FormField.FORM_FIELD_ELEMENT_VALUE)) { - if (parsingOption_) { - currentOptionValue_ = currentText_; - } - else { - currentField_.addValue(currentText_); - } + + else if (currentField_ != null) { + if (element.equals(FormField.FORM_FIELD_ELEMENT_REQUIRED)) { + currentField_.setRequired(true); + } + else if (element.equals(FormField.FORM_FIELD_ELEMENT_DESC)) { + currentField_.setDescription(currentText_); + } + else if (element.equals(FormField.FORM_FIELD_ELEMENT_OPTION)) { + currentField_.addOption( + new Option(currentOptionLabel_, currentOptionValue_)); + parsingOption_ = false; + } + else if (element.equals(FormField.FORM_FIELD_ELEMENT_VALUE)) { + if (parsingOption_) { + currentOptionValue_ = currentText_; + } + else { + currentField_.addValue(currentText_); + } + } } if (level_ >= PayloadLevel && currentField_ != null) { - if (element.equals("field")) { - if (parsingReported_) { - form.addReportedField(currentField_); - } else if (currentItem_ != null) { - currentFields_.add(currentField_); - } else { - form.addField(currentField_); + if (element.equals("field")) { + if (parsingReported_) { + getPayloadInternal().addReportedField(currentField_); + } + else if (parsingItem_) { + currentFields_.add(currentField_); + } + else { + if (currentPages_.size() > 0) { + for (FormPage page : currentPages_) { + for (String pRef : page.getFieldRefs()) { + if (pRef.equals(currentField_.getName())) { + page.addField(currentField_); + } + } + } + for (FormSection section : currentSections_) { + for (String sRef : section.getFieldRefs()) { + if (sRef.equals(currentField_.getName())) { + section.addField(currentField_); + } + } + } + } else { + form.addField(currentField_); + } } currentField_ = null; - } + } + } + if (level_ > PayloadLevel) { + if (element.equals("section")) { + if (sectionStack_.size() > 1) { + // Add the section at the top of the stack to the level below + sectionStack_.get(sectionStack_.size()-2).addChildSection(sectionStack_.get(sectionStack_.size()-1)); + sectionStack_.remove(sectionStack_.size()-1); + } + else if (sectionStack_.size() == 1) { + // Add the remaining section on the stack to it's parent page + currentPage_.addChildSection(sectionStack_.get(sectionStack_.size()-1)); + sectionStack_.remove(sectionStack_.size()-1); + } + } + if (currentReportedRef_ != null && !hasReportedRef_) { + if (sectionStack_.size() > 0) { + sectionStack_.get(sectionStack_.size()-1).addReportedRef(currentReportedRef_); + } else if (currentPage_ != null) { + currentPage_.addReportedRef(currentReportedRef_); + } + hasReportedRef_ = true; + currentReportedRef_ = null; + } + if (currentTextElement_ != null) { + if (element.equals("text")) { + currentTextElement_.setTextString(currentText_); + } + if (sectionStack_.size() > 0) { + sectionStack_.get(sectionStack_.size()-1).addTextElement(currentTextElement_); + } else if (currentPage_ != null) { + currentPage_.addTextElement(currentTextElement_); + } + currentTextElement_ = null; + } } } diff --git a/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java index c7a53cf..a40551e 100644 --- a/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java +++ b/src/com/isode/stroke/parser/payloadparsers/SearchPayloadParser.java @@ -13,6 +13,8 @@ import com.isode.stroke.elements.SearchPayload; import com.isode.stroke.jid.JID; import com.isode.stroke.parser.AttributeMap; import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.payloadparsers.FormParserFactory; +import com.isode.stroke.parser.payloadparsers.FormParser; public class SearchPayloadParser extends GenericPayloadParser { @@ -22,60 +24,60 @@ public class SearchPayloadParser extends GenericPayloadParser { private int level = 0; private String currentText = ""; - //private FormParserFactory formParserFactory = new FormParserFactory(); /* Not ported yet*/ - //private FormParser formParser; - SearchPayload.Item currentItem; + private FormParserFactory formParserFactory = new FormParserFactory(); + private FormParser formParser; + private SearchPayload.Item currentItem; public SearchPayloadParser() { super(new SearchPayload()); + formParserFactory = new FormParserFactory(); } public void handleStartElement(String element, String ns, AttributeMap attributes) { if (level == TopLevel) { - } - else if (level == PayloadLevel) { - //if (element.equals("x") && ns.equals("jabber:x:data")) { - // assert formParser == null; - // formParser = dynamic_cast(formParserFactory->createPayloadParser()); - //} /* Not ported yet */ - //else - if (element.equals("item")) { - assert currentItem == null; - currentItem = new SearchPayload.Item(); - currentItem.jid = JID.fromString(attributes.getAttribute("jid")); + } - else { + else if (level == PayloadLevel) { + if (element.equals("x") && ns.equals("jabber:x:data")) { + assert (formParser == null); + formParser = (FormParser)(formParserFactory.createPayloadParser()); + } + else + if (element.equals("item")) { + assert currentItem == null; + currentItem = new SearchPayload.Item(); + currentItem.jid = JID.fromString(attributes.getAttribute("jid")); + } + else { + currentText = ""; + } + } + else if (level == ItemLevel && currentItem != null) { currentText = ""; } - } - else if (level == ItemLevel && currentItem != null) { - currentText = ""; - } - //if (formParser) { - // formParser->handleStartElement(element, ns, attributes); - //} /* Not ported yet */ + if (formParser != null) { + formParser.handleStartElement(element, ns, attributes); + } - ++level; + ++level; } public void handleEndElement(String element, String ns) { --level; - //if (formParser) { - // formParser->handleEndElement(element, ns); - //} /*Not Ported yet*/ + if (formParser != null) { + formParser.handleEndElement(element, ns); + } if (level == TopLevel) { } else if (level == PayloadLevel) { - //if (formParser) { - // getPayloadInternal()->setForm(formParser->getPayloadInternal()); - // delete formParser; - // formParser = NULL; - //} - //else /*Not ported yet*/ - if (element.equals("item")) { + if (formParser != null) { + getPayloadInternal().setForm(formParser.getPayloadInternal()); + formParser = null; + } + else if (element.equals("item")) { assert currentItem != null; getPayloadInternal().addItem(currentItem); currentItem = null; @@ -113,12 +115,12 @@ public class SearchPayloadParser extends GenericPayloadParser { } public void handleCharacterData(String data) { - //if (formParser) { - // formParser->handleCharacterData(data); - //} - //else { /*Not ported yet*/ + if (formParser != null) { + formParser.handleCharacterData(data); + } + else { currentText += data; - //} + } } } diff --git a/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java index 79153af..262ba3d 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java @@ -12,15 +12,22 @@ package com.isode.stroke.serializer.payloadserializers; 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.serializer.GenericPayloadSerializer; import com.isode.stroke.serializer.xml.XMLElement; import com.isode.stroke.serializer.xml.XMLTextNode; +import java.util.Vector; /** * Serializer for {@link Form} element. */ public class FormSerializer extends GenericPayloadSerializer { - + + private Vector fields_ = new Vector(); + public FormSerializer() { super(Form.class); } @@ -42,6 +49,14 @@ public class FormSerializer extends GenericPayloadSerializer { } // Add reported element + + for(FormPage page : form.getPages()) { + formElement.addNode(pageToXML(page)); + } + + for(FormField field : form.getFields()) { + formElement.addNode(fieldToXML(field, true)); + } if (!form.getReportedFields().isEmpty()) { XMLElement reportedElement = new XMLElement("reported"); @@ -59,12 +74,15 @@ public class FormSerializer extends GenericPayloadSerializer { } formElement.addNode(itemElement); } - - // Add fields - for (FormField field : form.getFields()) { - formElement.addNode(fieldToXML(field, true)); + + for(FormText text : form.getTextElements()) { + formElement.addNode(textToXML(text)); } + for(FormField field : fields_) { + formElement.addNode(fieldToXML(field,true)); + } + return formElement.serialize(); } @@ -117,6 +135,65 @@ public class FormSerializer extends GenericPayloadSerializer { return fieldElement; } + private XMLElement textToXML(FormText text) { + XMLElement textElement = new XMLElement("text"); + textElement.addNode(new XMLTextNode(text.getTextString())); + return textElement; + } + + private XMLElement fieldRefToXML(String ref) { + XMLElement fieldRefElement = new XMLElement("fieldref"); + fieldRefElement.setAttribute("var", ref); + return fieldRefElement; + } + + /*private XMLElement reportedRefToXML(FormReportedRef reportedRef) { + + }*/ + + private XMLElement pageToXML(FormPage page) { + XMLElement pageElement = new XMLElement("page"); + pageElement.setAttribute("xmlns", page.getXMLNS()); + if (!page.getLabel().isEmpty()) { + pageElement.setAttribute("label", page.getLabel()); + } + for(FormText text : page.getTextElements()) { + pageElement.addNode(textToXML(text)); + } + for (FormField field : page.getFields()) { + pageElement.addNode(fieldRefToXML(field.getName())); + fields_.add(field); + } + for(FormReportedRef reportedRef : page.getReportedRefs()) { + pageElement.addNode(new XMLElement("reportedref")); + } + for(FormSection section : page.getChildSections()) { + pageElement.addNode(sectionToXML(section)); + } + return pageElement; + } + + private XMLElement sectionToXML(FormSection section) { + XMLElement sectionElement = new XMLElement("section"); + if (!section.getLabel().isEmpty()) { + sectionElement.setAttribute("label", section.getLabel()); + } + for(FormText text : section.getTextElements()) { + sectionElement.addNode(textToXML(text)); + } + for(FormField field : section.getFields()) { + sectionElement.addNode(fieldRefToXML(field.getName())); + fields_.add(field); + } + for(FormReportedRef reportedRef : section.getReportedRefs()) { + sectionElement.addNode(new XMLElement("reportedref")); + } + for(FormSection childSection : section.getChildSections()) { + sectionElement.addNode(sectionToXML(childSection)); + } + return sectionElement; + } + private void multiLineify(String text, String elementName, XMLElement element) { if (text == null) { diff --git a/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java index 3edfe23..da87b5c 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/SearchPayloadSerializer.java @@ -7,6 +7,8 @@ package com.isode.stroke.serializer.payloadserializers; import com.isode.stroke.elements.SearchPayload; import com.isode.stroke.serializer.GenericPayloadSerializer; import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; +import com.isode.stroke.elements.Form; /** * SearchPayload to String. @@ -52,9 +54,10 @@ public class SearchPayloadSerializer extends GenericPayloadSerializergetForm()) { - // searchElement.addNode(boost::shared_ptr(new XMLRawTextNode(FormSerializer().serialize(form)))); - //} /* Not ported yet. When the time comes, look at Swiften to check if it's changed. It will have. */ + if (searchPayload.getForm() != null) { + Form form = searchPayload.getForm(); + searchElement.addNode(new XMLRawTextNode(new FormSerializer().serialize(form))); + } return searchElement.serialize(); } 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("" - + "Bot Configuration" - + "Hello!" - + "Fill out this form to configure your new bot!" - + ""); - 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("" - + "" - + "jabber:bot" - + "" - + "" - + "" - + "someText" - + "" - + "" - + "" - + "" - + "itemValue" - + "" - + "" - + "Section 1: Bot Info" - + "" - + "This is a bot.A quite good one actually" - + "" - + "" - + "1" - + "" - + "" - + "" - + "news" - + "search" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "20" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "Tell all your friends about your new bot!" - + "foo@bar.com" + "baz@fum.org" - + "" + "" + "foo" - + "" - + ""); - - - - 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("" + + "Bot Configuration" + + "Hello!" + + "Fill out this form to configure your new bot!" + + ""); + 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("" + + "" + + "jabber:bot" + + "" + + "" + + "" + + "someText" + + "" + + "" + + "" + + "" + + "itemValue" + + "" + + "" + + "Section 1: Bot Info" + + "" + + "This is a bot.A quite good one actually" + + "" + + "" + + "1" + + "" + + "" + + "" + + "news" + + "search" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "20" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "Tell all your friends about your new bot!" + + "foo@bar.com" + "baz@fum.org" + + "" + "" + "foo" + + "" + + ""); + + + + 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( + "" + + "" + + "" + + "P1T1" + + "" + + "
" + + "P1S1T1" + + "" + + "
" + + "
" + + "" + + "
" + + "
" + + "
" + + "
" + + "
" + + "" + + "" + + "" + + ""); + + 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( + "" + + "" + + "jabber:iq:search" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "Benvolio" + + "Montague" + + "benvolio@montague.net" + + "male" + + "" + + "" + + "Romeo" + + "Montague" + + "romeo@montague.net" + + "male" + + "" + + ""); + + assertNotNull(dataForm); + + List reported = dataForm.getReportedFields(); + assertEquals(4, reported.size()); + + List 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( + "" + + "Foo" + + "" + + "" + + "" + )); + + 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( + "" + + "" + + "Juliet" + + "Capulet" + + "JuliC" + + "juliet@shakespeare.lit" + + "" + + "" + + "Tybalt" + + "Capulet" + + "ty" + + "tybalt@shakespeare.lit" + + "" + + "" + )); + + 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( + "" + + "" + + "Use the enclosed form to search. If your Jabber client does not" + + " support Data Forms, visit http://shakespeare.lit/" + + "" + + "" + + "User Directory Search" + + "" + + "Please provide the following information" + + " to search for Shakespearean characters." + + "" + + "" + + "jabber:iq:search" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + )); + + 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("" + + " " + + " " + + " jabber:iq:search" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " Benvolio" + + " Montague" + + " benvolio@montague.net" + + " male" + + " " + + " " + + " Romeo" + + " Montague" + + " romeo@montague.net" + + " male" + + " " + + " " + + "")); + + SearchPayload payload = (SearchPayload)parser.getPayload(); + assertNotNull(payload); + + Form dataForm = payload.getForm(); + assertNotNull(dataForm); + + List reported = dataForm.getReportedFields(); + assertEquals(4, reported.size()); + + List 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( - "" - + "Bot Configuration" - + "Hello!" - + "Fill out this form to configure your new bot!" - + "", 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( - "" - + "" - + "" - + "someText" - + "" - + "" - + "" - + "" - + "itemValue" - + "" - + "" - + "" - + "This is some textthis is some more" - + "" - + "" - + "jabber:bot" - + "" - + "Section 1: Bot Info" - + "" - + "This is a bot.A quite good one actually" - + "" - + "" - + "1" - + "" - + "" - + "" - + "news" - + "search" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "20" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "jidSingleDescription" - + "user@example.com" - + "" - + "" - + "Tell all your friends about your new bot!" - + "foo@bar.com" - + "baz@fum.org" + "" + "", - - /* - + "0" - + "Fixed>" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "textPrivateVal" - + "" - + "" - + "" - + "" - + "" - */ - 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( + "" + + "Bot Configuration" + + "Hello!" + + "Fill out this form to configure your new bot!" + + "", 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( + "" + + "" + + "jabber:bot" + + "" + + "Section 1: Bot Info" + + "" + + "This is a bot.A quite good one actually" + + "" + + "" + + "1" + + "" + + "" + + "" + + "news" + + "search" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "20" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "Tell all your friends about your new bot!" + + "foo@bar.com" + + "baz@fum.org" + + "" + + "", + 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( + "" + + "" + + "P1T1" + + "" + + "" + + "
" + + "P1S1T1" + + "" + + "
" + + "
" + + "" + + "
" + + "
" + + "
" + + "
" + + "
" + + "" + + "" + + "" + + "", 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( + "" + + "" + + "jabber:iq:search" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "Benvolio" + + "Montague" + + "benvolio@montague.net" + + "male" + + "" + + "" + + "Romeo" + + "Montague" + + "romeo@montague.net" + + "male" + + "" + + "", 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 = - "" + - "Use the enclosed form to register." + - "" + - "Contest Registration" + - "" + - "jabber:iq:register" + - "" + - "" + - ""; + "" + + "Use the enclosed form to register." + + "" + + "Contest Registration" + + "" + + "jabber:iq:register" + + "" + + "" + + ""; 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 = - "" - + "" - + "" - + "urn:xmpp:mam:0" - + "" - + "" - + "2010-08-07T00:00:00Z" - + "" - + "" - + "" - + "10" - + "" - + ""; - - 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 = + "" + + "" + + "" + + "urn:xmpp:mam:0" + + "" + + "" + + "2010-08-07T00:00:00Z" + + "" + + "" + + "" + + "10" + + "" + + ""; + + 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("" + + "Juliet" + + "Capulet" + + "", 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("" + + "" + + "Juliet" + + "Capulet" + + "JuliC" + + "juliet@shakespeare.lit" + + "" + + "" + + "Tybalt" + + "Capulet" + + "ty" + + "tybalt@shakespeare.lit" + + "" + + "", 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("" + + "" + + "" + + "jabber:iq:search" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "Benvolio" + + "Montague" + + "benvolio@montague.net" + + "male" + + "" + + "" + + "Romeo" + + "Montague" + + "romeo@montague.net" + + "male" + + "" + + "" + + "", testling.serialize(payload)); + } +} \ No newline at end of file -- cgit v0.10.2-6-g49f6