Search in sources :

Example 91 with Element

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

the class JaxbToElementTest method JSONelementToJaxbTest.

@Test
public void JSONelementToJaxbTest() throws Exception {
    Element env = Element.parseJSON(getInfoResponseJSONwithEnv);
    Element el = env.listElements().get(0);
    org.w3c.dom.Document doc = el.toW3cDom();
    ZimbraLog.test.debug("JSONelementToJaxbTest toW3cDom Xml:\n%s", W3cDomUtil.asXML(doc));
    GetInfoResponse getInfoResp = JaxbUtil.elementToJaxb(el);
    Assert.assertEquals("Account name", "user1@tarka.local", getInfoResp.getAccountName());
}
Also used : GetInfoResponse(com.zimbra.soap.account.message.GetInfoResponse) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 92 with Element

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

the class JaxbToJsonTest method booleanMarshal.

/**
     * By default JAXB renders true and false in XML as "true" and "false"
     * For Zimbra SOAP the XML flavor uses "1" and "0"
     * The JSON flavor uses "true" and "false"
     * @throws Exception
     */
@Test
public void booleanMarshal() throws Exception {
    Element legacy0Elem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacy0Elem.addAttribute(MailConstants.A_WAIT_DISALLOWED, false);
    logDebug("NoOpResponse JSONElement ---> prettyPrint\n%1$s", legacy0Elem.prettyPrint());
    Element legacy1Elem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacy1Elem.addAttribute(MailConstants.A_WAIT_DISALLOWED, true);
    Element legacyFalseElem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacyFalseElem.addAttribute(MailConstants.A_WAIT_DISALLOWED, "false");
    Element legacyTrueElem = JSONElement.mFactory.createElement(MailConstants.NO_OP_RESPONSE);
    legacyTrueElem.addAttribute(MailConstants.A_WAIT_DISALLOWED, "true");
    // NoOpResponse has:
    //     @XmlAttribute(name=MailConstants.A_WAIT_DISALLOWED, required=false)
    //     private ZmBoolean waitDisallowed;
    NoOpResponse jaxb = NoOpResponse.create(false);
    Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
    logDebug("XMLElement from JAXB (false)---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    Assert.assertEquals("false Value of 'waitDisallowed'", "0", xmlElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    Element jsonElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.NO_OP_RESPONSE);
    logDebug("NoOpResponse JSONElement from JAXB (false)---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    Assert.assertEquals("false Value of 'waitDisallowed'", "false", jsonElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    // ensure that marshaling where Boolean has value true works
    jaxb.setWaitDisallowed(true);
    xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
    logDebug("XMLElement from JAXB (true) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    Assert.assertEquals("true Value of 'waitDisallowed'", "1", xmlElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    jsonElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.NO_OP_RESPONSE);
    logDebug("NoOpResponse JSONElement from JAXB (true) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    Assert.assertEquals("true Value of 'waitDisallowed'", "true", jsonElem.getAttribute(MailConstants.A_WAIT_DISALLOWED));
    // ensure that unmarshaling where XML Boolean representation is "1" for true works
    jaxb = JaxbUtil.elementToJaxb(legacy1Elem);
    Assert.assertEquals("legacy1Elem waitDisallowed value", Boolean.TRUE, jaxb.getWaitDisallowed());
    // ensure that unmarshaling where XML Boolean representation is "0" for false works
    jaxb = JaxbUtil.elementToJaxb(legacy0Elem);
    Assert.assertEquals("legacy0Elem waitDisallowed value", Boolean.FALSE, jaxb.getWaitDisallowed());
    // ensure that unmarshaling where XML Boolean representation is "false" works
    jaxb = JaxbUtil.elementToJaxb(legacyFalseElem);
    Assert.assertEquals("legacyFalseElem waitDisallowed value", Boolean.FALSE, jaxb.getWaitDisallowed());
    // ensure that unmarshaling where XML Boolean representation is "true" works
    jaxb = JaxbUtil.elementToJaxb(legacyTrueElem);
    Assert.assertEquals("legacyTrueElem waitDisallowed value", Boolean.TRUE, jaxb.getWaitDisallowed());
}
Also used : NoOpResponse(com.zimbra.soap.mail.message.NoOpResponse) 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 93 with Element

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

the class JaxbToJsonTest method enumElemList.

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

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

the class JaxbToJsonTest method ZmBooleanAntStringXmlElements.

/**
     *  {
     *    "status": [{
     *        "_content": "true"   # Actually get true not "true" but should be ok
     *      }],
     *    "message": [{
     *        "_content": "ver ndx message"
     *      }],
     *    "_jsns": "urn:zimbraAdmin"
     *  }
     */
@Test
public void ZmBooleanAntStringXmlElements() throws Exception {
    final String msg = "ver ndx message";
    // ---------------------------------  For Comparison - Element handling
    Element legacyElem = JSONElement.mFactory.createElement(AdminConstants.VERIFY_INDEX_RESPONSE);
    legacyElem.addElement(AdminConstants.E_STATUS).addText(String.valueOf(true));
    legacyElem.addElement(AdminConstants.E_MESSAGE).addText(msg);
    logDebug("VerifyIndexResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
    VerifyIndexResponse viResp = new VerifyIndexResponse(true, msg);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(viResp);
    logDebug("VerifyIndexResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Assert.assertEquals("status", true, jsonJaxbElem.getAttributeBool(AdminConstants.E_STATUS));
    Assert.assertEquals("message", msg, jsonJaxbElem.getAttribute(AdminConstants.E_MESSAGE));
    VerifyIndexResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, VerifyIndexResponse.class);
    Assert.assertEquals("roundtripped status", true, roundtripped.isStatus());
    Assert.assertEquals("roundtripped message", msg, roundtripped.getMessage());
}
Also used : VerifyIndexResponse(com.zimbra.soap.admin.message.VerifyIndexResponse) 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 95 with Element

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

the class JaxbToJsonTest method missingRequiredStringElem.

/** Permissive handling - if required things are not present, users need to handle this */
@Test
public void missingRequiredStringElem() throws Exception {
    final String attr1Val = "My attribute ONE";
    StringAttrStringElem tstr = new StringAttrStringElem();
    tstr.setAttr1(attr1Val);
    // tstr.setElem1(elem1Val);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    StringAttrStringElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, StringAttrStringElem.class);
    Assert.assertEquals("JSONElement attr1", attr1Val, jsonJaxbElem.getAttribute("attribute-1"));
    try {
        jsonJaxbElem.getElement("element1");
    } catch (ServiceException svcE) {
        Assert.assertEquals("JSONElement exception when getting missing item:", ServiceException.INVALID_REQUEST, svcE.getCode());
    }
    Assert.assertEquals("roundtripped attr1", attr1Val, roundtripped.getAttr1());
    Assert.assertEquals("roundtripped elem1", null, roundtripped.getElem1());
}
Also used : StringAttrStringElem(com.zimbra.soap.jaxb.StringAttrStringElem) ServiceException(com.zimbra.common.service.ServiceException) 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