Search in sources :

Example 16 with JSONElement

use of com.zimbra.common.soap.Element.JSONElement 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 17 with JSONElement

use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method wrapperAbsentIfEmpty.

/**
 * How to achieve a wrapped list where no element for the wrapper will appear in the XML if the list is empty.
 * This no longer works!  Fortunately, looks like we don't use any classes similar to WrapperAbsentIfEmpty
 * at the moment.
 * For JUDASPRIEST/Java 7 setNumbers is called with 2 integers.  For main/Java 8
 * it is called with an empty list.
 *     WrapperAbsentIfEmpty.setNumbers(List<Integer>) line: 51
 *     WrapperAbsentIfEmpty$JaxbAccessorM_getNumbers_setNumbers_java_util_List.set(Object, Object) line: 60
 *     Lister$CollectionLister<BeanT,T>.startPacking(BeanT, Accessor<BeanT,T>) line: 298
 *     Lister$CollectionLister<BeanT,T>.startPacking(Object, Accessor) line: 269
 *     Scope<BeanT,PropT,ItemT,PackT>.start(Accessor<BeanT,PropT>, Lister<BeanT,PropT,ItemT,PackT>) line: 142
 *     ArrayERProperty$ItemsLoader.startElement(UnmarshallingContext$State, TagName) line: 119
 *     UnmarshallingContext._startElement(TagName) line: 501
 *     UnmarshallingContext.startElement(TagName) line: 480
 *     InterningXmlVisitor.startElement(TagName) line: 75
 *     SAXConnector.startElement(String, String, String, Attributes) line: 150
 *     DOMScanner.visit(Element) line: 244
 *     DOMScanner.visit(Node) line: 281
 *     DOMScanner.visit(Element) line: 250
 *     DOMScanner.scan(Element) line: 127
 *     UnmarshallerImpl.unmarshal0(Node, JaxBeanInfo) line: 369
 *     UnmarshallerImpl.unmarshal(Node, Class<T>) line: 348
 *     JaxbUtil.rawW3cDomDocToJaxb(Document, Class<?>, boolean) line: 1420
 *     JaxbUtil.w3cDomDocToJaxb(Document, Class<?>, boolean) line: 1392
 *     JaxbUtil.elementToJaxb(Element, Class<?>) line: 1438
 *     JaxbToJsonTest.wrapperAbsentIfEmpty() line: 1516
 */
@Ignore("Bug:95509 - this is hypothetical functionality that isn't currently used, so low priority")
@Test
public void wrapperAbsentIfEmpty() throws Exception {
    WrapperAbsentIfEmpty jaxb = new WrapperAbsentIfEmpty();
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    WrapperAbsentIfEmpty roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrapperAbsentIfEmpty.class);
    logDebug("JSONElement from JAXB (empty numbers)--> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertNull("original getNumbers (empty list)", jaxb.getNumbers());
    Assert.assertNull("JSON roundtripped getNumbers (empty list)", roundtripped.getNumbers());
    Assert.assertEquals("JSON for empty Numbers", "{\n  \"_jsns\": \"urn:zimbraTest\"\n}", jsonJaxbElem.prettyPrint());
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement from JAXB (empty numbers) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    WrapperAbsentIfEmpty roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, WrapperAbsentIfEmpty.class);
    Assert.assertNull("XML roundtripped getNumbers (empty list)", roundtrippedX.getNumbers());
    Assert.assertEquals("XML for empty Numbers", "<wrapper-absent-if-empty xmlns=\"urn:zimbraTest\"/>", xmlElem.prettyPrint());
    jaxb.addNumber(314159);
    jaxb.addNumber(42);
    jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrapperAbsentIfEmpty.class);
    logDebug("JSONElement from JAXB (empty numbers)--> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("original size of Numbers (2 numbers)", 2, jaxb.getNumbers().size());
    Assert.assertNotNull("XML roundtripped getNumbers (when 2 numbers in list)", roundtrippedX.getNumbers());
    Assert.assertEquals("JSON roundtripped size of Numbers (2 numbers)", 2, roundtripped.getNumbers().size());
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement from JAXB (empty numbers) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, WrapperAbsentIfEmpty.class);
    Assert.assertEquals("XML roundtripped size of Numbers (2 numbers)", 2, roundtrippedX.getNumbers().size());
    Assert.assertEquals("XML when have 2 numbers", String.format("%s\n  %s\n    %s\n    %s\n  %s\n%s", "<wrapper-absent-if-empty xmlns=\"urn:zimbraTest\">", "<numbers>", "<number>314159</number>", "<number>42</number>", "</numbers>", "</wrapper-absent-if-empty>"), xmlElem.prettyPrint());
}
Also used : 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) WrapperAbsentIfEmpty(com.zimbra.soap.jaxb.WrapperAbsentIfEmpty) Ignore(org.junit.Ignore) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 18 with JSONElement

use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method transientHandling.

/**
 * XmlTransient handling - Need to ignore fields with {@link XmlElementRef} annotation.
 */
@Test
public void transientHandling() throws Exception {
    String str = "my string - should NOT be serialized";
    int num = 321;
    TransientTester jaxb = new TransientTester(str, num);
    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());
    TransientTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, TransientTester.class);
    TransientTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, TransientTester.class);
    Assert.assertEquals("roundtrippedX num", new Integer(num), roundtrippedX.getNummer());
    Assert.assertNull("roundtrippedX str", roundtrippedX.getToBeIgnored());
    Assert.assertEquals("roundtripped num", new Integer(num), roundtripped.getNummer());
    Assert.assertNull("roundtripped str", roundtripped.getToBeIgnored());
}
Also used : TransientTester(com.zimbra.soap.jaxb.TransientTester) 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 19 with JSONElement

use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method stringElemAndZimbraJsonlongAttr.

/**
 * Desired JSON :
 *      {
 *        "authToken": [{
 *            "_content": "authenticationtoken"
 *          }],
 *        "lifetime": 3000,
 *        "_jsns": "urn:zimbraAdmin"
 *      }
 */
@Test
public void stringElemAndZimbraJsonlongAttr() throws Exception {
    final long lifetime = 3000;
    final String lifetimeStr = new Long(lifetime).toString();
    final String authToken = "authenticationtoken";
    AuthResponse tstr = new AuthResponse();
    tstr.setAuthToken(authToken);
    tstr.setLifetime(lifetime);
    Element xmlElem = JaxbUtil.jaxbToElement(tstr, Element.XMLElement.mFactory, true, false);
    AuthResponse roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AuthResponse.class);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    AuthResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AuthResponse.class);
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("JSONElement lifetime", lifetimeStr, jsonJaxbElem.getAttribute("lifetime"));
    Assert.assertEquals("JSONElement authToken", authToken, jsonJaxbElem.getElement("authToken").getText());
    Assert.assertEquals("roundtripped lifetime", lifetime, roundtripped.getLifetime());
    Assert.assertEquals("roundtripped authToken", authToken, roundtripped.getAuthToken());
    Assert.assertEquals("roundtrippedX lifetime", lifetime, roundtrippedX.getLifetime());
    Assert.assertEquals("roundtrippedX authToken", authToken, roundtrippedX.getAuthToken());
}
Also used : 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) AuthResponse(com.zimbra.soap.admin.message.AuthResponse) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 20 with JSONElement

use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method stringAttrAndElem.

/**
 * Testing String attribute and String element.
 * Also testing differing namespaces on package, root element and field element
 * Desired JSON :
 *      {
 *        "attribute-1": "My attribute ONE",
 *        "element1": [{
 *            "_content": "My element ONE",
 *            "_jsns": "urn:ZimbraTest3"
 *          }],
 *        "_jsns": "urn:ZimbraTest2"
 *      }
 */
@Test
public void stringAttrAndElem() throws Exception {
    final String attr1Val = "My attribute ONE";
    final String elem1Val = "My element ONE";
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("string-tester", "urn:zimbraTest2"));
    jsonElem.addAttribute("attribute-1", attr1Val);
    jsonElem.addNonUniqueElement(QName.get("element1", "urn:zimbraTest3")).addText(elem1Val);
    StringAttrStringElem tstr = new StringAttrStringElem();
    tstr.setAttr1(attr1Val);
    tstr.setElem1(elem1Val);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    Element xmlElem = JaxbUtil.jaxbToElement(tstr, Element.XMLElement.mFactory, true, false);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    StringAttrStringElem roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, StringAttrStringElem.class);
    StringAttrStringElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, StringAttrStringElem.class);
    Assert.assertEquals("JSONElement attr1", attr1Val, jsonJaxbElem.getAttribute("attribute-1"));
    Assert.assertEquals("JSONElement elem1", elem1Val, jsonJaxbElem.getElement("element1").getText());
    Assert.assertEquals("roundtrippedX attr1", attr1Val, roundtrippedX.getAttr1());
    Assert.assertEquals("roundtrippedX elem1", elem1Val, roundtrippedX.getElem1());
    Assert.assertEquals("roundtripped attr1", attr1Val, roundtripped.getAttr1());
    Assert.assertEquals("roundtripped elem1", elem1Val, roundtripped.getElem1());
}
Also used : StringAttrStringElem(com.zimbra.soap.jaxb.StringAttrStringElem) 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

JSONElement (com.zimbra.common.soap.Element.JSONElement)49 XMLElement (com.zimbra.common.soap.Element.XMLElement)48 Element (com.zimbra.common.soap.Element)47 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)45 XmlElement (javax.xml.bind.annotation.XmlElement)45 Test (org.junit.Test)44 FilterTest (com.zimbra.soap.mail.type.FilterTest)43 KeyValuePair (com.zimbra.soap.type.KeyValuePair)9 StringAttribIntValue (com.zimbra.soap.jaxb.StringAttribIntValue)5 StringAttrStringElem (com.zimbra.soap.jaxb.StringAttrStringElem)3 ViewEnum (com.zimbra.soap.jaxb.ViewEnum)3 WrappedEnumElemList (com.zimbra.soap.jaxb.WrappedEnumElemList)3 FilterTests (com.zimbra.soap.mail.type.FilterTests)3 ServiceException (com.zimbra.common.service.ServiceException)2 CreateDistributionListResponse (com.zimbra.soap.account.message.CreateDistributionListResponse)2 GetDistributionListResponse (com.zimbra.soap.account.message.GetDistributionListResponse)2 DLInfo (com.zimbra.soap.account.type.DLInfo)2 DistributionListInfo (com.zimbra.soap.account.type.DistributionListInfo)2 VerifyIndexResponse (com.zimbra.soap.admin.message.VerifyIndexResponse)2 EnumAttribEnumElem (com.zimbra.soap.jaxb.EnumAttribEnumElem)2