Search in sources :

Example 31 with CamelExecutionException

use of org.apache.camel.CamelExecutionException in project camel by apache.

the class ContextScopedOnExceptionCorrectRouteContextTest method testContextScopedOnExceptionLogRouteBarFail.

public void testContextScopedOnExceptionLogRouteBarFail() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(Exception.class).log("Error due ${exception.message}").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    String routeId = exchange.getUnitOfWork().getRouteContext().getRoute().getId();
                    assertEquals("bar", routeId);
                }
            });
            from("direct:start").routeId("foo").to("mock:foo").to("direct:bar").to("mock:result");
            from("direct:bar").routeId("bar").to("mock:bar").throwException(new IllegalArgumentException("Forced bar error"));
        }
    });
    context.start();
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    getMockEndpoint("mock:bar").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedMessageCount(0);
    try {
        template.sendBody("direct:start", "Hello World");
        fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
        IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
        assertEquals("Forced bar error", cause.getMessage());
    }
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) CamelExecutionException(org.apache.camel.CamelExecutionException) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) CamelExecutionException(org.apache.camel.CamelExecutionException)

Example 32 with CamelExecutionException

use of org.apache.camel.CamelExecutionException in project camel by apache.

the class ContextScopedOnExceptionCorrectRouteContextTest method testContextScopedOnExceptionLogRouteFooFail.

public void testContextScopedOnExceptionLogRouteFooFail() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(Exception.class).log("Error due ${exception.message}").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    String routeId = exchange.getUnitOfWork().getRouteContext().getRoute().getId();
                    assertEquals("foo", routeId);
                }
            });
            from("direct:start").routeId("foo").to("mock:foo").throwException(new IllegalArgumentException("Forced foo error")).to("direct:bar").to("mock:result");
            from("direct:bar").routeId("bar").to("mock:bar");
            from("direct:killer").routeId("killer").to("mock:killer");
        }
    });
    context.start();
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    getMockEndpoint("mock:bar").expectedMessageCount(0);
    getMockEndpoint("mock:result").expectedMessageCount(0);
    try {
        template.sendBody("direct:start", "Hello World");
        fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
        IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
        assertEquals("Forced foo error", cause.getMessage());
    }
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) CamelExecutionException(org.apache.camel.CamelExecutionException) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) CamelExecutionException(org.apache.camel.CamelExecutionException)

Example 33 with CamelExecutionException

use of org.apache.camel.CamelExecutionException in project camel by apache.

the class AdviceWithTest method testAdvisedThrowException.

public void testAdvisedThrowException() throws Exception {
    context.getRouteDefinitions().get(0).adviceWith(context, new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:foo").to("mock:advised").throwException(new IllegalArgumentException("Damn"));
        }
    });
    getMockEndpoint("mock:foo").expectedMessageCount(0);
    getMockEndpoint("mock:advised").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedMessageCount(0);
    try {
        template.sendBody("direct:start", "Hello World");
        fail("Should have thrown exception");
    } catch (CamelExecutionException e) {
        assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
        assertEquals("Damn", e.getCause().getMessage());
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) RouteBuilder(org.apache.camel.builder.RouteBuilder) CamelExecutionException(org.apache.camel.CamelExecutionException)

Example 34 with CamelExecutionException

use of org.apache.camel.CamelExecutionException in project camel by apache.

the class BeanValidatorRouteTest method validateShouldFailWithOrderedChecksGroup.

@Test
public void validateShouldFailWithOrderedChecksGroup() throws Exception {
    if (isPlatform("aix")) {
        // cannot run on aix
        return;
    }
    final String url = "bean-validator://x?group=org.apache.camel.component.bean.validator.OrderedChecks";
    final Car car = createCar(null, "D-A");
    try {
        template.requestBody(url, car);
        fail("should throw exception");
    } catch (CamelExecutionException e) {
        assertIsInstanceOf(BeanValidationException.class, e.getCause());
        BeanValidationException exception = (BeanValidationException) e.getCause();
        Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations();
        assertEquals(1, constraintViolations.size());
        ConstraintViolation<Object> constraintViolation = constraintViolations.iterator().next();
        assertEquals("manufacturer", constraintViolation.getPropertyPath().toString());
        assertEquals(null, constraintViolation.getInvalidValue());
        assertEquals("may not be null", constraintViolation.getMessage());
    }
    car.setManufacturer("BMW");
    try {
        template.requestBody(url, car);
        fail("should throw exception");
    } catch (CamelExecutionException e) {
        assertIsInstanceOf(BeanValidationException.class, e.getCause());
        BeanValidationException exception = (BeanValidationException) e.getCause();
        Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations();
        assertEquals(1, constraintViolations.size());
        ConstraintViolation<Object> constraintViolation = constraintViolations.iterator().next();
        assertEquals("licensePlate", constraintViolation.getPropertyPath().toString());
        assertEquals("D-A", constraintViolation.getInvalidValue());
        assertEquals("size must be between 5 and 14", constraintViolation.getMessage());
    }
    car.setLicensePlate("DD-AB-123");
    Exchange exchange = template.request(url, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(car);
        }
    });
    assertNotNull(exchange);
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) Exchange(org.apache.camel.Exchange) Set(java.util.Set) Processor(org.apache.camel.Processor) ConstraintViolation(javax.validation.ConstraintViolation) CamelExecutionException(org.apache.camel.CamelExecutionException) Test(org.junit.Test)

Example 35 with CamelExecutionException

use of org.apache.camel.CamelExecutionException in project camel by apache.

the class BeanValidatorRouteTest method validateShouldFailWithExpliciteDefaultGroup.

@Test
public void validateShouldFailWithExpliciteDefaultGroup() throws Exception {
    if (isPlatform("aix")) {
        // cannot run on aix
        return;
    }
    final String url = "bean-validator://x?group=javax.validation.groups.Default";
    final Car car = createCar("BMW", null);
    try {
        template.requestBody(url, car);
        fail("should throw exception");
    } catch (CamelExecutionException e) {
        assertIsInstanceOf(BeanValidationException.class, e.getCause());
        BeanValidationException exception = (BeanValidationException) e.getCause();
        Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations();
        assertEquals(1, constraintViolations.size());
        ConstraintViolation<Object> constraintViolation = constraintViolations.iterator().next();
        assertEquals("licensePlate", constraintViolation.getPropertyPath().toString());
        assertEquals(null, constraintViolation.getInvalidValue());
        assertEquals("may not be null", constraintViolation.getMessage());
    }
    car.setLicensePlate("D-A");
    Exchange exchange = template.request(url, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(car);
        }
    });
    assertNotNull(exchange);
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) Exchange(org.apache.camel.Exchange) Set(java.util.Set) Processor(org.apache.camel.Processor) ConstraintViolation(javax.validation.ConstraintViolation) CamelExecutionException(org.apache.camel.CamelExecutionException) Test(org.junit.Test)

Aggregations

CamelExecutionException (org.apache.camel.CamelExecutionException)140 RouteBuilder (org.apache.camel.builder.RouteBuilder)60 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)58 Test (org.junit.Test)48 Exchange (org.apache.camel.Exchange)23 Processor (org.apache.camel.Processor)17 CamelExchangeException (org.apache.camel.CamelExchangeException)13 IOException (java.io.IOException)12 Date (java.util.Date)11 StopWatch (org.apache.camel.util.StopWatch)10 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)7 QuartzComponent (org.apache.camel.component.quartz.QuartzComponent)5 File (java.io.File)4 Set (java.util.Set)4 ConstraintViolation (javax.validation.ConstraintViolation)4 CamelContext (org.apache.camel.CamelContext)4 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)4 QuartzComponent (org.apache.camel.component.quartz2.QuartzComponent)4 List (java.util.List)3 JSONException (net.sf.json.JSONException)3