You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Tobias Letschka <tl...@covernet.de> on 2020/03/06 13:54:07 UTC

How to prepare mongoDB Endpoint for Tests

Hi riders

I am searching for a solution to produce a bson.types.Objectid and produce a body at a mongodb endpoint.

I have mocked the Endpoint:

@EndpointInject(uri = "mock:mongoFindBy")
private MockEndpoint mongoFindBy;

My route (Java DSL):


from(ActiveMQ.QUEUE_IN).routeId(MAPPING_ROUTE_ROUTE_ID)
        .setProperty(REALM_PROPERTY).exchange(exchange -> exchange.getIn().getHeader("Realm"))
        .log(INFO, "Processing starts, message sent to " + ActiveMQ.QUEUE_IN)
        .log(DEBUG, "Realm Header: ${exchangeProperty.realm_Property}" )

        // GET Input as JSON
        .setProperty(MONGO_ENTRY_PROPERTY).body()  
        .convertBodyTo(ObjectId.class)
        .log(DEBUG, "Received _id of Imported Order which should be mapped: ${exchangeProperty." + MONGO_ENTRY_PROPERTY + "}")

        // Remove useless Headers
        .removeHeaders("*")

        // Read Json from MongoDB
        .toF(MongoDB.MONGO_FIND_BY_ID).id(MONGO_FIND_ID)

        // Caching Body
        .setBody().body(Document.class, document -> document.toJson())
        .setProperty(JSON_INPUT_PROPERTY).body()

        .log(DEBUG, "Body after reading mongoDB - ${body}")

        .choice()
            .when(exchangeProperty(REALM_PROPERTY).contains("VU_MEYNENEN"))
                .to("direct:vumeynenMapping").id(VUMEYNEN_MAPPING_ID)
            .when(header(REALM_PROPERTY).contains("CSV"))
                .to("direct:csvExport").id(CSV_EXPORT_ID)
;

My Test:

@Before
public void before() throws Exception {

    // Mocking Endpoints
    context.getRouteDefinition(MappingRouteBuilder.MAPPING_ROUTE_ROUTE_ID).adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith("direct:start");


            mockEndpointsAndSkip("mongodb3:*");


            weaveById(MappingRouteBuilder.MONGO_FIND_ID).replace().to(mongoFindBy);
            weaveById(MappingRouteBuilder.VUMEYNEN_MAPPING_ID).replace().to(vumeynenSubroute);
            weaveById(MappingRouteBuilder.CSV_EXPORT_ID).replace().to(csvSubroute);
        }
    }); 
….

——————————————————


private ObjectId id = new ObjectId();

@Test
public void testMappingRouteSuccessful() throws InterruptedException {
    mongoFindBy.expectedMessageCount(1);
    mongoFindBy.expectedBodiesReceived(id);


    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setHeader("Realm", "CSV");
    exchange.getIn().setBody(id);

    producer.send("direct:start", exchange);
    

    mongoFindBy.assertIsSatisfied();

Cheers

Tobias