use of com.zimbra.common.soap.Element.JSONElement 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<Element.KeyValuePair> kvps = elem.listKeyValuePairs();
Assert.assertEquals("Number of keyValuePairs ", 4, kvps.size());
Element.KeyValuePair kvp4 = kvps.get(3);
Assert.assertEquals("KeyValuePair notificationMessage key", "notificationMessage", kvp4.getKey());
Assert.assertEquals("KeyValuePair notificationMessage value", notifMsg, kvp4.getValue());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method wrappedZimbraKeyValuePairsAnnotation.
/**
* Desired JSON :
* {
* "wrapper": {
* "_attrs": {
* "key1": "value1",
* "key2": "value2"
* }
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void wrappedZimbraKeyValuePairsAnnotation() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
Element wrapperElem = jsonElem.addUniqueElement("wrapper");
wrapperElem.addKeyValuePair("key1", "value1");
wrapperElem.addKeyValuePair("key2", "value2");
List<KeyValuePair> attrs = Lists.newArrayList();
attrs.add(new KeyValuePair("key1", "value1"));
attrs.add(new KeyValuePair("key2", "value2"));
WrappedKeyValuePairsTester jaxb = new WrappedKeyValuePairsTester(attrs);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
WrappedKeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrappedKeyValuePairsTester.class);
List<Element.KeyValuePair> elemKVPs = jsonJaxbElem.getElement("wrapper").listKeyValuePairs();
Assert.assertEquals("elemKVP num", 2, elemKVPs.size());
List<KeyValuePair> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 2, kvps.size());
}
use of com.zimbra.common.soap.Element.JSONElement 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.addNonUniqueElement(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.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method kvpForGetDLResp.
/**
* Desired JSON
* {
* "dl": [{
* "name": "my name",
* "id": "myId",
* "dynamic": true,
* "_attrs": {
* "mail": "fun@example.test",
* "zimbraMailStatus": "enabled"
* }
* }],
* "_jsns": "urn:zimbraAccount"
* }
*/
@Test
public void kvpForGetDLResp() 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());
// ObjectInfo declares this field:
// @ZimbraKeyValuePairs
// @XmlElement(name=AccountConstants.E_A /* a */, required=false)
// private final List<KeyValuePair> attrList;
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);
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("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
// ensure that the JAXB handles empty owners OK (not using empty list in JAXB field initializer)
Assert.assertNull("roundtripped owner", roundtripped.getDl().getOwners());
Assert.assertNull("roundtrippedX owner", roundtrippedX.getDl().getOwners());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method zmBooleanAntStringXmlElements.
/**
* {
* "status": [{
* "_content": "true" # Actually get true not "true" but should be ok
* }],
* "message": [{
* "_content": "ver ndx message"
* }],
* "_jsns": "urn:zimbraAdmin"
* }
*/
@Test
public void zmBooleanAntStringXmlElements() throws Exception {
final String msg = "ver ndx message";
// --------------------------------- For Comparison - Element handling
Element legacyElem = JSONElement.mFactory.createElement(AdminConstants.VERIFY_INDEX_RESPONSE);
legacyElem.addNonUniqueElement(AdminConstants.E_STATUS).addText(String.valueOf(true));
legacyElem.addNonUniqueElement(AdminConstants.E_MESSAGE).addText(msg);
logDebug("VerifyIndexResponse JSONElement ---> prettyPrint\n%1$s", legacyElem.prettyPrint());
VerifyIndexResponse viResp = new VerifyIndexResponse(true, msg);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(viResp);
logDebug("VerifyIndexResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("status", true, jsonJaxbElem.getAttributeBool(AdminConstants.E_STATUS));
Assert.assertEquals("message", msg, jsonJaxbElem.getAttribute(AdminConstants.E_MESSAGE));
VerifyIndexResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, VerifyIndexResponse.class);
Assert.assertEquals("roundtripped status", true, roundtripped.isStatus());
Assert.assertEquals("roundtripped message", msg, roundtripped.getMessage());
}
Aggregations