use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method xmlEnumValuesInAttrAndElem.
/**
* 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": [{
* "_content": ""
* }],
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void xmlEnumValuesInAttrAndElem() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("enum-tester", "urn:zimbraTest"));
jsonElem.addAttribute("fold1", ViewEnum.VIRTUAL_CONVERSATION.toString());
jsonElem.addNonUniqueElement("fold2").addText(ViewEnum.UNKNOWN.toString());
EnumAttribEnumElem tstr = new EnumAttribEnumElem();
tstr.setFold1(ViewEnum.VIRTUAL_CONVERSATION);
tstr.setFold2(ViewEnum.UNKNOWN);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr, QName.get("enum-tester", "urn:zimbraTest"));
EnumAttribEnumElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, EnumAttribEnumElem.class);
logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
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.getElement("fold2").getText());
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 mixedHandlingWithJaxbAndNoText.
/**
* {@link XmlMixed} handling
* In the places we use XmlMixed, we have either just text or just elements
* This tests where we have something that maps to a JAXB object.
* Side note: Also tests out case for {@link XmlElementRef} where name is derived from root element.
*/
@Test
public void mixedHandlingWithJaxbAndNoText() throws Exception {
String str = "my string";
int num = 321;
List<Object> elems = Lists.newArrayList();
MixedTester jaxb = new MixedTester();
StringAttribIntValue inner = new StringAttribIntValue(str, num);
elems.add(inner);
jaxb.setElems(elems);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false);
logDebug("XmlElement (for comparison) [Mixed has element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
MixedTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedTester.class);
Assert.assertEquals("roundtrippedX num elems", 1, roundtrippedX.getElems().size());
StringAttribIntValue rtXmlByRef = (StringAttribIntValue) roundtrippedX.getElems().get(0);
Assert.assertEquals("roundtrippedX [Mixed has element] str", str, rtXmlByRef.getAttrib1());
Assert.assertEquals("roundtrippedX [Mixed has element] num", num, rtXmlByRef.getMyValue());
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB [Mixed has element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
MixedTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedTester.class);
Assert.assertEquals("roundtripped [Mixed has element] num elems", 1, roundtripped.getElems().size());
StringAttribIntValue rtByRef = (StringAttribIntValue) roundtripped.getElems().get(0);
Assert.assertEquals("roundtripped [Mixed has element] str", str, rtByRef.getAttrib1());
Assert.assertEquals("roundtripped [Mixed has element] num", num, rtByRef.getMyValue());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method systemRetentionPolicyResponse.
@Test
public void systemRetentionPolicyResponse() throws Exception {
RetentionPolicy rp = new RetentionPolicy((Iterable<Policy>) null, (Iterable<Policy>) null);
GetSystemRetentionPolicyResponse jaxb = new GetSystemRetentionPolicyResponse(rp);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("json string from jaxb", jsonEmptyGetSystemRetentionPolicyResponse, jsonJaxbElem.prettyPrint());
GetSystemRetentionPolicyResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetSystemRetentionPolicyResponse.class);
Assert.assertEquals("roundtripped retention policy", jaxb.getRetentionPolicy().toString(), roundtripped.getRetentionPolicy().toString());
rp = new RetentionPolicy(Collections.singleton(Policy.newUserPolicy("200")), Collections.singleton(Policy.newUserPolicy("400")));
jaxb = new GetSystemRetentionPolicyResponse(rp);
jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("json string from jaxb", jsonGetSystemRetentionPolicyResponse, jsonJaxbElem.prettyPrint());
roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetSystemRetentionPolicyResponse.class);
Assert.assertEquals("roundtripped retention policy", jaxb.getRetentionPolicy().toString(), roundtripped.getRetentionPolicy().toString());
}
use of com.zimbra.common.soap.Element.JSONElement in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method bug65572BooleanAndXmlElements.
/**
* {
* "filterRules": [{
* "filterRule": [{
* "name": "filter.bug65572",
* "active": false,
* "filterTests": [{
* "condition": "anyof",
* "headerTest": [{
* "index": 0,
* "header": "X-Spam-Score",
* "caseSensitive": false,
* "stringComparison": "contains",
* "value": "0"
* }]
* }],
* "filterActions": [{
* "actionFlag": [{
* "flagName": "flagged",
* "index": 0
* }],
* "actionStop": [{
* "index": 1
* }]
* }]
* }]
* }],
* "_jsns": "urn:zimbraMail"
* }
*/
/**
* This also tests {@link XmlElements} - It is used in {@link FilterTests}
* @throws Exception
*/
@Test
public void bug65572BooleanAndXmlElements() throws Exception {
Element legacyXmlElem = mkFilterRulesResponse(XMLElement.mFactory);
Element legacyJsonElem = mkFilterRulesResponse(JSONElement.mFactory);
GetFilterRulesResponse jaxb = new GetFilterRulesResponse();
FilterTests tests = FilterTests.createForCondition("anyof");
FilterTest.HeaderTest hdrTest = FilterTest.HeaderTest.createForIndexNegative(0, null);
hdrTest.setHeaders("X-Spam-Score");
hdrTest.setCaseSensitive(false);
hdrTest.setStringComparison("contains");
hdrTest.setValue("0");
tests.addTest(hdrTest);
FilterAction.FlagAction flagAction = new FilterAction.FlagAction("flagged");
flagAction.setIndex(0);
FilterAction.StopAction stopAction = new FilterAction.StopAction();
stopAction.setIndex(1);
FilterRule rule1 = FilterRule.createForNameFilterTestsAndActiveSetting("filter.bug65572", tests, false);
rule1.addFilterAction(flagAction);
rule1.addFilterAction(stopAction);
jaxb.addFilterRule(rule1);
Element xmlElem = JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory);
logDebug("legacyXMLElement ---> prettyPrint\n%1$s", legacyXmlElem.prettyPrint());
logDebug("XMLElement from JAXB ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
// Attribute Ordering not reliable: Assert.assertEquals("XML", legacyXmlElem.prettyPrint(), xmlElem.prettyPrint());
Element xmlFr = xmlElem.getElement(MailConstants.E_FILTER_RULES).getElement(MailConstants.E_FILTER_RULE);
Assert.assertEquals("XMLElement from JAXB filter rule name", "filter.bug65572", xmlFr.getAttribute(MailConstants.A_NAME));
Assert.assertEquals("XMLElement from JAXB filter rule active", false, xmlFr.getAttributeBool(MailConstants.A_ACTIVE));
Element xmlFT = xmlFr.getElement(MailConstants.E_FILTER_TESTS);
Assert.assertEquals("XMLElement from JAXB filter tests condition", "anyof", xmlFT.getAttribute(MailConstants.A_CONDITION));
Element xmlHdrT = xmlFT.getElement(MailConstants.E_HEADER_TEST);
Assert.assertEquals("XMLElement from JAXB filter hdr test index", 0, xmlHdrT.getAttributeInt(MailConstants.A_INDEX));
Assert.assertEquals("XMLElement from JAXB filter hdr test hdr", "X-Spam-Score", xmlHdrT.getAttribute(MailConstants.A_HEADER));
Assert.assertEquals("XMLElement from JAXB filter hdr test caseSense", false, xmlHdrT.getAttributeBool(MailConstants.A_CASE_SENSITIVE));
Assert.assertEquals("XMLElement from JAXB filter hdr test comparison", "contains", xmlHdrT.getAttribute(MailConstants.A_STRING_COMPARISON));
Assert.assertEquals("XMLElement from JAXB filter hdr test value", 0, xmlHdrT.getAttributeInt(MailConstants.A_VALUE));
Element xmlFA = xmlFr.getElement(MailConstants.E_FILTER_ACTIONS);
Element xmlFlag = xmlFA.getElement(MailConstants.E_ACTION_FLAG);
Assert.assertEquals("XMLElement from JAXB action flag name", "flagged", xmlFlag.getAttribute(MailConstants.A_FLAG_NAME));
Assert.assertEquals("XMLElement from JAXB action flag index", 0, xmlFlag.getAttributeInt(MailConstants.A_INDEX));
Element xmlStop = xmlFA.getElement(MailConstants.E_ACTION_STOP);
Assert.assertEquals("XMLElement from JAXB action stop index", 1, xmlStop.getAttributeInt(MailConstants.A_INDEX));
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb, MailConstants.GET_FILTER_RULES_RESPONSE);
logDebug("GetFilterRulesResponse legacyJSONElement ---> prettyPrint\n%1$s", legacyJsonElem.prettyPrint());
logDebug("GetFilterRulesResponse JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("JSON", legacyJsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
GetFilterRulesResponse roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, GetFilterRulesResponse.class);
List<FilterRule> rules = roundtripped.getFilterRules();
Assert.assertEquals("num roundtripped rules", 1, rules.size());
FilterRule rtRule = rules.get(0);
Assert.assertEquals("roundtripped rule name", "filter.bug65572", rtRule.getName());
Assert.assertEquals("roundtripped rule active setting", false, rtRule.isActive());
Assert.assertEquals("roundtripped rule action count", 2, rtRule.getActionCount());
FilterTests rtTests = rtRule.getFilterTests();
Assert.assertEquals("roundtripped filterTests condition", "anyof", rtTests.getCondition());
List<FilterTest> rtFilterTests = rtTests.getTests();
Assert.assertEquals("num roundtripped filter tests", 1, rtFilterTests.size());
FilterTest.HeaderTest rtHdrTest = (FilterTest.HeaderTest) rtFilterTests.get(0);
Assert.assertEquals("roundtripped header test index", 0, rtHdrTest.getIndex());
Assert.assertEquals("roundtripped header test header", "X-Spam-Score", rtHdrTest.getHeaders());
Assert.assertEquals("roundtripped header test caseSens", false, rtHdrTest.isCaseSensitive());
Assert.assertEquals("roundtripped header test stringComparison", "contains", rtHdrTest.getStringComparison());
Assert.assertEquals("roundtripped header test value", "0", rtHdrTest.getValue());
List<FilterAction> rtActions = rtRule.getFilterActions();
Assert.assertEquals("num roundtripped actions", 2, rtActions.size());
FilterAction.FlagAction rtFlagAction = (FilterAction.FlagAction) rtActions.get(0);
Assert.assertEquals("roundtripped FlagAction name", "flagged", rtFlagAction.getFlag());
Assert.assertEquals("roundtripped FlagAction index", 0, rtFlagAction.getIndex());
FilterAction.StopAction rtStopAction = (FilterAction.StopAction) rtActions.get(1);
Assert.assertEquals("roundtripped StopAction index", 1, rtStopAction.getIndex());
}
Aggregations