Search in sources :

Example 76 with XmlElement

use of javax.xml.bind.annotation.XmlElement 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));
}
Also used : MixedTester(com.zimbra.soap.jaxb.MixedTester) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 77 with XmlElement

use of javax.xml.bind.annotation.XmlElement 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());
        }
    }
}
Also used : QName(org.dom4j.QName) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) AnyAttrTester(com.zimbra.soap.jaxb.AnyAttrTester) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 78 with XmlElement

use of javax.xml.bind.annotation.XmlElement 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());
}
Also used : GetDistributionListMembersResponse(com.zimbra.soap.account.message.GetDistributionListMembersResponse) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 79 with XmlElement

use of javax.xml.bind.annotation.XmlElement 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());
}
Also used : MixedTester(com.zimbra.soap.jaxb.MixedTester) StringAttribIntValue(com.zimbra.soap.jaxb.StringAttribIntValue) XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 80 with XmlElement

use of javax.xml.bind.annotation.XmlElement 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());
}
Also used : XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) FilterRule(com.zimbra.soap.mail.type.FilterRule) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) GetFilterRulesResponse(com.zimbra.soap.mail.message.GetFilterRulesResponse) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

XmlElement (javax.xml.bind.annotation.XmlElement)85 Test (org.junit.Test)31 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)29 Element (com.zimbra.common.soap.Element)28 JSONElement (com.zimbra.common.soap.Element.JSONElement)28 XMLElement (com.zimbra.common.soap.Element.XMLElement)28 FilterTest (com.zimbra.soap.mail.type.FilterTest)26 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)14 Method (java.lang.reflect.Method)13 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)13 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)13 XmlElements (javax.xml.bind.annotation.XmlElements)10 XmlElementRef (javax.xml.bind.annotation.XmlElementRef)8 KeyValuePair (com.zimbra.soap.type.KeyValuePair)7 Annotation (java.lang.annotation.Annotation)6 Type (java.lang.reflect.Type)6 ParameterizedType (java.lang.reflect.ParameterizedType)4 List (java.util.List)4 XmlList (javax.xml.bind.annotation.XmlList)4