Search in sources :

Example 1 with ElementRefTester

use of com.zimbra.soap.jaxb.ElementRefTester in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method elementRefHandling.

/**
 * XmlElementRef handling.  Note that slightly counter-intuitively, any name specified is ignored (unless
 * type=JAXBElement.class)
 * <pre>
 *   @XmlElementRef(name="ignored-name-root-elem-name-used-instead", type=StringAttribIntValue.class)
 *   private StringAttribIntValue byRef;
 *  </pre>
 * Desired JSON :
 * {
 *   "string-attr-int-value": [{
 *       "attr1": "my string",
 *       "_content": 321
 *     }],
 *   "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void elementRefHandling() throws Exception {
    String str = "my string";
    int num = 321;
    ElementRefTester jaxb = new ElementRefTester();
    StringAttribIntValue inner = new StringAttribIntValue(str, num);
    jaxb.setByRef(inner);
    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());
    ElementRefTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, ElementRefTester.class);
    ElementRefTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, ElementRefTester.class);
    StringAttribIntValue rtByRef = roundtripped.getByRef();
    StringAttribIntValue rtXmlByRef = roundtrippedX.getByRef();
    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());
}
Also used : 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) ElementRefTester(com.zimbra.soap.jaxb.ElementRefTester) 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 ElementRefTester (com.zimbra.soap.jaxb.ElementRefTester)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