@Override protected void setUp() throws Exception { super.setUp(); validEndpoint = resolveMandatoryEndpoint("mock:valid", MockEndpoint.class); invalidEndpoint = resolveMandatoryEndpoint("mock:invalid", MockEndpoint.class); validEndpoint.whenAnyExchangeReceived( ProcessorBuilder.setBody(Builder.constant("validResult"))); invalidEndpoint.whenAnyExchangeReceived( ProcessorBuilder.setBody(Builder.constant("invalidResult"))); }
@Test public void testHandleException() throws Exception { for (int i = 0; i < DELIVERY_COUNT; i++) { MockEndpoint finish = consumerContext.getEndpoint("mock:finish" + i, MockEndpoint.class); finish.whenAnyExchangeReceived( new Processor() { @Override public void process(Exchange exchange) throws Exception { throw new RuntimeException("TestException!!!"); } }); finish.expectedBodiesReceived("1234567890"); } MockEndpoint finish4 = consumerContext.getEndpoint("mock:finish4", MockEndpoint.class); finish4.expectedBodiesReceived("1234567890-1"); MockEndpoint exception = producerContext.getEndpoint("mock:exception", MockEndpoint.class); exception.expectedBodiesReceived("1234567890"); ProducerTemplate producerTemplate = producerContext.createProducerTemplate(); try { producerTemplate.sendBody("direct:start", "1234567890"); fail("CamelExecutionException expected"); } catch (CamelExecutionException e) { assertEquals("TestException!!!", e.getCause().getMessage()); } producerTemplate.sendBody("direct:start", "1234567890-1"); MockEndpoint.assertIsSatisfied(consumerContext); MockEndpoint.assertIsSatisfied(producerContext); }
@Test public void testOnline() throws InterruptedException { // Wait for 5 seconds while Camel route is exercised resultEndpoint.whenAnyExchangeReceived(null); Thread.sleep(5000); // Ensure online symptoms are reported List<Exchange> exchanges = resultEndpoint.getExchanges(); Assert.assertTrue(!exchanges.isEmpty()); Assert.assertEquals(1, alerts); Assert.assertEquals(exchanges.size(), builder.getPingProcessor().getSucceeded()); Assert.assertEquals(0, builder.getPingProcessor().getFailed()); Assert.assertEquals(exchanges.size(), builder.getPingProcessor().getTotal()); }
@Test public void testAsyncEndpointRollback() { end.whenAnyExchangeReceived( new Processor() { public void process(Exchange exchange) throws Exception { throw new IllegalArgumentException("Damn"); } }); try { producer.sendBody(3); fail("Should have thrown exception"); } catch (CamelExecutionException e) { assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause()); assertEquals(0, table.values().size()); } }
@Test public void pipelineAborts() { end.expectedMessageCount(1); end.whenAnyExchangeReceived( new Processor() { public void process(Exchange exchange) throws Exception { throw new MyException(); } }); after.expectedMessageCount(1); try { producer.sendBody(3); assertFalse(true); } catch (CamelExecutionException e) { assertEquals(MyException.class, e.getCause().getCause().getClass()); assertEquals(0, table.values().size()); } }
public void testFailedMessagesOpenCircuitToPreventMessageThree() throws Exception { expectsMessageCount(2, result); result.whenAnyExchangeReceived( new Processor() { @Override public void process(Exchange exchange) throws Exception { exchange.setException(new MyCustomException()); } }); Exchange exchangeOne = sendMessage("direct:start", "message one"); Exchange exchangeTwo = sendMessage("direct:start", "message two"); Exchange exchangeThree = sendMessage("direct:start", "message three"); assertMockEndpointsSatisfied(); assertTrue(exchangeOne.getException() instanceof MyCustomException); assertTrue(exchangeTwo.getException() instanceof MyCustomException); assertTrue(exchangeThree.getException() instanceof RejectedExecutionException); }
@Test public void testRoute3() throws InterruptedException { final int m_count = 5; resultEndpoint.expectedMessageCount(redeliveryCount + (redeliveryCount * m_count)); resultEndpoint.whenAnyExchangeReceived( (ex) -> { ex.getIn().setBody("arrived"); ex.setException(new Exception("test")); }); for (int i = 0; i < m_count; i++) { Exchange ex = sendMessage("direct:input", i); assertEquals("arrived", ex.getIn().getBody(String.class)); } assertMockEndpointsSatisfied(); }
@Test public void testOffline() throws InterruptedException { resultEndpoint.whenAnyExchangeReceived( new Processor() { @Override public void process(Exchange exchange) throws Exception { // Treat the endpoint as offline throw new Exception("OFFLINE"); } }); // Wait for 5 seconds while Camel route is exercised Thread.sleep(5000); // Ensure offline symptoms are reported final List<Exchange> exchanges = resultEndpoint.getExchanges(); Assert.assertTrue(!exchanges.isEmpty()); Assert.assertEquals(1, alerts); Assert.assertEquals(exchanges.size(), builder.getPingProcessor().getFailed()); Assert.assertEquals(0, builder.getPingProcessor().getSucceeded()); Assert.assertEquals(exchanges.size(), builder.getPingProcessor().getTotal()); }
public void testHalfOpenCircuitClosesAfterTimeout() throws Exception { expectsMessageCount(2, result); result.whenAnyExchangeReceived( new Processor() { @Override public void process(Exchange exchange) throws Exception { exchange.setException(new MyCustomException()); } }); sendMessage("direct:start", "message one"); sendMessage("direct:start", "message two"); sendMessage("direct:start", "message three"); assertMockEndpointsSatisfied(); result.reset(); expectsMessageCount(1, result); Thread.sleep(1000); sendMessage("direct:start", "message four"); assertMockEndpointsSatisfied(); }
@Test public void testSendObservableToEndpoint() throws Exception { Order[] expectedBodies = {new Order("o1", 1.10), new Order("o2", 2.20), new Order("o3", 3.30)}; Observable<Order> someObservable = Observable.from(expectedBodies); final MockEndpoint mockEndpoint = camelContext.getEndpoint("mock:results", MockEndpoint.class); mockEndpoint.expectedBodiesReceived((Object[]) expectedBodies); mockEndpoint.whenAnyExchangeReceived( new Processor() { @Override public void process(Exchange exchange) throws Exception { exchange.addOnCompletion(onCompletion); } }); // lets send events on the observable to the camel endpoint reactiveCamel.sendTo(someObservable, "mock:results"); mockEndpoint.assertIsSatisfied(); Assert.assertEquals(3, onCompletion.getDone()); }