Search in sources :

Example 86 with Element

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

the class JaxbToJsonTest method kvpForGetDLRespWithOwner.

/**
     * Ensuring that JAXB can handle having an owner in a list that is not an empty array when there are no owners
     */
@Test
public void kvpForGetDLRespWithOwner() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get(AccountConstants.E_GET_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
    populateGetDlResp(jsonElem);
    Element xmlElem = XMLElement.mFactory.createElement(QName.get(AccountConstants.E_GET_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
    populateGetDlResp(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("mail", "fun@example.test"));
    attrs.add(new KeyValuePair("zimbraMailStatus", "enabled"));
    DistributionListInfo dl = new DistributionListInfo("myId", "my name", null, attrs);
    dl.setDynamic(true);
    DistributionListGranteeInfo grantee = new DistributionListGranteeInfo(GranteeType.usr, "ownerId", "ownerName");
    dl.addOwner(grantee);
    GetDistributionListResponse jaxb = new GetDistributionListResponse(dl);
    Element xmlJaxbElem = JaxbUtil.jaxbToElement(jaxb, XMLElement.mFactory);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    GetDistributionListResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetDistributionListResponse.class);
    GetDistributionListResponse roundtrippedX = JaxbUtil.elementToJaxb(xmlJaxbElem, GetDistributionListResponse.class);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlJaxbElem.prettyPrint());
    List<? extends KeyValuePair> kvps = roundtripped.getDl().getAttrList();
    Assert.assertEquals("roundtripped kvps num", 2, kvps.size());
    Assert.assertEquals("roundtripped owner num", 1, roundtripped.getDl().getOwners().size());
    Assert.assertEquals("roundtrippedX owner num", 1, roundtrippedX.getDl().getOwners().size());
}
Also used : GetDistributionListResponse(com.zimbra.soap.account.message.GetDistributionListResponse) DistributionListInfo(com.zimbra.soap.account.type.DistributionListInfo) KeyValuePair(com.zimbra.soap.type.KeyValuePair) DistributionListGranteeInfo(com.zimbra.soap.account.type.DistributionListGranteeInfo) 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 87 with Element

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

the class JaxbToJsonTest method inviteTestMethods.

/**
     * Tests a list of strings.  Was using a JSON Serializer called ContentListSerializer but the ObjectMapper we
     * use for Zimbra JSON now handles lists of strings this way by default.  GetFilterRules uses JAXB rather than
     * Element already.
     * Similar situation using Element based code for GetDistributionListMembers response used multiple calls to:
     *     parent.addElement(AccountConstants.E_DLM).setText(member);
     * Desired JSON :
      {
        "condition": "anyof",
        "inviteTest": [{
            "index": 0,
            "method": [
              {
                "_content": "REQUEST"
              },
              {
                "_content": "REPLY"
              },
              {
                "_content": "CANCEL"
              }]
          }],
        "_jsns": "urn:zimbraMail"
      }
    */
@Test
public void inviteTestMethods() throws Exception {
    FilterTests tests = FilterTests.createForCondition("anyof");
    FilterTest.InviteTest inviteTest = new FilterTest.InviteTest();
    inviteTest.addMethod("REQUEST");
    inviteTest.addMethod("REPLY");
    inviteTest.addMethod("CANCEL");
    tests.addTest(inviteTest);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tests, QName.get(MailConstants.E_FILTER_TESTS, MailConstants.NAMESPACE));
    logDebug("filterTests JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    FilterTests roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, FilterTests.class);
    FilterTest.InviteTest rtInviteTest = (FilterTest.InviteTest) roundtripped.getTests().get(0);
    Assert.assertEquals("roundtripped num methods", 3, rtInviteTest.getMethods().size());
}
Also used : FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) 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 88 with Element

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

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

the class JaxbToJsonTest method populateCreateDlResp.

private void populateCreateDlResp(Element elem) {
    Element eDL = elem.addElement(AdminConstants.E_DL);
    eDL.addAttribute(AdminConstants.A_NAME, "my name");
    eDL.addAttribute(AdminConstants.A_ID, "myId");
    eDL.addAttribute(AdminConstants.A_DYNAMIC, true);
    Map<String, Object> unicodeAttrs = Maps.newHashMap();
    unicodeAttrs.put("key1", "value1");
    String[] strs = { "Hello", "There" };
    unicodeAttrs.put("key2", strs);
    encodeAttrs(eDL, unicodeAttrs, AdminConstants.A_N);
}
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)

Example 90 with Element

use of com.zimbra.common.soap.Element 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.addElement(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

Element (com.zimbra.common.soap.Element)980 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)315 XMLElement (com.zimbra.common.soap.Element.XMLElement)269 Account (com.zimbra.cs.account.Account)220 Test (org.junit.Test)175 JSONElement (com.zimbra.common.soap.Element.JSONElement)167 Provisioning (com.zimbra.cs.account.Provisioning)147 Mailbox (com.zimbra.cs.mailbox.Mailbox)138 ServiceException (com.zimbra.common.service.ServiceException)103 ItemId (com.zimbra.cs.service.util.ItemId)81 ArrayList (java.util.ArrayList)81 OperationContext (com.zimbra.cs.mailbox.OperationContext)80 HashMap (java.util.HashMap)77 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)56 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)54 Server (com.zimbra.cs.account.Server)49 FilterTest (com.zimbra.soap.mail.type.FilterTest)46 IOException (java.io.IOException)45 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)44 XmlElement (javax.xml.bind.annotation.XmlElement)44