Skip to content

Commit

Permalink
updated config, refactoring, typos
Browse files Browse the repository at this point in the history
Issue #213
  • Loading branch information
rsoika committed Sep 5, 2024
1 parent 1d85876 commit 9d59e47
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 439 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand All @@ -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();


}

/**
Expand All @@ -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);

}

Expand All @@ -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<Object> fileData = snapshotworkitem.getFile("test.txt");
FileData fileData=snapshotworkitem.getFileData("test.txt");
Assert.assertEquals("This is a test", new String(fileData.getContent()));
// List<Object> 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);
Expand All @@ -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<Object> 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<Object> 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()));

}

Expand All @@ -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();
}
}

Expand Down
Loading

0 comments on commit 9d59e47

Please # to comment.