use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method emptyEnumElemList.
/**
* Desired JSON :
* {
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void emptyEnumElemList() throws Exception {
EnumElemList tstr = new EnumElemList();
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
logDebug("JSONElement from JAXB FOR EMPTY LIST ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
EnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, EnumElemList.class);
List<Element> jsonElems = jsonJaxbElem.listElements();
List<ViewEnum> entries = roundtripped.getEntries();
Assert.assertEquals("jsonElems num", 0, jsonElems.size());
Assert.assertEquals("entries num", 0, entries.size());
}
use of com.zimbra.common.soap.Element.JSONElement 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.addNonUniqueElement(QName.get("strAttrStrElem", "urn:ZimbraTest5"));
saseElem.addAttribute("attribute-1", attr1Val);
saseElem.addNonUniqueElement(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());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method anyElementHandling.
/**
* {@link XmlAnyElement} handling
* <pre>
* @XmlAnyElement
* private List<org.w3c.dom.Element> elems = Lists.newArrayList();
* </pre>
*/
@Test
public void anyElementHandling() throws Exception {
String given = "Given information";
List<Object> elems = Lists.newArrayList();
AnyTester jaxb = new AnyTester();
DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = builder.newDocument();
org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
elem.setAttribute("myAttr", "myValue");
org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
child.setTextContent("Purple beans");
elem.appendChild(child);
org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
child2.setAttribute("name", "Kate");
child2.setAttribute("age", "23");
elem.appendChild(child2);
elems.add(elem);
org.w3c.dom.Element elem2 = doc.createElementNS("urn:wooky", "may");
elem2.setAttribute("fourth", "be with you");
jaxb.setGiven(given);
jaxb.setElems(Lists.newArrayList(elem, elem2));
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
AnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyTester.class);
Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
org.w3c.dom.Element w3ce = roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX elem name", "alien", w3ce.getLocalName());
logDebug("STRING from JAXB ---> prettyPrint\n%1$s", getZimbraJsonJaxbString(jaxb));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
AnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyTester.class);
Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
org.w3c.dom.Element rtElem = roundtripped.getElems().get(0);
Assert.assertEquals("roundtripped elem name", "alien", rtElem.getTagName());
Assert.assertEquals("roundtripped elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method multipleDispositionCONTENTAttributes.
/**
* <p>Demonstrates that CANNOT have more than one attribute with same name, even if using
* {@link Element.Disposition.CONTENT} to force treating the attribute as an element in XML.</p>
* Desired serialization to XML:
* <pre>
* <multi-content-attrs xmlns="urn:zimbraTest">
* <soapURL>https://soap.example.test</soapURL>
* </multi-content-attrs>
* </pre>
* Desired serialization to JSON:
* <pre>
* {
* "soapURL": "https://soap.example.test",
* "_jsns": "urn:zimbraTest"
* }
* </pre>
*/
@Test
public void multipleDispositionCONTENTAttributes() throws Exception {
final String httpSoap = "http://soap.example.test";
final String httpsSoap = "https://soap.example.test";
Element jsonElem = JSONElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
Element xmlElem = XMLElement.mFactory.createElement(QName.get("multi-content-attrs", "urn:zimbraTest"));
jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
jsonElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpSoap, Element.Disposition.CONTENT);
xmlElem.addAttribute(AccountConstants.E_SOAP_URL, httpsSoap, Element.Disposition.CONTENT);
logDebug("MultiContentAttrs XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("MultiContentAttrs JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
Assert.assertEquals("XMLElement soapURL as attribute", httpsSoap, xmlElem.getAttribute(AccountConstants.E_SOAP_URL));
Assert.assertEquals("XMLElement num soapURL elements", 1, xmlElem.listElements(AccountConstants.E_SOAP_URL).size());
Assert.assertEquals("XMLElement soapURL as element", httpsSoap, xmlElem.getElement(AccountConstants.E_SOAP_URL).getText());
Assert.assertEquals("JSONElement soapURL as attribute", httpsSoap, jsonElem.getAttribute(AccountConstants.E_SOAP_URL));
// Note difference from XMLElement - for JSON this is Always an attribute but for XML it can be treated as an element
Assert.assertEquals("JSONElement num soapURL elements", 0, jsonElem.listElements(AccountConstants.E_SOAP_URL).size());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method exerciseZimbraJsonAttribute.
/**
* <p>Exercise annotation {@link ZimbraJsonAttribute} which is needed in JAXB in addition to {@link XmlElement} or
* {@link XmlElementRef} annotations to provide a field which as an analog of something like:
* <pre>
* jsonElem.addAttribute("xml-elem-json-attr", "XML elem but JSON attribute", Element.Disposition.CONTENT);
* </pre>
* Desired XML serialization:
* <pre>
* <XmlElemJsonAttr xmlns="urn:zimbraTest">
* <xml-elem-json-attr>XML elem but JSON attribute</xml-elem-json-attr>
* <classic-elem>elem for both XML and JSON</classic-elem>
* </XmlElemJsonAttr>
* </pre>
* Desired JSON serialization:
* <pre>
* {
* "xml-elem-json-attr": "XML elem but JSON attribute",
* "classic-elem": [{
* "_content": "elem for both XML and JSON"
* }],
* "_jsns": "urn:zimbraTest"
* }
* </pre>
*/
@Test
public void exerciseZimbraJsonAttribute() throws Exception {
final String str1 = "XML elem but JSON attribute";
final String str2 = "elem for both XML and JSON";
Element jsonElem = JSONElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
Element xmlElem = XMLElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
jsonElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
jsonElem.addNonUniqueElement("classic-elem").setText(str2);
xmlElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
xmlElem.addNonUniqueElement("classic-elem").setText(str2);
logDebug("XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
XmlElemJsonAttr jaxb = new XmlElemJsonAttr(str1, str2);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("JSONElement and JSONElement from JAXB", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
XmlElemJsonAttr roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, XmlElemJsonAttr.class);
Assert.assertEquals("roundtripped xml-elem-json-attr", str1, roundtripped.getXmlElemJsonAttr());
Assert.assertEquals("roundtripped classic-elem", str2, roundtripped.getDefaultElem());
Assert.assertEquals("JSONElement xml-elem-json-attr as attribute", str1, jsonElem.getAttribute("xml-elem-json-attr"));
// Note difference from XMLElement - for JSON this is Always an attribute but for XML it can be treated as an element
Assert.assertEquals("JSONElement num xml-elem-json-attr elements", 0, jsonElem.listElements("xml-elem-json-attr").size());
}
Aggregations