use of javax.xml.bind.annotation.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.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());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method wrappedEnumElemList.
/**
* Testing form:
* {@code @XmlElementWrapper(name="wrapper", required=false)
* @XmlElement(name="enum-entry", required=false)
* private List<ViewEnum> entries = Lists.newArrayList();}
* Desired JSON :
* {
* "wrapper": [{
* "enum-entry": [
* {
* "_content": "appointment"
* },
* {
* "_content": "document"
* }]
* }],
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void wrappedEnumElemList() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("wrapped-enum-elem-list", "urn:zimbraTest"));
Element wrapElem = jsonElem.addNonUniqueElement("wrapper");
wrapElem.addNonUniqueElement("enum-entry").addText(ViewEnum.APPOINTMENT.toString());
wrapElem.addNonUniqueElement("enum-entry").addText(ViewEnum.DOCUMENT.toString());
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
WrappedEnumElemList tstr = new WrappedEnumElemList();
tstr.addEntry(ViewEnum.APPOINTMENT);
tstr.addEntry(ViewEnum.DOCUMENT);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
WrappedEnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrappedEnumElemList.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Element wElem = jsonJaxbElem.getElement("wrapper");
List<Element> jsonElems = wElem.listElements();
List<ViewEnum> entries = roundtripped.getEntries();
Assert.assertEquals("jsonElems num", 2, jsonElems.size());
Assert.assertEquals("entries num", 2, entries.size());
Assert.assertTrue("has APPOINTMENT", entries.contains(ViewEnum.APPOINTMENT));
Assert.assertTrue("has DOCUMENT", entries.contains(ViewEnum.DOCUMENT));
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method kvpForCreateDLRespBug74371.
/*
# zmsoap -z -t account -m user1 GetDistributionListRequest/dl=grendl@coco.local @by=name
<GetDistributionListResponse xmlns="urn:zimbraAccount">
<dl id="7a3e8ec5-4892-4b17-9225-cf17e8b3acc9" dynamic="1" name="grendl@coco.local" isOwner="1" isMember="1">
<a n="mail">grendl@coco.local</a>
<a n="zimbraMailStatus">enabled</a>
<a n="zimbraMailAlias">grendl@coco.local</a>
<a n="description">Wonder at that</a>
<a n="displayName">Gren DLfun</a>
<a n="zimbraDistributionListSubscriptionPolicy">ACCEPT</a>
<a n="zimbraDistributionListUnsubscriptionPolicy">ACCEPT</a>
</dl>
</GetDistributionListResponse>
# zmsoap --json -z -t account -m user1 GetDistributionListRequest/dl=grendl@coco.local @by=name
{
"dl": [{
"name": "grendl@coco.local",
"id": "7a3e8ec5-4892-4b17-9225-cf17e8b3acc9",
"dynamic": true,
"isMember": true,
"isOwner": true,
"_attrs": {
"mail": "grendl@coco.local",
"zimbraMailStatus": "enabled",
"zimbraMailAlias": "grendl@coco.local",
"description": "Wonder at that",
"displayName": "Gren DLfun",
"zimbraDistributionListSubscriptionPolicy": "ACCEPT",
"zimbraDistributionListUnsubscriptionPolicy": "ACCEPT"
}
}],
"_jsns": "urn:zimbraAccount"
}
Extract from mailbox.log for creation of this DL by ZWC - demonstrating the different handling of attrs - See Bug 74371
"CreateDistributionListResponse": [{
"dl": [{
"name": "grendl@coco.local",
"id": "7a3e8ec5-4892-4b17-9225-cf17e8b3acc9",
"dynamic": true,
"a": [
{
"n": "memberURL",
"_content": "ldap:///??sub?(|(zimbraMemberOf=7a3e8ec5-4892-4b17-9225-cf17e8b3acc9)(zimbraId=de47828e-94dd-45c3-9770-4dbd255564ca))"
},
{
"n": "mail",
"_content": "grendl@coco.local"
},
...
*/
/**
* Desired JSON
*/
// Re-enable when Bug 74371 is fixed? @Test
public void kvpForCreateDLRespBug74371() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get(AccountConstants.E_CREATE_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateCreateDlResp(jsonElem);
Element xmlElem = XMLElement.mFactory.createElement(QName.get(AccountConstants.E_CREATE_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateCreateDlResp(xmlElem);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
List<KeyValuePair> attrs = Lists.newArrayList();
attrs.add(new KeyValuePair("key1", "value1"));
attrs.add(new KeyValuePair("key2", "value2"));
DLInfo dl = new DLInfo("myId", "myRef", "my name", null, null, null, null, null);
CreateDistributionListResponse jaxb = new CreateDistributionListResponse(dl);
Element xmlJaxbElem = JaxbUtil.jaxbToElement(jaxb, XMLElement.mFactory);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
DLInfo roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, DLInfo.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlJaxbElem.prettyPrint());
Element eDL = jsonJaxbElem.getElement(AdminConstants.E_DL);
List<? extends KeyValuePair> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 2, kvps.size());
List<Element.KeyValuePair> elemKVPs = eDL.getElement("a").listKeyValuePairs();
Assert.assertEquals("elemKVP num", 2, elemKVPs.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method mixedAndAnyElementHandlingJustElement.
/**
* {@link XmlAnyElement} and {@link XmlMixed} handling
* In the places we use XmlMixed, we typically have either just text or just elements - this tests with elements
* that do NOT map to JAXB classes.
* Desired JSON:
* {
* "alien": {
* "myAttr": "myValue",
* "child": {
* "_content": "Purple beans"
* },
* "daughter": {
* "age": "23",
* "name": "Kate"
* },
* "_jsns": "urn:foreign"
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void mixedAndAnyElementHandlingJustElement() throws Exception {
List<Object> elems = Lists.newArrayList();
MixedAnyTester jaxb = new MixedAnyTester();
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);
jaxb.setElems(elems);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) [Mixed w3c element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
MixedAnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedAnyTester.class);
Assert.assertEquals("roundtrippedX [Mixed w3c element] num elems", 1, roundtrippedX.getElems().size());
org.w3c.dom.Element w3ce = (org.w3c.dom.Element) roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX [Mixed w3c element] elem name", "alien", w3ce.getLocalName());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB [Mixed w3c element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
MixedAnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedAnyTester.class);
Assert.assertEquals("roundtripped [Mixed w3c element] num elems", 1, roundtripped.getElems().size());
org.w3c.dom.Element rtElem = (org.w3c.dom.Element) roundtripped.getElems().get(0);
Assert.assertEquals("roundtripped [Mixed w3c element] elem name", "alien", rtElem.getTagName());
Assert.assertEquals("roundtripped [Mixed w3c element] elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
use of javax.xml.bind.annotation.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