Search in sources :

Example 1 with CalendaringDataInterface

use of com.zimbra.soap.mail.type.CalendaringDataInterface in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method bug61264AttributeDispositionCONTENThandling.

/**
 * the element referenced MailConstants.E_FRAG is treated in XML as an element with content but no attributes.
 * However in JSON, it is just treated like an ordinary attribute.
 * So, in JSON we DO want:
 *    "fr": "Here is some wonderful text and some more",
 * We do NOT want it to be treated as if it was an element which would look like:
 *    "fr": [{
 *           "_content": "Here is some wonderful text and some more"
 *         }],
 */
@Test
public void bug61264AttributeDispositionCONTENThandling() throws Exception {
    StringBuilder sb;
    final String uid = "uidString";
    final String frag = "Fragment text";
    final String name = "name Attribute";
    // From stacktrace of where the SOAP response gets assembled:
    // at com.zimbra.common.soap.Element.output(Element.java:432)
    // at com.zimbra.soap.SoapServlet.sendResponse(SoapServlet.java:349)
    // at com.zimbra.soap.SoapServlet.doWork(SoapServlet.java:307)
    // at com.zimbra.soap.SoapServlet.doPost(SoapServlet.java:206)
    // Bug 61264 is about issues with code that used JAXB which replaced code in GetCalendarItemSummaries
    // which started with an element created using:
    // calItemElem = lc.createElement(isAppointment ? MailConstants.E_APPOINTMENT : MailConstants.E_TASK);
    // At present, that code has been reverted to be element based.
    // For comparison purposes, create an JSONElement tree and a XMLElement tree
    Element jsoncalItemElem = JSONElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
    jsoncalItemElem.addAttribute(MailConstants.A_UID, uid);
    jsoncalItemElem.addAttribute("x_uid", uid);
    Element instElt = jsoncalItemElem.addNonUniqueElement(MailConstants.E_INSTANCE);
    instElt.addAttribute(MailConstants.E_FRAG, frag, Element.Disposition.CONTENT);
    instElt.addAttribute(MailConstants.A_CAL_IS_EXCEPTION, true);
    instElt.addAttribute(MailConstants.A_NAME, name);
    Element xmlcalItemElem = XMLElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
    xmlcalItemElem.addAttribute(MailConstants.A_UID, uid);
    xmlcalItemElem.addAttribute("x_uid", uid);
    Element xmlinstElt = xmlcalItemElem.addNonUniqueElement(MailConstants.E_INSTANCE);
    xmlinstElt.addAttribute(MailConstants.E_FRAG, frag, Element.Disposition.CONTENT);
    xmlinstElt.addAttribute(MailConstants.A_CAL_IS_EXCEPTION, true);
    xmlinstElt.addAttribute(MailConstants.A_NAME, name);
    CalendaringDataInterface calData = null;
    calData = new AppointmentData(uid, uid);
    InstanceDataInfo instance = new InstanceDataInfo();
    calData.addCalendaringInstance(instance);
    instance.setIsException(true);
    instance.setName(name);
    instance.setFragment(frag);
    Element jsonJaxbElem = JaxbUtil.jaxbToNamedElement(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE_STR, calData, JSONElement.mFactory);
    Element xmlJaxbElem = JaxbUtil.jaxbToNamedElement(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE_STR, calData, XMLElement.mFactory);
    // As AppointmentData doesn't have an XmlRootElement, this gives a poor choice for the root name.
    // Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData);
    // This is probably a closer analog to JSONElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
    // Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData, new QName("appt", null));
    Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData, new QName(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE));
    Element parent4legacyJson = JSONElement.mFactory.createElement(new QName("legacy-json", MailConstants.NAMESPACE));
    Element parent4jackson = JSONElement.mFactory.createElement(new QName("jacksonjson", MailConstants.NAMESPACE));
    parent4legacyJson.addNonUniqueElement(jsoncalItemElem);
    parent4jackson.addNonUniqueElement(jacksonJaxbElem);
    sb = new StringBuilder();
    xmlcalItemElem.output(sb);
    logDebug("bug61264 - XML from XMLElement\n%1$s", sb.toString());
    sb = new StringBuilder();
    xmlJaxbElem.output(sb);
    logDebug("bug61264 - XML from JAXB\n%1$s", sb.toString());
    // something that is appendable for Element.out(Appendable) to play with
    sb = new StringBuilder();
    jsoncalItemElem.output(sb);
    String jsonFromElement = sb.toString();
    logDebug("bug61264 - JSON from JSONElement\n%1$s", jsonFromElement);
    sb = new StringBuilder();
    jsonJaxbElem.output(sb);
    logDebug("bug61264 - JSON from JAXB\n%1$s", sb.toString());
    sb = new StringBuilder();
    jacksonJaxbElem.output(sb);
    logDebug("bug61264 - JSON from JAXB using Jackson\n%1$s", sb.toString());
    sb = new StringBuilder();
    parent4legacyJson.output(sb);
    logDebug("bug61264 - JSON from JAXB child using Jackson\n%1$s", sb.toString());
    sb = new StringBuilder();
    parent4jackson.output(sb);
    logDebug("bug61264 - JSON from JSONElement child\n%1$s", sb.toString());
    Assert.assertEquals("UID", uid, jacksonJaxbElem.getAttribute(MailConstants.A_UID));
    Assert.assertEquals("x_uid", uid, jacksonJaxbElem.getAttribute("x_uid"));
    Element instE = jacksonJaxbElem.getElement(MailConstants.E_INSTANCE);
    Assert.assertNotNull("instance elem", instE);
    Assert.assertEquals("fragment", frag, instE.getAttribute(MailConstants.E_FRAG));
    Assert.assertTrue("is exception", instE.getAttributeBool(MailConstants.A_CAL_IS_EXCEPTION));
    Assert.assertEquals("name", name, instE.getAttribute(MailConstants.A_NAME));
}
Also used : QName(org.dom4j.QName) 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) CalendaringDataInterface(com.zimbra.soap.mail.type.CalendaringDataInterface) AppointmentData(com.zimbra.soap.mail.type.AppointmentData) InstanceDataInfo(com.zimbra.soap.mail.type.InstanceDataInfo) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 2 with CalendaringDataInterface

use of com.zimbra.soap.mail.type.CalendaringDataInterface in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method bug61264_AttributeDispositionCONTENThandling.

/**
     * the element referenced MailConstants.E_FRAG is treated in XML as an element with content but no attributes.
     * However in JSON, it is just treated like an ordinary attribute.
     * So, in JSON we DO want:
     *    "fr": "Here is some wonderful text and some more",
     * We do NOT want it to be treated as if it was an element which would look like:
     *    "fr": [{
     *           "_content": "Here is some wonderful text and some more"
     *         }],
     */
@Test
public void bug61264_AttributeDispositionCONTENThandling() throws Exception {
    StringBuilder sb;
    final String uid = "uidString";
    final String frag = "Fragment text";
    final String name = "name Attribute";
    // From stacktrace of where the SOAP response gets assembled:
    //     at com.zimbra.common.soap.Element.output(Element.java:432)
    //     at com.zimbra.soap.SoapServlet.sendResponse(SoapServlet.java:349)
    //     at com.zimbra.soap.SoapServlet.doWork(SoapServlet.java:307)
    //     at com.zimbra.soap.SoapServlet.doPost(SoapServlet.java:206)
    // Bug 61264 is about issues with code that used JAXB which replaced code in GetCalendarItemSummaries
    // which started with an element created using:
    //     calItemElem = lc.createElement(isAppointment ? MailConstants.E_APPOINTMENT : MailConstants.E_TASK);
    // At present, that code has been reverted to be element based.
    // For comparison purposes, create an JSONElement tree and a XMLElement tree
    Element jsoncalItemElem = JSONElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
    jsoncalItemElem.addAttribute(MailConstants.A_UID, uid);
    jsoncalItemElem.addAttribute("x_uid", uid);
    Element instElt = jsoncalItemElem.addElement(MailConstants.E_INSTANCE);
    instElt.addAttribute(MailConstants.E_FRAG, frag, Element.Disposition.CONTENT);
    instElt.addAttribute(MailConstants.A_CAL_IS_EXCEPTION, true);
    instElt.addAttribute(MailConstants.A_NAME, name);
    Element xmlcalItemElem = XMLElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
    xmlcalItemElem.addAttribute(MailConstants.A_UID, uid);
    xmlcalItemElem.addAttribute("x_uid", uid);
    Element xmlinstElt = xmlcalItemElem.addElement(MailConstants.E_INSTANCE);
    xmlinstElt.addAttribute(MailConstants.E_FRAG, frag, Element.Disposition.CONTENT);
    xmlinstElt.addAttribute(MailConstants.A_CAL_IS_EXCEPTION, true);
    xmlinstElt.addAttribute(MailConstants.A_NAME, name);
    CalendaringDataInterface calData = null;
    calData = new AppointmentData(uid, uid);
    InstanceDataInfo instance = new InstanceDataInfo();
    calData.addCalendaringInstance(instance);
    instance.setIsException(true);
    instance.setName(name);
    instance.setFragment(frag);
    Element jsonJaxbElem = JaxbUtil.jaxbToNamedElement(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE_STR, calData, JSONElement.mFactory);
    Element xmlJaxbElem = JaxbUtil.jaxbToNamedElement(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE_STR, calData, XMLElement.mFactory);
    // As AppointmentData doesn't have an XmlRootElement, this gives a poor choice for the root name.
    //     Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData);
    // This is probably a closer analog to JSONElement.mFactory.createElement(MailConstants.E_APPOINTMENT);
    //     Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData, new QName("appt", null));
    Element jacksonJaxbElem = JacksonUtil.jaxbToJSONElement(calData, new QName(MailConstants.E_APPOINTMENT, MailConstants.NAMESPACE));
    Element parent4legacyJson = JSONElement.mFactory.createElement(new QName("legacy-json", MailConstants.NAMESPACE));
    Element parent4jackson = JSONElement.mFactory.createElement(new QName("jacksonjson", MailConstants.NAMESPACE));
    parent4legacyJson.addElement(jsoncalItemElem);
    parent4jackson.addElement(jacksonJaxbElem);
    sb = new StringBuilder();
    xmlcalItemElem.output(sb);
    logDebug("bug61264 - XML from XMLElement\n%1$s", sb.toString());
    sb = new StringBuilder();
    xmlJaxbElem.output(sb);
    logDebug("bug61264 - XML from JAXB\n%1$s", sb.toString());
    // something that is appendable for Element.out(Appendable) to play with
    sb = new StringBuilder();
    jsoncalItemElem.output(sb);
    String jsonFromElement = sb.toString();
    logDebug("bug61264 - JSON from JSONElement\n%1$s", jsonFromElement);
    sb = new StringBuilder();
    jsonJaxbElem.output(sb);
    logDebug("bug61264 - JSON from JAXB\n%1$s", sb.toString());
    sb = new StringBuilder();
    jacksonJaxbElem.output(sb);
    logDebug("bug61264 - JSON from JAXB using Jackson\n%1$s", sb.toString());
    sb = new StringBuilder();
    parent4legacyJson.output(sb);
    logDebug("bug61264 - JSON from JAXB child using Jackson\n%1$s", sb.toString());
    sb = new StringBuilder();
    parent4jackson.output(sb);
    logDebug("bug61264 - JSON from JSONElement child\n%1$s", sb.toString());
    Assert.assertEquals("UID", uid, jacksonJaxbElem.getAttribute(MailConstants.A_UID));
    Assert.assertEquals("x_uid", uid, jacksonJaxbElem.getAttribute("x_uid"));
    Element instE = jacksonJaxbElem.getElement(MailConstants.E_INSTANCE);
    Assert.assertNotNull("instance elem", instE);
    Assert.assertEquals("fragment", frag, instE.getAttribute(MailConstants.E_FRAG));
    Assert.assertTrue("is exception", instE.getAttributeBool(MailConstants.A_CAL_IS_EXCEPTION));
    Assert.assertEquals("name", name, instE.getAttribute(MailConstants.A_NAME));
}
Also used : QName(org.dom4j.QName) 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) CalendaringDataInterface(com.zimbra.soap.mail.type.CalendaringDataInterface) AppointmentData(com.zimbra.soap.mail.type.AppointmentData) InstanceDataInfo(com.zimbra.soap.mail.type.InstanceDataInfo) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

Element (com.zimbra.common.soap.Element)2 JSONElement (com.zimbra.common.soap.Element.JSONElement)2 XMLElement (com.zimbra.common.soap.Element.XMLElement)2 AppointmentData (com.zimbra.soap.mail.type.AppointmentData)2 CalendaringDataInterface (com.zimbra.soap.mail.type.CalendaringDataInterface)2 FilterTest (com.zimbra.soap.mail.type.FilterTest)2 InstanceDataInfo (com.zimbra.soap.mail.type.InstanceDataInfo)2 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)2 XmlElement (javax.xml.bind.annotation.XmlElement)2 QName (org.dom4j.QName)2 Test (org.junit.Test)2