Search in sources :

Example 1 with StringAttribIntValue

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

Example 2 with StringAttribIntValue

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

Example 3 with StringAttribIntValue

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

the class JaxbToJsonTest method stringAttrintValue.

/**
 * Desired JSON :
 *      {
 *        "attr1": "Attribute ONE",
 *        "_content": "3",
 *        "_jsns": "urn:zimbraTest"
 *      }
 */
@Test
public void stringAttrintValue() throws Exception {
    final String attrib1 = "Attribute ONE";
    final int value = 3;
    final String valueStr = new Integer(value).toString();
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("string-attr-int-value", "urn:zimbraTest"));
    jsonElem.addAttribute("attr1", attrib1);
    jsonElem.addText(valueStr);
    StringAttribIntValue tstr = new StringAttribIntValue(attrib1, value);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    StringAttribIntValue roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, StringAttribIntValue.class);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("attr1", attrib1, jsonJaxbElem.getAttribute("attr1"));
    Assert.assertEquals("XmlValue", valueStr, jsonJaxbElem.getText());
    Assert.assertEquals("roundtripped attrib1", attrib1, roundtripped.getAttrib1());
    Assert.assertEquals("roundtripped value", value, roundtripped.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) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 4 with StringAttribIntValue

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

Example 5 with StringAttribIntValue

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

the class JaxbToJsonTest method mixedHandlingWithJaxbAndNoText.

/**
 * {@link XmlMixed} handling
 * In the places we use XmlMixed, we have either just text or just elements
 * This tests where we have something that maps to a JAXB object.
 * Side note:  Also tests out case for {@link XmlElementRef} where name is derived from root element.
 */
@Test
public void mixedHandlingWithJaxbAndNoText() throws Exception {
    String str = "my string";
    int num = 321;
    List<Object> elems = Lists.newArrayList();
    MixedTester jaxb = new MixedTester();
    StringAttribIntValue inner = new StringAttribIntValue(str, num);
    elems.add(inner);
    jaxb.setElems(elems);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement (for comparison) [Mixed has element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    MixedTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedTester.class);
    Assert.assertEquals("roundtrippedX num elems", 1, roundtrippedX.getElems().size());
    StringAttribIntValue rtXmlByRef = (StringAttribIntValue) roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX [Mixed has element] str", str, rtXmlByRef.getAttrib1());
    Assert.assertEquals("roundtrippedX [Mixed has element] num", num, rtXmlByRef.getMyValue());
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB [Mixed has element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    MixedTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedTester.class);
    Assert.assertEquals("roundtripped [Mixed has element] num elems", 1, roundtripped.getElems().size());
    StringAttribIntValue rtByRef = (StringAttribIntValue) roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped [Mixed has element] str", str, rtByRef.getAttrib1());
    Assert.assertEquals("roundtripped [Mixed has element] num", num, rtByRef.getMyValue());
}
Also used : MixedTester(com.zimbra.soap.jaxb.MixedTester) 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)5 JSONElement (com.zimbra.common.soap.Element.JSONElement)5 XMLElement (com.zimbra.common.soap.Element.XMLElement)5 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)5 FilterTest (com.zimbra.soap.mail.type.FilterTest)5 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)5 XmlElement (javax.xml.bind.annotation.XmlElement)5 Test (org.junit.Test)5 ElementRefTester (com.zimbra.soap.jaxb.ElementRefTester)1 ElementRefsTester (com.zimbra.soap.jaxb.ElementRefsTester)1 EnumAttribs (com.zimbra.soap.jaxb.EnumAttribs)1 MixedTester (com.zimbra.soap.jaxb.MixedTester)1 UniqueTester (com.zimbra.soap.jaxb.UniqueTester)1