use of net.sf.json.JSONObject in project camel by apache.
the class SpringXmlJsonDataFormatTest method testMarshalAndUnmarshal.
@Test
public void testMarshalAndUnmarshal() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:json");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
MockEndpoint mockXML = getMockEndpoint("mock:xml");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object json = template.requestBody("direct:marshal", in);
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.sendBody("direct:unmarshal", jsonString);
mockJSON.assertIsSatisfied();
mockXML.assertIsSatisfied();
}
use of net.sf.json.JSONObject in project camel by apache.
the class XmlJsonDataFormatTest method testMarshalAndUnmarshalInline.
@Test
public void testMarshalAndUnmarshalInline() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage1.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:jsonInline");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
MockEndpoint mockXML = getMockEndpoint("mock:xmlInline");
mockXML.expectedMessageCount(1);
mockXML.message(0).body().isInstanceOf(String.class);
Object json = template.requestBody("direct:marshalInline", in);
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.sendBody("direct:unmarshalInline", jsonString);
mockJSON.assertIsSatisfied();
mockXML.assertIsSatisfied();
}
use of net.sf.json.JSONObject 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 net.sf.json.JSONObject in project camel by apache.
the class XmlJsonDataFormatTest method testNamespacesDroppedInlineWithOptions.
@Test
public void testNamespacesDroppedInlineWithOptions() throws Exception {
InputStream inStream = getClass().getResourceAsStream("testMessage2-namespaces.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:jsonInlineOptions");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
Object json = template.requestBody("direct:marshalInlineOptions", in);
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
assertEquals("JSON must contain 1 top-level element", 1, obj.entrySet().size());
assertTrue("Top-level element must be named root", obj.has("root"));
// a namespace prefix exists
for (Object key : obj.getJSONObject("root").keySet()) {
assertFalse("A key contains a colon", ((String) key).contains(":"));
}
mockJSON.assertIsSatisfied();
}
use of net.sf.json.JSONObject in project camel by apache.
the class XmlJsonOptionsTest method testTypeHintsToJSON.
@Test
public void testTypeHintsToJSON() throws Exception {
InputStream inStream = getClass().getClassLoader().getResourceAsStream("org/apache/camel/dataformat/xmljson/testMessage5-typeHints.xml");
String in = context.getTypeConverter().convertTo(String.class, inStream);
MockEndpoint mockJSON = getMockEndpoint("mock:jsonTypeHints");
mockJSON.expectedMessageCount(1);
mockJSON.message(0).body().isInstanceOf(byte[].class);
Object json = template.requestBody("direct:marshalTypeHints", in);
String jsonString = context.getTypeConverter().convertTo(String.class, json);
JSONObject obj = (JSONObject) JSONSerializer.toJSON(jsonString);
assertEquals("root.a must be number", Integer.valueOf(1), obj.getJSONObject("root").get("a"));
assertEquals("root.b must be boolean", Boolean.TRUE, obj.getJSONObject("root").get("b"));
mockJSON.assertIsSatisfied();
}
Aggregations