Search in sources :

Example 1 with KeyValuePairsTester

use of com.zimbra.soap.jaxb.KeyValuePairsTester in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method zimbraKeyValuePairsAnnotation.

/**
 * Check that JSON can be deserialised into a JAXB object when some of the JSON represents Zimbra KeyValuePairs
 * (i.e. An "_attrs" array).
 * In this case, the target objects for each keyvalue pair use the defaults of "a" for the element name and
 * "n" for the attribute name in the XML form.
 * Desired XML :<br />
 * &lt;key-value-pairs-tester xmlns="urn:zimbraTest">
 *   &lt;a n="key1">value1&lt;/a>
 *   &lt;a n="key2">value2-a&lt;/a>
 *   &lt;a n="key2">value2-b&lt;/a>
 * &lt;/key-value-pairs-tester>
 *<br />
 * Desired JSON :<br />
 * {
 *   "_attrs": {
 *     "key1": "value1",
 *     "key2": [
 *       "value2-a",
 *       "value2-b"]
 *   },
 *   "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void zimbraKeyValuePairsAnnotation() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
    jsonElem.addKeyValuePair("key1", "value1");
    jsonElem.addKeyValuePair("key2", "value2-a");
    jsonElem.addKeyValuePair("key2", "value2-b");
    List<KeyValuePair> attrs = Lists.newArrayList();
    attrs.add(new KeyValuePair("key1", "value1"));
    attrs.add(new KeyValuePair("key2", "value2-a"));
    attrs.add(new KeyValuePair("key2", "value2-b"));
    KeyValuePairsTester jaxb = new KeyValuePairsTester(attrs);
    logDebug("XMLElement (from JAXB) ---> prettyPrint\n%1$s", JaxbUtil.jaxbToElement(jaxb, Element.XMLElement.mFactory, true, false).prettyPrint());
    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());
    KeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, KeyValuePairsTester.class);
    List<Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
    Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
    Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
    List<KeyValuePair> kvps = roundtripped.getAttrList();
    Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) 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) WrappedKeyValuePairsTester(com.zimbra.soap.jaxb.WrappedKeyValuePairsTester) KeyValuePairsTester(com.zimbra.soap.jaxb.KeyValuePairsTester) OddKeyValuePairsTester(com.zimbra.soap.jaxb.OddKeyValuePairsTester) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 KeyValuePairsTester (com.zimbra.soap.jaxb.KeyValuePairsTester)1 OddKeyValuePairsTester (com.zimbra.soap.jaxb.OddKeyValuePairsTester)1 WrappedKeyValuePairsTester (com.zimbra.soap.jaxb.WrappedKeyValuePairsTester)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 KeyValuePair (com.zimbra.soap.type.KeyValuePair)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 Test (org.junit.Test)1