use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method xmlEnumValuesInAttributes.
/**
* If you just use a pair of annotation introspectors (JacksonAnnotationIntrospector/JaxbAnnotationIntrospector)
* then "XmlEnumValue"'s are ignored - AnnotationIntrospector.Pair's findEnumValue(Enum<?> e) method won't
* call the secondary's findEnumValue unless primary's findEnumValue returns null.
* (if we made JaxbAnnotationIntrospector the primary, this would work but other things wouldn't)
* To fix this, the current code makes use of ZmPairAnnotationIntrospector which overrides findEnumValue
* Desired JSON :
* {
* "fold1": "virtual conversation",
* "fold2": "",
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void xmlEnumValuesInAttributes() throws Exception {
EnumAttribs tstr = new EnumAttribs();
tstr.setFold1(ViewEnum.VIRTUAL_CONVERSATION);
tstr.setFold2(ViewEnum.UNKNOWN);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr, QName.get("enum-tester", "urn:zimbraTest"));
EnumAttribs roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, EnumAttribs.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
// Want 'virtual conversation' not 'VIRTUAL_CONVERSATION'
Assert.assertEquals("fold1 value", ViewEnum.VIRTUAL_CONVERSATION.toString(), jsonJaxbElem.getAttribute("fold1"));
// Want '' not 'UNKNOWN'
Assert.assertEquals("fold2 value", ViewEnum.UNKNOWN.toString(), jsonJaxbElem.getAttribute("fold2"));
Assert.assertEquals("roundtripped fold1", ViewEnum.VIRTUAL_CONVERSATION, 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 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());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method mixedHandlingJustText.
/**
* {@link XmlMixed} handling
* In the places we use XmlMixed, we typically have either just text or just elements
* This tests where we have just text.
*/
@Test
public void mixedHandlingJustText() throws Exception {
String textStr = "text string";
List<Object> elems = Lists.newArrayList();
MixedTester jaxb = new MixedTester();
elems.add(textStr);
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 has just text] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
MixedTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedTester.class);
Assert.assertEquals("roundtrippedX [Mixed has just text] num elems", 1, roundtrippedX.getElems().size());
Assert.assertEquals("roundtrippedX [Mixed has just text] str", textStr, (String) roundtrippedX.getElems().get(0));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB [Mixed has just text] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
MixedTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedTester.class);
Assert.assertEquals("roundtripped [Mixed has just text] num elems", 1, roundtripped.getElems().size());
Assert.assertEquals("roundtripped [Mixed has just text] str", textStr, (String) roundtripped.getElems().get(0));
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method anyAttributeHandling.
/**
* {@link XmlAnyAttribute} handling - the field with this annotation needs to be a {@link Map}
* <pre>
* @XmlAnyAttribute
* private Map<javax.xml.namespace.QName,Object> extraAttributes = Maps.newHashMap();
* </pre>
* Desired JSON:
* {
* "given": "Given information",
* "attr2": "222",
* "attr1": "First attr",
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void anyAttributeHandling() throws Exception {
String given = "Given information";
String first = "First attr";
int second = 222;
Map<javax.xml.namespace.QName, Object> extras = Maps.newHashMap();
extras.put(new javax.xml.namespace.QName("attr1"), first);
// Would expect this to work with integer but the XML JAXB fails saying can't cast Integer to String
extras.put(new javax.xml.namespace.QName("attr2"), new Integer(second).toString());
AnyAttrTester jaxb = new AnyAttrTester();
jaxb.setGiven(given);
jaxb.setExtraAttributes(extras);
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());
AnyAttrTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyAttrTester.class);
Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
Map<javax.xml.namespace.QName, Object> rtXextras = roundtrippedX.getExtraAttributes();
Assert.assertEquals("roundtrippedX num extras", 2, rtXextras.size());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
AnyAttrTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyAttrTester.class);
Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
Map<javax.xml.namespace.QName, Object> rtextras = roundtripped.getExtraAttributes();
Assert.assertEquals("roundtripped num extras", 2, rtextras.size());
for (Entry<javax.xml.namespace.QName, Object> attrib : rtextras.entrySet()) {
if ("attr1".equals(attrib.getKey().getLocalPart())) {
Assert.assertTrue("attr1 attribute has correct value", first.equals(attrib.getValue()));
} else if ("attr2".equals(attrib.getKey().getLocalPart())) {
Assert.assertTrue("attr2 attribute has correct value", "222".equals(attrib.getValue()));
} else {
Assert.fail("Unexpected attribute name for attrib " + attrib.toString());
}
}
}
use of com.zimbra.common.soap.Element.JSONElement 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.addNonUniqueElement(AccountConstants.E_DLM).setText("dlmember1@no.where");
legacyElem.addNonUniqueElement(AccountConstants.E_DLM).setText("dlmember2@no.where");
legacyElem.addNonUniqueElement(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());
}
Aggregations