Search in sources :

Example 1 with Customer

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

the class AbstractJpaMethodTest method produceNewEntity.

@Test
public void produceNewEntity() throws Exception {
    setUp("jpa://" + Customer.class.getName() + "?usePersist=" + (usePersist() ? "true" : "false"));
    Customer customer = createDefaultCustomer();
    Customer receivedCustomer = template.requestBody(endpoint, customer, Customer.class);
    assertEquals(customer.getName(), receivedCustomer.getName());
    assertNotNull(receivedCustomer.getId());
    assertEquals(customer.getAddress().getAddressLine1(), receivedCustomer.getAddress().getAddressLine1());
    assertEquals(customer.getAddress().getAddressLine2(), receivedCustomer.getAddress().getAddressLine2());
    assertNotNull(receivedCustomer.getAddress().getId());
    List<?> results = entityManager.createQuery("select o from " + Customer.class.getName() + " o").getResultList();
    assertEquals(1, results.size());
    Customer persistedCustomer = (Customer) results.get(0);
    assertEquals(receivedCustomer.getName(), persistedCustomer.getName());
    assertEquals(receivedCustomer.getId(), persistedCustomer.getId());
    assertEquals(receivedCustomer.getAddress().getAddressLine1(), persistedCustomer.getAddress().getAddressLine1());
    assertEquals(receivedCustomer.getAddress().getAddressLine2(), persistedCustomer.getAddress().getAddressLine2());
    assertEquals(receivedCustomer.getAddress().getId(), persistedCustomer.getAddress().getId());
}
Also used : Customer(org.apache.camel.examples.Customer) Test(org.junit.Test)

Example 2 with Customer

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

the class AbstractJpaMethodTest method consumeEntity.

@Test
public void consumeEntity() throws Exception {
    setUp("jpa://" + Customer.class.getName() + "?usePersist=" + (usePersist() ? "true" : "false"));
    final Customer customer = createDefaultCustomer();
    save(customer);
    final CountDownLatch latch = new CountDownLatch(1);
    consumer = endpoint.createConsumer(new Processor() {

        public void process(Exchange e) {
            receivedExchange = e;
            assertNotNull(e.getIn().getHeader(JpaConstants.ENTITYMANAGER, EntityManager.class));
            latch.countDown();
        }
    });
    consumer.start();
    assertTrue(latch.await(50, TimeUnit.SECONDS));
    consumer.stop();
    Thread.sleep(1000);
    assertNotNull(receivedExchange);
    Customer receivedCustomer = receivedExchange.getIn().getBody(Customer.class);
    assertEquals(customer.getName(), receivedCustomer.getName());
    assertEquals(customer.getId(), receivedCustomer.getId());
    assertEquals(customer.getAddress().getAddressLine1(), receivedCustomer.getAddress().getAddressLine1());
    assertEquals(customer.getAddress().getAddressLine2(), receivedCustomer.getAddress().getAddressLine2());
    assertEquals(customer.getAddress().getId(), receivedCustomer.getAddress().getId());
    // give a bit time for consumer to delete after done
    Thread.sleep(1000);
    assertEntitiesInDatabase(0, Customer.class.getName());
    assertEntitiesInDatabase(0, Address.class.getName());
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) Processor(org.apache.camel.Processor) Address(org.apache.camel.examples.Address) Customer(org.apache.camel.examples.Customer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with Customer

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

the class JpaUseMergeTest method produceExistingEntity.

@Test
public void produceExistingEntity() throws Exception {
    setUp("jpa://" + Customer.class.getName() + "?usePersist=false");
    final Customer customer = createDefaultCustomer();
    transactionTemplate.execute(new TransactionCallback<Object>() {

        public Object doInTransaction(TransactionStatus status) {
            entityManager.joinTransaction();
            entityManager.persist(customer);
            entityManager.flush();
            return null;
        }
    });
    assertEntitiesInDatabase(1, Customer.class.getName());
    assertEntitiesInDatabase(1, Address.class.getName());
    // do detach the persisted entity first before modifying it as we intend to merge it later on below
    entityManager.detach(customer);
    customer.setName("Max Mustermann");
    customer.getAddress().setAddressLine1("Musterstr. 1");
    customer.getAddress().setAddressLine2("11111 Enterhausen");
    Customer receivedCustomer = template.requestBody(endpoint, customer, Customer.class);
    assertEquals(customer.getName(), receivedCustomer.getName());
    assertNotNull(receivedCustomer.getId());
    assertEquals(customer.getAddress().getAddressLine1(), receivedCustomer.getAddress().getAddressLine1());
    assertEquals(customer.getAddress().getAddressLine2(), receivedCustomer.getAddress().getAddressLine2());
    assertNotNull(receivedCustomer.getAddress().getId());
    List<?> results = entityManager.createQuery("select o from " + Customer.class.getName() + " o").getResultList();
    assertEquals(1, results.size());
    Customer persistedCustomer = (Customer) results.get(0);
    assertEquals(receivedCustomer.getName(), persistedCustomer.getName());
    assertEquals(receivedCustomer.getId(), persistedCustomer.getId());
    assertEquals(receivedCustomer.getAddress().getAddressLine1(), persistedCustomer.getAddress().getAddressLine1());
    assertEquals(receivedCustomer.getAddress().getAddressLine2(), persistedCustomer.getAddress().getAddressLine2());
    assertEquals(receivedCustomer.getAddress().getId(), persistedCustomer.getAddress().getId());
}
Also used : Address(org.apache.camel.examples.Address) Customer(org.apache.camel.examples.Customer) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 4 with Customer

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

the class JpaWithNamedQueryAndParametersTest method assertReceivedResult.

protected void assertReceivedResult(Exchange exchange) {
    assertNotNull(exchange);
    Customer result = exchange.getIn().getBody(Customer.class);
    assertNotNull("Received a POJO", result);
    assertEquals("name property", "Willem", result.getName());
}
Also used : Customer(org.apache.camel.examples.Customer)

Example 5 with Customer

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

the class JpaPollingConsumerLockEntityTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    Customer customer = new Customer();
    customer.setName("Donald Duck");
    saveEntityInDB(customer);
    Customer customer2 = new Customer();
    customer2.setName("Goofy");
    saveEntityInDB(customer2);
    assertEntityInDB(2, Customer.class);
}
Also used : Customer(org.apache.camel.examples.Customer) Before(org.junit.Before)

Aggregations

Customer (org.apache.camel.examples.Customer)11 Test (org.junit.Test)7 Address (org.apache.camel.examples.Address)4 Exchange (org.apache.camel.Exchange)3 Processor (org.apache.camel.Processor)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 EntityManager (javax.persistence.EntityManager)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)1 SpringRouteBuilder (org.apache.camel.spring.SpringRouteBuilder)1 Before (org.junit.Before)1