Search in sources :

Example 1 with MixedAnyTester

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

Aggregations

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