Search in sources :

Example 6 with Consumer

use of org.springframework.integration.jpa.test.Consumer in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithJpaQueryAndDeletePerRow.

/**
 * In this test, a Jpa Polling Channel Adapter will use JpQL query
 * to retrieve a list of records from the database. Additionaly, the records
 * will be deleted after the polling.
 *
 * @throws Exception
 */
@Test
@DirtiesContext
public void testWithJpaQueryAndDeletePerRow() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(jpaOperations);
    jpaExecutor.setJpaQuery("from Student s");
    jpaExecutor.setDeleteAfterPoll(true);
    jpaExecutor.setDeleteInBatch(false);
    jpaExecutor.afterPropertiesSet();
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    final SourcePollingChannelAdapter adapter = JpaTestUtils.getSourcePollingChannelAdapter(jpaPollingChannelAdapter, this.outputChannel, this.poller, this.context, this.getClass().getClassLoader());
    adapter.start();
    Thread.sleep(1000);
    final Consumer consumer = new Consumer();
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    received.add(consumer.poll(10000));
    final Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull("Message is null.", message);
    assertNotNull(message.getPayload());
    final Collection<?> students = message.getPayload();
    assertTrue(students.size() == 3);
    Long studentCount = waitForDeletes(students);
    assertEquals(Long.valueOf(0), studentCount);
}
Also used : Consumer(org.springframework.integration.jpa.test.Consumer) Message(org.springframework.messaging.Message) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) ArrayList(java.util.ArrayList) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Collection(java.util.Collection) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 7 with Consumer

use of org.springframework.integration.jpa.test.Consumer in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithJpaQueryAndMaxResults.

/**
 * In this test, a Jpa Polling Channel Adapter will use JpQL query
 * to retrieve a list of records from the database with a maxRows value of 1.
 *
 * @throws Exception
 */
@Test
public void testWithJpaQueryAndMaxResults() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setJpaQuery("from Student");
    jpaExecutor.setMaxResultsExpression(new LiteralExpression("1"));
    jpaExecutor.afterPropertiesSet();
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    final SourcePollingChannelAdapter adapter = JpaTestUtils.getSourcePollingChannelAdapter(jpaPollingChannelAdapter, this.outputChannel, this.poller, this.context, this.getClass().getClassLoader());
    adapter.start();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    final Consumer consumer = new Consumer();
    received.add(consumer.poll(10000));
    Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull(message);
    assertNotNull(message.getPayload());
    Collection<?> primeNumbers = message.getPayload();
    assertTrue(primeNumbers.size() == 1);
}
Also used : Message(org.springframework.messaging.Message) Consumer(org.springframework.integration.jpa.test.Consumer) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) LiteralExpression(org.springframework.expression.common.LiteralExpression) ArrayList(java.util.ArrayList) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Collection(java.util.Collection) Test(org.junit.Test)

Example 8 with Consumer

use of org.springframework.integration.jpa.test.Consumer in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithEntityClass.

/**
 * In this test, a Jpa Polling Channel Adapter will use a plain entity class
 * to retrieve a list of records from the database.
 *
 * @throws Exception
 */
@Test
@DirtiesContext
public void testWithEntityClass() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setEntityClass(StudentDomain.class);
    jpaExecutor.afterPropertiesSet();
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    final SourcePollingChannelAdapter adapter = JpaTestUtils.getSourcePollingChannelAdapter(jpaPollingChannelAdapter, this.outputChannel, this.poller, this.context, this.getClass().getClassLoader());
    adapter.start();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    final Consumer consumer = new Consumer();
    received.add(consumer.poll(10000));
    Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull(message);
    assertNotNull(message.getPayload());
    Collection<?> primeNumbers = message.getPayload();
    assertTrue(primeNumbers.size() == 3);
}
Also used : Message(org.springframework.messaging.Message) Consumer(org.springframework.integration.jpa.test.Consumer) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) ArrayList(java.util.ArrayList) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Collection(java.util.Collection) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 9 with Consumer

use of org.springframework.integration.jpa.test.Consumer in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithNativeSqlQuery.

/**
 * In this test, a Jpa Polling Channel Adapter will use a Native SQL query
 * to retrieve a list of records from the database.
 *
 * @throws Exception
 */
@Test
public void testWithNativeSqlQuery() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setNativeQuery("select * from Student where lastName = 'Last One'");
    jpaExecutor.afterPropertiesSet();
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    final SourcePollingChannelAdapter adapter = JpaTestUtils.getSourcePollingChannelAdapter(jpaPollingChannelAdapter, this.outputChannel, this.poller, this.context, this.getClass().getClassLoader());
    adapter.start();
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final List<Message<Collection<?>>> received = new ArrayList<Message<Collection<?>>>();
    final Consumer consumer = new Consumer();
    received.add(consumer.poll(10000));
    Message<Collection<?>> message = received.get(0);
    adapter.stop();
    assertNotNull(message);
    assertNotNull(message.getPayload());
    Collection<?> students = message.getPayload();
    assertTrue(students.size() == 1);
}
Also used : Message(org.springframework.messaging.Message) Consumer(org.springframework.integration.jpa.test.Consumer) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) ArrayList(java.util.ArrayList) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)9 Collection (java.util.Collection)9 Test (org.junit.Test)9 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)9 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)9 Consumer (org.springframework.integration.jpa.test.Consumer)9 Message (org.springframework.messaging.Message)9 DirtiesContext (org.springframework.test.annotation.DirtiesContext)4 LiteralExpression (org.springframework.expression.common.LiteralExpression)1 StudentDomain (org.springframework.integration.jpa.test.entity.StudentDomain)1