Package org.apache.camel.component.mock

Examples of org.apache.camel.component.mock.MockEndpoint


    }
   
    @Test
    public void testCustomTailTrackLocation() throws Exception {
        assertEquals(0, cappedTestCollection.count());
        final MockEndpoint mock = getMockEndpoint("mock:test");
       
        // get the custom tracking collection and drop it (tailTrackDb=einstein&tailTrackCollection=curie&tailTrackField=newton)
        DBCollection trackingCol = mongo.getDB("einstein").getCollection("curie");
        trackingCol.drop();
        trackingCol = mongo.getDB("einstein").getCollection("curie");
       
        // create a capped collection with max = 1000
        cappedTestCollection = db.createCollection(cappedTestCollectionName,
                BasicDBObjectBuilder.start().add("capped", true).add("size", 1000000000).add("max", 1000).get());
       
        addTestRoutes();
        context.startRoute("tailableCursorConsumer3");
       
        mock.expectedMessageCount(300);
        // pump 300 records
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 300; i++) {
                    cappedTestCollection.insert(BasicDBObjectBuilder.start("increasing", i).add("string", "value" + i).get(), WriteConcern.SAFE)
                }
            }
        });
       
        // start the data pumping
        t.start();
        // before we continue wait for the data pump to end
        t.join();
        mock.assertIsSatisfied();
        mock.reset();
       
        // stop the route to ensure that our lastVal is persisted, and check it
        context.stopRoute("tailableCursorConsumer3");
        // ensure that the persisted lastVal is 300, newton is the name of the trackingField we are using
        assertEquals(300, trackingCol.findOne(new BasicDBObject("persistentId", "darwin")).get("newton"));
        context.startRoute("tailableCursorConsumer3");
       
        // expect 300 messages and not 600
        mock.expectedMessageCount(300);
        // pump 300 records
        t = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 301; i <= 600; i++) {
                    cappedTestCollection.insert(BasicDBObjectBuilder.start("increasing", i).add("string", "value" + i).get(), WriteConcern.SAFE)
                }
            }
        });
        // start the data pumping
        t.start();
        // before we continue wait for the data pump to end
        t.join();
        mock.assertIsSatisfied();
        // check that the first received body contains increasing=301 and not increasing=1, i.e. it's not starting from the top
        Object firstBody = mock.getExchanges().get(0).getIn().getBody();
        assertTrue(firstBody instanceof DBObject);
        assertEquals(301, ((DBObject) firstBody).get("increasing"));
        // check that the persisted lastVal after stopping the route is 600, newton is the name of the trackingField we are using
        context.stopRoute("tailableCursorConsumer3");
        assertEquals(600, trackingCol.findOne(new BasicDBObject("persistentId", "darwin")).get("newton"));
View Full Code Here


    @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();
    }
View Full Code Here

    public void testUnmarshalJSONObject() throws Exception {
        InputStream inStream = getClass().getResourceAsStream("testMessage1.json");
        String in = context.getTypeConverter().convertTo(String.class, inStream);
        JSON json = JSONSerializer.toJSON(in);

        MockEndpoint mockXML = getMockEndpoint("mock:xml");
        mockXML.expectedMessageCount(1);
        mockXML.message(0).body().isInstanceOf(String.class);

        Object marshalled = template.requestBody("direct:unmarshal", json);
        Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
        assertEquals("The XML document has an unexpected root node", "o", document.getDocumentElement().getLocalName());

        mockXML.assertIsSatisfied();
    }
View Full Code Here

        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();
    }
View Full Code Here

    @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();
    }
View Full Code Here

    @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"));
        // check that no child of the top-level element has a colon in its key,
        // which would denote that
        // a namespace prefix exists
        for (Object key : obj.getJSONObject("root").keySet()) {
            assertFalse("A key contains a colon", ((String) key).contains(":"));
        }

        mockJSON.assertIsSatisfied();
    }
View Full Code Here

    @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();
    }
View Full Code Here

        mockXML.assertIsSatisfied();
    }

    @Test
    public void testJsonArraysToXml() throws Exception {
        MockEndpoint mockXML = getMockEndpoint("mock:xmlInlineOptionsArray");
        mockXML.expectedMessageCount(1);
        mockXML.message(0).body().isInstanceOf(String.class);

        Object marshalled = template.requestBody("direct:unmarshalInlineOptionsArray", "[1, 2, 3, 4]");
        Document document = context.getTypeConverter().convertTo(Document.class, marshalled);
        assertEquals("There should be exactly 4 XML elements with tag 'el' (each array element)", 4, document.getDocumentElement().getElementsByTagName("el").getLength());
        assertEquals("The document root should be named 'ar' (the array root)", "ar", document.getDocumentElement().getLocalName());
        mockXML.assertIsSatisfied();
    }
View Full Code Here

        mockXML.assertIsSatisfied();
    }

    @Test
    public void testXmlArraysToJson() throws Exception {
        MockEndpoint mockJSON = getMockEndpoint("mock:jsonInlineOptionsArray");
        mockJSON.expectedMessageCount(1);
        mockJSON.message(0).body().isInstanceOf(byte[].class);

        Object json = template.requestBody("direct:marshalInlineOptionsArray", "<ar><el>1</el><el>2</el><el>3</el><el>4</el></ar>");
        String jsonString = context.getTypeConverter().convertTo(String.class, json);
        JSONArray array = (JSONArray) JSONSerializer.toJSON(jsonString);
        assertTrue("Expected a JSON array with string elements: 1, 2, 3, 4", array.containsAll(Arrays.asList("1", "2", "3", "4")));
        mockJSON.assertIsSatisfied();
    }
View Full Code Here

    @Test
    public void testMalformedXML() throws Exception {
        String in = "<noRoot>abc</noRoot><noRoot>abc</noRoot>";

        MockEndpoint mockJSON = getMockEndpoint("mock:json");
        mockJSON.expectedMessageCount(0);

        MockEndpoint mockException = getMockEndpoint("mock:exception");
        mockException.expectedMessageCount(1);

        try {
            template.requestBody("direct:marshal", in);
            fail("Exception expected");
        } catch (CamelExecutionException e) {
            assertEquals("JSONException expected", JSONException.class, e.getCause().getClass());
        }

        List<Exchange> exchs = mockException.getExchanges();
        assertEquals("Only one exchange was expected in mock:exception", 1, exchs.size());

        Exception e = (Exception) exchs.get(0).getProperty(Exchange.EXCEPTION_CAUGHT);
        assertNotNull("Exception expected", e);
        assertEquals("JSONException expected", JSONException.class, e.getClass());
View Full Code Here

TOP

Related Classes of org.apache.camel.component.mock.MockEndpoint

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.