use of org.w3c.dom.Document in project camel by apache.
the class JsonToXmlAttributesTest method shouldCreateElementWithAttribute.
@Test
public void shouldCreateElementWithAttribute() throws ParserConfigurationException, IOException, SAXException {
// Given
InputStream inStream = getClass().getResourceAsStream("jsonToXmlElementWithAttributeMessage.json");
String in = context.getTypeConverter().convertTo(String.class, inStream);
// When
String xml = template.requestBody("direct:unmarshal", in, String.class);
// Then
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
NodeList nodeList = document.getDocumentElement().getElementsByTagName("element");
assertEquals(1, nodeList.getLength());
Element element = (Element) nodeList.item(0);
assertEquals("elementContent", element.getTextContent());
assertEquals("attributeValue", element.getAttribute("attribute"));
}
use of org.w3c.dom.Document in project camel by apache.
the class SpringXmlJsonDataFormatTest method testSomeOptionsToXML.
@Test
public void testSomeOptionsToXML() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.json");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockXML = getMockEndpoint("mock:xmlWithOptions");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object marshalled = template.requestBody("direct:unmarshalWithOptions", in);
Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
assertEquals("The XML document doesn't carry newRoot as the root name", "newRoot", document.getDocumentElement().getLocalName());
// with expandable properties, array elements are converted to XML as a
// sequence of repetitive XML elements with the local name equal to the
// JSON key
// for example: { number: [1,2,3] }, normally converted to:
// <number><e>1</e><e>2</e><e>3</e></number> (where e can be modified by
// setting elementName)
// would be converted to
// <number>1</number><number>2</number><number>3</number>, if number is
// set as an expandable property
assertEquals("The number of direct child elements of newRoot with tag d (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("d").getLength());
assertEquals("The number of direct child elements of newRoot with tag e (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("e").getLength());
mockXML.assertIsSatisfied();
}
use of org.w3c.dom.Document in project camel by apache.
the class XmlJsonDataFormatTest method testUnmarshalToXMLInlineOptions.
@Test
public void testUnmarshalToXMLInlineOptions() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.json");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockXML = getMockEndpoint("mock:xmlInlineOptions");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object marshalled = template.requestBody("direct:unmarshalInlineOptions", in);
Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
assertEquals("The XML document doesn't carry newRoot as the root name", "newRoot", document.getDocumentElement().getLocalName());
assertEquals("The number of direct child elements of newRoot with tag d (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("d").getLength());
assertEquals("The number of direct child elements of newRoot with tag e (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("e").getLength());
mockXML.assertIsSatisfied();
}
use of org.w3c.dom.Document in project camel by apache.
the class XmlJsonDataFormatTest method testMarshalXMLSources.
@Test
public void testMarshalXMLSources() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.xml");
DOMSource inDOM = context.getTypeConverter().convertTo(DOMSource.class, inStream);
inStream = getClass().getResourceAsStream("testMessage1.xml");
SAXSource inSAX = context.getTypeConverter().convertTo(SAXSource.class, inStream);
inStream = getClass().getResourceAsStream("testMessage1.xml");
Document inDocument = context.getTypeConverter().convertTo(Document.class, inStream);
// save the expected body of the message to set it later
Object expectedBody = template.requestBody("direct:marshal", inDOM);
MockEndpoint mockJSON = getMockEndpoint("mock:json");
// reset the mock endpoint to get rid of the previous message
mockJSON.reset();
// all three messages should arrive, should be of type byte[] and
// identical to one another
mockJSON.expectedMessageCount(3);
mockJSON.allMessages().body().isInstanceOf(byte[].class);
mockJSON.expectedBodiesReceived(Arrays.asList(expectedBody, expectedBody, expectedBody));
// start bombarding the route
Object json = template.requestBody("direct:marshal", inDOM);
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
assertEquals("JSONObject doesn't contain 7 keys", 7, obj.entrySet().size());
template.requestBody("direct:marshal", inSAX);
template.requestBody("direct:marshal", inDocument);
mockJSON.assertIsSatisfied();
}
use of org.w3c.dom.Document in project camel by apache.
the class XmlJsonOptionsTest method testSomeOptionsToXML.
@Test
public void testSomeOptionsToXML() throws Exception {
InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage1.json");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockXML = getMockEndpoint("mock:xml");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object marshalled = template.requestBody("direct:unmarshal", in);
Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
assertEquals("The XML document doesn't carry newRoot as the root name", "newRoot", document.getDocumentElement().getLocalName());
// with expandable properties, array elements are converted to XML as a
// sequence of repetitive XML elements with the local name equal to the
// JSON key
// for example: { number: [1,2,3] }, normally converted to:
// <number><e>1</e><e>2</e><e>3</e></number> (where e can be modified by
// setting elementName)
// would be converted to
// <number>1</number><number>2</number><number>3</number>, if number is
// set as an expandable property
assertEquals("The number of direct child elements of newRoot with tag d (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("d").getLength());
assertEquals("The number of direct child elements of newRoot with tag e (expandable property) is not 3", 3, document.getDocumentElement().getElementsByTagName("e").getLength());
mockXML.assertIsSatisfied();
}
Aggregations