Search in sources :

Example 11 with StudentDomain

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

the class JpaPollingChannelAdapterTests method testWithJpaQueryOneResultOnly.

/**
 * In this test, a Jpa Polling Channel Adapter will use JpQL query
 * to retrieve a list of records from the database.
 *
 * @throws Exception
 */
@Test
public void testWithJpaQueryOneResultOnly() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setJpaQuery("from Student s where s.firstName = 'First Two'");
    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);
    StudentDomain student = (StudentDomain) students.iterator().next();
    assertEquals("Last Two", student.getLastName());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) 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)

Example 12 with StudentDomain

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

the class JpaExecutorTests method selectWithMessageAsParameterSource.

@Test
@Transactional
public void selectWithMessageAsParameterSource() {
    String query = "select s from Student s where s.firstName = :firstName";
    Message<Map<String, String>> message = MessageBuilder.withPayload(Collections.singletonMap("firstName", "First One")).build();
    JpaExecutor executor = getJpaExecutorForMessageAsParamSource(query);
    StudentDomain student = (StudentDomain) executor.poll(message);
    Assert.assertNotNull(student);
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) Map(java.util.Map) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with StudentDomain

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

the class JpaExecutorTests method selectWithPayloadAsParameterSource.

@Test
@Transactional
public void selectWithPayloadAsParameterSource() {
    String query = "select s from Student s where s.firstName = :firstName";
    Message<String> message = MessageBuilder.withPayload("First One").build();
    JpaExecutor executor = getJpaExecutorForPayloadAsParamSource(query);
    StudentDomain student = (StudentDomain) executor.poll(message);
    Assert.assertNotNull(student);
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with StudentDomain

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

the class AbstractJpaOperationsTests method testPersist.

/**
 * Test method for {@link org.springframework.integration.jpa.core.DefaultJpaOperations#persist(java.lang.Object)}.
 */
public void testPersist() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final StudentDomain student = JpaTestUtils.getTestStudent();
    Assert.assertNull(student.getRollNumber());
    jpaOperations.persist(student, 1, false);
    Assert.assertNotNull(student.getRollNumber());
    assertTrue(entityManager.contains(student));
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain)

Example 15 with StudentDomain

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

the class AbstractJpaOperationsTests method testPersistCollection.

public void testPersistCollection() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final StudentDomain student1 = JpaTestUtils.getTestStudent();
    final StudentDomain student2 = JpaTestUtils.getTestStudent();
    final StudentDomain student3 = JpaTestUtils.getTestStudent();
    student1.setFirstName("Karl");
    student2.setFirstName("Otto");
    student3.setFirstName("Wilhelm");
    List<StudentDomain> students = new ArrayList<StudentDomain>(3);
    students.add(student1);
    students.add(student2);
    students.add(student3);
    Assert.assertNull(student1.getRollNumber());
    Assert.assertNull(student2.getRollNumber());
    Assert.assertNull(student3.getRollNumber());
    jpaOperations.persist(students, 1, true);
    Assert.assertNotNull(student1.getRollNumber());
    Assert.assertNotNull(student2.getRollNumber());
    Assert.assertNotNull(student3.getRollNumber());
    assertFalse(entityManager.contains(student1));
    assertFalse(entityManager.contains(student2));
    assertFalse(entityManager.contains(student3));
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) ArrayList(java.util.ArrayList)

Aggregations

StudentDomain (org.springframework.integration.jpa.test.entity.StudentDomain)35 Test (org.junit.Test)21 BeanFactory (org.springframework.beans.factory.BeanFactory)7 ArrayList (java.util.ArrayList)5 TransactionStatus (org.springframework.transaction.TransactionStatus)5 Transactional (org.springframework.transaction.annotation.Transactional)5 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)4 ExpressionEvaluatingParameterSourceFactory (org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory)4 ParameterSource (org.springframework.integration.jpa.support.parametersource.ParameterSource)4 Calendar (java.util.Calendar)3 Date (java.util.Date)3 ParameterSourceFactory (org.springframework.integration.jpa.support.parametersource.ParameterSourceFactory)3 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 List (java.util.List)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 Map (java.util.Map)1