Search in sources :

Example 81 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method multipleDispositionCONTENTAttributes.

/**
     * <p>Demonstrates that CANNOT have more than one attribute with same name, even if using
     * {@link Element.Disposition.CONTENT} to force treating the attribute as an element in XML.</p>
     * Desired serialization to XML:
     * <pre>
     *     &lt;multi-content-attrs xmlns="urn:zimbraTest">
     *       &lt;soapURL>https://soap.example.test&lt;/soapURL>
     *     &lt;/multi-content-attrs>
     * </pre>
     * Desired serialization to JSON:
     * <pre>
     *     {
     *       "soapURL": "https://soap.example.test",
     *       "_jsns": "urn:zimbraTest"
     *     }
     * </pre>
     */
@Test
public void multipleDispositionCONTENTAttributes() throws Exception {
    final String httpSoap = "http://soap.example.test";
    final String httpsSoap = "https://soap.example.test";
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
    Element xmlElem = XMLElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
    jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
    jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
    xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
    xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
    logDebug("MultiContentAttrs XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    logDebug("MultiContentAttrs JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    Assert.assertEquals("XMLElement soapURL as attribute", httpsSoap, xmlElem.getAttribute(AccountConstants.E_SOAP_URL));
    Assert.assertEquals("XMLElement num soapURL elements", 1, xmlElem.listElements(AccountConstants.E_SOAP_URL).size());
    Assert.assertEquals("XMLElement soapURL as element", httpsSoap, xmlElem.getElement(AccountConstants.E_SOAP_URL).getText());
    Assert.assertEquals("JSONElement soapURL as attribute", httpsSoap, jsonElem.getAttribute(AccountConstants.E_SOAP_URL));
    // 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 soapURL elements", 0, jsonElem.listElements(AccountConstants.E_SOAP_URL).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) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 82 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method zimbraOddKeyValuePairsAnnotation.

/**
     * Check that JSON can be deserialised into a JAXB object when some of the JSON represents Zimbra KeyValuePairs
     * (i.e. An "_attrs" array).
     * In this case, the target objects for each keyvalue pair do NOT use the defaults of "a" for the element name and
     * "n" for the attribute name in the XML form.
     * Desired XML :<br />
     * &lt;key-value-pairs-tester xmlns="urn:zimbraTest">
     *   &lt;oddElemName name="key1">value1&lt;/oddElemName>
     *   &lt;oddElemName name="key2">value2-a&lt;/oddElemName>
     *   &lt;oddElemName name="key2">value2-b&lt;/oddElemName>
     * &lt;/key-value-pairs-tester>
     *<br />
     * Desired JSON :<br />
     * {
     *   "_attrs": {
     *     "key1": "value1",
     *     "key2": [
     *       "value2-a",
     *       "value2-b"]
     *   },
     *   "_jsns": "urn:zimbraTest"
     * }
     */
@Test
public void zimbraOddKeyValuePairsAnnotation() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
    jsonElem.addKeyValuePair("key1", "value1", "oddElemName", "name");
    jsonElem.addKeyValuePair("key2", "value2-a", "oddElemName", "name");
    jsonElem.addKeyValuePair("key2", "value2-b", "oddElemName", "name");
    List<Attr> attrs = Lists.newArrayList();
    attrs.add(new Attr("key1", "value1"));
    attrs.add(new Attr("key2", "value2-a"));
    attrs.add(new Attr("key2", "value2-b"));
    OddKeyValuePairsTester jaxb = new OddKeyValuePairsTester(attrs);
    logDebug("XMLElement (from JAXB) ---> prettyPrint\n%1$s", JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false).prettyPrint());
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    String origJson = jsonJaxbElem.prettyPrint();
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", origJson);
    OddKeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, OddKeyValuePairsTester.class);
    List<com.zimbra.common.soap.Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
    Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
    Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
    List<Attr> kvps = roundtripped.getAttrList();
    Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
    jsonJaxbElem = JacksonUtil.jaxbToJSONElement(roundtripped);
    String finalJson = jsonJaxbElem.prettyPrint();
    if (!origJson.equals(finalJson)) {
        logDebug("JSONElement from roundtripped JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
        Assert.assertEquals("roundtripped JSON pretty print", origJson, finalJson);
    }
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) 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) Attr(com.zimbra.soap.account.type.Attr) OddKeyValuePairsTester(com.zimbra.soap.jaxb.OddKeyValuePairsTester) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 83 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method contentList.

/**
     * Test for "List of strings" field annotated with {@link XmlElement}.
     * Desired JSON:
     * {
     *   "more": false,
     *   "total": 23,
     *   "dlm": [
     *     {
     *       "_content": "dlmember1@no.where"
     *     },
     *     {
     *       "_content": "dlmember2@no.where"
     *     },
     *     {
     *       "_content": "dlmember3@no.where"
     *     }],
     *   "_jsns": "urn:zimbraAccount"
     * }
     */
@Test
public void contentList() throws Exception {
    Element legacyElem = JSONElement.mFactory.createElement(AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
    legacyElem.addAttribute(AccountConstants.A_MORE, false);
    legacyElem.addAttribute(AccountConstants.A_TOTAL, 23);
    legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember1@no.where");
    legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember2@no.where");
    legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember3@no.where");
    logDebug("GetDistributionListMembersResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
    // GetDistributionListMembersResponse has:
    //      @XmlElement(name=AccountConstants.E_DLM, required=false)
    //      private List<String> dlMembers = Lists.newArrayList();
    GetDistributionListMembersResponse jaxb = new GetDistributionListMembersResponse();
    jaxb.setMore(false);
    jaxb.setTotal(23);
    jaxb.addDlMember("dlmember1@no.where");
    jaxb.addDlMember("dlmember2@no.where");
    jaxb.addDlMember("dlmember3@no.where");
    Element elem = JacksonUtil.jaxbToJSONElement(jaxb, AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
    logDebug("GetDistributionListMembersResponse JSONElement from JAXB ---> prettyPrint\n%1$s", elem.prettyPrint());
    List<Element> dlMembers = elem.listElements(AccountConstants.E_DLM);
    Assert.assertEquals("Number of dlMembers", 3, dlMembers.size());
    Element dlMem3 = dlMembers.get(2);
    Assert.assertEquals("dlMember 3", "dlmember3@no.where", dlMem3.getText());
    Assert.assertEquals("total", 23, elem.getAttributeInt(AccountConstants.A_TOTAL));
    Assert.assertEquals("more", false, elem.getAttributeBool(AccountConstants.A_MORE));
    Assert.assertEquals("prettyPrint", legacyElem.prettyPrint(), elem.prettyPrint());
}
Also used : GetDistributionListMembersResponse(com.zimbra.soap.account.message.GetDistributionListMembersResponse) 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 84 with XMLElement

use of com.zimbra.common.soap.Element.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 85 with XMLElement

use of com.zimbra.common.soap.Element.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)

Aggregations

XMLElement (com.zimbra.common.soap.Element.XMLElement)140 Element (com.zimbra.common.soap.Element)111 Test (org.junit.Test)30 JSONElement (com.zimbra.common.soap.Element.JSONElement)29 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)25 XmlElement (javax.xml.bind.annotation.XmlElement)25 FilterTest (com.zimbra.soap.mail.type.FilterTest)24 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)9 HashMap (java.util.HashMap)8 Account (com.zimbra.cs.account.Account)6 ArrayList (java.util.ArrayList)6 ZAuthToken (com.zimbra.common.auth.ZAuthToken)5 KeyValuePair (com.zimbra.soap.type.KeyValuePair)5 ByteBuilder (com.zimbra.common.mime.HeaderUtils.ByteBuilder)4 ServiceException (com.zimbra.common.service.ServiceException)4 DataSource (com.zimbra.cs.account.DataSource)4 Auth (com.zimbra.cs.service.account.Auth)4 URL (java.net.URL)4 Cookie (javax.servlet.http.Cookie)4 Signature (com.zimbra.cs.account.Signature)3