Search in sources :

Example 36 with JSONElement

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

the class JaxbToJsonTest method wrappedEnumElemList.

/**
 *  Testing form:
 *      {@code @XmlElementWrapper(name="wrapper", required=false)
 *      @XmlElement(name="enum-entry", required=false)
 *      private List<ViewEnum> entries = Lists.newArrayList();}
 * Desired JSON :
 *      {
 *        "wrapper": [{
 *            "enum-entry": [
 *              {
 *                "_content": "appointment"
 *              },
 *              {
 *                "_content": "document"
 *              }]
 *          }],
 *        "_jsns": "urn:zimbraTest"
 *      }
 */
@Test
public void wrappedEnumElemList() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("wrapped-enum-elem-list", "urn:zimbraTest"));
    Element wrapElem = jsonElem.addNonUniqueElement("wrapper");
    wrapElem.addNonUniqueElement("enum-entry").addText(ViewEnum.APPOINTMENT.toString());
    wrapElem.addNonUniqueElement("enum-entry").addText(ViewEnum.DOCUMENT.toString());
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    WrappedEnumElemList tstr = new WrappedEnumElemList();
    tstr.addEntry(ViewEnum.APPOINTMENT);
    tstr.addEntry(ViewEnum.DOCUMENT);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    WrappedEnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrappedEnumElemList.class);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Element wElem = jsonJaxbElem.getElement("wrapper");
    List<Element> jsonElems = wElem.listElements();
    List<ViewEnum> entries = roundtripped.getEntries();
    Assert.assertEquals("jsonElems num", 2, jsonElems.size());
    Assert.assertEquals("entries num", 2, entries.size());
    Assert.assertTrue("has APPOINTMENT", entries.contains(ViewEnum.APPOINTMENT));
    Assert.assertTrue("has DOCUMENT", entries.contains(ViewEnum.DOCUMENT));
}
Also used : WrappedEnumElemList(com.zimbra.soap.jaxb.WrappedEnumElemList) 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) ViewEnum(com.zimbra.soap.jaxb.ViewEnum) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 37 with JSONElement

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

the class JaxbToJsonTest method missingRequiredWrappedAndInt.

/**
 * Permissive handling - if required things are not present, users need to handle this
 */
@Test
public void missingRequiredWrappedAndInt() throws Exception {
    WrappedRequired tstr = new WrappedRequired();
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    WrappedRequired roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrappedRequired.class);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    try {
        jsonJaxbElem.getElement("wrapper");
    } catch (ServiceException svcE) {
        Assert.assertEquals("JSONElement exception when getting missing wrapper:", ServiceException.INVALID_REQUEST, svcE.getCode());
    }
    try {
        jsonJaxbElem.getAttributeInt("required-int");
    } catch (ServiceException svcE) {
        Assert.assertEquals("JSONElement exception when getting missing int:", ServiceException.INVALID_REQUEST, svcE.getCode());
    }
    try {
        jsonJaxbElem.getAttributeInt("required-bool");
    } catch (ServiceException svcE) {
        Assert.assertEquals("JSONElement exception when getting missing bool:", ServiceException.INVALID_REQUEST, svcE.getCode());
    }
    try {
        jsonJaxbElem.getAttributeInt("required-complex");
    } catch (ServiceException svcE) {
        Assert.assertEquals("JSONElement exception when getting missing complex element:", ServiceException.INVALID_REQUEST, svcE.getCode());
    }
    Assert.assertEquals("roundtripped entries", 0, roundtripped.getEntries().size());
    Assert.assertEquals("roundtripped required int", 0, roundtripped.getRequiredInt());
    Assert.assertEquals("roundtripped required ZmBoolean", null, roundtripped.getRequiredBool());
    Assert.assertEquals("roundtripped required complex", null, roundtripped.getRequiredComplex());
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) WrappedRequired(com.zimbra.soap.jaxb.WrappedRequired) 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 38 with JSONElement

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

the class JaxbToJsonTest method kvpForCreateDLRespBug74371.

/*
# zmsoap -z -t account -m user1 GetDistributionListRequest/dl=grendl@coco.local @by=name
<GetDistributionListResponse xmlns="urn:zimbraAccount">
  <dl id="7a3e8ec5-4892-4b17-9225-cf17e8b3acc9" dynamic="1" name="grendl@coco.local" isOwner="1" isMember="1">
    <a n="mail">grendl@coco.local</a>
    <a n="zimbraMailStatus">enabled</a>
    <a n="zimbraMailAlias">grendl@coco.local</a>
    <a n="description">Wonder at that</a>
    <a n="displayName">Gren DLfun</a>
    <a n="zimbraDistributionListSubscriptionPolicy">ACCEPT</a>
    <a n="zimbraDistributionListUnsubscriptionPolicy">ACCEPT</a>
  </dl>
</GetDistributionListResponse>
# zmsoap --json -z -t account -m user1 GetDistributionListRequest/dl=grendl@coco.local @by=name
{
  "dl": [{
      "name": "grendl@coco.local",
      "id": "7a3e8ec5-4892-4b17-9225-cf17e8b3acc9",
      "dynamic": true,
      "isMember": true,
      "isOwner": true,
      "_attrs": {
        "mail": "grendl@coco.local",
        "zimbraMailStatus": "enabled",
        "zimbraMailAlias": "grendl@coco.local",
        "description": "Wonder at that",
        "displayName": "Gren DLfun",
        "zimbraDistributionListSubscriptionPolicy": "ACCEPT",
        "zimbraDistributionListUnsubscriptionPolicy": "ACCEPT"
      }
    }],
  "_jsns": "urn:zimbraAccount"
}

Extract from mailbox.log for creation of this DL by ZWC - demonstrating the different handling of attrs - See Bug 74371

      "CreateDistributionListResponse": [{
          "dl": [{
              "name": "grendl@coco.local",
              "id": "7a3e8ec5-4892-4b17-9225-cf17e8b3acc9",
              "dynamic": true,
              "a": [
                {
                  "n": "memberURL",
                  "_content": "ldap:///??sub?(|(zimbraMemberOf=7a3e8ec5-4892-4b17-9225-cf17e8b3acc9)(zimbraId=de47828e-94dd-45c3-9770-4dbd255564ca))"
                },
                {
                  "n": "mail",
                  "_content": "grendl@coco.local"
                },
                ...
     */
/**
 * Desired JSON
 */
// Re-enable when Bug 74371 is fixed? @Test
public void kvpForCreateDLRespBug74371() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get(AccountConstants.E_CREATE_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
    populateCreateDlResp(jsonElem);
    Element xmlElem = XMLElement.mFactory.createElement(QName.get(AccountConstants.E_CREATE_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
    populateCreateDlResp(xmlElem);
    logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    List<KeyValuePair> attrs = Lists.newArrayList();
    attrs.add(new KeyValuePair("key1", "value1"));
    attrs.add(new KeyValuePair("key2", "value2"));
    DLInfo dl = new DLInfo("myId", "myRef", "my name", null, null, null, null, null);
    CreateDistributionListResponse jaxb = new CreateDistributionListResponse(dl);
    Element xmlJaxbElem = JaxbUtil.jaxbToElement(jaxb, XMLElement.mFactory);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    DLInfo roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, DLInfo.class);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlJaxbElem.prettyPrint());
    Element eDL = jsonJaxbElem.getElement(AdminConstants.E_DL);
    List<? extends KeyValuePair> kvps = roundtripped.getAttrList();
    Assert.assertEquals("roundtripped kvps num", 2, kvps.size());
    List<Element.KeyValuePair> elemKVPs = eDL.getElement("a").listKeyValuePairs();
    Assert.assertEquals("elemKVP num", 2, elemKVPs.size());
    Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) 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) CreateDistributionListResponse(com.zimbra.soap.account.message.CreateDistributionListResponse) DLInfo(com.zimbra.soap.account.type.DLInfo)

Example 39 with JSONElement

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

the class JaxbToJsonTest method mixedAndAnyElementHandlingJustElement.

/**
 * {@link XmlAnyElement} and {@link XmlMixed} handling
 * In the places we use XmlMixed, we typically have either just text or just elements - this tests with elements
 * that do NOT map to JAXB classes.
 * Desired JSON:
 * {
 *   "alien": {
 *     "myAttr": "myValue",
 *     "child": {
 *       "_content": "Purple beans"
 *     },
 *     "daughter": {
 *       "age": "23",
 *       "name": "Kate"
 *     },
 *     "_jsns": "urn:foreign"
 *   },
 *   "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void mixedAndAnyElementHandlingJustElement() throws Exception {
    List<Object> elems = Lists.newArrayList();
    MixedAnyTester jaxb = new MixedAnyTester();
    DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = builder.newDocument();
    org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
    elem.setAttribute("myAttr", "myValue");
    org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
    child.setTextContent("Purple beans");
    elem.appendChild(child);
    org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
    child2.setAttribute("name", "Kate");
    child2.setAttribute("age", "23");
    elem.appendChild(child2);
    elems.add(elem);
    jaxb.setElems(elems);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
    logDebug("XmlElement (for comparison) [Mixed w3c element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    MixedAnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedAnyTester.class);
    Assert.assertEquals("roundtrippedX [Mixed w3c element] num elems", 1, roundtrippedX.getElems().size());
    org.w3c.dom.Element w3ce = (org.w3c.dom.Element) roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX [Mixed w3c element] elem name", "alien", w3ce.getLocalName());
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB [Mixed w3c element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    MixedAnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedAnyTester.class);
    Assert.assertEquals("roundtripped [Mixed w3c element] num elems", 1, roundtripped.getElems().size());
    org.w3c.dom.Element rtElem = (org.w3c.dom.Element) roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped [Mixed w3c element] elem name", "alien", rtElem.getTagName());
    Assert.assertEquals("roundtripped [Mixed w3c element] elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
Also used : MixedAnyTester(com.zimbra.soap.jaxb.MixedAnyTester) DocumentBuilder(javax.xml.parsers.DocumentBuilder) 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 40 with JSONElement

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

the class JaxbToJsonTest method keyValuePairs.

/**
 * Desired JSON :
 * {
 *   "_attrs": {
 *     "key1": "value1",
 *     "key2": [
 *       "value2-a",
 *       "value2-b"]
 *   },
 *   "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void keyValuePairs() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
    jsonElem.addKeyValuePair("key1", "value1");
    jsonElem.addKeyValuePair("key2", "value2-a");
    jsonElem.addKeyValuePair("key2", "value2-b");
    KVPairs kvPairs = new KVPairs();
    kvPairs.addKeyValuePair(new KeyValuePair("key1", "value1"));
    kvPairs.addKeyValuePair(new KeyValuePair("key2", "value2-a"));
    kvPairs.addKeyValuePair(new KeyValuePair("key2", "value2-b"));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(kvPairs);
    KVPairs roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, KVPairs.class);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    List<Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
    Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
    List<KeyValuePair> kvps = roundtripped.getKeyValuePairs();
    Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
    Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) 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) KVPairs(com.zimbra.soap.jaxb.KVPairs) 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