use of javax.xml.bind.annotation.XmlElement 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 javax.xml.bind.annotation.XmlElement 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 javax.xml.bind.annotation.XmlElement 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());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method stringElemAndZimbraJsonlongAttr.
/**
* Desired JSON :
* {
* "authToken": [{
* "_content": "authenticationtoken"
* }],
* "lifetime": 3000,
* "_jsns": "urn:zimbraAdmin"
* }
*/
@Test
public void stringElemAndZimbraJsonlongAttr() throws Exception {
final long lifetime = 3000;
final String lifetimeStr = new Long(lifetime).toString();
final String authToken = "authenticationtoken";
AuthResponse tstr = new AuthResponse();
tstr.setAuthToken(authToken);
tstr.setLifetime(lifetime);
Element xmlElem = JaxbUtil.jaxbToElement(tstr, Element.XMLElement.mFactory, true, false);
AuthResponse roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AuthResponse.class);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
AuthResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AuthResponse.class);
logDebug("XmlElement (for comparison) ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("JSONElement lifetime", lifetimeStr, jsonJaxbElem.getAttribute("lifetime"));
Assert.assertEquals("JSONElement authToken", authToken, jsonJaxbElem.getElement("authToken").getText());
Assert.assertEquals("roundtripped lifetime", lifetime, roundtripped.getLifetime());
Assert.assertEquals("roundtripped authToken", authToken, roundtripped.getAuthToken());
Assert.assertEquals("roundtrippedX lifetime", lifetime, roundtrippedX.getLifetime());
Assert.assertEquals("roundtrippedX authToken", authToken, roundtrippedX.getAuthToken());
}
use of javax.xml.bind.annotation.XmlElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method keyValuePairMbxSearch.
/**
* CreateXMbxSearchRequest currently extends AdminKeyValuePairs which uses the {@link ZimbraKeyValuePairs}
* annotation.
*
* Want JSON for KeyValuePairs to look something like :
* "_attrs": {
* "query": "Kitchen",
* "accounts": "*"
* },
*/
@Test
public void keyValuePairMbxSearch() throws Exception {
final String notifMsg = "Search task %taskId% completed with status %status%. \nImported: %numMsgs% messages. \nSearch query used: %query%.";
Element legacyElem = JSONElement.mFactory.createElement(XMbxSearchConstants.CREATE_XMBX_SEARCH_REQUEST);
legacyElem.addKeyValuePair("query", "Kitchen");
legacyElem.addKeyValuePair("accounts", "*");
legacyElem.addKeyValuePair("limit", "0");
legacyElem.addKeyValuePair("notificationMessage", notifMsg);
logDebug("CreateXmbxSearchRequest JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
// CreateXMbxSearchRequest extends AdminKeyValuePairs which uses ZimbraKeyValuePairs annotation to flag need
// for special handling required when serializing to JSON:
// @ZimbraKeyValuePairs
// @XmlElement(name=AdminConstants.E_A /* a */, required=false)
// private List<KeyValuePair> keyValuePairs;
CreateXMbxSearchRequest jaxb = new CreateXMbxSearchRequest();
jaxb.addKeyValuePair(new KeyValuePair("query", "Kitchen"));
jaxb.addKeyValuePair(new KeyValuePair("accounts", "*"));
jaxb.addKeyValuePair(new KeyValuePair("limit", "0"));
jaxb.addKeyValuePair(new KeyValuePair("notificationMessage", notifMsg));
Element elem = JacksonUtil.jaxbToJSONElement(jaxb, XMbxSearchConstants.CREATE_XMBX_SEARCH_REQUEST);
logDebug("CreateXmbxSearchRequest JSONElement from JAXB ---> prettyPrint\n%1$s", elem.prettyPrint());
List<com.zimbra.common.soap.Element.KeyValuePair> kvps = elem.listKeyValuePairs();
Assert.assertEquals("Number of keyValuePairs ", 4, kvps.size());
com.zimbra.common.soap.Element.KeyValuePair kvp4 = kvps.get(3);
Assert.assertEquals("KeyValuePair notificationMessage key", "notificationMessage", kvp4.getKey());
Assert.assertEquals("KeyValuePair notificationMessage value", notifMsg, kvp4.getValue());
}
Aggregations