Search in sources :

Example 6 with MultiSteps

use of org.apache.camel.examples.MultiSteps in project camel by apache.

the class JpaWithNamedQueryTest method assertReceivedResult.

protected void assertReceivedResult(Exchange exchange) {
    assertNotNull(exchange);
    MultiSteps result = exchange.getIn().getBody(MultiSteps.class);
    assertNotNull("Received a POJO", result);
    assertEquals("address property", "foo@bar.com", result.getAddress());
}
Also used : MultiSteps(org.apache.camel.examples.MultiSteps)

Example 7 with MultiSteps

use of org.apache.camel.examples.MultiSteps in project camel by apache.

the class JpaWithNamedQueryTest method testProducerInsertsIntoDatabaseThenConsumerFiresMessageExchange.

@Test
public void testProducerInsertsIntoDatabaseThenConsumerFiresMessageExchange() throws Exception {
    transactionTemplate.execute(new TransactionCallback<Object>() {

        public Object doInTransaction(TransactionStatus status) {
            entityManager.joinTransaction();
            // lets delete any exiting records before the test
            entityManager.createQuery("delete from " + entityName).executeUpdate();
            // now lets create a dummy entry
            MultiSteps dummy = new MultiSteps("cheese");
            dummy.setStep(4);
            entityManager.persist(dummy);
            return null;
        }
    });
    List<?> results = entityManager.createQuery(queryText).getResultList();
    assertEquals("Should have no results: " + results, 0, results.size());
    // lets produce some objects
    template.send(endpoint, new Processor() {

        public void process(Exchange exchange) {
            exchange.getIn().setBody(new MultiSteps("foo@bar.com"));
        }
    });
    // now lets assert that there is a result
    results = entityManager.createQuery(queryText).getResultList();
    assertEquals("Should have results: " + results, 1, results.size());
    MultiSteps mail = (MultiSteps) results.get(0);
    assertEquals("address property", "foo@bar.com", mail.getAddress());
    // now lets create a consumer to consume it
    consumer = endpoint.createConsumer(new Processor() {

        public void process(Exchange e) {
            LOG.info("Received exchange: " + e.getIn());
            receivedExchange = e;
            latch.countDown();
        }
    });
    consumer.start();
    assertTrue(latch.await(50, TimeUnit.SECONDS));
    assertReceivedResult(receivedExchange);
    // lets now test that the database is updated
    // we need to sleep as we will be invoked from inside the transaction!
    // org.apache.openjpa.persistence.InvalidStateException: This operation cannot be performed while a Transaction is active.
    Thread.sleep(2000);
    transactionTemplate.execute(new TransactionCallback<Object>() {

        public Object doInTransaction(TransactionStatus status) {
            // make use of the EntityManager having the relevant persistence-context
            EntityManager entityManager2 = receivedExchange.getIn().getHeader(JpaConstants.ENTITYMANAGER, EntityManager.class);
            entityManager2.joinTransaction();
            // now lets assert that there are still 2 entities left
            List<?> rows = entityManager2.createQuery("select x from MultiSteps x").getResultList();
            assertEquals("Number of entities: " + rows, 2, rows.size());
            int counter = 1;
            for (Object rowObj : rows) {
                assertTrue("Rows are not instances of MultiSteps", rowObj instanceof MultiSteps);
                final MultiSteps row = (MultiSteps) rowObj;
                LOG.info("entity: " + counter++ + " = " + row);
                if (row.getAddress().equals("foo@bar.com")) {
                    LOG.info("Found updated row: " + row);
                    assertEquals("Updated row step for: " + row, getUpdatedStepValue(), row.getStep());
                } else {
                    // dummy row
                    assertEquals("dummy row step for: " + row, 4, row.getStep());
                    assertEquals("Not the expected row: " + row, "cheese", row.getAddress());
                }
            }
            return null;
        }
    });
    JpaConsumer jpaConsumer = (JpaConsumer) consumer;
    assertURIQueryOption(jpaConsumer);
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) Processor(org.apache.camel.Processor) TransactionStatus(org.springframework.transaction.TransactionStatus) MultiSteps(org.apache.camel.examples.MultiSteps) List(java.util.List) Test(org.junit.Test)

Aggregations

MultiSteps (org.apache.camel.examples.MultiSteps)7 List (java.util.List)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)1 EntityManager (javax.persistence.EntityManager)1 Exchange (org.apache.camel.Exchange)1 Processor (org.apache.camel.Processor)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 Customer (org.apache.camel.examples.Customer)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)1 Before (org.junit.Before)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1