ESHttpXmlUnmarshaller implements ESHttpUnmarshaller { @Override public final Object unmarshal(final DeserializerRegistry registry, final SerializedDataType dataType, final EnhancedMimeType mimeType, final Object data) { if (data == null) { return null; } if (!(data instanceof Node)) { throw new IllegalArgumentException("Can only unmarshal DOM nodes, but was: " + data + " [" + data.getClass().getName() + "]"); } final Node node = (Node) data; final String transferEncodingData = mimeType.getParameter("transfer-encoding"); if (transferEncodingData == null) { final Node childNode = findLastChildElement(node); final Deserializer deSer = registry.getDeserializer(dataType, mimeType); return deSer.unmarshal(childNode, dataType, mimeType); } final Node childNode = findLastChildElement(node); final String base64str = childNode.getTextContent(); final byte[] bytes = Base64.decodeBase64(base64str); final Deserializer deSer = registry.getDeserializer(dataType, mimeType); return deSer.unmarshal(bytes, dataType, mimeType); }  @Override final Object unmarshal(final DeserializerRegistry registry, final SerializedDataType dataType,
            final EnhancedMimeType mimeType, final Object data);  }
@Test public void testNull() throws MimeTypeParseException { final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); assertThat(new ESHttpXmlUnmarshaller().unmarshal(registry, new SerializedDataType("whatever"), new EnhancedMimeType("application/xml"), null)).isNull(); }
@Test public void testXmlXml() throws IOException, MimeTypeParseException { final SerializedDataType dataType = new SerializedDataType(MyEvent.TYPE.asBaseType()); final EnhancedMimeType mimeType = new EnhancedMimeType("application/xml"); final DeserializerRegistry registry = createRegistry(); final Node node = parse("/event-xml-xml-xml.xml", "/Event/Data"); final Object obj = new ESHttpXmlUnmarshaller().unmarshal(registry, dataType, mimeType, node); assertThat(obj).isInstanceOf(MyEvent.class); final MyEvent event = (MyEvent) obj; assertThat(event.getId()).isEqualTo("68616d90-cf72-4c2a-b913-32bf6e6506ed"); assertThat(event.getDescription()).isEqualTo("Hello, XML!"); }
@Test public void testXmlOther() throws IOException, MimeTypeParseException { final SerializedDataType dataType = new SerializedDataType(MyEvent.TYPE.asBaseType()); final EnhancedMimeType mimeType = new EnhancedMimeType( "application/json; version=1; encoding=utf-8; transfer-encoding=base64"); final DeserializerRegistry registry = createRegistry(); final Node node = parse("/event-xml-xml-other.xml", "/Event/Data"); final Object obj = new ESHttpXmlUnmarshaller().unmarshal(registry, dataType, mimeType, node); assertThat(obj).isInstanceOf(JsonObject.class); final JsonObject event = (JsonObject) obj; assertThat(event.getString("id")).isEqualTo("68616d90-cf72-4c2a-b913-32bf6e6506e"); assertThat(event.getString("description")).isEqualTo("Hello, JSON!"); }