use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method missingRequiredEnumAttrib.
/**
* Permissive handling - if required things are not present, users need to handle this
*/
@Test
public void missingRequiredEnumAttrib() throws Exception {
EnumAttribEnumElem tstr = new EnumAttribEnumElem();
// tstr.setFold1(ViewEnum.VIRTUAL_CONVERSATION);
tstr.setFold2(ViewEnum.UNKNOWN);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
EnumAttribEnumElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, EnumAttribEnumElem.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("fold2 value", ViewEnum.UNKNOWN.toString(), jsonJaxbElem.getElement("fold2").getText());
Assert.assertEquals("roundtripped fold1", null, roundtripped.getFold1());
Assert.assertEquals("roundtripped fold2", ViewEnum.UNKNOWN, roundtripped.getFold2());
}
use of com.zimbra.common.soap.Element.JSONElement 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));
}
use of com.zimbra.common.soap.Element.JSONElement 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());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method stringAttrintValue.
/**
* Desired JSON :
* {
* "attr1": "Attribute ONE",
* "_content": "3",
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void stringAttrintValue() throws Exception {
final String attrib1 = "Attribute ONE";
final int value = 3;
final String valueStr = new Integer(value).toString();
Element jsonElem = JSONElement.mFactory.createElement(QName.get("string-attr-int-value", "urn:zimbraTest"));
jsonElem.addAttribute("attr1", attrib1);
jsonElem.addText(valueStr);
StringAttribIntValue tstr = new StringAttribIntValue(attrib1, value);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
StringAttribIntValue roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, StringAttribIntValue.class);
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("attr1", attrib1, jsonJaxbElem.getAttribute("attr1"));
Assert.assertEquals("XmlValue", valueStr, jsonJaxbElem.getText());
Assert.assertEquals("roundtripped attrib1", attrib1, roundtripped.getAttrib1());
Assert.assertEquals("roundtripped value", value, roundtripped.getMyValue());
}
use of com.zimbra.common.soap.Element.JSONElement 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.addNonUniqueElement("enum-entry").addText(ViewEnum.APPOINTMENT.toString());
jsonElem.addNonUniqueElement("enum-entry").addText(ViewEnum.UNKNOWN.toString());
jsonElem.addNonUniqueElement("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));
}
Aggregations