Search in sources :

Example 1 with NamespaceDeltaElem

use of com.zimbra.soap.jaxb.NamespaceDeltaElem 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)

Aggregations

Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 NamespaceDeltaElem (com.zimbra.soap.jaxb.NamespaceDeltaElem)1 StringAttrStringElem (com.zimbra.soap.jaxb.StringAttrStringElem)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 Test (org.junit.Test)1