Search in sources :

Example 1 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterTests method testWithNamedQuery.

/**
 * In this test, a Jpa Polling Channel Adapter will use Named query
 * to retrieve a list of records from the database.
 *
 * @throws Exception
 */
@Test
public void testWithNamedQuery() throws Exception {
    testTrigger.reset();
    // ~~~~SETUP~~~~~
    final JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setNamedQuery("selectStudent");
    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)

Example 2 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor in project spring-integration by spring-projects.

the class JpaOutboundChannelAdapterTests method saveEntityWithMergeWithoutSpecifyingEntityClass.

@Test
public void saveEntityWithMergeWithoutSpecifyingEntityClass() throws InterruptedException {
    List<?> results1 = this.jdbcTemplate.queryForList("Select * from Student");
    Assert.assertNotNull(results1);
    Assert.assertTrue(results1.size() == 3);
    JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setBeanFactory(mock(BeanFactory.class));
    jpaExecutor.afterPropertiesSet();
    final JpaOutboundGateway jpaOutboundChannelAdapter = new JpaOutboundGateway(jpaExecutor);
    jpaOutboundChannelAdapter.setProducesReply(false);
    StudentDomain testStudent = JpaTestUtils.getTestStudent();
    final Message<StudentDomain> message = MessageBuilder.withPayload(testStudent).build();
    TransactionTemplate transactionTemplate = new TransactionTemplate(this.transactionManager);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jpaOutboundChannelAdapter.handleMessage(message);
        }
    });
    List<?> results2 = this.jdbcTemplate.queryForList("Select * from Student");
    Assert.assertNotNull(results2);
    Assert.assertTrue(results2.size() == 4);
    Assert.assertNull(testStudent.getRollNumber());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) BeanFactory(org.springframework.beans.factory.BeanFactory) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 3 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor in project spring-integration by spring-projects.

the class JpaMessageHandlerParserTests method testProcedureParametersAreSet.

@SuppressWarnings("unchecked")
@Test
public void testProcedureParametersAreSet() throws Exception {
    setUp("JpaMessageHandlerParserTestsWithEmFactory.xml", getClass());
    final JpaExecutor jpaExecutor = TestUtils.getPropertyValue(this.consumer, "handler.jpaExecutor", JpaExecutor.class);
    final List<JpaParameter> jpaParameters = TestUtils.getPropertyValue(jpaExecutor, "jpaParameters", List.class);
    assertTrue(jpaParameters.size() == 3);
    JpaParameter parameter1 = jpaParameters.get(0);
    JpaParameter parameter2 = jpaParameters.get(1);
    JpaParameter parameter3 = jpaParameters.get(2);
    assertEquals("firstName", parameter1.getName());
    assertEquals("firstaName", parameter2.getName());
    assertEquals("updatedDateTime", parameter3.getName());
    assertEquals("kenny", parameter1.getValue());
    assertEquals("cartman", parameter2.getValue());
    assertNull(parameter3.getValue());
    assertNull(parameter1.getExpression());
    assertNull(parameter2.getExpression());
    assertEquals("new java.util.Date()", parameter3.getExpression());
}
Also used : JpaParameter(org.springframework.integration.jpa.support.JpaParameter) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) Test(org.junit.Test)

Example 4 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor in project spring-integration by spring-projects.

the class JpaMessageHandlerParserTests method testJpaMessageHandlerParser.

@Test
public void testJpaMessageHandlerParser() throws Exception {
    setUp("JpaMessageHandlerParserTests.xml", getClass());
    final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(this.consumer, "inputChannel", AbstractMessageChannel.class);
    assertEquals("target", inputChannel.getComponentName());
    final JpaExecutor jpaExecutor = TestUtils.getPropertyValue(this.consumer, "handler.jpaExecutor", JpaExecutor.class);
    assertNotNull(jpaExecutor);
    final String query = TestUtils.getPropertyValue(jpaExecutor, "jpaQuery", String.class);
    assertEquals("from Student", query);
    final JpaOperations jpaOperations = TestUtils.getPropertyValue(jpaExecutor, "jpaOperations", JpaOperations.class);
    assertNotNull(jpaOperations);
    final PersistMode persistMode = TestUtils.getPropertyValue(jpaExecutor, "persistMode", PersistMode.class);
    assertEquals(PersistMode.PERSIST, persistMode);
    @SuppressWarnings("unchecked") List<JpaParameter> jpaParameters = TestUtils.getPropertyValue(jpaExecutor, "jpaParameters", List.class);
    assertNotNull(jpaParameters);
    assertTrue(jpaParameters.size() == 3);
    assertEquals(Integer.valueOf(10), TestUtils.getPropertyValue(jpaExecutor, "flushSize", Integer.class));
    assertTrue(TestUtils.getPropertyValue(jpaExecutor, "clearOnFlush", Boolean.class));
}
Also used : PersistMode(org.springframework.integration.jpa.support.PersistMode) JpaParameter(org.springframework.integration.jpa.support.JpaParameter) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) JpaOperations(org.springframework.integration.jpa.core.JpaOperations) Test(org.junit.Test)

Example 5 with JpaExecutor

use of org.springframework.integration.jpa.core.JpaExecutor in project spring-integration by spring-projects.

the class JpaPollingChannelAdapterUnitTests method testReceiveNotNull.

/**
 */
@Test
public void testReceiveNotNull() {
    JpaExecutor jpaExecutor = mock(JpaExecutor.class);
    when(jpaExecutor.poll()).thenReturn("Spring");
    final JpaPollingChannelAdapter jpaPollingChannelAdapter = new JpaPollingChannelAdapter(jpaExecutor);
    assertNotNull("Expecting a Message to be returned.", jpaPollingChannelAdapter.receive());
    assertEquals("Spring", jpaPollingChannelAdapter.receive().getPayload());
}
Also used : JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)24 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)24 ArrayList (java.util.ArrayList)9 Collection (java.util.Collection)9 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)9 Consumer (org.springframework.integration.jpa.test.Consumer)9 Message (org.springframework.messaging.Message)9 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)7 JpaOperations (org.springframework.integration.jpa.core.JpaOperations)7 StudentDomain (org.springframework.integration.jpa.test.entity.StudentDomain)4 DirtiesContext (org.springframework.test.annotation.DirtiesContext)4 BeanFactory (org.springframework.beans.factory.BeanFactory)3 LiteralExpression (org.springframework.expression.common.LiteralExpression)3 JpaParameter (org.springframework.integration.jpa.support.JpaParameter)3 PersistMode (org.springframework.integration.jpa.support.PersistMode)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 JpaOutboundGateway (org.springframework.integration.jpa.outbound.JpaOutboundGateway)2 OutboundGatewayType (org.springframework.integration.jpa.support.OutboundGatewayType)2