From 05835c051b57d3231e3ddf8dc160f1477a6494ca Mon Sep 17 00:00:00 2001
From: Thomas Lenz <thomas.lenz@egiz.gv.at>
Date: Thu, 21 Nov 2019 08:59:34 +0100
Subject: add stop condition to process-flow engine that can be set dynamically
 by tasks

---
 .../core/impl/idp/process/test/HalloWeltTask.java  |  2 +-
 .../core/impl/idp/process/test/HelloWorldTask.java |  2 +-
 .../impl/idp/process/test/ProcessEngineTest.java   | 96 ++++++++++++++++------
 .../impl/idp/process/test/StopProcessFlagTask.java | 52 ++++++++++++
 .../impl/idp/process/test/ThrowExceptionTask.java  | 52 ++++++++++++
 5 files changed, 175 insertions(+), 29 deletions(-)
 create mode 100644 eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/StopProcessFlagTask.java
 create mode 100644 eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ThrowExceptionTask.java

(limited to 'eaaf_core/src/test/java/at/gv')

diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HalloWeltTask.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HalloWeltTask.java
index 40399e58..743a61da 100644
--- a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HalloWeltTask.java
+++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HalloWeltTask.java
@@ -44,7 +44,7 @@ public class HalloWeltTask implements Task {
 	@Override
 	public IRequest execute(IRequest pendingReq, ExecutionContext executionContext) {
 		System.out.println("Hallo Welt");
-		return null;
+		return pendingReq;
 	}
 
 }
diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HelloWorldTask.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HelloWorldTask.java
index a2d8b3fb..c6da16b4 100644
--- a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HelloWorldTask.java
+++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/HelloWorldTask.java
@@ -44,7 +44,7 @@ public class HelloWorldTask implements Task {
 	@Override
 	public IRequest execute(IRequest pendingReq, ExecutionContext executionContext) {
 		System.out.println("Hello World");
-		return null;
+		return pendingReq;
 	}
 
 }
diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ProcessEngineTest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ProcessEngineTest.java
index 78fdde61..dc45534e 100644
--- a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ProcessEngineTest.java
+++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ProcessEngineTest.java
@@ -33,6 +33,7 @@ import static org.junit.Assert.assertEquals;
 import java.io.IOException;
 import java.io.InputStream;
 
+import org.apache.commons.lang3.RandomStringUtils;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -44,6 +45,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 import at.gv.egiz.eaaf.core.api.idp.process.ProcessEngine;
 import at.gv.egiz.eaaf.core.exceptions.ProcessExecutionException;
+import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException;
 import at.gv.egiz.eaaf.core.impl.idp.module.test.TestRequestImpl;
 import at.gv.egiz.eaaf.core.impl.idp.process.ProcessDefinitionParser;
 import at.gv.egiz.eaaf.core.impl.idp.process.ProcessDefinitionParserException;
@@ -64,7 +66,7 @@ public class ProcessEngineTest {
 	public void init() throws IOException, ProcessDefinitionParserException {
 		
 		if (!isInitialized) {
-			ProcessDefinitionParser pdp = new ProcessDefinitionParser();
+			final ProcessDefinitionParser pdp = new ProcessDefinitionParser();
 
 			if (pe == null) {
 				pe = applicationContext.getBean("processEngine", ProcessEngine.class);
@@ -79,29 +81,19 @@ public class ProcessEngineTest {
 				((ProcessEngineImpl) pe).registerProcessDefinition(pdp.parse(in));
 			}
 
+			try (InputStream in = ProcessEngineTest.class.getResourceAsStream("/process/test/SampleProcessDefinition4.xml")) {
+				((ProcessEngineImpl) pe).registerProcessDefinition(pdp.parse(in));
+			}
+			
+			try (InputStream in = ProcessEngineTest.class.getResourceAsStream("/process/test/SampleProcessDefinition5.xml")) {
+				((ProcessEngineImpl) pe).registerProcessDefinition(pdp.parse(in));
+			}
+			
 			//initHibernateForTesting();
 			isInitialized = true;
 		}
 	}
 	
-	private static void initHibernateForTesting() throws IOException{
-
-//		InputStream in = ProcessEngineTest.class.getResourceAsStream("/at/gv/egovernment/moa/id/process/hibernate.configuration.test.properties");
-//		Properties props = new Properties();
-//		props.load(in);
-//
-//		try {
-//			//ConfigurationDBUtils.initHibernate(props);
-//			Configuration config = new Configuration();
-//			config.addProperties(props);
-//			//config.addAnnotatedClass(ProcessInstanceStore.class);
-//			config.addAnnotatedClass(AssertionStore.class);
-//			//MOASessionDBUtils.initHibernate(config, props);
-//		} catch (Exception e) {
-//			e.printStackTrace();
-//		}
-	}
-	
 	@Test
 	public void wrongProcessDefinition() throws IOException {
 		try (InputStream in = ProcessEngineTest.class.getResourceAsStream("/process/test/SampleProcessDefinition3.xml")) {
@@ -109,7 +101,7 @@ public class ProcessEngineTest {
 				((ProcessEngineImpl) pe).registerProcessDefinition(in);
 				Assert.fail();
 				
-			} catch (ProcessDefinitionParserException e) {
+			} catch (final ProcessDefinitionParserException e) {
 				Assert.assertTrue(e.getMessage().contains("Post-validation find an error in process definition"));
 			}
 		}
@@ -119,9 +111,9 @@ public class ProcessEngineTest {
 	@Test
 	public void testSampleProcess1() throws IOException, ProcessDefinitionParserException, ProcessExecutionException {
 		
-		TestRequestImpl testReq = new TestRequestImpl();
+		final TestRequestImpl testReq = new TestRequestImpl();
 		
-		String piId = pe.createProcessInstance("SampleProcess1");
+		final String piId = pe.createProcessInstance("SampleProcess1");
 		ProcessInstance pi = pe.getProcessInstance(piId);
 		assertEquals(NOT_STARTED, pi.getState());
 
@@ -139,7 +131,7 @@ public class ProcessEngineTest {
 			throw new ProcessExecutionException("ProcessInstance should be removed already, but it was found.");
 			//assertEquals(ENDED, pi.getState());
 			
-		} catch (IllegalArgumentException e) {
+		} catch (final IllegalArgumentException e) {
 			// do nothing because processInstance should be already removed 
 			
 		}
@@ -148,9 +140,9 @@ public class ProcessEngineTest {
 	@Test
 	public void testSampleProcess2() throws IOException, ProcessDefinitionParserException, ProcessExecutionException {
 
-		TestRequestImpl testReq = new TestRequestImpl();
+		final TestRequestImpl testReq = new TestRequestImpl();
 		
-		String piId = pe.createProcessInstance("SampleProcess2");
+		final String piId = pe.createProcessInstance("SampleProcess2");
 		ProcessInstance pi = pe.getProcessInstance(piId);
 		assertEquals(NOT_STARTED, pi.getState());
 
@@ -168,14 +160,64 @@ public class ProcessEngineTest {
 			throw new ProcessExecutionException("ProcessInstance should be removed already, but it was found.");
 			//assertEquals(ENDED, pi.getState());
 			
-		} catch (IllegalArgumentException e) {
+		} catch (final IllegalArgumentException e) {
 			// do nothing because processInstance should be already removed 
 			
 		}
+				
+	}
+
+	@Test
+	public void testSampleProcess4() throws IOException, ProcessDefinitionParserException, ProcessExecutionException {
+
+		final TestRequestImpl testReq = new TestRequestImpl();
+		testReq.setPendingReqId(RandomStringUtils.randomAlphanumeric(5));
 		
-		
+		final String piId = pe.createProcessInstance("SampleProcess4");
+		final ProcessInstance pi = pe.getProcessInstance(piId);
+		assertEquals(NOT_STARTED, pi.getState());
+
+		// start process
+		testReq.setProcessInstanceID(piId);
+		try {
+			pe.start(testReq);
+			Assert.fail("Task exception not handled");
+			
+		} catch (final ProcessExecutionException e1) {			
+			org.springframework.util.Assert.isInstanceOf(TaskExecutionException.class, e1.getCause(), "No TaskExecutionException");
+			Assert.assertEquals("Wrong error-msg", "jUnit Test", e1.getCause().getMessage());
+			Assert.assertEquals("Wrong pendingReqId", testReq.getPendingRequestId(), ((TaskExecutionException) e1.getCause()).getPendingRequestID());
+			org.springframework.util.Assert.isInstanceOf(RuntimeException.class, e1.getCause().getCause(), "Wrong Exception in TaskExecutionException");
+		}
+	
+				
 	}
+	
+	@Test
+	public void testSampleProcess5() throws IOException, ProcessDefinitionParserException, ProcessExecutionException {
 
+		final TestRequestImpl testReq = new TestRequestImpl();
+		
+		final String piId = pe.createProcessInstance("SampleProcess5");
+		ProcessInstance pi = pe.getProcessInstance(piId);
+		assertEquals(NOT_STARTED, pi.getState());
+
+		// start process
+		testReq.setProcessInstanceID(piId);
+		pe.start(testReq);
+
+		try {
+			pi = pe.getProcessInstance(piId);
+			
+		} catch (final IllegalArgumentException e) {
+			Assert.assertTrue("wrong error-msg", e.getMessage().contains("does not/no longer exist."));
+			Assert.assertTrue("wrong process-instance-id", e.getMessage().contains(piId));
+			
+		}
+		
+		
+	}
+	
 	@Test(expected = IllegalArgumentException.class)
 	public void testProcessInstanceDoesNotExist() {
 		pe.getProcessInstance("does not exist");
diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/StopProcessFlagTask.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/StopProcessFlagTask.java
new file mode 100644
index 00000000..8cd76eaa
--- /dev/null
+++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/StopProcessFlagTask.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright 2017 Graz University of Technology
+ * EAAF-Core Components has been developed in a cooperation between EGIZ,  
+ * A-SIT Plus, A-SIT, and Graz University of Technology.
+ *
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
+ * the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ * You may obtain a copy of the Licence at:
+ * https://joinup.ec.europa.eu/news/understanding-eupl-v12
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and
+ * limitations under the Licence.
+ *  
+ * This product combines work with different licenses. See the "NOTICE" text
+ * file for details on the various modules and licenses.
+ * The "NOTICE" text file is part of the distribution. Any derivative works
+ * that you distribute must include a readable copy of the "NOTICE" text file.
+ *******************************************************************************/
+/*******************************************************************************
+ *******************************************************************************/
+/*******************************************************************************
+ *******************************************************************************/
+package at.gv.egiz.eaaf.core.impl.idp.process.test;
+
+import org.springframework.stereotype.Service;
+
+import at.gv.egiz.eaaf.core.api.IRequest;
+import at.gv.egiz.eaaf.core.api.idp.process.ExecutionContext;
+import at.gv.egiz.eaaf.core.api.idp.process.Task;
+
+/**
+ * Simple task that just outputs a "Hello World" text to the console.
+ * 
+ * @author tknall
+ * 
+ */
+@Service("HelloWorldTask")
+public class StopProcessFlagTask implements Task {
+ 
+	@Override
+	public IRequest execute(IRequest pendingReq, ExecutionContext executionContext) {
+		System.out.println("Stop process-flow dynamically from task");
+		executionContext.setCanceleProcessFlag();
+		
+		return pendingReq;
+	}
+
+}
diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ThrowExceptionTask.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ThrowExceptionTask.java
new file mode 100644
index 00000000..ecd139c8
--- /dev/null
+++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/idp/process/test/ThrowExceptionTask.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright 2017 Graz University of Technology
+ * EAAF-Core Components has been developed in a cooperation between EGIZ,  
+ * A-SIT Plus, A-SIT, and Graz University of Technology.
+ *
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
+ * the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ * You may obtain a copy of the Licence at:
+ * https://joinup.ec.europa.eu/news/understanding-eupl-v12
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and
+ * limitations under the Licence.
+ *  
+ * This product combines work with different licenses. See the "NOTICE" text
+ * file for details on the various modules and licenses.
+ * The "NOTICE" text file is part of the distribution. Any derivative works
+ * that you distribute must include a readable copy of the "NOTICE" text file.
+ *******************************************************************************/
+/*******************************************************************************
+ *******************************************************************************/
+/*******************************************************************************
+ *******************************************************************************/
+package at.gv.egiz.eaaf.core.impl.idp.process.test;
+
+import org.springframework.stereotype.Service;
+
+import at.gv.egiz.eaaf.core.api.IRequest;
+import at.gv.egiz.eaaf.core.api.idp.process.ExecutionContext;
+import at.gv.egiz.eaaf.core.api.idp.process.Task;
+import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException;
+
+/**
+ * Simple task that just outputs a "Hello World" text to the console.
+ * 
+ * @author tknall
+ * 
+ */
+@Service("HelloWorldTask")
+public class ThrowExceptionTask implements Task {
+ 
+	@Override
+	public IRequest execute(IRequest pendingReq, ExecutionContext executionContext) throws TaskExecutionException {
+		System.out.println("Stop process-flow dynamically from task");
+		throw new TaskExecutionException(pendingReq, "jUnit Test", new RuntimeException("jUnit test exception handling"));
+		
+	}
+
+}
-- 
cgit v1.2.3