Search in sources :

Example 86 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method mixedAndAnyElementHandlingJustElement.

/**
     * {@link XmlAnyElement} and {@link XmlMixed} handling
     * In the places we use XmlMixed, we typically have either just text or just elements - this tests with elements
     * that do NOT map to JAXB classes.
     * Desired JSON:
     * {
     *   "alien": {
     *     "myAttr": "myValue",
     *     "child": {
     *       "_content": "Purple beans"
     *     },
     *     "daughter": {
     *       "age": "23",
     *       "name": "Kate"
     *     },
     *     "_jsns": "urn:foreign"
     *   },
     *   "_jsns": "urn:zimbraTest"
     * }
     */
@Test
public void mixedAndAnyElementHandlingJustElement() throws Exception {
    List<Object> elems = Lists.newArrayList();
    MixedAnyTester jaxb = new MixedAnyTester();
    DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = builder.newDocument();
    org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
    elem.setAttribute("myAttr", "myValue");
    org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
    child.setTextContent("Purple beans");
    elem.appendChild(child);
    org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
    child2.setAttribute("name", "Kate");
    child2.setAttribute("age", "23");
    elem.appendChild(child2);
    elems.add(elem);
    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 w3c element] ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
    MixedAnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, MixedAnyTester.class);
    Assert.assertEquals("roundtrippedX [Mixed w3c element] num elems", 1, roundtrippedX.getElems().size());
    org.w3c.dom.Element w3ce = (org.w3c.dom.Element) roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX [Mixed w3c element] elem name", "alien", w3ce.getLocalName());
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB [Mixed w3c element] ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    MixedAnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, MixedAnyTester.class);
    Assert.assertEquals("roundtripped [Mixed w3c element] num elems", 1, roundtripped.getElems().size());
    org.w3c.dom.Element rtElem = (org.w3c.dom.Element) roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped [Mixed w3c element] elem name", "alien", rtElem.getTagName());
    Assert.assertEquals("roundtripped [Mixed w3c element] elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
Also used : MixedAnyTester(com.zimbra.soap.jaxb.MixedAnyTester) DocumentBuilder(javax.xml.parsers.DocumentBuilder) 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 87 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method wrappedEnumElemList.

/**
     *  Testing form:
     *      {@code @XmlElementWrapper(name="wrapper", required=false)
     *      @XmlElement(name="enum-entry", required=false)
     *      private List<ViewEnum> entries = Lists.newArrayList();}
     * Desired JSON :
     *      {
     *        "wrapper": [{
     *            "enum-entry": [
     *              {
     *                "_content": "appointment"
     *              },
     *              {
     *                "_content": "document"
     *              }]
     *          }],
     *        "_jsns": "urn:zimbraTest"
     *      }
     */
@Test
public void wrappedEnumElemList() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("wrapped-enum-elem-list", "urn:zimbraTest"));
    Element wrapElem = jsonElem.addElement("wrapper");
    wrapElem.addElement("enum-entry").addText(ViewEnum.APPOINTMENT.toString());
    wrapElem.addElement("enum-entry").addText(ViewEnum.DOCUMENT.toString());
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    WrappedEnumElemList tstr = new WrappedEnumElemList();
    tstr.addEntry(ViewEnum.APPOINTMENT);
    tstr.addEntry(ViewEnum.DOCUMENT);
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
    WrappedEnumElemList roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, WrappedEnumElemList.class);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    Element wElem = jsonJaxbElem.getElement("wrapper");
    List<Element> jsonElems = wElem.listElements();
    List<ViewEnum> entries = roundtripped.getEntries();
    Assert.assertEquals("jsonElems num", 2, jsonElems.size());
    Assert.assertEquals("entries num", 2, entries.size());
    Assert.assertTrue("has APPOINTMENT", entries.contains(ViewEnum.APPOINTMENT));
    Assert.assertTrue("has DOCUMENT", entries.contains(ViewEnum.DOCUMENT));
}
Also used : WrappedEnumElemList(com.zimbra.soap.jaxb.WrappedEnumElemList) 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) ViewEnum(com.zimbra.soap.jaxb.ViewEnum) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 88 with XMLElement

use of com.zimbra.common.soap.Element.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 89 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method bug65572_BooleanAndXmlElements.

/**
     *
    {
        "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 bug65572_BooleanAndXmlElements() 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)

Example 90 with XMLElement

use of com.zimbra.common.soap.Element.XMLElement in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method anyElementHandling.

/**
     * {@link XmlAnyElement} handling
     * <pre>
     *     @XmlAnyElement
     *     private List<org.w3c.dom.Element> elems = Lists.newArrayList();
     * </pre>
     */
@Test
public void anyElementHandling() throws Exception {
    String given = "Given information";
    List<Object> elems = Lists.newArrayList();
    AnyTester jaxb = new AnyTester();
    DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
    org.w3c.dom.Document doc = builder.newDocument();
    org.w3c.dom.Element elem = doc.createElementNS("urn:foreign", "alien");
    elem.setAttribute("myAttr", "myValue");
    org.w3c.dom.Element child = doc.createElementNS("urn:foreign", "child");
    child.setTextContent("Purple beans");
    elem.appendChild(child);
    org.w3c.dom.Element child2 = doc.createElementNS("urn:foreign", "daughter");
    child2.setAttribute("name", "Kate");
    child2.setAttribute("age", "23");
    elem.appendChild(child2);
    elems.add(elem);
    org.w3c.dom.Element elem2 = doc.createElementNS("urn:wooky", "may");
    elem2.setAttribute("fourth", "be with you");
    jaxb.setGiven(given);
    jaxb.setElems(Lists.newArrayList(elem, elem2));
    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());
    AnyTester roundtrippedX = JaxbUtil.elementToJaxb(xmlElem, AnyTester.class);
    Assert.assertEquals("roundtrippedX given", given, roundtrippedX.getGiven());
    Assert.assertEquals("roundtrippedX num elems", 2, roundtrippedX.getElems().size());
    org.w3c.dom.Element w3ce = roundtrippedX.getElems().get(0);
    Assert.assertEquals("roundtrippedX elem name", "alien", w3ce.getLocalName());
    logDebug("STRING from JAXB ---> prettyPrint\n%1$s", getZimbraJsonJaxbString(jaxb));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    AnyTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, AnyTester.class);
    Assert.assertEquals("roundtripped given", given, roundtripped.getGiven());
    Assert.assertEquals("roundtripped num elems", 2, roundtripped.getElems().size());
    org.w3c.dom.Element rtElem = roundtripped.getElems().get(0);
    Assert.assertEquals("roundtripped elem name", "alien", rtElem.getTagName());
    Assert.assertEquals("roundtripped elem namespace", "urn:foreign", rtElem.getNamespaceURI());
}
Also used : MixedAnyTester(com.zimbra.soap.jaxb.MixedAnyTester) AnyTester(com.zimbra.soap.jaxb.AnyTester) DocumentBuilder(javax.xml.parsers.DocumentBuilder) 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)

Aggregations

XMLElement (com.zimbra.common.soap.Element.XMLElement)140 Element (com.zimbra.common.soap.Element)111 Test (org.junit.Test)30 JSONElement (com.zimbra.common.soap.Element.JSONElement)29 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)25 XmlElement (javax.xml.bind.annotation.XmlElement)25 FilterTest (com.zimbra.soap.mail.type.FilterTest)24 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)9 HashMap (java.util.HashMap)8 Account (com.zimbra.cs.account.Account)6 ArrayList (java.util.ArrayList)6 ZAuthToken (com.zimbra.common.auth.ZAuthToken)5 KeyValuePair (com.zimbra.soap.type.KeyValuePair)5 ByteBuilder (com.zimbra.common.mime.HeaderUtils.ByteBuilder)4 ServiceException (com.zimbra.common.service.ServiceException)4 DataSource (com.zimbra.cs.account.DataSource)4 Auth (com.zimbra.cs.service.account.Auth)4 URL (java.net.URL)4 Cookie (javax.servlet.http.Cookie)4 Signature (com.zimbra.cs.account.Signature)3