Search in sources :

Example 36 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method exerciseZimbraJsonAttribute.

/**
     * <p>Exercise annotation {@link ZimbraJsonAttribute} which is needed in JAXB in addition to {@link XmlElement} or
     * {@link XmlElementRef} annotations to provide a field which as an analog of something like:
     * <pre>
     *      jsonElem.addAttribute("xml-elem-json-attr", "XML elem but JSON attribute", Element.Disposition.CONTENT);
     * </pre>
     * Desired XML serialization:
     * <pre>
     *     &lt;XmlElemJsonAttr xmlns="urn:zimbraTest">
     *       &lt;xml-elem-json-attr>XML elem but JSON attribute&lt;/xml-elem-json-attr>
     *       &lt;classic-elem>elem for both XML and JSON&lt;/classic-elem>
     *     &lt;/XmlElemJsonAttr>
     * </pre>
     * Desired JSON serialization:
     * <pre>
     *     {
     *       "xml-elem-json-attr": "XML elem but JSON attribute",
     *       "classic-elem": [{
     *           "_content": "elem for both XML and JSON"
     *         }],
     *       "_jsns": "urn:zimbraTest"
     *     }
     * </pre>
     */
@Test
public void exerciseZimbraJsonAttribute() throws Exception {
    final String str1 = "XML elem but JSON attribute";
    final String str2 = "elem for both XML and JSON";
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
    Element xmlElem = XMLElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
    jsonElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
    jsonElem.addElement("classic-elem").setText(str2);
    xmlElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
    xmlElem.addElement("classic-elem").setText(str2);
    logDebug("XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    logDebug("JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    XmlElemJsonAttr jaxb = new XmlElemJsonAttr(str1, str2);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("JSONElement and JSONElement from JAXB", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
    XmlElemJsonAttr roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, XmlElemJsonAttr.class);
    Assert.assertEquals("roundtripped xml-elem-json-attr", str1, roundtripped.getXmlElemJsonAttr());
    Assert.assertEquals("roundtripped classic-elem", str2, roundtripped.getDefaultElem());
    Assert.assertEquals("JSONElement xml-elem-json-attr as attribute", str1, jsonElem.getAttribute("xml-elem-json-attr"));
    // Note difference from XMLElement - for JSON this is Always an attribute but for XML it can be treated as an element
    Assert.assertEquals("JSONElement num xml-elem-json-attr elements", 0, jsonElem.listElements("xml-elem-json-attr").size());
}
Also used : XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElemJsonAttr(com.zimbra.soap.jaxb.XmlElemJsonAttr) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 37 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method elementRefsHandling.

/**
     * XmlElementRefs handling
     * Desired JSON :
     * {
     *   "string-attr-int-value": [{
     *       "attr1": "my string",
     *       "_content": 321
     *     }],
     *   "enumEttribs": [{
     *       "fold1": "chat",
     *       "fold2": "remote folder"
     *     }],
     *   "_jsns": "urn:zimbraTest"
     * }
     */
@Test
public void elementRefsHandling() throws Exception {
    String str = "my string";
    int num = 321;
    List<Object> elems = Lists.newArrayList();
    ElementRefsTester jaxb = new ElementRefsTester();
    StringAttribIntValue inner = new StringAttribIntValue(str, num);
    elems.add(inner);
    EnumAttribs ea = new EnumAttribs();
    ea.setFold1(ViewEnum.CHAT);
    ea.setFold2(ViewEnum.REMOTE_FOLDER);
    elems.add(ea);
    jaxb.setElems(elems);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    ElementRefsTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, ElementRefsTester.class);
    ElementRefsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, ElementRefsTester.class);
    Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
    Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
    StringAttribIntValue rtByRef = (StringAttribIntValue) roundtripped.getElems().get(0);
    StringAttribIntValue rtXmlByRef = (StringAttribIntValue) roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX str", str, rtXmlByRef.getAttrib1());
    Assert.assertEquals("roundtrippedX num", num, rtXmlByRef.getMyValue());
    Assert.assertEquals("roundtripped str", str, rtByRef.getAttrib1());
    Assert.assertEquals("roundtripped num", num, rtByRef.getMyValue());
    EnumAttribs rtea = (EnumAttribs) roundtripped.getElems().get(1);
    Assert.assertEquals("roundtripped fold1", ViewEnum.CHAT, rtea.getFold1());
    Assert.assertEquals("roundtripped fold2", ViewEnum.REMOTE_FOLDER, rtea.getFold2());
}
Also used : EnumAttribs(com.zimbra.soap.jaxb.EnumAttribs) StringAttribIntValue(com.zimbra.soap.jaxb.StringAttribIntValue) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) ElementRefsTester(com.zimbra.soap.jaxb.ElementRefsTester) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 38 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method mixedAndAnyElementHandlingJustElement.

/**
     * {@link XmlAnyElement} and {@link XmlMixed} handling
     * In the places we use XmlMixed, we typically have either just text or just elements - this tests with elements
     * that do NOT map to JAXB classes.
     * Desired JSON:
     * {
     *   "alien": {
     *     "myAttr": "myValue",
     *     "child": {
     *       "_content": "Purple beans"
     *     },
     *     "daughter": {
     *       "age": "23",
     *       "name": "Kate"
     *     },
     *     "_jsns": "urn:foreign"
     *   },
     *   "_jsns": "urn:zimbraTest"
     * }
     */
@Test
public void mixedAndAnyElementHandlingJustElement() throws Exception {
    List<Object> elems = Lists.newArrayList();
    MixedAnyTester jaxb = new MixedAnyTester();
    DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = builder.newDocument();
    org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
    elem.setAttribute("myAttr", "myValue");
    org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
    child.setTextContent("Purple beans");
    elem.appendChild(child);
    org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
    child2.setAttribute("name", "Kate");
    child2.setAttribute("age", "23");
    elem.appendChild(child2);
    elems.add(elem);
    jaxb.setElems(elems);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement (for comparison) [Mixed w3c element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    MixedAnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedAnyTester.class);
    Assert.assertEquals("roundtrippedX [Mixed w3c element] num elems", 1, roundtrippedX.getElems().size());
    org.w3c.dom.Element w3ce = (org.w3c.dom.Element) roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX [Mixed w3c element] elem name", "alien", w3ce.getLocalName());
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB [Mixed w3c element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    MixedAnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedAnyTester.class);
    Assert.assertEquals("roundtripped [Mixed w3c element] num elems", 1, roundtripped.getElems().size());
    org.w3c.dom.Element rtElem = (org.w3c.dom.Element) roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped [Mixed w3c element] elem name", "alien", rtElem.getTagName());
    Assert.assertEquals("roundtripped [Mixed w3c element] elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
Also used : MixedAnyTester(com.zimbra.soap.jaxb.MixedAnyTester) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 39 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method wrappedEnumElemList.

/**
     *  Testing form:
     *      {@code @XmlElementWrapper(name="wrapper", required=false)
     *      @XmlElement(name="enum-entry", required=false)
     *      private List<ViewEnum> entries = Lists.newArrayList();}
     * Desired JSON :
     *      {
     *        "wrapper": [{
     *            "enum-entry": [
     *              {
     *                "_content": "appointment"
     *              },
     *              {
     *                "_content": "document"
     *              }]
     *          }],
     *        "_jsns": "urn:zimbraTest"
     *      }
     */
@Test
public void wrappedEnumElemList() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("wrapped-enum-elem-list", "urn:zimbraTest"));
    Element wrapElem = jsonElem.addElement("wrapper");
    wrapElem.addElement("enum-entry").addText(ViewEnum.APPOINTMENT.toString());
    wrapElem.addElement("enum-entry").addText(ViewEnum.DOCUMENT.toString());
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    WrappedEnumElemList tstr = new WrappedEnumElemList();
    tstr.addEntry(ViewEnum.APPOINTMENT);
    tstr.addEntry(ViewEnum.DOCUMENT);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    WrappedEnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrappedEnumElemList.class);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Element wElem = jsonJaxbElem.getElement("wrapper");
    List<Element> jsonElems = wElem.listElements();
    List<ViewEnum> entries = roundtripped.getEntries();
    Assert.assertEquals("jsonElems num", 2, jsonElems.size());
    Assert.assertEquals("entries num", 2, entries.size());
    Assert.assertTrue("has APPOINTMENT", entries.contains(ViewEnum.APPOINTMENT));
    Assert.assertTrue("has DOCUMENT", entries.contains(ViewEnum.DOCUMENT));
}
Also used : WrappedEnumElemList(com.zimbra.soap.jaxb.WrappedEnumElemList) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) ViewEnum(com.zimbra.soap.jaxb.ViewEnum) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 40 with XmlElement

use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method mixedHandlingWithJaxbAndNoText.

/**
     * {@link XmlMixed} handling
     * In the places we use XmlMixed, we have either just text or just elements
     * This tests where we have something that maps to a JAXB object.
     * Side note:  Also tests out case for {@link XmlElementRef} where name is derived from root element.
     */
@Test
public void mixedHandlingWithJaxbAndNoText() throws Exception {
    String str = "my string";
    int num = 321;
    List<Object> elems = Lists.newArrayList();
    MixedTester jaxb = new MixedTester();
    StringAttribIntValue inner = new StringAttribIntValue(str, num);
    elems.add(inner);
    jaxb.setElems(elems);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement (for comparison) [Mixed has element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    MixedTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedTester.class);
    Assert.assertEquals("roundtrippedX num elems", 1, roundtrippedX.getElems().size());
    StringAttribIntValue rtXmlByRef = (StringAttribIntValue) roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX [Mixed has element] str", str, rtXmlByRef.getAttrib1());
    Assert.assertEquals("roundtrippedX [Mixed has element] num", num, rtXmlByRef.getMyValue());
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB [Mixed has element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    MixedTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedTester.class);
    Assert.assertEquals("roundtripped [Mixed has element] num elems", 1, roundtripped.getElems().size());
    StringAttribIntValue rtByRef = (StringAttribIntValue) roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped [Mixed has element] str", str, rtByRef.getAttrib1());
    Assert.assertEquals("roundtripped [Mixed has element] num", num, rtByRef.getMyValue());
}
Also used : MixedTester(com.zimbra.soap.jaxb.MixedTester) StringAttribIntValue(com.zimbra.soap.jaxb.StringAttribIntValue) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

XmlElement (javax.xml.bind.annotation.XmlElement)51 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)26 Element (com.zimbra.common.soap.Element)25 JSONElement (com.zimbra.common.soap.Element.JSONElement)25 XMLElement (com.zimbra.common.soap.Element.XMLElement)25 Test (org.junit.Test)25 FilterTest (com.zimbra.soap.mail.type.FilterTest)24 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)8 Field (java.lang.reflect.Field)7 XmlElements (javax.xml.bind.annotation.XmlElements)7 KeyValuePair (com.zimbra.soap.type.KeyValuePair)5 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)5 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)3 Annotation (java.lang.annotation.Annotation)3 TypeMirror (javax.lang.model.type.TypeMirror)3 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)3 QName (javax.xml.namespace.QName)3 GetDistributionListResponse (com.zimbra.soap.account.message.GetDistributionListResponse)2