use of com.zimbra.soap.jaxb.OddKeyValuePairsTester in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method zimbraOddKeyValuePairsAnnotation.
/**
* 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 do NOT use the defaults of "a" for the element name and
* "n" for the attribute name in the XML form.
* Desired XML :<br />
* <key-value-pairs-tester xmlns="urn:zimbraTest">
* <oddElemName name="key1">value1</oddElemName>
* <oddElemName name="key2">value2-a</oddElemName>
* <oddElemName name="key2">value2-b</oddElemName>
* </key-value-pairs-tester>
*<br />
* Desired JSON :<br />
* {
* "_attrs": {
* "key1": "value1",
* "key2": [
* "value2-a",
* "value2-b"]
* },
* "_jsns": "urn:zimbraTest"
* }
*/
@Test
public void zimbraOddKeyValuePairsAnnotation() throws Exception {
Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
jsonElem.addKeyValuePair("key1", "value1", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-a", "oddElemName", "name");
jsonElem.addKeyValuePair("key2", "value2-b", "oddElemName", "name");
List<Attr> attrs = Lists.newArrayList();
attrs.add(new Attr("key1", "value1"));
attrs.add(new Attr("key2", "value2-a"));
attrs.add(new Attr("key2", "value2-b"));
OddKeyValuePairsTester jaxb = new OddKeyValuePairsTester(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());
String origJson = jsonJaxbElem.prettyPrint();
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", origJson);
OddKeyValuePairsTester roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, OddKeyValuePairsTester.class);
List<Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
List<Attr> kvps = roundtripped.getAttrList();
Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
jsonJaxbElem = JacksonUtil.jaxbToJSONElement(roundtripped);
String finalJson = jsonJaxbElem.prettyPrint();
if (!origJson.equals(finalJson)) {
logDebug("JSONElement from roundtripped JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("roundtripped JSON pretty print", origJson, finalJson);
}
}
Aggregations