Search in sources :

Example 6 with SendEmail

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

the class JpaRouteSharedEntityManagerTest method testRouteJpaNotShared.

@Test
public void testRouteJpaNotShared() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.sendBody("direct:startNotshared", new SendEmail("one@somewhere.org"));
    int countStart = getBrokerCount();
    assertThat("brokerCount", countStart, equalTo(1));
    // start route
    context.startRoute("jpaOwn");
    // not the cleanest way to check the number of open connections
    int countEnd = getBrokerCount();
    assertThat("brokerCount", countEnd, equalTo(2));
    latch.countDown();
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 7 with SendEmail

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

the class JpaRouteTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    // should auto setup transaction manager and entity factory
    JpaComponent jpa = context.getComponent("jpa", JpaComponent.class);
    assertNotNull("Should have been auto assigned", jpa.getEntityManagerFactory());
    assertNotNull("Should have been auto assigned", jpa.getTransactionManager());
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    ValueBuilder header = mock.message(0).header(JpaConstants.ENTITYMANAGER);
    header.isNotNull();
    header.isInstanceOf(EntityManager.class);
    template.sendBody("direct:start", new SendEmail("someone@somewhere.org"));
    assertMockEndpointsSatisfied();
    assertEntityInDB(1);
}
Also used : ValueBuilder(org.apache.camel.builder.ValueBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) JpaComponent(org.apache.camel.component.jpa.JpaComponent) Test(org.junit.Test)

Example 8 with SendEmail

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

the class JpaTXRollbackTest method testTXRollback.

@Test
public void testTXRollback() throws Exception {
    // first create three records
    template.sendBody("jpa://" + SendEmail.class.getName(), new SendEmail("foo@beer.org"));
    template.sendBody("jpa://" + SendEmail.class.getName(), new SendEmail("bar@beer.org"));
    template.sendBody("jpa://" + SendEmail.class.getName(), new SendEmail("kaboom@beer.org"));
    // should rollback the entire
    MockEndpoint mock = getMockEndpoint("mock:result");
    // we should retry and try again
    mock.expectedMinimumMessageCount(4);
    // start route
    context.startRoute("foo");
    assertMockEndpointsSatisfied();
    assertTrue("Should be >= 2, was: " + foo.intValue(), foo.intValue() >= 2);
    assertTrue("Should be >= 2, was: " + bar.intValue(), bar.intValue() >= 2);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) Test(org.junit.Test)

Example 9 with SendEmail

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

the class JpaWireTapTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    // should auto setup transaction manager and entity factory
    JpaComponent jpa = context.getComponent("jpa", JpaComponent.class);
    assertNotNull("Should have been auto assigned", jpa.getEntityManagerFactory());
    assertNotNull("Should have been auto assigned", jpa.getTransactionManager());
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    template.sendBody("direct:start", new SendEmail("someone@somewhere.org"));
    assertMockEndpointsSatisfied();
    assertEntityInDB(2);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) JpaComponent(org.apache.camel.component.jpa.JpaComponent) Test(org.junit.Test)

Example 10 with SendEmail

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

the class JpaTest 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();
            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 SendEmail("foo@bar.com"));
        }
    });
    // now lets assert that there is a result
    results = entityManager.createQuery(queryText).getResultList();
    assertEquals("Should have results: " + results, 1, results.size());
    SendEmail mail = (SendEmail) 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;
            // should have a EntityManager
            EntityManager entityManager = e.getIn().getHeader(JpaConstants.ENTITYMANAGER, EntityManager.class);
            assertNotNull("Should have a EntityManager as header", entityManager);
            latch.countDown();
        }
    });
    consumer.start();
    assertTrue(latch.await(50, TimeUnit.SECONDS));
    assertNotNull(receivedExchange);
    SendEmail result = receivedExchange.getIn().getBody(SendEmail.class);
    assertNotNull("Received a POJO", result);
    assertEquals("address property", "foo@bar.com", result.getAddress());
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) Processor(org.apache.camel.Processor) TransactionStatus(org.springframework.transaction.TransactionStatus) SendEmail(org.apache.camel.examples.SendEmail) Test(org.junit.Test)

Aggregations

SendEmail (org.apache.camel.examples.SendEmail)16 Test (org.junit.Test)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)13 EntityManager (javax.persistence.EntityManager)3 Exchange (org.apache.camel.Exchange)3 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 JpaComponent (org.apache.camel.component.jpa.JpaComponent)2 JpaEndpoint (org.apache.camel.component.jpa.JpaEndpoint)2 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Processor (org.apache.camel.Processor)1 ValueBuilder (org.apache.camel.builder.ValueBuilder)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1