Hello,

I'm back with some code.
The mock route builder:
@Component
public class MockedTransactionToDBPublisherRouteBuilder extends
TransactionToDBPublisherRouteBuilder {

    /**
     * @throws java.sql.SQLException
     */
    public MockedTransactionToDBPublisherRouteBuilder(DataSource datasource,
            TransactionTemplate transactionTemplate, String
redeliveryPolicy, ProducerTemplate producer,
            ConsumerTemplate consumer) {
        super(datasource, transactionTemplate, redeliveryPolicy, producer,
consumer);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getExceptionEndpoint() {
        return "mock:exception";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getDeadLetterEndpoint() {
        return "mock:deadletter";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getInputEndpoint() {
        return "direct:input";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getOutputEndpoint() {
        return "mock:output";
    }
}


The route initialized in TransactionToDBPublisherRouteBuilder:
    protected void createRoute(Processor processor) {
        from(getInputEndpoint())
                // when the exchange is a TRANSACTION and is completed
update the number of passed transactions
                .onCompletion()
                .onCompleteOnly()
               
.onWhen(TransactionPredicates.isMessageType(MessageType.TRANSACTION))
                .process(endOfDaySyncProcessor)
                .end()
                // the onCompletion callback ended and normal processing is
back
                .choice()
                // the End Of Day must be handled separately since it is
processed after all the TRANSACTIONs
               
.when(TransactionPredicates.isMessageType(MessageType.END_OF_DAY))
                .process(endOfDaySyncProcessor)
                // let all the other exchanges follow the standard route
                // this path can't go through the endOfDaySync route because
we need to intercept the callback
                .otherwise()
                .process(processor)
                .to(getOutputEndpoint())
                .end();

        // this is the route where the End Of Day go after all the
transactions have been integrated
        from(getSyncEndOfDayEndpoint())
                .process(processor)
                .to(getOutputEndpoint());
    }


The base test class:
@ContextConfiguration(locations = { "transactionToDb-context.xml" })
abstract public class AbstractTransactionToDBPublisherRouteBuilderTest
extends AbstractJUnit4SpringContextTests {
    protected static final String CHARSET = "UTF-8";

    @Autowired
    protected CamelContext camelContext;

    @Autowired
    protected DataSource DATA_SOURCE;

    @Autowired
    protected TransactionTemplate TRANSACTION_TEMPLATE;

    @EndpointInject(uri = "mock:output")
    protected MockEndpoint resultEndpoint;

    @EndpointInject(uri = "mock:exception")
    protected MockEndpoint exceptionEndpoint;

    @Produce(uri = "direct:input")
    protected ProducerTemplate template;

    @BeforeClass
    public static void beforeClass() {
        // force jibx to use the staxReaderFactory
        System.setProperty("org.jibx.runtime.impl.parser",
"org.jibx.runtime.impl.StAXReaderFactory");
    }

    @Before
    public void setUp() throws Exception {
        // delete all records from the database
        TestUtil.clearDatabase(DATA_SOURCE.getConnection());
    }

    protected Exchange createExchangeWithBodyAndHeader(Object body,
MessageType type) {
        Exchange exchange = new DefaultExchange(camelContext);
        Message message = exchange.getIn();
        message.setHeader("testClass", getClass().getName());
        message.setHeader(MessageConstant.ERES_CONTENT_TYPE,
type.toString());
        message.setBody(body);
        return exchange;
    }
}


The test:
    @Test
    public void testRouteWithPendingTransactions() throws Exception {
        resultEndpoint.setExpectedMessageCount(4);

        // sending Transaction 1
        InputStream is =
getClass().getClassLoader().getResourceAsStream("Transaction1.xml");
        String msg = TestUtil.getInputStreamAsString(is, CHARSET);
        is.close();

        Exchange exchange = createExchangeWithBodyAndHeader(msg,
MessageType.TRANSACTION);
        template.send(exchange);

        // sending Transaction 2
        is =
getClass().getClassLoader().getResourceAsStream("Transaction2.xml");
        msg = TestUtil.getInputStreamAsString(is, CHARSET);
        is.close();

        exchange = createExchangeWithBodyAndHeader(msg,
MessageType.TRANSACTION);
        template.send(exchange);

        // sending End Of Process which must wait for 3 transactions to be
processed
        is =
getClass().getClassLoader().getResourceAsStream("endBusinessDate.xml");
        msg = TestUtil.getInputStreamAsString(is, CHARSET);
        is.close();

        // turn the End Of Day into Exchange and send them to the Direct
Input
        exchange = createExchangeWithBodyAndHeader(msg,
MessageType.END_OF_DAY);
        template.send(exchange);

        resultEndpoint.assertIsNotSatisfied(1000);

        // sending Transaction 3, which should trigger the processing of the
End Of Process also
        is =
getClass().getClassLoader().getResourceAsStream("Transaction3.xml");
        msg = TestUtil.getInputStreamAsString(is, CHARSET);
        is.close();

        exchange = createExchangeWithBodyAndHeader(msg,
MessageType.TRANSACTION);
        template.send(exchange);

        // assert that we got all the messages
        resultEndpoint.assertIsSatisfied(1000);
    }


The syncProcessor is working fine but none of the onCompletion events get to
it.

Thanks for your support.



--
View this message in context: 
http://camel.465427.n5.nabble.com/onCompletion-not-triggerd-in-testing-tp5743163p5743249.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to