use of com.zimbra.soap.jaxb.StringAttrStringElem 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());
}
use of com.zimbra.soap.jaxb.StringAttrStringElem 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());
}
use of com.zimbra.soap.jaxb.StringAttrStringElem in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method elemsInDiffNamespace.
/**
* Desired JSON :
* {
* "strAttrStrElem": [{
* "attribute-1": "My attribute ONE",
* "element1": [{
* "_content": "My element ONE",
* "_jsns": "urn:ZimbraTest3"
* }],
* "_jsns": "urn:ZimbraTest5"
* }],
* "_jsns": "urn:ZimbraTest4"
* }
*/
@Test
public void elemsInDiffNamespace() throws Exception {
final String attr1Val = "My attribute ONE";
final String elem1Val = "My element ONE";
Element jsonElem = JSONElement.mFactory.createElement(QName.get("ns-delta", "urn:ZimbraTest4"));
Element saseElem = jsonElem.addElement(QName.get("strAttrStrElem", "urn:ZimbraTest5"));
saseElem.addAttribute("attribute-1", attr1Val);
saseElem.addElement(QName.get("element1", "urn:ZimbraTest3")).addText(elem1Val);
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
NamespaceDeltaElem tstr = new NamespaceDeltaElem();
StringAttrStringElem tstrSase = new StringAttrStringElem();
tstrSase.setAttr1(attr1Val);
tstrSase.setElem1(elem1Val);
tstr.setSase(tstrSase);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
NamespaceDeltaElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, NamespaceDeltaElem.class);
Element saseJsonJaxbElem = jsonJaxbElem.getElement(QName.get("strAttrStrElem", "urn:ZimbraTest5"));
Assert.assertEquals("JSONElement attr1", attr1Val, saseJsonJaxbElem.getAttribute("attribute-1"));
Assert.assertEquals("JSONElement elem1", elem1Val, saseJsonJaxbElem.getElement("element1").getText());
logDebug("roundtripped attr1=%1$s", roundtripped.getSase().getAttr1());
logDebug("roundtripped elem1=%1$s", roundtripped.getSase().getElem1());
Assert.assertEquals("roundtripped attr1", attr1Val, roundtripped.getSase().getAttr1());
Assert.assertEquals("roundtripped elem1", elem1Val, roundtripped.getSase().getElem1());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
}
Aggregations