Search in sources :

Example 1 with UniqueTester

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

the class JaxbToJsonTest method uniqeElementHandling.

/**
 * A JSON element added via {@code addNonUniqueElement} is always serialized as an array, because there could be
 * further {@code addNonUniqueElement} calls with the same element name.  On the other hand, a JSON element added via
 * {@code addUniqueElement} won't be serialized as an array as their is an implicit assumption that there will
 * be only one element with that name.
 * Desired JSON :
 * {
 *   "unique-str-elem": {
 *      "_content": "Unique element ONE",
 *      "_jsns": "urn:zimbraTest1"
 *    },
 *    "non-unique-elem": [{
 *        "_content": "Unique element ONE",
 *        "_jsns": "urn:zimbraTest1"
 *      }],
 *    "unique-complex-elem": {
 *      "attr1": "Attribute ONE",
 *      "_content": "3"
 *    },
 *    "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void uniqeElementHandling() throws Exception {
    final String elem1 = "Unique element ONE";
    final String attrib1 = "Attribute ONE";
    final int value = 3;
    final String valueStr = new Integer(value).toString();
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("unique-tester", "urn:zimbraTest"));
    jsonElem.addUniqueElement(QName.get("unique-str-elem", "urn:zimbraTest1")).setText(elem1);
    jsonElem.addNonUniqueElement(QName.get("non-unique-elem", "urn:zimbraTest1")).setText(elem1);
    jsonElem.addUniqueElement("unique-complex-elem").addAttribute("attr1", attrib1).setText(valueStr);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    UniqueTester tstr = new UniqueTester();
    tstr.setUniqueStrElem(elem1);
    tstr.setNonUniqueStrElem(elem1);
    StringAttribIntValue complex = new StringAttribIntValue(attrib1, value);
    tstr.setUniqueComplexElem(complex);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    UniqueTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, UniqueTester.class);
    Assert.assertEquals("roundtripped non-unique elem", elem1, roundtripped.getNonUniqueStrElem());
    Assert.assertEquals("roundtripped unique elem", elem1, roundtripped.getUniqueStrElem());
    StringAttribIntValue roundtrippedComplex = tstr.getUniqueComplexElem();
    Assert.assertEquals("roundtripped complex attrib1", attrib1, roundtrippedComplex.getAttrib1());
    Assert.assertEquals("roundtripped complex value", value, roundtrippedComplex.getMyValue());
}
Also used : UniqueTester(com.zimbra.soap.jaxb.UniqueTester) 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) 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 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)1 UniqueTester (com.zimbra.soap.jaxb.UniqueTester)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