Search in sources :

Example 1 with SendEmail

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

the class JpaBatchConsumerTest method testBatchConsumer.

@Test
public void testBatchConsumer() throws Exception {
    // first create two records
    template.sendBody("jpa://" + SendEmail.class.getName(), new SendEmail("foo@beer.org"));
    template.sendBody("jpa://" + SendEmail.class.getName(), new SendEmail("bar@beer.org"));
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    mock.message(0).property(Exchange.BATCH_INDEX).isEqualTo(0);
    mock.message(0).property(Exchange.BATCH_COMPLETE).isEqualTo(false);
    mock.message(1).property(Exchange.BATCH_INDEX).isEqualTo(1);
    mock.message(1).property(Exchange.BATCH_COMPLETE).isEqualTo(true);
    mock.expectedPropertyReceived(Exchange.BATCH_SIZE, 2);
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) Test(org.junit.Test)

Example 2 with SendEmail

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

the class JpaFlushOnSendTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.sendBody("direct:start", new SendEmail("someone@somewhere.org"));
    assertMockEndpointsSatisfied();
    assertEntityInDB();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SendEmail(org.apache.camel.examples.SendEmail) Test(org.junit.Test)

Example 3 with SendEmail

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

the class JpaProducerConcurrentTest method doSendMessages.

private void doSendMessages(int files, int poolSize) throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(files);
    getMockEndpoint("mock:result").assertNoDuplicates(body());
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    // we access the responses Map below only inside the main thread,
    // so no need for a thread-safe Map implementation
    Map<Integer, Future<SendEmail>> responses = new HashMap<Integer, Future<SendEmail>>();
    for (int i = 0; i < files; i++) {
        final int index = i;
        Future<SendEmail> out = executor.submit(new Callable<SendEmail>() {

            public SendEmail call() throws Exception {
                return template.requestBody("direct:start", new SendEmail("user" + index + "@somewhere.org"), SendEmail.class);
            }
        });
        responses.put(index, out);
    }
    assertMockEndpointsSatisfied(30, TimeUnit.SECONDS);
    assertEquals(files, responses.size());
    // get them so they are complete
    for (Future<SendEmail> future : responses.values()) {
        SendEmail sendEmail = future.get();
        assertNotNull(sendEmail);
        log.info("Persisted the SendEmail entity with the id {} and the address {}", sendEmail.getId(), sendEmail.getAddress());
    }
    // assert in the database
    assertEntityInDB(files);
    executor.shutdownNow();
}
Also used : HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) SendEmail(org.apache.camel.examples.SendEmail)

Example 4 with SendEmail

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

the class JpaProducerPassingEntityManagerTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    context.startRoute("foo");
    JpaEndpoint jpa = context.getEndpoint("jpa://" + SendEmail.class.getName(), JpaEndpoint.class);
    EntityManagerFactory emf = jpa.getEntityManagerFactory();
    // The entity instance is different if it is retrieved from different EntityManager instance
    EntityManager entityManager = emf.createEntityManager();
    template.sendBody("direct:start", new SendEmail("foo@beer.org"));
    Exchange exchange = mock.getReceivedExchanges().get(0);
    SendEmail persistedEntity = exchange.getIn().getBody(SendEmail.class);
    SendEmail emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
    assertNotSame(emfindEntity, persistedEntity);
    entityManager.close();
    mock.reset();
    // The same EntityManager returns same entity instance from its 1st level cache
    entityManager = emf.createEntityManager();
    template.sendBodyAndHeader("direct:start", new SendEmail("bar@beer.org"), JpaConstants.ENTITYMANAGER, entityManager);
    exchange = mock.getReceivedExchanges().get(0);
    persistedEntity = exchange.getIn().getBody(SendEmail.class);
    emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
    assertSame(emfindEntity, persistedEntity);
    entityManager.close();
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaEndpoint(org.apache.camel.component.jpa.JpaEndpoint) SendEmail(org.apache.camel.examples.SendEmail) Test(org.junit.Test)

Example 5 with SendEmail

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

the class JpaRouteMaximumResultsTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(1);
    template.sendBody("direct:start", new SendEmail("one@somewhere.org"));
    template.sendBody("direct:start", new SendEmail("two@somewhere.org"));
    template.sendBody("direct:start", new SendEmail("three@somewhere.org"));
    assertMockEndpointsSatisfied();
    assertEntityInDB(3);
    // should not consume 3 at once
    assertTrue("Should not consume all 3 at once", mock.getReceivedCounter() < 3);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) 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