From 9d59e474f4439624dd9ad0badfd96645c1b2bbd6 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Thu, 5 Sep 2024 08:56:35 +0200 Subject: [PATCH] updated config, refactoring, typos Issue #213 --- .../imixs/archive/core/SnapshotException.java | 2 +- .../imixs/archive/core/SnapshotService.java | 23 +- .../archive/core/TestSnapshotService.java | 188 +++++------ .../engine/WorkflowMockEnvironment.java | 309 ------------------ pom.xml | 55 +++- 5 files changed, 138 insertions(+), 439 deletions(-) delete mode 100644 imixs-archive-api/src/test/java/org/imixs/workflow/engine/WorkflowMockEnvironment.java diff --git a/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotException.java b/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotException.java index 3d5f8d62..1f214119 100644 --- a/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotException.java +++ b/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotException.java @@ -29,7 +29,7 @@ /** * An SnapshotException is a runtime exception which is thrown by a - * SnapshotService if data is not read or writable. + * SnapshotService if data is not read or writable. * * @see org.imixs.archive.core.SnapshotService * @author rsoika diff --git a/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotService.java b/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotService.java index d63c2923..df736135 100644 --- a/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotService.java +++ b/imixs-archive-api/src/main/java/org/imixs/archive/core/SnapshotService.java @@ -37,6 +37,15 @@ import java.util.logging.Level; import java.util.logging.Logger; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.imixs.workflow.FileData; +import org.imixs.workflow.ItemCollection; +import org.imixs.workflow.WorkflowKernel; +import org.imixs.workflow.engine.DocumentEvent; +import org.imixs.workflow.engine.DocumentService; +import org.imixs.workflow.engine.EventLogService; +import org.imixs.workflow.exceptions.AccessDeniedException; + import jakarta.annotation.Resource; import jakarta.annotation.security.DeclareRoles; import jakarta.annotation.security.RunAs; @@ -47,15 +56,6 @@ import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; -import org.eclipse.microprofile.config.inject.ConfigProperty; -import org.imixs.workflow.FileData; -import org.imixs.workflow.ItemCollection; -import org.imixs.workflow.WorkflowKernel; -import org.imixs.workflow.engine.DocumentEvent; -import org.imixs.workflow.engine.DocumentService; -import org.imixs.workflow.engine.EventLogService; -import org.imixs.workflow.exceptions.AccessDeniedException; - /** * This service component provides a mechanism to transfer the content of a * workitem into a snapshot workitem. Attached files will be linked from the @@ -138,7 +138,7 @@ public class SnapshotService { // overwriting // file content public static final String ITEM_BACKUPRESTORE = "$backuprestore"; - + public static final String PROPERTY_SNAPSHOT_WORKITEMLOB_SUPPORT = "snapshot.workitemlob_suport"; public static final String PROPERTY_SNAPSHOT_HISTORY = "snapshot.history"; public static final String PROPERTY_SNAPSHOT_OVERWRITEFILECONTENT = "snapshot.overwriteFileContent"; @@ -353,7 +353,8 @@ public void onSave(@Observes DocumentEvent documentEvent) { eventLogService.createEvent(EVENTLOG_TOPIC_ADD, snapshot.getUniqueID()); } else { // 10. write backup event log entry... - // If no ArchiveService is connected, but a BackupService, than we create immediately a Backup Event + // If no ArchiveService is connected, but a BackupService, than we create + // immediately a Backup Event if (backupServiceEndpoint.isPresent() && !backupServiceEndpoint.get().isEmpty()) { if (debug) { logger.finest("......create event log entry " + EVENTLOG_TOPIC_BACKUP); diff --git a/imixs-archive-api/src/test/java/org/imixs/archive/core/TestSnapshotService.java b/imixs-archive-api/src/test/java/org/imixs/archive/core/TestSnapshotService.java index ec0ed916..5b167295 100644 --- a/imixs-archive-api/src/test/java/org/imixs/archive/core/TestSnapshotService.java +++ b/imixs-archive-api/src/test/java/org/imixs/archive/core/TestSnapshotService.java @@ -1,9 +1,12 @@ package org.imixs.archive.core; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.security.NoSuchAlgorithmException; -import java.security.Principal; import java.util.Optional; import java.util.logging.Logger; @@ -16,14 +19,10 @@ import org.imixs.workflow.exceptions.ModelException; import org.imixs.workflow.exceptions.PluginException; import org.imixs.workflow.exceptions.ProcessingErrorException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; import org.mockito.MockitoAnnotations; -import org.mockito.Spy; - -import jakarta.ejb.SessionContext; /** * Test the SnapshotService EJB. The WorkflowArchiveMockEnvironment provides a @@ -34,43 +33,35 @@ */ public class TestSnapshotService { private final static Logger logger = Logger.getLogger(TestSnapshotService.class.getName()); - - @Spy - SnapshotService snapshotService; - - SessionContext ctx; + @InjectMocks + SnapshotService snapshotService; - ItemCollection documentContext; - ItemCollection documentActivity, documentProcess; + ItemCollection workitem; + ItemCollection event; - WorkflowMockEnvironment workflowMockEnvironment; + WorkflowMockEnvironment workflowEnvironment; /** * Load test model and mock SnapshotPlugin * * @throws ModelException */ - @Before + @BeforeEach public void setup() throws PluginException, ModelException { + // Ensures that @Mock and @InjectMocks annotations are processed + MockitoAnnotations.openMocks(this); - workflowMockEnvironment = new WorkflowMockEnvironment(); - workflowMockEnvironment.setModelPath("/bpmn/TestSnapshotService.bpmn"); - workflowMockEnvironment.setup(); + workflowEnvironment = new WorkflowMockEnvironment(); - MockitoAnnotations.initMocks(this); + workflowEnvironment.setUp(); + workflowEnvironment.loadBPMNModel("/bpmn/TestSnapshotService.bpmn"); // mock session context - ctx = Mockito.mock(SessionContext.class); - snapshotService.ejbCtx = ctx; - // simulate SessionContext ctx.getCallerPrincipal().getName() - Principal principal = Mockito.mock(Principal.class); - when(principal.getName()).thenReturn("manfred"); - when(ctx.getCallerPrincipal()).thenReturn(principal); + snapshotService.ejbCtx = workflowEnvironment.getWorkflowContext().getSessionContext(); - snapshotService.documentService = workflowMockEnvironment.getDocumentService(); + snapshotService.documentService = workflowEnvironment.getDocumentService(); - } /** @@ -85,30 +76,28 @@ public void setup() throws PluginException, ModelException { @Test public void testOnSave() throws AccessDeniedException, ProcessingErrorException, PluginException, ModelException { // load test workitem - ItemCollection workitem = workflowMockEnvironment.getDatabase().get("W0000-00001"); - workitem.replaceItemValue(WorkflowKernel.MODELVERSION, WorkflowMockEnvironment.DEFAULT_MODEL_VERSION); - workitem.replaceItemValue(WorkflowKernel.TASKID, 1000); - workitem.replaceItemValue(WorkflowKernel.EVENTID, 10); + ItemCollection workitem = workflowEnvironment.getDocumentService().load("W0000-00001"); + workitem.model("1.0.0").task(1000).event(10); DocumentEvent documentEvent = new DocumentEvent(workitem, DocumentEvent.ON_DOCUMENT_SAVE); - snapshotService.archiveServiceEndpoint=Optional.of(""); - snapshotService.backupServiceEndpoint=Optional.of(""); + snapshotService.archiveServiceEndpoint = Optional.of(""); + snapshotService.backupServiceEndpoint = Optional.of(""); snapshotService.onSave(documentEvent); - workitem = workflowMockEnvironment.getWorkflowService().processWorkItem(workitem); + workitem = workflowEnvironment.getWorkflowService().processWorkItem(workitem); - Assert.assertEquals("1.0.0", workitem.getItemValueString("$ModelVersion")); + assertEquals("1.0.0", workitem.getItemValueString("$ModelVersion")); // test the $snapshotID - Assert.assertTrue(workitem.hasItem(SnapshotService.SNAPSHOTID)); + assertTrue(workitem.hasItem(SnapshotService.SNAPSHOTID)); String snapshotID = workitem.getItemValueString(SnapshotService.SNAPSHOTID); - Assert.assertFalse(snapshotID.isEmpty()); + assertFalse(snapshotID.isEmpty()); logger.info("$snapshotid=" + snapshotID); - Assert.assertTrue(snapshotID.startsWith("W0000-00001")); + assertTrue(snapshotID.startsWith("W0000-00001")); // load the snapshot workitem - ItemCollection snapshotworkitem = workflowMockEnvironment.getDatabase().get(snapshotID); - Assert.assertNotNull(snapshotworkitem); + ItemCollection snapshotworkitem = workflowEnvironment.getDatabase().get(snapshotID); + assertNotNull(snapshotworkitem); } @@ -129,64 +118,60 @@ public void testOnSave() throws AccessDeniedException, ProcessingErrorException, public void testOnSaveVersion() throws AccessDeniedException, ProcessingErrorException, PluginException, ModelException { // load test workitem - ItemCollection workitem = workflowMockEnvironment.getDatabase().get("W0000-00001"); - workitem.replaceItemValue(WorkflowKernel.MODELVERSION, WorkflowMockEnvironment.DEFAULT_MODEL_VERSION); - workitem.replaceItemValue(WorkflowKernel.TASKID, 1000); - // trigger the split event - workitem.replaceItemValue(WorkflowKernel.EVENTID, 10); + ItemCollection workitem = workflowEnvironment.getDocumentService().load("W0000-00001"); + workitem.model("1.0.0").task(1000).event(10); byte[] data = "This is a test".getBytes(); // we attache a file.... - //workitem.addFile(data, "test.txt", null); + // workitem.addFile(data, "test.txt", null); workitem.addFileData(new FileData("test.txt", data, null, null)); - - + DocumentEvent documentEvent = new DocumentEvent(workitem, DocumentEvent.ON_DOCUMENT_SAVE); - snapshotService.archiveServiceEndpoint=Optional.of(""); - snapshotService.backupServiceEndpoint=Optional.of(""); + snapshotService.archiveServiceEndpoint = Optional.of(""); + snapshotService.backupServiceEndpoint = Optional.of(""); snapshotService.onSave(documentEvent); - workitem = workflowMockEnvironment.getWorkflowService().processWorkItem(workitem); + workitem = workflowEnvironment.getWorkflowService().processWorkItem(workitem); - Assert.assertEquals("1.0.0", workitem.getItemValueString("$ModelVersion")); + assertEquals("1.0.0", workitem.getItemValueString("$ModelVersion")); // test the $snapshotID - Assert.assertTrue(workitem.hasItem(SnapshotService.SNAPSHOTID)); + assertTrue(workitem.hasItem(SnapshotService.SNAPSHOTID)); String snapshotID = workitem.getItemValueString(SnapshotService.SNAPSHOTID); - Assert.assertFalse(snapshotID.isEmpty()); + assertFalse(snapshotID.isEmpty()); logger.info("$snapshotid=" + snapshotID); - Assert.assertTrue(snapshotID.startsWith("W0000-00001")); + assertTrue(snapshotID.startsWith("W0000-00001")); // load the snapshot workitem - ItemCollection snapshotworkitem = workflowMockEnvironment.getDatabase().get(snapshotID); - Assert.assertNotNull(snapshotworkitem); + ItemCollection snapshotworkitem = workflowEnvironment.getDatabase().get(snapshotID); + assertNotNull(snapshotworkitem); // test the file content - //List fileData = snapshotworkitem.getFile("test.txt"); - FileData fileData=snapshotworkitem.getFileData("test.txt"); - Assert.assertEquals("This is a test", new String(fileData.getContent())); - + // List fileData = snapshotworkitem.getFile("test.txt"); + FileData fileData = snapshotworkitem.getFileData("test.txt"); + assertEquals("This is a test", new String(fileData.getContent())); + /* * Now we trigger a second event to create a version be we remove the file * content before... - */ + */ workitem.replaceItemValue(WorkflowKernel.EVENTID, 20); documentEvent = new DocumentEvent(workitem, DocumentEvent.ON_DOCUMENT_SAVE); snapshotService.onSave(documentEvent); - workitem = workflowMockEnvironment.getWorkflowService().processWorkItem(workitem); + workitem = workflowEnvironment.getWorkflowService().processWorkItem(workitem); // remove the file content from the origin.... workitem.removeFile("test.txt"); - workflowMockEnvironment.getDocumentService().save(workitem); + workflowEnvironment.getDocumentService().save(workitem); - /* + /* * now lets check the version. The version should have the file content. */ // now lets check the version.... String versionID = workitem.getItemValueString("$uniqueIdVersions"); - ItemCollection version = workflowMockEnvironment.getDatabase().get(versionID); - Assert.assertNotNull(version); + ItemCollection version = workflowEnvironment.getDatabase().get(versionID); + assertNotNull(version); // simulate snaptshot cdi event DocumentEvent documentEvent2 = new DocumentEvent(version, DocumentEvent.ON_DOCUMENT_SAVE); @@ -197,17 +182,17 @@ public void testOnSaveVersion() logger.info("Version $snapshotid=" + versionSnapshot); // version snapshot id MUST not be equal - Assert.assertFalse(snapshotID.equals(versionSnapshot)); + assertFalse(snapshotID.equals(versionSnapshot)); // we load the snapshot version and we expect again the fail content.... - ItemCollection snapshotworkitemVersion = workflowMockEnvironment.getDatabase().get(versionSnapshot); - Assert.assertNotNull(snapshotworkitemVersion); + ItemCollection snapshotworkitemVersion = workflowEnvironment.getDatabase().get(versionSnapshot); + assertNotNull(snapshotworkitemVersion); // test the file content - //List fileDataVersion = snapshotworkitemVersion.getFile("test.txt"); - FileData fileDataVersion=snapshotworkitemVersion.getFileData("test.txt"); - //byte[] contentVersion = (byte[]) fileDataVersion.get(1); - Assert.assertEquals("This is a test", new String(fileDataVersion.getContent())); + // List fileDataVersion = snapshotworkitemVersion.getFile("test.txt"); + FileData fileDataVersion = snapshotworkitemVersion.getFileData("test.txt"); + // byte[] contentVersion = (byte[]) fileDataVersion.get(1); + assertEquals("This is a test", new String(fileDataVersion.getContent())); } @@ -227,58 +212,57 @@ public void testOnSaveVersion() public void testFileDataAndDMSEntries() throws AccessDeniedException, ProcessingErrorException, PluginException, ModelException { // load test workitem - ItemCollection workitem = workflowMockEnvironment.getDatabase().get("W0000-00001"); - workitem.replaceItemValue(WorkflowKernel.MODELVERSION, WorkflowMockEnvironment.DEFAULT_MODEL_VERSION); - workitem.replaceItemValue(WorkflowKernel.TASKID, 1000); - workitem.replaceItemValue(WorkflowKernel.EVENTID, 10); + ItemCollection workitem = workflowEnvironment.getDocumentService().load("W0000-00001"); + + workitem.model("1.0.0").task(1000).event(10); // add file... byte[] dummyContent = { 1, 2, 3 }; - FileData filedata = new FileData("test.txt", dummyContent, "text",null); + FileData filedata = new FileData("test.txt", dummyContent, "text", null); workitem.addFileData(filedata); DocumentEvent documentEvent = new DocumentEvent(workitem, DocumentEvent.ON_DOCUMENT_SAVE); - snapshotService.archiveServiceEndpoint=Optional.of(""); - snapshotService.backupServiceEndpoint=Optional.of(""); + snapshotService.archiveServiceEndpoint = Optional.of(""); + snapshotService.backupServiceEndpoint = Optional.of(""); snapshotService.onSave(documentEvent); - workitem = workflowMockEnvironment.getWorkflowService().processWorkItem(workitem); + workitem = workflowEnvironment.getWorkflowService().processWorkItem(workitem); - Assert.assertEquals("1.0.0", workitem.getItemValueString("$ModelVersion")); + assertEquals("1.0.0", workitem.getItemValueString("$ModelVersion")); // test the $snapshotID - Assert.assertTrue(workitem.hasItem(SnapshotService.SNAPSHOTID)); + assertTrue(workitem.hasItem(SnapshotService.SNAPSHOTID)); String snapshotID = workitem.getItemValueString(SnapshotService.SNAPSHOTID); - Assert.assertFalse(snapshotID.isEmpty()); + assertFalse(snapshotID.isEmpty()); logger.info("$snapshotid=" + snapshotID); - Assert.assertTrue(snapshotID.startsWith("W0000-00001")); + assertTrue(snapshotID.startsWith("W0000-00001")); // load the snapshot workitem - ItemCollection snapshotworkitem = workflowMockEnvironment.getDatabase().get(snapshotID); - Assert.assertNotNull(snapshotworkitem); + ItemCollection snapshotworkitem = workflowEnvironment.getDatabase().get(snapshotID); + assertNotNull(snapshotworkitem); // test the file data of workitem FileData testfiledataOrigin = workitem.getFileData("test.txt"); - Assert.assertEquals(filedata.getName(), testfiledataOrigin.getName()); - Assert.assertEquals(filedata.getContentType(), testfiledataOrigin.getContentType()); - Assert.assertTrue(testfiledataOrigin.getContent().length == 0); + assertEquals(filedata.getName(), testfiledataOrigin.getName()); + assertEquals(filedata.getContentType(), testfiledataOrigin.getContentType()); + assertTrue(testfiledataOrigin.getContent().length == 0); // test the file data of snapshot FileData testfiledataSnapshot = snapshotworkitem.getFileData("test.txt"); - Assert.assertEquals(filedata.getName(), testfiledataSnapshot.getName()); - Assert.assertEquals(filedata.getContentType(), testfiledataSnapshot.getContentType()); - Assert.assertTrue(testfiledataSnapshot.getContent().length == 3); + assertEquals(filedata.getName(), testfiledataSnapshot.getName()); + assertEquals(filedata.getContentType(), testfiledataSnapshot.getContentType()); + assertTrue(testfiledataSnapshot.getContent().length == 3); // now test the DMS item - ItemCollection dmsItemCol =new ItemCollection(testfiledataOrigin.getAttributes()); - Assert.assertNotNull(dmsItemCol); - Assert.assertEquals(3, dmsItemCol.getItemValueInteger("size")); + ItemCollection dmsItemCol = new ItemCollection(testfiledataOrigin.getAttributes()); + assertNotNull(dmsItemCol); + assertEquals(3, dmsItemCol.getItemValueInteger("size")); try { - Assert.assertTrue(testfiledataSnapshot.validateMD5(dmsItemCol.getItemValueString("md5checksum"))); + assertTrue(testfiledataSnapshot.validateMD5(dmsItemCol.getItemValueString("md5checksum"))); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); - Assert.fail(); + fail(); } } diff --git a/imixs-archive-api/src/test/java/org/imixs/workflow/engine/WorkflowMockEnvironment.java b/imixs-archive-api/src/test/java/org/imixs/workflow/engine/WorkflowMockEnvironment.java deleted file mode 100644 index 516683a6..00000000 --- a/imixs-archive-api/src/test/java/org/imixs/workflow/engine/WorkflowMockEnvironment.java +++ /dev/null @@ -1,309 +0,0 @@ -package org.imixs.workflow.engine; - -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.io.InputStream; -import java.security.Principal; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.Logger; - -import jakarta.ejb.SessionContext; -import javax.xml.parsers.ParserConfigurationException; - -import org.imixs.workflow.ItemCollection; -import org.imixs.workflow.Model; -import org.imixs.workflow.ModelManager; -import org.imixs.workflow.WorkflowContext; -import org.imixs.workflow.WorkflowKernel; -import org.imixs.workflow.bpmn.BPMNModel; -import org.imixs.workflow.bpmn.BPMNParser; -import org.imixs.workflow.exceptions.AccessDeniedException; -import org.imixs.workflow.exceptions.ModelException; -import org.imixs.workflow.exceptions.PluginException; -import org.imixs.workflow.exceptions.ProcessingErrorException; -import org.junit.Before; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.xml.sax.SAXException; - -/** - * The WorkflowMockEnvironment provides a mocked database environment for jUnit - * tests. The WorkflowMockEnvironment can be used to test the WorkflowService. - * - * This test class mocks a complete workflow environment without the class - * WorkflowService - * - * The test class generates a test database with process entities and activity - * entities which can be accessed from a plug-in or the workflowKernel. - * - * A JUnit Test can save, load and process workitems. - * - * JUnit tests can also manipulate the model by changing entities through - * calling the methods: - * - * getActivityEntity,setActivityEntity,getProcessEntity,setProcessEntity - * - * - * @version 2.0 - * @see AbstractPluginTest, TestWorkflowService, ModelPluginMock - * @author rsoika - */ -public class WorkflowMockEnvironment { - private final static Logger logger = Logger.getLogger(WorkflowMockEnvironment.class.getName()); - public static final String DEFAULT_MODEL_VERSION = "1.0.0"; - - Map database = null; - protected DocumentService documentService; - protected WorkflowService workflowService; - - @Spy - protected ModelService modelService; - protected SessionContext ctx; - - protected WorkflowContext workflowContext; - private BPMNModel model = null; - - private String modelPath = null;// "/bpmn/plugin-test.bpmn"; - - public String getModelPath() { - return modelPath; - } - - public void setModelPath(String modelPath) { - this.modelPath = modelPath; - } - - public WorkflowContext getWorkflowContext() { - return workflowContext; - } - - public WorkflowService getWorkflowService() { - return workflowService; - } - - public ModelService getModelService() { - return modelService; - } - - public DocumentService getDocumentService() { - return documentService; - } - - @Before - public void setup() throws PluginException, ModelException { - MockitoAnnotations.initMocks(this); - // setup db - createTestDatabase(); - - // mock session context - ctx = Mockito.mock(SessionContext.class); - // simulate SessionContext ctx.getCallerPrincipal().getName() - Principal principal = Mockito.mock(Principal.class); - when(principal.getName()).thenReturn("manfred"); - when(ctx.getCallerPrincipal()).thenReturn(principal); - - // mock Entity service - documentService = Mockito.mock(DocumentService.class); - // Simulate fineProfile("1.0.0") -> entityService.load()... - when(documentService.load(Mockito.anyString())).thenAnswer(new Answer() { - @Override - public ItemCollection answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - String id = (String) args[0]; - ItemCollection result = database.get(id); - if (result != null) { - // set author access=true - result.replaceItemValue(DocumentService.ISAUTHOR, true); - } - return result; - } - }); - - // simulate save() method - when(documentService.save(Mockito.any(ItemCollection.class))).thenAnswer(new Answer() { - @Override - public ItemCollection answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - ItemCollection entity = (ItemCollection) args[0]; - // test if uniqueid is available - if (entity.getUniqueID().isEmpty()) { - entity.replaceItemValue(WorkflowKernel.UNIQUEID, WorkflowKernel.generateUniqueID()); - } - database.put(entity.getItemValueString(WorkflowKernel.UNIQUEID), entity); - return entity; - } - }); - - // Mock modelService (using the @spy) annotation - Mockito.doNothing().when(modelService).init(); - - // load model - loadModel(); - - // Mock modelManager - ModelManager modelManager = Mockito.mock(ModelManager.class); - try { - when(modelManager.getModel(Mockito.anyString())).thenReturn(this.getModel()); - when(modelManager.getModelByWorkitem(Mockito.any(ItemCollection.class))).thenReturn(this.getModel()); - } catch (ModelException e) { - e.printStackTrace(); - } - - // Mock context - workflowContext = Mockito.mock(WorkflowContext.class); - when(workflowContext.getModelManager()).thenReturn(modelManager); - - // Mock WorkflowService - workflowService = Mockito.mock(WorkflowService.class); - workflowService.documentService = documentService; - workflowService.ctx = ctx; - - workflowService.modelService = modelService; - when(workflowService.getModelManager()).thenReturn(modelService); - when(workflowService.getWorkListByRef(Mockito.anyString())).thenAnswer(new Answer>() { - @Override - public List answer(InvocationOnMock invocation) throws Throwable { - - List result = new ArrayList<>(); - Object[] args = invocation.getArguments(); - String id = (String) args[0]; - - // iterate over all data and return matching workitems. - Collection allEntities = database.values(); - for (ItemCollection aentity : allEntities) { - if (aentity.getItemValueString(WorkflowService.UNIQUEIDREF).equals(id)) { - result.add(aentity); - } - } - - return result; - } - }); - - - // AdaptText - when(workflowService.adaptText(Mockito.anyString(),Mockito.any(ItemCollection.class))).thenAnswer(new Answer(){ - @Override - public String answer(InvocationOnMock invocation) throws Throwable, PluginException { - - Object[] args = invocation.getArguments(); - String text=(String) args[0]; - ItemCollection document = (ItemCollection) args[1]; - - TextEvent textEvent=new TextEvent(text, document); - - TextItemValueAdapter tiva=new TextItemValueAdapter(); - tiva.onEvent(textEvent); - - - return textEvent.getText(); - } - }); - - - - - when(workflowService.evalWorkflowResult(Mockito.any(ItemCollection.class),Mockito.any(ItemCollection.class))).thenCallRealMethod(); - when(workflowService.evalWorkflowResult(Mockito.any(ItemCollection.class),Mockito.any(ItemCollection.class),Mockito.anyBoolean())).thenCallRealMethod(); - when(workflowService.processWorkItem(Mockito.any(ItemCollection.class))).thenCallRealMethod(); - when(workflowService.getUserName()).thenCallRealMethod(); - when(workflowService.getWorkItem(Mockito.anyString())).thenCallRealMethod(); - - try { - when(workflowService.getEvents(Mockito.any(ItemCollection.class))).thenCallRealMethod(); - } catch (ModelException e) { - - e.printStackTrace(); - } - - } - - - /** - * Create a test database with some workItems and a simple model - */ - protected void createTestDatabase() { - - database = new HashMap(); - - ItemCollection entity = null; - - logger.info("createSimpleDatabase...."); - - // create workitems - for (int i = 1; i < 6; i++) { - entity = new ItemCollection(); - entity.replaceItemValue("type", "workitem"); - entity.replaceItemValue(WorkflowKernel.UNIQUEID, "W0000-0000" + i); - entity.replaceItemValue("txtName", "Workitem " + i); - entity.replaceItemValue("$ModelVersion", "1.0.0"); - entity.replaceItemValue("$ProcessID", 100); - entity.replaceItemValue("$ActivityID", 10); - entity.replaceItemValue(DocumentService.ISAUTHOR, true); - database.put(entity.getItemValueString(WorkflowKernel.UNIQUEID), entity); - } - - } - - public Model getModel() { - return model; - } - - /** - * loads a model from the given path - * - * @param modelPath - */ - public void loadModel(String modelPath) { - setModelPath(modelPath); - loadModel(); - } - - /** - * loads the current model - */ - public void loadModel() { - if (this.modelPath != null) { - InputStream inputStream = WorkflowMockEnvironment.class.getResourceAsStream(this.modelPath); - try { - logger.info("loading model: " + this.modelPath + "...."); - model = BPMNParser.parseModel(inputStream, "UTF-8"); - - this.modelService.addModel(model); - } catch (ModelException | ParseException | ParserConfigurationException | SAXException | IOException e) { - e.printStackTrace(); - } - - } - - } - - public Map getDatabase() { - return database; - } - - /** - * Mocks a processing life cycle - * - * @param workitem - * @return - * @throws ModelException - * @throws PluginException - * @throws ProcessingErrorException - * @throws AccessDeniedException - */ - public ItemCollection processWorkItem(ItemCollection workitem) - throws AccessDeniedException, ProcessingErrorException, PluginException, ModelException { - workitem = workflowService.processWorkItem(workitem); - return workitem; - } -} diff --git a/pom.xml b/pom.xml index c9230533..ca967a20 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,6 @@ - + 4.0.0 org.imixs.workflow imixs-archive @@ -83,12 +85,15 @@ 11 11 10.0.0 - 6.0.7 - 2.0.2 - 6.0.0 + 6.1.0-SNAPSHOT + 2.1.0-SNAPSHOT + 6.1.0-SNAPSHOT 2.0.21 6.0 4.0 + + 5.9.2 + 5.8.0 @@ -318,6 +323,34 @@ + + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + org.mockito + mockito-junit-jupiter + ${mockito.version} + test + + org.eclipse.parsson jakarta.json @@ -336,18 +369,8 @@ 3.0.0 test - - junit - junit - 4.13.1 - test - - - org.mockito - mockito-core - 5.8.0 - test - + + org.imixs.workflow imixs-mock