INT-2571 make use of use-payload-as-parameter-source in JPAExecutor
INT-2571: Suggested changes made and added an additional test case INT-2571: Fix the javadoc generation warning in the class INT-2571 Polishing Factor out common code into determineParamterSource()
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<import resource="classpath:/hibernateJpa-context.xml" />
|
||||
|
||||
</beans>
|
||||
@@ -14,20 +14,39 @@ package org.springframework.integration.jpa.core;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.jpa.support.JpaParameter;
|
||||
import org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory;
|
||||
import org.springframework.integration.jpa.test.entity.StudentDomain;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @author Amol Nayak
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class JpaExecutorTests {
|
||||
|
||||
@Autowired
|
||||
protected EntityManager entityManager;
|
||||
|
||||
/**
|
||||
* In this test, the {@link JpaExecutor}'s poll method will be called without
|
||||
* specifying a 'query', 'namedQuery' or 'entityClass' property. This should
|
||||
@@ -69,4 +88,122 @@ public class JpaExecutorTests {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void testSetMultipleQueryTypes() {
|
||||
JpaExecutor executor = new JpaExecutor(mock(EntityManager.class));
|
||||
executor.setJpaQuery("select s from Student s");
|
||||
Assert.assertNotNull(executor.getJpaQuery());
|
||||
|
||||
try {
|
||||
executor.setNamedQuery("NamedQuery");
|
||||
} catch (IllegalArgumentException e) {
|
||||
Assert.assertEquals("You can define only one of the "
|
||||
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'", e.getMessage());
|
||||
}
|
||||
Assert.assertNull(executor.getNamedQuery());
|
||||
|
||||
try {
|
||||
executor.setNativeQuery("select * from Student");
|
||||
} catch (IllegalArgumentException e) {
|
||||
Assert.assertEquals("You can define only one of the "
|
||||
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'", e.getMessage());
|
||||
}
|
||||
Assert.assertNull(executor.getNativeQuery());
|
||||
|
||||
executor = new JpaExecutor(mock(EntityManager.class));
|
||||
executor.setNamedQuery("NamedQuery");
|
||||
Assert.assertNotNull(executor.getNamedQuery());
|
||||
|
||||
try {
|
||||
executor.setJpaQuery("select s from Student s");
|
||||
} catch (IllegalArgumentException e) {
|
||||
Assert.assertEquals("You can define only one of the "
|
||||
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'", e.getMessage());
|
||||
}
|
||||
Assert.assertNull(executor.getJpaQuery());
|
||||
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void updateWithMessageAsParameterSource() {
|
||||
String query = "update Student s set s.firstName = :firstName where s.lastName = 'Last One'";
|
||||
Message<Map<String, String>> message =
|
||||
MessageBuilder.withPayload(Collections.singletonMap("firstName", "First One")).build();
|
||||
JpaExecutor executor = getJpaExecutorForMessageAsParamSource(query);
|
||||
Integer rowsAffected = (Integer) executor.executeOutboundJpaOperation(message);
|
||||
Assert.assertTrue(1 == rowsAffected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void updateWithPayloadAsParameterSource() {
|
||||
String query = "update Student s set s.firstName = :firstName where s.lastName = 'Last One'";
|
||||
Message<String> message =
|
||||
MessageBuilder.withPayload("First One").build();
|
||||
JpaExecutor executor = getJpaExecutorForPayloadAsParamSource(query);
|
||||
Integer rowsAffected = (Integer) executor.executeOutboundJpaOperation(message);
|
||||
Assert.assertTrue(1 == rowsAffected);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param query
|
||||
* @return
|
||||
*/
|
||||
private JpaExecutor getJpaExecutorForMessageAsParamSource(String query) {
|
||||
JpaExecutor executor = new JpaExecutor(entityManager);
|
||||
ExpressionEvaluatingParameterSourceFactory factory =
|
||||
new ExpressionEvaluatingParameterSourceFactory();
|
||||
factory.setParameters(
|
||||
Collections.singletonList(new JpaParameter("firstName", null, "payload['firstName']")));
|
||||
executor.setParameterSourceFactory(factory);
|
||||
executor.setJpaQuery(query);
|
||||
executor.setExpectSingleResult(true);
|
||||
executor.setUsePayloadAsParameterSource(false);
|
||||
executor.afterPropertiesSet();
|
||||
return executor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param query
|
||||
* @return
|
||||
*/
|
||||
private JpaExecutor getJpaExecutorForPayloadAsParamSource(String query) {
|
||||
JpaExecutor executor = new JpaExecutor(entityManager);
|
||||
ExpressionEvaluatingParameterSourceFactory factory =
|
||||
new ExpressionEvaluatingParameterSourceFactory();
|
||||
factory.setParameters(
|
||||
Collections.singletonList(new JpaParameter("firstName", null, "#this")));
|
||||
executor.setParameterSourceFactory(factory);
|
||||
executor.setJpaQuery(query);
|
||||
executor.setExpectSingleResult(true);
|
||||
executor.setUsePayloadAsParameterSource(true);
|
||||
executor.afterPropertiesSet();
|
||||
return executor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user