Skip to content

Commit

Permalink
Add new tests to test invalid or duplicate titles; improve test logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Nov 22, 2024
1 parent 79a3f6f commit a035cde
Showing 1 changed file with 85 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,66 +75,109 @@ public static void cleanDatabase() throws Exception {
MockDatabase.cleanDatabase();
}

@Test
public void shouldCreateNewProcess() throws Exception {
CreateProcessForm underTest = new CreateProcessForm();
underTest.getProcessDataTab().setDocType("Monograph");
Process newProcess = new Process();
Workpiece newWorkPiece = new Workpiece();
TempProcess tempProcess = new TempProcess(newProcess, newWorkPiece);
underTest.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess)));
underTest.getMainProcess().setProject(ServiceManager.getProjectService().getById(1));
underTest.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1));
underTest.getMainProcess().setTitle("title");
underTest.updateRulesetAndDocType(tempProcess.getProcess().getRuleset());
// Helper to create and initialize a CreateProcessForm with common properties
private CreateProcessForm setupCreateProcessForm(String docType, String title) throws Exception {
CreateProcessForm form = new CreateProcessForm();
form.getProcessDataTab().setDocType(docType);

Process process = new Process();
Workpiece workPiece = new Workpiece();
TempProcess tempProcess = new TempProcess(process, workPiece);
form.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess)));

form.getMainProcess().setProject(ServiceManager.getProjectService().getById(1));
form.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1));
form.getMainProcess().setTitle(title);

form.updateRulesetAndDocType(tempProcess.getProcess().getRuleset());
return form;
}

// Helper to manage script permissions
private void setScriptPermissions(boolean enable) throws Exception {
File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
if (!SystemUtils.IS_OS_WINDOWS) {
ExecutionPermission.setExecutePermission(script);
if (enable) {
ExecutionPermission.setExecutePermission(script);
} else {
ExecutionPermission.setNoExecutePermission(script);
}
}
}

// Helper to clean up database and file system
private void cleanUpProcess(Process process) throws Exception {
Integer processId = process.getId();
processService.remove(processId);
fileService.delete(URI.create(processId.toString()));
}

@Test
public void shouldCreateNewProcess() throws Exception {
CreateProcessForm underTest = setupCreateProcessForm("Monograph", "title");

setScriptPermissions(true);
long before = processService.count();
underTest.createNewProcess();
if (!SystemUtils.IS_OS_WINDOWS) {
ExecutionPermission.setNoExecutePermission(script);
}
setScriptPermissions(false);

long after = processService.count();
assertEquals(before + 1, after, "No process was created!");

// clean up database, index and file system
Integer processId = newProcess.getId();
processService.remove(processId);
fileService.delete(URI.create(processId.toString()));
cleanUpProcess(underTest.getMainProcess());
}

/**
* tests creation of processes without workflow.
*/
@Test
public void shouldCreateNewProcessWithoutWorkflow() throws Exception {
CreateProcessForm underTest = new CreateProcessForm();
underTest.getProcessDataTab().setDocType("MultiVolumeWork");
Process newProcess = new Process();
Workpiece newWorkPiece = new Workpiece();
TempProcess tempProcess = new TempProcess(newProcess, newWorkPiece);
underTest.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess)));
underTest.getMainProcess().setProject(ServiceManager.getProjectService().getById(1));
underTest.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1));
underTest.getMainProcess().setTitle("title");
underTest.updateRulesetAndDocType(tempProcess.getProcess().getRuleset());
CreateProcessForm underTest = setupCreateProcessForm("MultiVolumeWork", "title");

File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
ExecutionPermission.setExecutePermission(script);
setScriptPermissions(true);
long before = processService.count();
underTest.createNewProcess();
ExecutionPermission.setNoExecutePermission(script);
setScriptPermissions(false);

long after = processService.count();
assertEquals(before + 1, after, "No process was created!");
assertTrue(underTest.getMainProcess().getTasks().isEmpty(), "Process should not have tasks");
assertNull(underTest.getMainProcess().getSortHelperStatus(), "Process should not have sortHelperStatus");

assertTrue(newProcess.getTasks().isEmpty(), "Process should not have tasks");
assertNull(newProcess.getSortHelperStatus(), "process should not have sortHelperStatus");
cleanUpProcess(underTest.getMainProcess());
}

// clean up database, index and file system
Integer processId = newProcess.getId();
processService.remove(processId);
fileService.delete(URI.create(processId.toString()));
@Test
public void shouldNotCreateNewProcessWithInvalidTitle() throws Exception {
CreateProcessForm underTest = setupCreateProcessForm("Monograph", "title with whitespaces");

long before = processService.count();
underTest.createNewProcess();
long after = processService.count();

assertEquals(before, after, "A process with an invalid title was created!");
}

@Test
public void shouldNotAllowDuplicateTitles() throws Exception {
// First process creation
CreateProcessForm underTest = setupCreateProcessForm("Monograph", "title");

setScriptPermissions(true);
long before = processService.count();
underTest.createNewProcess();
setScriptPermissions(false);

long after = processService.count();
assertEquals(before + 1, after, "First process creation failed. No process was created!");

// Second process creation with duplicate title
CreateProcessForm underTestTwo = setupCreateProcessForm("Monograph", "title");

long beforeDuplicate = processService.count();
underTestTwo.createNewProcess();
long afterDuplicate = processService.count();

assertEquals(beforeDuplicate, afterDuplicate, "A duplicate process with the same title was created!");

cleanUpProcess(underTest.getMainProcess());
}

}

0 comments on commit a035cde

Please sign in to comment.