Search in sources :

Example 6 with Customer

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

the class JpaProducerWithQueryTest method testProducerWithNamedQuery.

@Test
public void testProducerWithNamedQuery() throws Exception {
    template.sendBody("direct:deleteCustomers", "");
    Customer c1 = new Customer();
    c1.setName("Willem");
    template.sendBody("direct:addCustomer", c1);
    Customer c2 = new Customer();
    c2.setName("Dummy");
    template.sendBody("direct:addCustomer", c2);
    Object answer = template.requestBody("direct:namedQuery", "Willem");
    List list = (List) answer;
    assertEquals(1, list.size());
    assertEquals("Willem", ((Customer) list.get(0)).getName());
    answer = template.requestBody("direct:deleteCustomers", "");
    assertEquals(2, ((Integer) answer).intValue());
}
Also used : Customer(org.apache.camel.examples.Customer) List(java.util.List) Test(org.junit.Test)

Example 7 with Customer

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

the class JpaPollingConsumerLockEntityTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() {
    return new SpringRouteBuilder() {

        public void configure() {
            AggregationStrategy enrichStrategy = new AggregationStrategy() {

                @Override
                public Exchange aggregate(Exchange originalExchange, Exchange jpaExchange) {
                    Customer customer = jpaExchange.getIn().getBody(Customer.class);
                    customer.setOrderCount(customer.getOrderCount() + 1);
                    return jpaExchange;
                }
            };
            onException(Exception.class).setBody().simple("${exception}").to("mock:error").handled(true);
            from("direct:locked").onException(OptimisticLockException.class).redeliveryDelay(60).maximumRedeliveries(2).end().pollEnrich().simple("jpa://" + Customer.class.getName() + "?lockModeType=OPTIMISTIC_FORCE_INCREMENT&query=select c from Customer c where c.name like '${header.name}'").aggregationStrategy(enrichStrategy).to("jpa://" + Customer.class.getName()).setBody().simple("orders: ${body.orderCount}").to("mock:locked");
            from("direct:not-locked").pollEnrich().simple("jpa://" + Customer.class.getName() + "?query=select c from Customer c where c.name like '${header.name}'").aggregationStrategy(enrichStrategy).to("jpa://" + Customer.class.getName()).setBody().simple("orders: ${body.orderCount}").to("mock:not-locked");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) Customer(org.apache.camel.examples.Customer) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

Example 8 with Customer

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

the class JpaPollingConsumerTest method testPollingConsumer.

@Test
public void testPollingConsumer() throws Exception {
    Customer customer = new Customer();
    customer.setName("Donald Duck");
    saveEntityInDB(customer);
    Customer customer2 = new Customer();
    customer2.setName("Goofy");
    saveEntityInDB(customer2);
    assertEntitiesInDatabase(2, Customer.class.getName());
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello Donald Duck how are you today?");
    template.sendBodyAndHeader("direct:start", "Hello NAME how are you today?", "name", "Donald%");
    assertMockEndpointsSatisfied();
}
Also used : Customer(org.apache.camel.examples.Customer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 9 with Customer

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

the class JpaWithNamedQueryAndParametersTest 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
            Customer dummy = new Customer();
            dummy.setName("Test");
            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) {
            Customer customer = new Customer();
            customer.setName("Willem");
            exchange.getIn().setBody(customer);
        }
    });
    // now lets assert that there is a result
    results = entityManager.createQuery(queryText).getResultList();
    assertEquals("Should have results: " + results, 1, results.size());
    Customer customer = (Customer) results.get(0);
    assertEquals("name property", "Willem", customer.getName());
    // 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(10, TimeUnit.SECONDS));
    assertReceivedResult(receivedExchange);
    JpaConsumer jpaConsumer = (JpaConsumer) consumer;
    assertURIQueryOption(jpaConsumer);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Customer(org.apache.camel.examples.Customer) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 10 with Customer

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

the class AbstractJpaMethodTest method createDefaultCustomer.

protected Customer createDefaultCustomer() {
    Customer customer = new Customer();
    customer.setName("Christian Mueller");
    Address address = new Address();
    address.setAddressLine1("Hahnstr. 1");
    address.setAddressLine2("60313 Frankfurt am Main");
    customer.setAddress(address);
    return customer;
}
Also used : Address(org.apache.camel.examples.Address) Customer(org.apache.camel.examples.Customer)

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