Camel 3 testing - AdviceWith interception onException fails

61 Views Asked by At

Been upgrading tests in a Java app from Camel 2.24.0 to 3.14.10, using SpringBoot 2.7.18 (also tried with SpringBoot 2.6.15) and JUnit 5.

Simple route tests ending in a receiver endpoint pass ok the AssertThat condition mockReceiver.expectedMessageCount(1).

But on the tests that are throwing an exception on purpose to onException Camel clause, seems the message is not intercepted: the AssertThat condition mockError.expectedMessageCount(1) fails:

Received message count. Expected <1> but was <0>

And in the log: The latch did not reach 0 within the specified time

Have debugged and I see my JsonValidatorProcessor getting the test json, and throwing my InvalidValidationException as expected. Then it puts the exception as json String into the Camel exchange with exchanche.getIn().setBody(jsonObject) and goes to the ExceptionProcessor I have in the errorRoute.

But the message finally does not arrive to the mockError endpoint.

The test:

@CamelSpringBootTest
@SpringBootTest
@ActiveProfiles("test")
@EnableAutoConfiguration
@UseAdviceWith
public class MessageValidationRouteTest {
    
    @Autowired
    CamelContext camelContext;
    
    @Autowired
    MessageValidationRoute msgValidationRoute;
    
    @Produce("direct:startTest")
    ProducerTemplate producer;

    JSONObject input;

    @EndpointInject("mock:receiveMessage")
    MockEndpoint mockReceiver;

    @EndpointInject("mock:errorMessage")
    MockEndpoint mockError;
    
    @BeforeEach
    public void prepare() throws Exception {
        camelContext.addRoutes(msgValidationRoute);

        AdviceWith.adviceWith(camelContext, MessageValidationRoute.ROUTE_ID, routeAdvice -> {
            
            routeAdvice.replaceFromWith("direct:startTest");

            routeAdvice.interceptSendToEndpoint(NextRoute.INPUT_ENDPOINT).skipSendToOriginalEndpoint().to(mockReceiver);
            
            routeAdvice.interceptSendToEndpoint(ErrorRoute.INPUT_ENDPOINT).skipSendToOriginalEndpoint().to(mockError);
        });

        camelContext.start();

    }

    @Test   
    public void validateGetTradeApiTest() throws Exception {
        String inputJson = new String(Files.readAllBytes(Paths.get("gettrade.json")));
        input = new JSONObject(inputJson);
        mockReceiver.expectedMessageCount(1);
        producer.sendBody(input);
        mockReceiver.assertIsSatisfied(); // this works
    }

    @Test
    public void validateWrongApiTest() throws Exception {
        String inputJson = new String(Files.readAllBytes(Paths.get("getWrongTrade.json")));
        input = new JSONObject(inputJson);
        mockError.expectedMessageCount(1);
        producer.sendBody(input);
        mockError.assertIsSatisfied(); // this fails and other tests sending to mockError fail too  
    }
}

And the Camel route:

@Component
public class MessageValidationRoute extends RouteBuilder {
    public static final String ROUTE_ID = "MessageValidationRoute";
    
    public static final String INPUT_ENDPOINT = "direct:startValidation";

    @Autowired
    JsonValidatorProcessor jsonValProcessor;

    @Override
    public void configure() throws Exception {
        onException(Exception.class).handled(true).to(ErrorRoute.INPUT_ENDPOINT).end();
        
        from(INPUT_ENDPOINT).routeId(ROUTE_ID).process(jsonValProcessor)
            log(LoggingLevel.DEBUG, log, "Validation Passed - Sending to Next Route").to(NextRoute.INPUT_ENDPOINT);
    }
}

The log, the InputValidationException is thrown correctly:

2024-01-19 23:43:01:290 [main] INFO  org.apache.camel.impl.engine.AbstractCamelContext.logStartSummary - Apache Camel 3.14.10 (camel-1) started in 2s744ms (build:605ms init:2s29ms start:110ms)
2024-01-19 23:43:01:394 [main] ERROR c.d.g.i.tqs.camel.processors.ExceptionProcessor.process - Exception of type com.db.ged.imagine.tqs.exceptions.InputValidationException in Route MessageValidationRoute - Error: Invalid API method found: getTrude. Please send a valid method. Operation cancelled.
2024-01-19 23:43:01:401 [main] INFO  org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied - Asserting: mock://errorMessage is satisfied
2024-01-19 23:43:11:421 [main] WARN  org.apache.camel.component.mock.MockEndpoint.waitForCompleteLatch - The latch did not reach 0 within the specified time
2024-01-19 23:43:11:453 [main] INFO  org.apache.camel.impl.engine.AbstractCamelContext.doStop - Apache Camel 3.14.10 (camel-1) shutting down (timeout:10s)

(I have tried removing the end() of the onException clause of the route but no luck).

(I have tried with a bigger timeout than the 10 seconds default, same problem).

(I have tried adding annotation @MockEndpoints to the class, but does not solve and the Ok test validateGetTradeApiTest() fails too then).

Any clue on how to achieve it, please? (Test class has to be separated from business route class).

Thanks in advance.

0

There are 0 best solutions below