Java Code Examples for org.apache.camel.component.mock.MockEndpoint#whenAnyExchangeReceived()

The following examples show how to use org.apache.camel.component.mock.MockEndpoint#whenAnyExchangeReceived() . 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 File: TransactionPolicyNestedRequiresNewSpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureMock1() throws InterruptedException {
    AuditLogDao auditLogDao = getMandatoryBean(AuditLogDao.class, "auditLogDao");
    MessageDao messageDao = getMandatoryBean(MessageDao.class, "messageDao");

    String message = "ping";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut1 = getMockEndpoint("mock:out1");
    mockOut1.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    MockEndpoint mockOut2 = getMockEndpoint("mock:out2");
    mockOut2.setExpectedMessageCount(1);
    mockOut2.message(0).body().isEqualTo(message);

    try {
        template.sendBody("direct:policies", message);
        fail();
    } catch (Exception e) {
        assertEquals("boom!", ExceptionUtils.getRootCause(e).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(1, auditLogDao.getAuditCount(message));
    assertEquals(0, messageDao.getMessageCount(message));
}
 
Example 2
Source File: JmsTransactionRequestReplyTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactedExceptionThrown() throws InterruptedException {
    String message = "this message will explode";

    // the back-end executed; it's status will be unaffected by the rollback
    MockEndpoint mockBackEndReply = getMockEndpoint("mock:backEndReply");
    mockBackEndReply.setExpectedMessageCount(1);

    String backendReply = "Backend processed: this message will explode";
    mockBackEndReply.message(0).body().isEqualTo(backendReply);

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

    template.sendBody("jms:inbound", message);

    // when transacted, ActiveMQ receives a failed signal when the exception is thrown
    // the message is placed into a dead letter queue
    final String dlqMessage = consumer.receiveBody("jms:ActiveMQ.DLQ", MAX_WAIT_TIME, String.class);
    assertNotNull("Timed out waiting for DLQ message", dlqMessage);
    log.info("dlq message = {}", dlqMessage);
    assertEquals(message, dlqMessage);
    assertNull(consumer.receiveBody("jms:auditQueue", MAX_WAIT_TIME, String.class));
}
 
Example 3
Source File: JmsTransactionTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactedExceptionThrown() throws InterruptedException {
    String message = "this message will explode";

    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    // even though the route throws an exception, we don't have to deal with it here as we
    // don't send the message to the route directly, but to the broker, which acts as a middleman.
    template.sendBody("jms:inbound", message);

    // when transacted, ActiveMQ receives a failed signal when the exception is thrown
    // the message is placed into a dead letter queue
    assertEquals(message, consumer.receiveBody("jms:ActiveMQ.DLQ", MAX_WAIT_TIME, String.class));

    // the sending operation is performed in the same transaction, so it is rolled back
    assertNull(consumer.receiveBody("jms:outbound", MAX_WAIT_TIME, String.class));
}
 
Example 4
Source File: SignaturesSpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: DatabaseTransactionSpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonTransactedExceptionThrown() 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:nonTransacted", message);
        fail();
    } catch (CamelExecutionException cee) {
        assertEquals("boom!", ExceptionUtils.getRootCause(cee).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(1, auditLogDao.getAuditCount(message)); // the insert was not rolled back
}
 
Example 6
Source File: TransactionPolicyTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureMock1() throws InterruptedException {
    String message = "ping";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut1 = getMockEndpoint("mock:out1");
    mockOut1.whenAnyExchangeReceived(new ExceptionThrowingProcessor());
    mockOut1.message(0).body().isEqualTo(message);

    MockEndpoint mockOut2 = getMockEndpoint("mock:out2");
    mockOut2.setExpectedMessageCount(0);

    try {
        template.sendBody("direct:policies", message);
        fail();
    } catch (Exception e) {
        assertEquals("boom!", ExceptionUtils.getRootCause(e).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(0, auditLogDao.getAuditCount(message));
    assertEquals(0, messageDao.getMessageCount(message));
}
 
Example 7
Source File: JmsTransactionEndpointSpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactedExceptionThrown() throws InterruptedException {
    String message = "this message will explode";

    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    // even though the route throws an exception, we don't have to deal with it here as we
    // don't send the message to the route directly, but to the broker, which acts as a middleman.
    template.sendBody("jms:inbound", message);

    // when transacted, ActiveMQ receives a failed signal when the exception is thrown
    // the message is placed into a dead letter queue
    assertEquals(message, consumer.receiveBody("jms:ActiveMQ.DLQ", MAX_WAIT_TIME, String.class));

    // the sending operation is performed in the same transaction, so it is rolled back
    assertNull(consumer.receiveBody("jms:outbound", MAX_WAIT_TIME, String.class));
}
 
Example 8
Source File: TransactionPolicyNestedSpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureMock1() throws InterruptedException {
    AuditLogDao auditLogDao = getMandatoryBean(AuditLogDao.class, "auditLogDao");
    MessageDao messageDao = getMandatoryBean(MessageDao.class, "messageDao");

    String message = "ping";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut1 = getMockEndpoint("mock:out1");
    mockOut1.whenAnyExchangeReceived(new ExceptionThrowingProcessor());
    mockOut1.message(0).body().isEqualTo(message);

    MockEndpoint mockOut2 = getMockEndpoint("mock:out2");
    mockOut2.setExpectedMessageCount(1);

    try {
        template.sendBody("direct:policies", message);
        fail();
    } catch (Exception e) {
        assertEquals("boom!", ExceptionUtils.getRootCause(e).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(1, auditLogDao.getAuditCount(message));
    assertEquals(0, messageDao.getMessageCount(message));
}
 
Example 9
Source File: SignaturesTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageSigningMismatchedKeys() throws InterruptedException {
    MockEndpoint mockVerified = getMockEndpoint("mock:verified");
    mockVerified.setExpectedMessageCount(0);

    MockEndpoint mockSigned = getMockEndpoint("mock:signed");
    mockSigned.whenAnyExchangeReceived(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            // let's override the key used by the verifying endpoint
            exchange.getIn().setHeader(DigitalSignatureConstants.KEYSTORE_ALIAS, "system_b");
        }
    });

    try {
        template.sendBody("direct:sign", "foo");
        fail();
    } catch (CamelExecutionException cex) {
        assertTrue(ExceptionUtils.getRootCause(cex) instanceof SignatureException);
        String rootCauseMessage = ExceptionUtils.getRootCauseMessage(cex);
        assertEquals("SignatureException: Cannot verify signature of exchange", rootCauseMessage);
    }
}
 
Example 10
Source File: JmsTransactionRequestReplySpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactedExceptionThrown() throws InterruptedException {
    String message = "this message will explode";

    // the back-end executed; it's status will be unaffected by the rollback
    MockEndpoint mockBackEndReply = getMockEndpoint("mock:backEndReply");
    mockBackEndReply.setExpectedMessageCount(1);

    String backendReply = "Backend processed: this message will explode";
    mockBackEndReply.message(0).body().isEqualTo(backendReply);

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

    template.sendBody("jms:inbound", message);

    // when transacted, ActiveMQ receives a failed signal when the exception is thrown
    // the message is placed into a dead letter queue
    final String dlqMessage = consumer.receiveBody("jms:ActiveMQ.DLQ", MAX_WAIT_TIME, String.class);
    assertNotNull("Timed out waiting for DLQ message", dlqMessage);
    log.info("dlq message = {}", dlqMessage);
    assertEquals(message, dlqMessage);
    assertNull(consumer.receiveBody("jms:auditQueue", MAX_WAIT_TIME, String.class));
}
 
Example 11
Source File: TransactionPolicyNestedTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureMock2() throws InterruptedException {
    String message = "ping";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut1 = getMockEndpoint("mock:out1");
    mockOut1.setExpectedMessageCount(0);

    MockEndpoint mockOut2 = getMockEndpoint("mock:out2");
    mockOut2.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    try {
        template.sendBody("direct:policies", message);
        fail();
    } catch (Exception e) {
        assertEquals("boom!", ExceptionUtils.getRootCause(e).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(1, auditLogDao.getAuditCount(message));
    assertEquals(0, messageDao.getMessageCount(message));
}
 
Example 12
Source File: TransactionPolicyNestedTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureMock1() throws InterruptedException {
    String message = "ping";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut1 = getMockEndpoint("mock:out1");
    mockOut1.whenAnyExchangeReceived(new ExceptionThrowingProcessor());
    mockOut1.message(0).body().isEqualTo(message);

    MockEndpoint mockOut2 = getMockEndpoint("mock:out2");
    mockOut2.setExpectedMessageCount(1);

    try {
        template.sendBody("direct:policies", message);
        fail();
    } catch (Exception e) {
        assertEquals("boom!", ExceptionUtils.getRootCause(e).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(1, auditLogDao.getAuditCount(message));
    assertEquals(0, messageDao.getMessageCount(message));
}
 
Example 13
Source File: IdempotentConsumerInTransactionTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@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.sendBodyAndHeader("direct:transacted", message, "messageId", "foo");
        fail();
    } catch (CamelExecutionException cee) {
        assertEquals("boom!", ExceptionUtils.getRootCause(cee).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(0, auditLogDao.getAuditCount(message)); // the insert was rolled back

    // even though the transaction rolled back, the repository should still contain an entry for this messageId
    assertTrue(idempotentRepository.contains("foo"));
}
 
Example 14
Source File: TransactionPolicySpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureMock1() throws InterruptedException {
    AuditLogDao auditLogDao = getMandatoryBean(AuditLogDao.class, "auditLogDao");
    MessageDao messageDao = getMandatoryBean(MessageDao.class, "messageDao");

    String message = "ping";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut1 = getMockEndpoint("mock:out1");
    mockOut1.whenAnyExchangeReceived(new ExceptionThrowingProcessor());
    mockOut1.message(0).body().isEqualTo(message);

    MockEndpoint mockOut2 = getMockEndpoint("mock:out2");
    mockOut2.setExpectedMessageCount(0);

    try {
        template.sendBody("direct:policies", message);
        fail();
    } catch (Exception e) {
        assertEquals("boom!", ExceptionUtils.getRootCause(e).getMessage());
    }

    assertMockEndpointsSatisfied();
    assertEquals(0, auditLogDao.getAuditCount(message));
    assertEquals(0, messageDao.getMessageCount(message));
}
 
Example 15
Source File: JmsDatabaseTransactionSpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactedExceptionThrown() throws InterruptedException {
    AuditLogDao auditLogDao = getMandatoryBean(AuditLogDao.class, "auditLogDao");

    String message = "this message will explode";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    // even though the route throws an exception, we don't have to deal with it here as we
    // don't send the message to the route directly, but to the broker, which acts as a middleman.
    template.sendBody("jms:inbound", message);

    // the consumption of the message is non-transacted
    assertNull(consumer.receiveBody("jms:ActiveMQ.DLQ", MAX_WAIT_TIME, String.class));

    // the send operation is performed while a database transaction is going on, so it is rolled back
    // on exception
    assertNull(consumer.receiveBody("jms:outbound", MAX_WAIT_TIME, String.class));

    assertEquals(0, auditLogDao.getAuditCount(message)); // the insert is rolled back
}
 
Example 16
Source File: XATransactionSpringTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactedRolledBack() throws InterruptedException {
    AuditLogDao auditLogDao = getMandatoryBean(AuditLogDao.class, "auditLogDao");

    String message = "this message will explode";
    assertEquals(0, auditLogDao.getAuditCount(message));

    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.whenAnyExchangeReceived(new ExceptionThrowingProcessor());

    // even though the route throws an exception, we don't have to deal with it here as we
    // don't send the message to the route directly, but to the broker, which acts as a middleman.
    template.sendBody("nonTxJms:inbound", message);

    // the consumption of the message is transacted, so a message should end up in the DLQ
    assertEquals(message, consumer.receiveBody("jms:ActiveMQ.DLQ", MAX_WAIT_TIME, String.class));

    // the send operation is performed while a database transaction is going on, so it is rolled back
    // on exception
    assertNull(consumer.receiveBody("jms:outbound", MAX_WAIT_TIME, String.class));

    assertEquals(0, auditLogDao.getAuditCount(message)); // the insert is rolled back
}
 
Example 17
Source File: MirandaTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMiranda() throws Exception {
    context.setTracing(true);

    MockEndpoint mock = getMockEndpoint("mock:miranda");
    mock.expectedBodiesReceived("ID=123");
    mock.whenAnyExchangeReceived(new Processor() {
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("ID=123,STATUS=IN PROGRESS");
        }
    });

    String out = template.requestBody("http://localhost:9080/service/order?id=123", null, String.class);
    assertEquals("IN PROGRESS", out);

    assertMockEndpointsSatisfied();
}
 
Example 18
Source File: SignaturesTest.java    From camel-cookbook-examples with Apache License 2.0 6 votes vote down vote up
@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 19
Source File: GangliaIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void sendDefaultConfigurationShouldSucceed() throws Exception {

    CamelContext camelctx = assertSingleCamelContext();

    ProducerTemplate template = camelctx.createProducerTemplate();
    Integer port = template.requestBody("direct:getPort", null, Integer.class);

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.setMinimumExpectedMessageCount(0);
    mockEndpoint.setAssertPeriod(100L);
    mockEndpoint.whenAnyExchangeReceived(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            Ganglia_metadata_msg metadataMessage = exchange.getIn().getBody(Ganglia_metadata_msg.class);
            if (metadataMessage != null) {
                Assert.assertEquals(DEFAULT_METRIC_NAME, metadataMessage.gfull.metric.name);
                Assert.assertEquals(DEFAULT_TYPE.getGangliaType(), metadataMessage.gfull.metric.type);
                Assert.assertEquals(DEFAULT_SLOPE.getGangliaSlope(), metadataMessage.gfull.metric.slope);
                Assert.assertEquals(DEFAULT_UNITS, metadataMessage.gfull.metric.units);
                Assert.assertEquals(DEFAULT_TMAX, metadataMessage.gfull.metric.tmax);
                Assert.assertEquals(DEFAULT_DMAX, metadataMessage.gfull.metric.dmax);
            } else {
                Ganglia_value_msg valueMessage = exchange.getIn().getBody(Ganglia_value_msg.class);
                if (valueMessage != null) {
                    Assert.assertEquals("28.0", valueMessage.gstr.str);
                    Assert.assertEquals("%s", valueMessage.gstr.fmt);
                } else {
                    Assert.fail("Mock endpoint should only receive non-null metadata or value messages");
                }
            }
        }
    });

    template.sendBody(String.format("ganglia:localhost:%d?mode=UNICAST", port), "28.0");

    mockEndpoint.assertIsSatisfied();
}
 
Example 20
Source File: SimulateErrorUsingMockTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimulateErrorUsingMock() throws Exception {
    getMockEndpoint("mock:ftp").expectedBodiesReceived("Camel rocks");

    MockEndpoint http = getMockEndpoint("mock:http");
    http.whenAnyExchangeReceived(new Processor() {
        public void process(Exchange exchange) throws Exception {
            exchange.setException(new ConnectException("Simulated connection error"));
        }
    });

    template.sendBody("direct:file", "Camel rocks");

    assertMockEndpointsSatisfied();
}