Search in sources :

Example 1 with ElementRefsTester

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

Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 ElementRefsTester (com.zimbra.soap.jaxb.ElementRefsTester)1 EnumAttribs (com.zimbra.soap.jaxb.EnumAttribs)1 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)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