使用Apache Camel AdviceWith和weaveById进行测试

时间:2017-06-19 16:18:57

标签: java dependency-injection apache-camel

我目前正在尝试使用Apache Camel测试现有路线,但我不确定我是否正确使用它,因为我不完全理解Camel背后的所有概念。

话虽如此,我想做的就是以下示例路线:

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
    }

}

这里的要点只是获取 ImportDocumentProcess 并创建依赖于前一个对象的 ImportDocumentTraitement 。 ImportDocumentProcess通过交换。

以下是处理器代码:

@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);
    }

}

我已经阅读了一些关于 AdviceWith WeaveById 的内容,我想测试两条路线之间的交换状态。

以下是我对处理器测试的尝试:

@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();
    }

}

最后一件事:我正在使用 Camel 2.18.0。

如何测试每条路线之间的交换状态? 我错过了什么?

编辑:刚刚编辑了测试类的代码(编译和工作)但是我得到以下断言错误:

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

这又增加了一个问题:为什么邮件没有被正确捕获?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你向testroute发送任何消息吗?我在代码中看不到。例如

template.sendBody(&#34; direct:testExampleEndpoint&#34;,&#34; Hello World&#34;);