Search in sources :

Example 31 with JSONElement

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

the class JaxbToJsonTest method emptyEnumElemList.

/**
 * Desired JSON :
 * {
 *   "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void emptyEnumElemList() throws Exception {
    EnumElemList tstr = new EnumElemList();
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    logDebug("JSONElement from JAXB FOR EMPTY LIST ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    EnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, EnumElemList.class);
    List<Element> jsonElems = jsonJaxbElem.listElements();
    List<ViewEnum> entries = roundtripped.getEntries();
    Assert.assertEquals("jsonElems num", 0, jsonElems.size());
    Assert.assertEquals("entries num", 0, entries.size());
}
Also used : EnumElemList(com.zimbra.soap.jaxb.EnumElemList) 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 32 with JSONElement

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

the class JaxbToJsonTest method elemsInDiffNamespace.

/**
 * Desired JSON :
 * {
 *   "strAttrStrElem": [{
 *       "attribute-1": "My attribute ONE",
 *       "element1": [{
 *           "_content": "My element ONE",
 *           "_jsns": "urn:ZimbraTest3"
 *         }],
 *       "_jsns": "urn:ZimbraTest5"
 *     }],
 *   "_jsns": "urn:ZimbraTest4"
 * }
 */
@Test
public void elemsInDiffNamespace() throws Exception {
    final String attr1Val = "My attribute ONE";
    final String elem1Val = "My element ONE";
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("ns-delta", "urn:ZimbraTest4"));
    Element saseElem = jsonElem.addNonUniqueElement(QName.get("strAttrStrElem", "urn:ZimbraTest5"));
    saseElem.addAttribute("attribute-1", attr1Val);
    saseElem.addNonUniqueElement(QName.get("element1", "urn:ZimbraTest3")).addText(elem1Val);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    NamespaceDeltaElem tstr = new NamespaceDeltaElem();
    StringAttrStringElem tstrSase = new StringAttrStringElem();
    tstrSase.setAttr1(attr1Val);
    tstrSase.setElem1(elem1Val);
    tstr.setSase(tstrSase);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    NamespaceDeltaElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, NamespaceDeltaElem.class);
    Element saseJsonJaxbElem = jsonJaxbElem.getElement(QName.get("strAttrStrElem", "urn:ZimbraTest5"));
    Assert.assertEquals("JSONElement attr1", attr1Val, saseJsonJaxbElem.getAttribute("attribute-1"));
    Assert.assertEquals("JSONElement elem1", elem1Val, saseJsonJaxbElem.getElement("element1").getText());
    logDebug("roundtripped attr1=%1$s", roundtripped.getSase().getAttr1());
    logDebug("roundtripped elem1=%1$s", roundtripped.getSase().getElem1());
    Assert.assertEquals("roundtripped attr1", attr1Val, roundtripped.getSase().getAttr1());
    Assert.assertEquals("roundtripped elem1", elem1Val, roundtripped.getSase().getElem1());
    Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
}
Also used : StringAttrStringElem(com.zimbra.soap.jaxb.StringAttrStringElem) NamespaceDeltaElem(com.zimbra.soap.jaxb.NamespaceDeltaElem) 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 33 with JSONElement

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

the class JaxbToJsonTest method anyElementHandling.

/**
 * {@link XmlAnyElement} handling
 * <pre>
 *     @XmlAnyElement
 *     private List<org.w3c.dom.Element> elems = Lists.newArrayList();
 * </pre>
 */
@Test
public void anyElementHandling() throws Exception {
    String given = "Given information";
    List<Object> elems = Lists.newArrayList();
    AnyTester jaxb = new AnyTester();
    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);
    org.w3c.dom.Element elem2 = doc.createElementNS("urn:wooky", "may");
    elem2.setAttribute("fourth", "be with you");
    jaxb.setGiven(given);
    jaxb.setElems(Lists.newArrayList(elem, elem2));
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    AnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyTester.class);
    Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
    Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
    org.w3c.dom.Element w3ce = roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX elem name", "alien", w3ce.getLocalName());
    logDebug("STRING from JAXB ---> prettyPrint\n%1$s", getZimbraJsonJaxbString(jaxb));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    AnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyTester.class);
    Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
    Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
    org.w3c.dom.Element rtElem = roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped elem name", "alien", rtElem.getTagName());
    Assert.assertEquals("roundtripped elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
Also used : MixedAnyTester(com.zimbra.soap.jaxb.MixedAnyTester) AnyTester(com.zimbra.soap.jaxb.AnyTester) 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 34 with JSONElement

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

use of com.zimbra.common.soap.Element.JSONElement 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.addNonUniqueElement("classic-elem").setText(str2);
    xmlElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
    xmlElem.addNonUniqueElement("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)

Aggregations

JSONElement (com.zimbra.common.soap.Element.JSONElement)49 XMLElement (com.zimbra.common.soap.Element.XMLElement)48 Element (com.zimbra.common.soap.Element)47 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)45 XmlElement (javax.xml.bind.annotation.XmlElement)45 Test (org.junit.Test)44 FilterTest (com.zimbra.soap.mail.type.FilterTest)43 KeyValuePair (com.zimbra.soap.type.KeyValuePair)9 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)5 StringAttrStringElem (com.zimbra.soap.jaxb.StringAttrStringElem)3 ViewEnum (com.zimbra.soap.jaxb.ViewEnum)3 WrappedEnumElemList (com.zimbra.soap.jaxb.WrappedEnumElemList)3 FilterTests (com.zimbra.soap.mail.type.FilterTests)3 ServiceException (com.zimbra.common.service.ServiceException)2 CreateDistributionListResponse (com.zimbra.soap.account.message.CreateDistributionListResponse)2 GetDistributionListResponse (com.zimbra.soap.account.message.GetDistributionListResponse)2 DLInfo (com.zimbra.soap.account.type.DLInfo)2 DistributionListInfo (com.zimbra.soap.account.type.DistributionListInfo)2 VerifyIndexResponse (com.zimbra.soap.admin.message.VerifyIndexResponse)2 EnumAttribEnumElem (com.zimbra.soap.jaxb.EnumAttribEnumElem)2