use of com.zimbra.common.soap.Element in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method kvpForGetDLRespWithOwner.
/**
* Ensuring that JAXB can handle having an owner in a list that is not an empty array when there are no owners
*/
@Test
public void kvpForGetDLRespWithOwner() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get(AccountConstants.E_GET_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateGetDlResp(jsonElem);
Element xmlElem = XMLElement.mFactory.createElement(QName.get(AccountConstants.E_GET_DISTRIBUTION_LIST_RESPONSE, AccountConstants.NAMESPACE_STR));
populateGetDlResp(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("mail", "fun@example.test"));
attrs.add(new KeyValuePair("zimbraMailStatus", "enabled"));
DistributionListInfo dl = new DistributionListInfo("myId", "my name", null, attrs);
dl.setDynamic(true);
DistributionListGranteeInfo grantee = new DistributionListGranteeInfo(GranteeType.usr, "ownerId", "ownerName");
dl.addOwner(grantee);
GetDistributionListResponse jaxb = new GetDistributionListResponse(dl);
Element xmlJaxbElem = JaxbUtil.jaxbToElement(jaxb, XMLElement.mFactory);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
GetDistributionListResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetDistributionListResponse.class);
GetDistributionListResponse roundtrippedX = JaxbUtil.elementToJaxb(xmlJaxbElem, GetDistributionListResponse.class);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlJaxbElem.prettyPrint());
List<? extends KeyValuePair> kvps = roundtripped.getDl().getAttrList();
Assert.assertEquals("roundtripped kvps num", 2, kvps.size());
Assert.assertEquals("roundtripped owner num", 1, roundtripped.getDl().getOwners().size());
Assert.assertEquals("roundtrippedX owner num", 1, roundtrippedX.getDl().getOwners().size());
}
use of com.zimbra.common.soap.Element in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method inviteTestMethods.
/**
* Tests a list of strings. Was using a JSON Serializer called ContentListSerializer but the ObjectMapper we
* use for Zimbra JSON now handles lists of strings this way by default. GetFilterRules uses JAXB rather than
* Element already.
* Similar situation using Element based code for GetDistributionListMembers response used multiple calls to:
* parent.addElement(AccountConstants.E_DLM).setText(member);
* Desired JSON :
{
"condition": "anyof",
"inviteTest": [{
"index": 0,
"method": [
{
"_content": "REQUEST"
},
{
"_content": "REPLY"
},
{
"_content": "CANCEL"
}]
}],
"_jsns": "urn:zimbraMail"
}
*/
@Test
public void inviteTestMethods() throws Exception {
FilterTests tests = FilterTests.createForCondition("anyof");
FilterTest.InviteTest inviteTest = new FilterTest.InviteTest();
inviteTest.addMethod("REQUEST");
inviteTest.addMethod("REPLY");
inviteTest.addMethod("CANCEL");
tests.addTest(inviteTest);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tests, QName.get(MailConstants.E_FILTER_TESTS, MailConstants.NAMESPACE));
logDebug("filterTests JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
FilterTests roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, FilterTests.class);
FilterTest.InviteTest rtInviteTest = (FilterTest.InviteTest) roundtripped.getTests().get(0);
Assert.assertEquals("roundtripped num methods", 3, rtInviteTest.getMethods().size());
}
use of com.zimbra.common.soap.Element in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method transientHandling.
/** XmlTransient handling - Need to ignore fields with {@link XmlElementRef} annotation. */
@Test
public void transientHandling() throws Exception {
String str = "my string - should NOT be serialized";
int num = 321;
TransientTester jaxb = new TransientTester(str, num);
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());
TransientTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, TransientTester.class);
TransientTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, TransientTester.class);
Assert.assertEquals("roundtrippedX num", new Integer(num), roundtrippedX.getNummer());
Assert.assertNull("roundtrippedX str", roundtrippedX.getToBeIgnored());
Assert.assertEquals("roundtripped num", new Integer(num), roundtripped.getNummer());
Assert.assertNull("roundtripped str", roundtripped.getToBeIgnored());
}
use of com.zimbra.common.soap.Element in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method populateCreateDlResp.
private void populateCreateDlResp(Element elem) {
Element eDL = elem.addElement(AdminConstants.E_DL);
eDL.addAttribute(AdminConstants.A_NAME, "my name");
eDL.addAttribute(AdminConstants.A_ID, "myId");
eDL.addAttribute(AdminConstants.A_DYNAMIC, true);
Map<String, Object> unicodeAttrs = Maps.newHashMap();
unicodeAttrs.put("key1", "value1");
String[] strs = { "Hello", "There" };
unicodeAttrs.put("key2", strs);
encodeAttrs(eDL, unicodeAttrs, AdminConstants.A_N);
}
use of com.zimbra.common.soap.Element 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());
}
Aggregations