Java Code Examples for org.apache.camel.component.mock.MockEndpoint

The following examples show how to use org.apache.camel.component.mock.MockEndpoint. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: vertx-camel-bridge   Source File: OutboundEndpointTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testWithMockWithASingleMessageHeadersNotCopied() throws Exception {
  MockEndpoint endpoint = (MockEndpoint) camel.getComponent("mock").createEndpoint("mock:foo");
  camel.addEndpoint("output", endpoint);

  bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel)
      .addOutboundMapping(fromVertx("test").toCamel("output").withoutHeadersCopy()));

  camel.start();
  BridgeHelper.startBlocking(bridge);

  vertx.eventBus().send("test", "hello", new DeliveryOptions().addHeader("key", "value"));

  await().atMost(DEFAULT_TIMEOUT).until(() -> !endpoint.getExchanges().isEmpty());
  endpoint.expectedBodiesReceived("hello");

  Exchange exchange = endpoint.getExchanges().get(0);
  assertThat(exchange.getIn().getBody()).isEqualTo("hello");
  assertThat(exchange.getIn().getHeaders()).doesNotContainKey("key");
}
 
Example 2
Source Project: syndesis   Source File: ODataReadRouteSplitResultsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReferenceODataRouteKeyPredicate() throws Exception {
    String resourcePath = "Airports";
    String keyPredicate = "KLAX";

    context = new SpringCamelContext(applicationContext);

    Connector odataConnector = createODataConnector(new PropertyBuilder<String>()
                                                        .property(SERVICE_URI, REF_SERVICE_URI)
                                                        .property(KEY_PREDICATE, keyPredicate));

    Step odataStep = createODataStep(odataConnector, resourcePath);
    Integration odataIntegration = createIntegration(odataStep, mockStep);

    RouteBuilder routes = newIntegrationRouteBuilder(odataIntegration);
    context.addRoutes(routes);
    MockEndpoint result = initMockEndpoint();
    result.setMinimumExpectedMessageCount(1);

    context.start();

    result.assertIsSatisfied();
    testResult(result, 0, REF_SERVER_AIRPORT_DATA_KLAX);
}
 
Example 3
Source Project: camelinaction2   Source File: ReuseErrorHandlerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOrderOk() throws Exception {
    // we do not expect any errors and therefore no messages in the dead letter queue
    getMockEndpoint("mock:dead").expectedMessageCount(0);

    // we expect the file to be converted to csv and routed to the 2nd route
    MockEndpoint file = getMockEndpoint("mock:file");
    file.expectedMessageCount(1);

    // we expect the 2nd route to complete
    MockEndpoint mock = getMockEndpoint("mock:queue.order");
    mock.expectedBodiesReceived("amount=1,name=Camel in Action,id=123,status=OK");

    template.sendBodyAndHeader("file://target/orders", "amount=1#name=Camel in Action", Exchange.FILE_NAME, "order.txt");

    assertMockEndpointsSatisfied();
}
 
Example 4
Source Project: syndesis   Source File: ODataReadRouteSplitResultsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEndSlashOnServiceUri() throws Exception {
    Connector odataConnector = createODataConnector(new PropertyBuilder<String>()
                                                            .property(SERVICE_URI, defaultTestServer.servicePlainUri() + FORWARD_SLASH));

    Step odataStep = createODataStep(odataConnector, defaultTestServer.resourcePath());
    Integration odataIntegration = createIntegration(odataStep, mockStep);

    RouteBuilder routes = newIntegrationRouteBuilder(odataIntegration);
    context.addRoutes(routes);

    MockEndpoint result = initMockEndpoint();
    result.setMinimumExpectedMessageCount(defaultTestServer.getResultCount());

    context.start();

    result.assertIsSatisfied();
    testResult(result, 0, TEST_SERVER_DATA_1);
    testResult(result, 1, TEST_SERVER_DATA_2);
    testResult(result, 2, TEST_SERVER_DATA_3);

    Olingo4Endpoint olingo4Endpoint = context.getEndpoint(OLINGO4_READ_FROM_ENDPOINT, Olingo4Endpoint.class);
    assertNotNull(olingo4Endpoint);
    String endpointServiceURI = olingo4Endpoint.getConfiguration().getServiceUri();
    assertEquals(defaultTestServer.servicePlainUri(), endpointServiceURI);
}
 
Example 5
Source Project: wildfly-camel   Source File: CouchDBIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCouchDBConsumer() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            fromF("couchdb:http://%s:5984/%s?username=%s&password=%s", TestUtils.getDockerHost(), COUCHDB_NAME, COUCHDB_USERNAME, COUCHDB_PASSWORD)
            .to("mock:result");
        }
    });

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedHeaderReceived(CouchDbConstants.HEADER_METHOD, "UPDATE");
    mockEndpoint.expectedMessageCount(1);

    camelctx.start();
    try {
        JsonElement element = new Gson().fromJson(getJSONString(), JsonElement.class);
        client.save(element);

        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 6
Source Project: wildfly-camel   Source File: StaxIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStaxJAXBSplit() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .split(stax(Record.class)).streaming()
            .to("mock:result");
        }
    });

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedMessageCount(5);
    mockEndpoint.allMessages().body().isInstanceOf(Record.class);

    camelctx.start();
    try (InputStream input = getClass().getResourceAsStream("/" + RECORDS_XML)) {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:start", input);
        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 7
@Test
public void testTransactedRollback() throws InterruptedException {
    AuditLogDao auditLogDao = getMandatoryBean(AuditLogDao.class, "auditLogDao");
    String message = "this message will explode";
    assertEquals(0, auditLogDao.getAuditCount(message));

    // the message does not proceed further down the route after the rollback statement
    MockEndpoint mockCompleted = getMockEndpoint("mock:out");
    mockCompleted.setExpectedMessageCount(0);

    // no exception is thrown despite the transaction rolling back
    template.sendBody("direct:transacted", message);

    assertMockEndpointsSatisfied();
    assertEquals(0, auditLogDao.getAuditCount(message)); // the insert was rolled back
}
 
Example 8
@Test
public void testMessageModificationAfterSigning() throws InterruptedException {
    MockEndpoint mockSigned = getMockEndpoint("mock:signed");
    mockSigned.whenAnyExchangeReceived(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            Message in = exchange.getIn();
            in.setBody(in.getBody(String.class) + "modified");
        }
    });

    MockEndpoint mockVerified = getMockEndpoint("mock:verified");
    mockVerified.setExpectedMessageCount(0);

    try {
        template.sendBody("direct:sign", "foo");
        fail();
    } catch (CamelExecutionException cex) {
        assertTrue(ExceptionUtils.getRootCause(cex) instanceof SignatureException);
        assertEquals("SignatureException: Cannot verify signature of exchange",
            ExceptionUtils.getRootCauseMessage(cex));
    }

    assertMockEndpointsSatisfied();
}
 
Example 9
@Test
public void sendInOnlyNoResponse() throws Exception {
    CamelContext camelctx = createCamelContext();

    MockEndpoint result = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    result.expectedMessageCount(0);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        HttpEntity mockHttpEntity = mock(HttpEntity.class);
        when(mockHttpEntity.getContent()).thenReturn(null);
        when(closeableHttpResponse.getEntity()).thenReturn(mockHttpEntity);
        when(closeableHttpResponse.getStatusLine())
                .thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, ""));

        result.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 10
Source Project: wildfly-camel   Source File: HL7NettyIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testHl7NettyEncodeDecode() throws Exception {
    CamelContext camelctx = camelContextRegistry.getCamelContext("hl7-context");
    Assert.assertNotNull("Expected hl7-context to not be null", camelctx);
    Assert.assertEquals(ServiceStatus.Started, camelctx.getStatus());

    String body = "MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4\r";

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedBodiesReceived(body);

    ProducerTemplate template = camelctx.createProducerTemplate();
    template.sendBody("direct:start", body);

    mockEndpoint.assertIsSatisfied(5000);
}
 
Example 11
@Test
public void testTransactedExceptionThrown() throws InterruptedException {
    String message = "this message will explode";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockCompleted = getMockEndpoint("mock:out");
    mockCompleted.setExpectedMessageCount(1);
    mockCompleted.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    try {
        template.sendBody("direct:transacted", message);
        fail();
    } catch (CamelExecutionException cee) {
        assertEquals("boom!", ExceptionUtils.getRootCause(cee).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(0, auditLogDao.getAuditCount(message)); // the insert was rolled back
}
 
Example 12
Source Project: camel-cookbook-examples   Source File: DlcTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDlqMultistepOriginal() throws Exception {
    final MockEndpoint mockResult = getMockEndpoint("mock:result");
    mockResult.expectedMessageCount(1);
    mockResult.expectedBodiesReceived("Foo");
    mockResult.message(0).exchangeProperty(Exchange.EXCEPTION_CAUGHT).isNull();
    mockResult.message(0).header("myHeader").isEqualTo("changed");

    final MockEndpoint mockError = getMockEndpoint("mock:error");
    mockError.expectedMessageCount(1);
    mockError.expectedBodiesReceived("KaBoom");
    mockError.message(0).exchangeProperty(Exchange.EXCEPTION_CAUGHT).isNotNull();
    mockError.message(0).exchangeProperty(Exchange.FAILURE_ROUTE_ID).isEqualTo("myFlakyRouteOriginal");
    mockError.message(0).header("myHeader").isEqualTo("multistep");

    template.sendBodyAndHeader("direct:multirouteOriginal", "Foo", "myHeader", "original");
    template.sendBodyAndHeader("direct:multirouteOriginal", "KaBoom", "myHeader", "original");

    assertMockEndpointsSatisfied();
}
 
Example 13
@Test
public void testNoElementsProcessedAfterException() throws Exception {
    String[] array = new String[]{"one", "two", "three"};

    MockEndpoint mockSplit = getMockEndpoint("mock:split");
    mockSplit.expectedMessageCount(1);
    mockSplit.expectedBodiesReceived("one");

    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.expectedMessageCount(0);

    try {
        template.sendBody("direct:in", array);
        fail("Exception not thrown");
    } catch (CamelExecutionException ex) {
        assertTrue(ex.getCause() instanceof CamelExchangeException);
        log.info(ex.getMessage());
        assertMockEndpointsSatisfied();
    }
}
 
Example 14
Source Project: camelinaction2   Source File: AggregateABCEagerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testABCEND() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    // we expect ABC in the published message
    // notice: Only 1 message is expected
    mock.expectedBodiesReceived("ABC");

    // send the first message
    template.sendBodyAndHeader("direct:start", "A", "myId", 1);
    // send the 2nd message with the same correlation key
    template.sendBodyAndHeader("direct:start", "B", "myId", 1);
    // the F message has another correlation key
    template.sendBodyAndHeader("direct:start", "F", "myId", 2);
    // now we have 3 messages with the same correlation key
    // and the Aggregator should publish the message
    template.sendBodyAndHeader("direct:start", "C", "myId", 1);
    // and now the END message to trigger completion
    template.sendBodyAndHeader("direct:start", "END", "myId", 1);

    assertMockEndpointsSatisfied();
}
 
Example 15
@Test
public void testABCInvalid() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    // we expect ABC in the published message
    // notice: Only 1 message is expected
    mock.expectedBodiesReceived("ABC");

    // send the first message
    template.sendBodyAndHeader("direct:start", "A", "myId", 1);
    // send the 2nd message with the same correlation key
    template.sendBodyAndHeader("direct:start", "B", "myId", 1);
    // the F message has another correlation key
    template.sendBodyAndHeader("direct:start", "F", "myId", 2);
    // this message has no correlation key so its ignored
    template.sendBody("direct:start", "C");
    // so we resend the message this time with a correlation key
    template.sendBodyAndHeader("direct:start", "C", "myId", 1);

    assertMockEndpointsSatisfied();
}
 
Example 16
Source Project: camelinaction   Source File: FailoverLoadBalancerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadBalancer() throws Exception {
    // A should get the 1st, 3rd and 4th message
    MockEndpoint a = getMockEndpoint("mock:a");
    a.expectedBodiesReceived("Hello", "Cool", "Bye");

    // B should get the 2nd
    MockEndpoint b = getMockEndpoint("mock:b");
    b.expectedBodiesReceived("Kaboom");

    // send in 4 messages
    template.sendBody("direct:start", "Hello");
    template.sendBody("direct:start", "Kaboom");
    template.sendBody("direct:start", "Cool");
    template.sendBody("direct:start", "Bye");

    assertMockEndpointsSatisfied();
}
 
Example 17
Source Project: camelinaction2   Source File: RouteScopeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOrderActiveMQ() throws Exception {
    // we expect the file to be converted to csv and routed to the 2nd route
    MockEndpoint file = getMockEndpoint("mock:file");
    file.expectedMessageCount(1);

    // we do not expect the 2nd route to complete
    MockEndpoint mock = getMockEndpoint("mock:queue.order");
    mock.expectedMessageCount(0);

    template.sendBodyAndHeader("file://target/orders", "amount=1#name=ActiveMQ in Action", Exchange.FILE_NAME, "order.txt");

    // wait 10 seconds to let this test run
    Thread.sleep(10000);

    assertMockEndpointsSatisfied();
}
 
Example 18
@Test
public void testSplitStopOnException() throws Exception {
    MockEndpoint split = getMockEndpoint("mock:split");
    // we expect 1 messages to be split since the 2nd message should fail
    split.expectedBodiesReceived("Camel rocks");

    // and no combined aggregated message since we stop on exception
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(0);

    // now send a message with an unknown letter (F) which forces an exception to occur
    try {
        template.sendBody("direct:start", "A,F,C");
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
        IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
        assertEquals("Key not a known word F", iae.getMessage());
    }

    assertMockEndpointsSatisfied();
}
 
Example 19
@Test
public void testTransactedExceptionThrown() throws InterruptedException {
    AuditLogDao auditLogDao = getMandatoryBean(AuditLogDao.class, "auditLogDao");
    String message = "this message will explode";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockCompleted = getMockEndpoint("mock:out");
    mockCompleted.setExpectedMessageCount(1);
    mockCompleted.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    try {
        template.sendBody("direct:transacted", message);
        fail();
    } catch (CamelExecutionException cee) {
        assertEquals("boom!", ExceptionUtils.getRootCause(cee).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(0, auditLogDao.getAuditCount(message)); // the insert was rolled back
}
 
Example 20
@Override
public void setUp() throws Exception {
    super.setUp();
    camelContext.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("flowable:camelProcess");

            from("flowable:camelProcess:serviceTask1").setBody().exchangeProperty("var1").to("mock:service1").setProperty("var2").constant("var2").setBody()
                            .exchangeProperties();

            from("flowable:camelProcess:serviceTask2?copyVariablesToBodyAsMap=true").to("mock:service2");

            from("direct:receive").to("flowable:camelProcess:receive");
        }
    });

    service1 = (MockEndpoint) camelContext.getEndpoint("mock:service1");
    service1.reset();
    service2 = (MockEndpoint) camelContext.getEndpoint("mock:service2");
    service2.reset();
}
 
Example 21
Source Project: camelinaction   Source File: SpringTopicLoadBalancerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadBalancer() throws Exception {
    // both mocks should get all the messages as its topic strategy
    MockEndpoint a = getMockEndpoint("mock:a");
    a.expectedMessageCount(4);

    MockEndpoint b = getMockEndpoint("mock:b");
    b.expectedMessageCount(4);

    // send in 4 messages
    template.sendBody("direct:start", "Hello");
    template.sendBody("direct:start", "Camel rocks");
    template.sendBody("direct:start", "Cool");
    template.sendBody("direct:start", "Bye");

    assertMockEndpointsSatisfied();
}
 
Example 22
Source Project: camelinaction   Source File: TopicLoadBalancerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadBalancer() throws Exception {
    // both mocks should get all the messages as its topic strategy
    MockEndpoint a = getMockEndpoint("mock:a");
    a.expectedMessageCount(4);

    MockEndpoint b = getMockEndpoint("mock:b");
    b.expectedMessageCount(4);

    // send in 4 messages
    template.sendBody("direct:start", "Hello");
    template.sendBody("direct:start", "Camel rocks");
    template.sendBody("direct:start", "Cool");
    template.sendBody("direct:start", "Bye");

    assertMockEndpointsSatisfied();
}
 
Example 23
Source Project: wildfly-camel   Source File: WordpressIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPostSingleRequest() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            configureComponent(getContext());

            from("wordpress:post?id=114913").to("mock:resultSingle");

        }
    });

    camelctx.start();
    try {
        MockEndpoint mock = camelctx.getEndpoint("mock:resultSingle", MockEndpoint.class);
        mock.expectedMinimumMessageCount(1);
        mock.allMessages().body().isInstanceOf(Post.class);

        mock.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 24
Source Project: syndesis   Source File: ActivityLoggingWithSplitTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLoggingWithSuccessStep() throws Exception {
    final MockEndpoint result = context.getEndpoint("mock:end", MockEndpoint.class);
    result.expectedBodiesReceived("Hello", "World");
    context.createProducerTemplate().sendBody("direct:start", new String[] {"Hello", "World"});
    result.assertIsSatisfied();

    // There should be 1 exchanges logged.
    assertEquals(1, findExchangesLogged().size());
    // There should be 1 "status":"begin"
    assertEquals(1, findActivityEvents(x -> "begin".equals(x.status)).size());
    // There should be 1 "status":"done"
    assertEquals(1, findActivityEvents(x -> "done".equals(x.status)).size());
    // There should be no failed flag on activity with "status":"done"
    assertEquals("false", findActivityEvent(x -> "done".equals(x.status)).failed);

    // There should be log activities
    assertEquals(2, findActivityEvents(x -> "log".equals(x.step) && ObjectHelper.isEmpty(x.duration)).size());
    assertEquals(2, findActivityEvents(x -> "log".equals(x.step) && ObjectHelper.isNotEmpty(x.duration)).size());
    assertEquals("hi", findActivityEvent(x -> "log".equals(x.step)).message);

    // There should be step activity tracking events
    assertEquals(6, findActivityEvents(x -> ObjectHelper.isNotEmpty(x.duration)).size());
}
 
Example 25
Source Project: syndesis   Source File: ODataReadRouteNoSplitResultsTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Despite split being set to false, the existence of a key predicate
 * forces the split since a predicate demands only 1 result which is
 * pointless putting into an array.
 */
@Test
public void testODataRouteWithKeyPredicate() throws Exception {
    String keyPredicate = "1";
    Connector odataConnector = createODataConnector(new PropertyBuilder<String>()
                                                        .property(SERVICE_URI, defaultTestServer.servicePlainUri())
                                                        .property(KEY_PREDICATE, keyPredicate));

    Step odataStep = createODataStep(odataConnector, defaultTestServer.resourcePath());
    Integration odataIntegration = createIntegration(odataStep, mockStep);

    RouteBuilder routes = newIntegrationRouteBuilder(odataIntegration);
    context.addRoutes(routes);
    MockEndpoint result = initMockEndpoint();
    result.setMinimumExpectedMessageCount(1);

    context.start();

    result.assertIsSatisfied();

    //
    // We expect the result object to be a single json object & not an array
    //
    testResult(result, 0, TEST_SERVER_DATA_1);
}
 
Example 26
@Test
public void testOverrideUsingConstantValue() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:out", MockEndpoint.class);
        mockEndpoint.expectedMessageCount(1);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:in-3", null, HEADER_COUNTER_DECREMENT, 7.0D);

        Assert.assertEquals(417.0D, metricsRegistry.find("C").counter().count(), 0.01D);
        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 27
@Test
public void testSimpleExpressionReferenceToList() throws Exception {
    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.expectedMessageCount(3);
    mockOut.expectedBodiesReceived("one", "two", "three");

    List<String> list = new LinkedList<String>();
    list.add("one");
    list.add("two");
    list.add("three");
    ListWrapper wrapper = new ListWrapper();
    wrapper.setWrapped(list);

    template.sendBody("direct:in", wrapper);

    assertMockEndpointsSatisfied();
}
 
Example 28
@Test
public void testSplitStopOnException() throws Exception {
    MockEndpoint split = getMockEndpoint("mock:split");
    // we expect 1 messages to be split since the 2nd message should fail
    split.expectedBodiesReceived("Camel rocks");

    // and no combined aggregated message since we stop on exception
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(0);

    // now send a message with an unknown letter (F) which forces an exception to occur
    try {
        template.sendBody("direct:start", "A,F,C");
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
        IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause());
        assertEquals("Key not a known word F", iae.getMessage());
    }

    assertMockEndpointsSatisfied();
}
 
Example 29
Source Project: camelinaction   Source File: SpringRouteScopeTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOrderActiveMQ() throws Exception {
    // we expect the file to be converted to csv and routed to the 2nd route
    MockEndpoint file = getMockEndpoint("mock:file");
    file.expectedMessageCount(1);

    // we do not expect the 2nd route to complete
    MockEndpoint mock = getMockEndpoint("mock:queue.order");
    mock.expectedMessageCount(0);

    template.sendBodyAndHeader("file://target/orders", "amount=1#name=ActiveMQ in Action", Exchange.FILE_NAME, "order.txt");

    // wait 10 seconds to let this test run
    Thread.sleep(10000);

    assertMockEndpointsSatisfied();
}
 
Example 30
@Test
public void testSplittingInParallel() throws InterruptedException {
    List<String> messageFragments = new ArrayList<String>();
    int fragmentCount = 50;
    for (int i = 0; i < fragmentCount; i++) {
        messageFragments.add("fragment" + i);
    }

    MockEndpoint mockSplit = getMockEndpoint("mock:split");
    mockSplit.setExpectedMessageCount(fragmentCount - 1);

    ArrayList<String> expectedFragments = new ArrayList<String>(messageFragments);
    int indexDelayed = 20;
    expectedFragments.remove(indexDelayed);
    mockSplit.expectedBodiesReceivedInAnyOrder(expectedFragments);

    MockEndpoint mockDelayed = getMockEndpoint("mock:delayed");
    mockDelayed.setExpectedMessageCount(1);

    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.setExpectedMessageCount(1);


    template.sendBody("direct:in", messageFragments);
    assertMockEndpointsSatisfied();
}