Unit Test for BPMN file - unit-testing

Hi we are using Activiti 6.0.0 along with Spring-Boot application on Spring Microservices. I have to write an Unit tests on bpmn files which have DelegateExpression. Can anyone point me the some ideas for UnitTest?

I've done a fully tested activiti project before. upon your question i uploaded it on my github activiti-test, so you can access complete and executable source of this answer. This project is based on Maven, Activiti, Spring, H2 and JUnit. but if you want to run it over Spring Boot so you can see my other sample project Spring-boot kickstart sample.
The activit-test project structure :
Maven Project POM.xml
You can import (activiti-test) project as existing Maven Project in your IDE or anywhere you are comfortable.
Important dependency is :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
.
.
.
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
.
.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
.
.
.
2. applicationContext.xml
The important code block is starting activiti engine (bean: processEngineConfiguration)
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<!-- H2 -->
<property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
<property name="driverClass" value="org.h2.Driver" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="databaseSchemaUpdate" value="true" /> <!-- important and valid values : true,false,create-drop -->
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="jobExecutorActivate" value="true" />
<property name="asyncExecutorEnabled" value="true" />
<property name="asyncExecutorActivate" value="true" />
</bean>
3. Create activiti database Schema
At the sample code there is a class bean named InitDb that call
org.activiti.engine.impl.db.DbSchemaCreate.main(null);
4. Prepare Test environment
There is a class named BaseTest to make other testing easy :
first part get instance of activiti service and the second part is a method to deploy a process by BaseTest child class that comes in the next code block.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(BaseTest.APPCONTEXT)
public abstract class BaseTest implements ApplicationContextAware {
public static final String APPCONTEXT = "/applicationContext.xml";
Logger logger = Logger.getLogger(BaseTest.class);
public static ApplicationContext CONTEXT;
#Autowired(required=true)
protected ProcessDeployer processDeployer;
#Autowired
protected MyProcessEngine processEngine;
protected RuntimeService runtimeService;
protected TaskService taskService;
protected RepositoryService repositoryService;
protected HistoryService historyService;
#PostConstruct
public void init() {
runtimeService = processEngine.getProcessEngine().getRuntimeService();
taskService = processEngine.getProcessEngine().getTaskService();
repositoryService = processEngine.getProcessEngine().getRepositoryService();
historyService = processEngine.getProcessEngine().getHistoryService();
}
.
.
.
.
private List<String> deploymentIds = new ArrayList<String>();
#Before
public void deploy()
{
if(deploymentIds.size() > 0)
for(String deploymentId : deploymentIds)
repositoryService.deleteDeployment(deploymentId);
deploymentIds.clear();
long beforeCount = processDeployer.getDeployedCount();
logger.info("Number of Process Deployed Before start to deploy any new thing : " + beforeCount);
if(getBpmnFiles() != null)
for(String bpmn : getBpmnFiles())
{
if(bpmn != null)
deploymentIds.add(processDeployer.deploy(bpmn));
}
logger.info(processDeployer.getDeployedCount() + " Processes Deployed Successfully");
}
protected abstract String[] getBpmnFiles();
.
.
.
}
5. Simple Test Process Model
First of all we need a bpmn diagram you can find (SampleProcess.bpmn)
This process has a start event, one user task and an end event.
bpmn xml content is :
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1" targetNamespace="http://org.eclipse.bpmn2/default/process">
<bpmn2:process id="process_1" name="sampleProcess">
<bpmn2:startEvent id="StartEvent_1">
<bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
</bpmn2:startEvent>
<bpmn2:endEvent id="EndEvent_1">
<bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:sequenceFlow id="SequenceFlow_2" sourceRef="StartEvent_1" targetRef="UserTask_1"/>
<bpmn2:userTask id="UserTask_1" name="User Task 1">
<bpmn2:incoming>SequenceFlow_2</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_3" sourceRef="UserTask_1" targetRef="EndEvent_1"/>
</bpmn2:process>
</bpmn2:definitions>
6. Run a Test
After all you can run a test like this :
public class SampleProcessTest extends BaseTest {
#Override
protected String[] getBpmnFiles() {
return new String[] {"com/test/activiti/sampleProcess/SampleProcess.bpmn"};
}
#Test
public void sampleFlowTest()
{
ProcessInstance pi = super.runtimeService.startProcessInstanceByKey("process_1");
Task ut1 = super.taskService.createTaskQuery()
.processInstanceId(pi.getId())
.active()
.singleResult();
assertNotNull(ut1);
super.taskService.complete(ut1.getId());
HistoricProcessInstance historicProcess = super.historyService.createHistoricProcessInstanceQuery()
.processInstanceId(pi.getId())
.finished()
.singleResult();
assertNotNull("Process must be finished",historicProcess);
}
}
7.Delegate Expression example
At the source link there is lots of example of DelegateExpression, but for now we create a sample BPMN process has two service tasks that define by DelegateExpression to Autowire with Spring bean :
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="awProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask1" name="Service Task" activiti:delegateExpression="${awServiceTask1}"></serviceTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<serviceTask id="servicetask2" name="Service Task" activiti:delegateExpression="${com.test.activiti.autowiredservicetask.AWServiceTask2}"></serviceTask>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow3" sourceRef="servicetask2" targetRef="endevent1"></sequenceFlow>
</process>
</definitions>
and AWServiceTask1 :
#Component("awServiceTask1")
public class AWServiceTask1 implements JavaDelegate {
Logger logger = Logger.getLogger(AWServiceTask1.class);
#Override
public void execute(DelegateExecution execution) throws Exception {
logger.info("Object Identity : " + this.toString());
}
#PostConstruct
public void init()
{
logger.info("awServiceTask1 is created");
}
}
and similar way is the code of AWServiceTask2, you can find the fully source at activiti-test.
and test class is :
public class TestAWServiceTask extends BaseTest {
Logger logger = Logger.getLogger(TestAWServiceTask.class);
public static String KEY = "awProcess";
#Test
public void autoWireTest()
{
processDeployer.printAllProcessDefinition();
processEngine.getProcessEngine().getRuntimeService().startProcessInstanceByKey(KEY);
}
#Override
protected String[] getBpmnFiles() {
return new String[] {
"com/test/activiti/autowiredservicetask/AutoWireServiceTask.bpmn"};
}
}
I hope you got your desired answer

Related

Spring JMS Integration Unit Test leaves message on queue

I am using Spring 4.3.5.Release and ActiveMQ 5.14.3 to handle message queuing.
Here is my definition from the application context file:
<!-- Activemq connection factory -->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- brokerURL, You may have different IP or port -->
<constructor-arg index="0" value="${message.broker.url}" />
</bean>
<!-- Pooled Spring connection factory -->
<bean id="jmsConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
</bean>
<!-- ======================================================= -->
<!-- JMS Send, define default destination and JmsTemplate -->
<!-- ======================================================= -->
<!-- Default Destination Queue Definition -->
<bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- name of the queue -->
<constructor-arg index="0" value="${default.message.queue}" />
</bean>
<bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.DynamicDestinationResolver"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory"/>
<property name="defaultDestination" ref="defaultDestination" />
<property name="destinationResolver" ref="jmsDestinationResolver"/>
<property name="pubSubDomain" value="${pub.sub.domain}"/>
<property name="receiveTimeout" value="${receive.timeout}"/>
</bean>
And here is the code for creating a message on the default queue:
public boolean sendResponse(final MyObjectDTO myObject) {
boolean success = false;
this.jmsTemplate.convertAndSend(ebvResponse);
success = true;
return success;
}
Here is my unit test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations =
{ "classpath:/spring/my-platform-services-context.xml" })
#Transactional
public class MessageUtilTest extends TestCase {
#Autowired
private MessageUtil messageUtil;
#Test
public void testConvertSendMessageToDefault() throws JsonParseException, JsonMappingException, IOException {
MyObjectDTO myObject = new ManualCoverageDTO();
myObject.setMessage(message);
boolean success = messageUtil.sendResponse(myObject);
assertEquals(true, success);
}
}
This test works great, and a message gets on the queue correctly!
I expect when the test is over, because the unit test is Transactional, that the message would roll back off the queue, but it doesn't seem to be.
I know this is an "integrated" test since it is actually touching the ActiveMQ server and putting a message on the queue.
So, how can I make this really transactional, so that the message I just put on the queue really rolls back when it is done, do I have to manually tell this test to rollback?
I've have done hundreds of "integrated" unit tests with the database, and after every insert, update, or delete within one test, and the end of the test, the database is rolled back to the state it was before the test, I'd like the same thing to happen with my message queues or topics.
Any help with this would be great. Thanks!
You need to set sessionTransacted on the JmsTemplate to true.
But, bear in mind that you won't be able to receive the test message anyplace, unless you commit it.

Spring Test context scope

I am new in Spring test, and I have ran a lot of unit test successfully according to the documents step by step,however I have some questions:
1 Can all the TestCase use a global Spring Context
Now I configure each TeseCase the spring context like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
#Transactional
public class UserDaoTests {}
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
#Transactional
public class AccoutDaoTests {}
As shown, spring will load and destroy the same context again and again.
So I wonder if I can setup a global Spring context, and then make all the TestCases run inside this context?
2 Transaction management
It said that the Transaction Manager will rollback the operations to the database.
But I have not found in which case this feature will work.
Because in my application I use ORMLite instead of Spring JDBC.
And this is the configuration:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- our daos -->
<bean id="ormliteSource" class="com.j256.ormlite.jdbc.DataSourceConnectionSource" init-method="initialize">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1" value="${jdbc.url}"/>
</bean>
<bean id="userDao" class="com.j256.ormlite.spring.DaoFactory" factory-method="createDao">
<constructor-arg index="0" ref="ormliteSource"/>
<constructor-arg index="1" value="com.springapp.model.User"/>
</bean>
And then in my test case:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(..)
#Transactional
public class UserDaoTests {
#Autowired
private Dao<User, Long> userDao;
#Test
public void testCreate() {
User u = new User();
u.setName("ysl");
u.setLocked(true);
try {
userDao.create(u);
} catch (SQLException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
Then I run the test, and I found that all the tests are successfully passed, but when I check the database, I found that there are some test data inserted, it seems that the rollback does not work.
Do I miss anything?
Spring should detect if it can setup a shared application context for you. Did you specify different xmls in different test cases? If your test case could run against a shared application context, you may try with:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(.....)
public abstract class AbstractSpringTests
public class ConcreteTests1 extends AbstractSpringTests
public class ConcreteTests2 extends AbstractSpringTests
In this case, spring should setup only one application context.
According to the reference, you should add #TransactionConfiguration instead of #Transactinal on test class(rollback is the default strategy if I'm not mistaken)
If you want a transaction to commit — unusual, but occasionally useful when you want a particular
test to populate or modify the database — the TestContext framework can be instructed to cause the
transaction to commit instead of roll back via the #TransactionConfiguration and #Rollback
annotations.

access the jaxb2 marshaller in endpoint handler (spring ws)

I am using Spring-WS 1.5. I have the following configuration in my spring-servlet.xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.test.mypackage"/>
<bean class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
<constructor-arg ref="jaxbmarshaller"/>
</bean>
<bean id="jaxbmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.test.myclass1</value>
<value>com.test.myclass2</value>
</list>
</property>
</bean>
<bean id="endpointMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
<property name="interceptors">
<list>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
</list>
</property>
</bean>
</beans>
The 'GenericMarshallingMethodEndpointAdapter' is unmarshalling the request and marshalling the response with this configuration and working as expected. However, I would like to marshal the request within my endpoint method handler and would like to access the marshaller. How do I access the marshaller. Do I have access to the one supplied to the GenericMarshallingMethodEndpointAdapter.
#Endpoint
public class MyEndpoint {
private static final String NAMESPACE_URI = "http://www.test.org/9";
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "RequestData")
public JAXBElement<ResponseType> myEndpointHandler(JAXBElement<RequestType> request) {
/* how do I access the marshaller here to marshal the request */
}
}
You can autowire it:
#Service
public class MarshallingServiceImpl implements MarshallingService{
#Autowired
private Jaxb2Marshaller jaxbmarshaller;
#Override
public String marshal(Entity entity) {
StringResult marshalled = new StringResult();
jaxbmarshaller.marshal(entity, marshalled);
return marshalled.toString();
}
}

EntityManagerFactory not being injected using #PersistenceUnit

I'm a java beginner. I'm in trouble to configure a persistance unit using JTA transactions.
I need to use a PostgreSQL database that is already defined, configured and populated. Using netbeans, i created the persistance.xml and glassfish-resources.xml as fallows:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="WellWatcherPU" transaction-type="JTA">
<jta-data-source>WellWatcherDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
and
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.postgresql.ds.PGSimpleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="post-gre-sql_geowellex_geowellexPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="5432"/>
<property name="databaseName" value="DBNAME"/>
<property name="User" value="USER"/>
<property name="Password" value="PASSWORD"/>
<property name="URL" value="jdbc:postgresql://localhost:5432/DBNAME"/>
<property name="driverClass" value="org.postgresql.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="WellWatcherDB" object-type="user" pool-name="post-gre-sql_geowellex_geowellexPool"/>
</resources>
And this is how i get the EntityManagerFactory and EntityManager (as used in the netBeans example)
public class EUserDao {
#Resource
private UserTransaction utx = null;
#PersistenceUnit(unitName = "WellWatcherPU")
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager(); <-------- NullPointerException here
}
public EUser getOne(long userId){
EntityManager em = getEntityManager();
try {
return em.find(EUser.class, userId);
} finally {
em.close();
}
}
EDIT:
And here is my glassfish deploy log:
Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(1901223982)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--connecting(DatabaseLogin(
platform=>DatabasePlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(1462281761)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--Connected: jdbc:postgresql://localhost:5432/geowellex?loginTimeout=0&prepareThreshold=0
User: geowellex
Database: PostgreSQL Version: 9.1.3
Driver: PostgreSQL Native Driver Version: PostgreSQL 8.3 JDBC3 with SSL (build 603)
Informações: [EL Config]: 2012-05-10 12:01:13.534--ServerSession(2017352940)--Connection(766700859)--Thread(Thread[admin-thread-pool-4848(5),5,grizzly-kernel])--connecting(DatabaseLogin(
platform=>PostgreSQLPlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
What's wrong?
Most likely problem is that your EUserDao is just regular class. Injection works only for container managed classes. Annotations like #PersistenceUnit and #Resource are not processed for normal classes.
Following types of classes are container managed classes (and in those #PersistenceUnit can be used):
Servlet: servlets, servlet filters, event listeners
JSP: tag handlers, tag library event listeners
JSF: scoped managed beans
JAX-WS: service endpoints, handlers
EJB: beans, interceptors
Managed Beans: managed beans
CDI: CDI-style managed beans, decorators
Java EE Platform: main class (static), login callback handler
I see that in your code declare:
private EntityManagerFactory emf = null;
but never create one... like this
emf = Persistence.createEntityManagerFactory("WellWatcherPU");
Thats why you get a Null Pointer Exception when use the object!
public EntityManager getEntityManager() {
return emf.createEntityManager(); <-------- NullPointerException here
}

property injection in apache cxf using spring

I have the following class
public class HeaderClass{
#Resource
private WebServiceContext webServiceContext;
public String getUserAgent() {
MessageContext msgCtx = webServiceContext.getMessageContext();
HttpServletRequest request = (HttpServletRequest)msgCtx.get(AbstractHTTPDestination.HTTP_REQUEST);
return request.getHeader("user-agent")
}
In my service bean class I want to inject this HeaderClass, so that I can use it there as follows:
package mypack;
#Path("/MyService")
public class MyServiceClass {
//May be some annotation has to be given here which I don't know
HeaderClass header;
public void useHeader() {
//Code to use the header
System.out.println(header.getUserAgent());
}
}
I have the following inside beans.xml file
<jaxrs:server id="SampleService" address="/">
<jaxrs:features>
<cxf:logging />
</jaxrs:features>
<jaxrs:serviceBeans>
<ref bean="MyServiceClass"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="MyServiceClass" class="mypack.MyServiceClass"/>
I don't know how to add the property HeaderClass in the bean "MyServiceClass"
I am using apache cxf with spring configuration file (beans.xml).
Please help.
One way to achieve this is to add those lines to your beans.xml:
<bean id="HeaderClass" class="mypack.HeaderClass"/>
<bean id="MyServiceClass" class="mypack.MyServiceClass">
<property name="header" ref="HeaderClass" />
</bean>
You may also need to add a setHeader() method to your MyServiceClass.