use of com.zimbra.soap.jaxb.AnyAttrTester 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());
}
}
}
Aggregations