use of com.zimbra.common.soap.Element.XMLElement 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.XMLElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method zimbraOddKeyValuePairsAnnotation.
/**
* Check that JSON can be deserialised into a JAXB object when some of the JSON represents Zimbra KeyValuePairs
* (i.e. An "_attrs" array).
* In this case, the target objects for each keyvalue pair do NOT use the defaults of "a" for the element name and
* "n" for the attribute name in the XML form.
* Desired XML :<br />
* <key-value-pairs-tester xmlns="urn:zimbraTest">
* <oddElemName name="key1">value1</oddElemName>
* <oddElemName name="key2">value2-a</oddElemName>
* <oddElemName name="key2">value2-b</oddElemName>
* </key-value-pairs-tester>
*<br />
* Desired JSON :<br />
* {
* "_attrs": {
* "key1": "value1",
* "key2": [
* "value2-a",
* "value2-b"]
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void zimbraOddKeyValuePairsAnnotation() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
jsonElem.addKeyValuePair("key1", "value1", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-a", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-b", "oddElemName", "name");
List<Attr> attrs = Lists.newArrayList();
attrs.add(new Attr("key1", "value1"));
attrs.add(new Attr("key2", "value2-a"));
attrs.add(new Attr("key2", "value2-b"));
OddKeyValuePairsTester jaxb = new OddKeyValuePairsTester(attrs);
logDebug("XMLElement (from JAXB) ---> prettyPrint\n%1$s", JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false).prettyPrint());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
String origJson = jsonJaxbElem.prettyPrint();
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", origJson);
OddKeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, OddKeyValuePairsTester.class);
List<com.zimbra.common.soap.Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
List<Attr> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
jsonJaxbElem = JacksonUtil.jaxbToJSONElement(roundtripped);
String finalJson = jsonJaxbElem.prettyPrint();
if (!origJson.equals(finalJson)) {
logDebug("JSONElement from roundtripped JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("roundtripped JSON pretty print", origJson, finalJson);
}
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method contentList.
/**
* Test for "List of strings" field annotated with {@link XmlElement}.
* Desired JSON:
* {
* "more": false,
* "total": 23,
* "dlm": [
* {
* "_content": "dlmember1@no.where"
* },
* {
* "_content": "dlmember2@no.where"
* },
* {
* "_content": "dlmember3@no.where"
* }],
* "_jsns": "urn:zimbraAccount"
* }
*/
@Test
public void contentList() throws Exception {
Element legacyElem = JSONElement.mFactory.createElement(AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
legacyElem.addAttribute(AccountConstants.A_MORE, false);
legacyElem.addAttribute(AccountConstants.A_TOTAL, 23);
legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember1@no.where");
legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember2@no.where");
legacyElem.addElement(AccountConstants.E_DLM).setText("dlmember3@no.where");
logDebug("GetDistributionListMembersResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
// GetDistributionListMembersResponse has:
// @XmlElement(name=AccountConstants.E_DLM, required=false)
// private List<String> dlMembers = Lists.newArrayList();
GetDistributionListMembersResponse jaxb = new GetDistributionListMembersResponse();
jaxb.setMore(false);
jaxb.setTotal(23);
jaxb.addDlMember("dlmember1@no.where");
jaxb.addDlMember("dlmember2@no.where");
jaxb.addDlMember("dlmember3@no.where");
Element elem = JacksonUtil.jaxbToJSONElement(jaxb, AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
logDebug("GetDistributionListMembersResponse JSONElement from JAXB ---> prettyPrint\n%1$s", elem.prettyPrint());
List<Element> dlMembers = elem.listElements(AccountConstants.E_DLM);
Assert.assertEquals("Number of dlMembers", 3, dlMembers.size());
Element dlMem3 = dlMembers.get(2);
Assert.assertEquals("dlMember 3", "dlmember3@no.where", dlMem3.getText());
Assert.assertEquals("total", 23, elem.getAttributeInt(AccountConstants.A_TOTAL));
Assert.assertEquals("more", false, elem.getAttributeBool(AccountConstants.A_MORE));
Assert.assertEquals("prettyPrint", legacyElem.prettyPrint(), elem.prettyPrint());
}
use of com.zimbra.common.soap.Element.XMLElement 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.addElement("classic-elem").setText(str2);
xmlElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
xmlElem.addElement("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());
}
use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method elementRefsHandling.
/**
* XmlElementRefs handling
* Desired JSON :
* {
* "string-attr-int-value": [{
* "attr1": "my string",
* "_content": 321
* }],
* "enumEttribs": [{
* "fold1": "chat",
* "fold2": "remote folder"
* }],
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void elementRefsHandling() throws Exception {
String str = "my string";
int num = 321;
List<Object> elems = Lists.newArrayList();
ElementRefsTester jaxb = new ElementRefsTester();
StringAttribIntValue inner = new StringAttribIntValue(str, num);
elems.add(inner);
EnumAttribs ea = new EnumAttribs();
ea.setFold1(ViewEnum.CHAT);
ea.setFold2(ViewEnum.REMOTE_FOLDER);
elems.add(ea);
jaxb.setElems(elems);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
ElementRefsTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, ElementRefsTester.class);
ElementRefsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, ElementRefsTester.class);
Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
StringAttribIntValue rtByRef = (StringAttribIntValue) roundtripped.getElems().get(0);
StringAttribIntValue rtXmlByRef = (StringAttribIntValue) roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX str", str, rtXmlByRef.getAttrib1());
Assert.assertEquals("roundtrippedX num", num, rtXmlByRef.getMyValue());
Assert.assertEquals("roundtripped str", str, rtByRef.getAttrib1());
Assert.assertEquals("roundtripped num", num, rtByRef.getMyValue());
EnumAttribs rtea = (EnumAttribs) roundtripped.getElems().get(1);
Assert.assertEquals("roundtripped fold1", ViewEnum.CHAT, rtea.getFold1());
Assert.assertEquals("roundtripped fold2", ViewEnum.REMOTE_FOLDER, rtea.getFold2());
}
Aggregations