Search in sources :

Example 31 with JSONObject

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();
}
Also used : JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 32 with JSONObject

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();
}
Also used : JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 33 with JSONObject

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();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SAXSource(javax.xml.transform.sax.SAXSource) JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 34 with JSONObject

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();
}
Also used : JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 35 with JSONObject

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();
}
Also used : JSONObject(net.sf.json.JSONObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Aggregations

JSONObject (net.sf.json.JSONObject)493 Test (org.junit.Test)99 JSONArray (net.sf.json.JSONArray)94 IOException (java.io.IOException)49 HashMap (java.util.HashMap)48 ArrayList (java.util.ArrayList)36 JSON (net.sf.json.JSON)26 PrintWriter (java.io.PrintWriter)25 Map (java.util.Map)23 File (java.io.File)21 InputStream (java.io.InputStream)18 URISyntaxException (java.net.URISyntaxException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 JsonConfig (net.sf.json.JsonConfig)14 FreeStyleBuild (hudson.model.FreeStyleBuild)13 URI (java.net.URI)13 URL (java.net.URL)13 JSONException (net.sf.json.JSONException)13 Context (org.zaproxy.zap.model.Context)12 Transactional (org.springframework.transaction.annotation.Transactional)11