Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing with Apache Camel AdviceWith and weaveById

I am currently trying to test an existing route with Apache Camel, but I am not sure I am doing it correctly, because I don't fully understand all the concepts behind Camel.

That being said, here is what I would like to do, on the following example route :

public class TestExampleRoute extends SpringRouteBuilder {

    /** The Constant ENDPOINT_EDOSSIER_IMPORT. direct:edossierImport */
    public static final String ENDPOINT_EXAMPLE = "direct:testExampleEndpoint";

    @Override
    public void configure() throws Exception {
        // @formatter:off
        from(ENDPOINT_EXAMPLE).routeId("testExample")

            .bean(TestExampleProcessor.class, "getImportDocumentProcess").id("getImportDocumentProcess")
            .bean(TestExampleProcessor.class, "createImportDocumentTraitement").id("createImportDocumentTraitement")

            .to(BaseEndpoint.LOG_MESSAGE_SHOW_ALL_MULTILINE);
        // @formatter:on
    }

}

The point here is just to fetch an ImportDocumentProcess and create an ImportDocumentTraitement that depends on the previous object. The ImportDocumentProcess is passes through the exchange.

Here is the processor code :

@Component("testExampleProcessor")
public class TestExampleProcessor {

    /** The Constant LOGGER. */
    private static final Logger LOGGER = LogManager.getLogger(TestExampleProcessor.class);

    @Autowired
    ImportDocumentTraitementService importDocumentTraitementService;

    @Autowired
    ImportDocumentProcessDAO importDocumentProcessDAO;

    @Autowired
    ImportDocumentTraitementDAO importDocumentTraitementDAO;

    // ---- Constants to name camel headers and bodies
    private static final String HEADER_ENTREPRISE = "entreprise";
    private static final String HEADER_UTILISATEUR = "utilisateur";
    private static final String HEADER_IMPORTDOCPROCESS = "importDocumentProcess";

    public void getImportDocumentProcess(@Header(HEADER_ENTREPRISE) Entreprise entreprise, Exchange exchange) {
        LOGGER.info("Entering TestExampleProcessor method : getImportDocumentProcess");

        Utilisateur utilisateur = SessionUtils.getUtilisateur();
        ImportDocumentProcess importDocumentProcess = importDocumentProcessDAO.getImportDocumentProcessByEntreprise(
                entreprise);

        exchange.getIn().setHeader(HEADER_UTILISATEUR, utilisateur);
        exchange.getIn().setHeader(HEADER_IMPORTDOCPROCESS, importDocumentProcess);
    }

    public void createImportDocumentTraitement(@Header(HEADER_ENTREPRISE) Entreprise entreprise,
            @Header(HEADER_UTILISATEUR) Utilisateur utilisateur,
            @Header(HEADER_IMPORTDOCPROCESS) ImportDocumentProcess importDocumentProcess, Exchange exchange) {
        LOGGER.info("Entering TestExampleProcessor method : createImportDocumentTraitement");

        long nbImportTraitementBefore = this.importDocumentTraitementDAO.countNumberOfImportDocumentTraitement();
        ImportDocumentTraitement importDocumentTraitement = this.importDocumentTraitementService.createImportDocumentTraitement(
                entreprise, utilisateur, importDocumentProcess, "md5_fichier_example_test", "fichier_example_test.xml");
        long nbImportTraitementAfter = this.importDocumentTraitementDAO.countNumberOfImportDocumentTraitement();

        exchange.getIn().setHeader("nbImportTraitementBefore", Long.valueOf(nbImportTraitementBefore));
        exchange.getIn().setHeader("nbImportTraitementAfter", Long.valueOf(nbImportTraitementAfter));
        exchange.getIn().setHeader("importDocumentTraitement", importDocumentTraitement);
    }

}

I have read a few things about AdviceWith and WeaveById and I would like to put test the state of the exchange between two pieces of route.

Here is my attempt for a processor test :

@ContextConfiguration(locations = { "classpath:/camel-context.xml" })
public class TestExampleProcessorTest extends CamelTestSupport {

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new TestExampleRoute();
    }

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

    @Before
    public void mockEndPoints() throws Exception {
        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {

            @Override
            public void configure() throws Exception {
                weaveById("getImportDocumentProcess").replace().multicast().to("mock:catchTestEndpoint");
            }
        });
    }

    @Test
    public void testAdvised() throws Exception {
        MockEndpoint mockEndpoint = getMockEndpoint("mock:catchTestEndpoint");

        context.start();
        mockEndpoint.expectedMessageCount(1);
        mockEndpoint.assertIsSatisfied();
        context.stop();
    }

}

One last thing : I am using Camel 2.18.0.

How can I test the state of the exchange between each piece of route ? What am I missing ?

EDIT : Just edited the code of the the test class (Which compiles and works) BUT I get the following assertion error :

java.lang.AssertionError: mock://catchTestEndpoint Received message count. Expected: <1> but was: <0>

This adds one more question : why is the message not caught correctly ?

Thanks for your help.

like image 964
matthieusb Avatar asked Jun 19 '17 16:06

matthieusb


1 Answers

Do you send any message to you testroute? I can't see that in the code. For example

template.sendBody("direct:testExampleEndpoint", "Hello World");

like image 127
anpt Avatar answered Oct 28 '22 14:10

anpt