Showing posts with label Process Flow Automation. Show all posts
Showing posts with label Process Flow Automation. Show all posts

Monday, August 24, 2020

Camunda BPMN Automation


Configuring the Maven in the Eclipse

To use proxy settings in eclipse while using maven, follow these steps:
·         Open your Eclipse and go to Window -> Preferences.
·         Click on the Browse button of User Settings, and select the settings.xml.




Installing the Camunda Archetype

·       For testing we need to add camunda’s maven archetype. For that navigate to https://docs.camunda.org/manual/7.12/user-guide/process-applications/maven-archetypes/


  • Add archetype catalog (Preferences -> Maven -> Archetypes -> AddRemoteCatalog): https://app.camunda.com/nexus/content/repositories/camunda-bpm/



Creating Camunda Maven Project

       ·       Create Maven project from archetype (File -> New -> Project..-> Maven -> Maven Project)
     ·       Select camunda-archetype-servlet-war from the catalog that you created before.
         ·       The resulting project look like this , the BPMN files should add into the src/main/resources
           ·       Create the java classes at src/test/java
    package explorer of Eclipse 
    ·       After the Execution the output can be viewed in the target folder.
Example of Bpmn File




  • Add TestNG to the project .
sample code:

package com.camunda.camundaDemo;

import static org.assertj.core.api.Assertions.assertThat;
import static org.camunda.bpm.engine.test.assertions.ProcessEngineAssertions.init;
import static org.camunda.bpm.engine.test.assertions.ProcessEngineTests.runtimeService;
import static org.junit.Assert.assertEquals;

import java.util.HashMap;

import org.apache.ibatis.logging.LogFactory;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.camunda.bpm.extension.process_test_coverage.junit.rules.TestCoverageProcessEngineRuleBuilder;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

/**
 * Test case starting an in-memory database-backed Process Engine.
 */
@Deployment(resources = { "process.bpmn" })
public class ProcessUnitTestCustom {

	@ClassRule
	@Rule
	public static ProcessEngineRule rule = TestCoverageProcessEngineRuleBuilder.create().withDetailedCoverageLogging()
			.build();

	private static final String PROCESS_DEFINITION_KEY = "camundaDemo";

	static {
		LogFactory.useSlf4jLogging();
	}

	@Before
	public void setup() {
		init(rule.getProcessEngine());
	}

	RuntimeService runtimeService;
	ProcessInstance processInstance;
	TaskService taskService;
	Task checkWeatherTask;
	@Test
	
	public void testHappyPath() {




			HashMap<String, Object> hm = new HashMap<String, Object>();
			hm.put("weatherOk", true);

			ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("camundaDemo", hm);

			processInstance.getProcessDefinitionId();
			processInstance.getId();
			// Now: Drive the process by API and assert correct behavior by
			// camunda-bpm-assert
			
			assertThat(processInstance.isEnded()).isTrue();

		}

	

	 @Test
	 @Deployment(resources = { "process.bpmn" })
	public void testNotHappyPath() {
		// ProcessInstance processInstance =
		// runtimeService().startProcessInstanceByKey(PROCESS_DEFINITION_KEY);

		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("weatherOk", false);

		ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("camundaDemo", hm);

		processInstance.getProcessDefinitionId();
		processInstance.getId();
		// Now: Drive the process by API and assert correct behavior by
		// camunda-bpm-assert
		
		assertThat(processInstance.isEnded()).isTrue();

	}

	/***
	 * Fetch the next immediate test from the task service.
	 * 
	 * @param taskService Current Task service.
	 * @return Next Task.
	 */
	public Task getNextTask(TaskService taskService) {
		return taskService.createTaskQuery().singleResult();
	}

}
Output:

Full Test Coverage :



output of testHappyPath()



Output of  testNotHappyPath()