Skip to content

Commit

Permalink
Refactor CalendarST to load test resources dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
solth committed Nov 30, 2023
1 parent 2219be0 commit d4770be
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 39 deletions.
14 changes: 2 additions & 12 deletions Kitodo/src/test/java/org/kitodo/MockDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,16 +744,6 @@ public static int insertRuleset(String rulesetTitle, String rulesetFilename, int
return ruleset.getId();
}

public static void insertProcessForCalendarHierarchyTests() throws DAOException, DataException {
int rulesetId = MockDatabase.insertRuleset("Newspaper", "newspaper.xml", 1);
Process tenthProcess = new Process();
tenthProcess.setProject(ServiceManager.getProjectService().getById(1));
tenthProcess.setTemplate(ServiceManager.getTemplateService().getById(1));
tenthProcess.setRuleset(ServiceManager.getRulesetService().getById(rulesetId));
tenthProcess.setTitle("NewspaperOverallProcess");
ServiceManager.getProcessService().save(tenthProcess);
}

private static void insertTemplates() throws DAOException, DataException {
Project project = ServiceManager.getProjectService().getById(1);

Expand Down Expand Up @@ -1076,8 +1066,8 @@ private static void insertFolders() throws DAOException, DataException {
* @throws DAOException when loading test project fails
* @throws DataException when saving dummy process fails
*/
public static int insertDummyProcess(int dummyProcessId) throws DAOException, DataException {
Project firstProject = ServiceManager.getProjectService().getById(2);
public static int insertDummyProcess(int dummyProcessId, int projectId) throws DAOException, DataException {
Project firstProject = ServiceManager.getProjectService().getById(projectId);
Template template = firstProject.getTemplates().get(0);
Process dummyProcess = new Process();
dummyProcess.setTitle("Dummy_process_" + dummyProcessId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void tearDown() throws Exception {
*/
@Before
public void prepareNewspaperProcess() throws DAOException, DataException, IOException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(2);
newspaperTestProcessId = MockDatabase.insertTestProcess(NEWSPAPER_TEST_PROCESS_TITLE, 1, 1, rulesetId);
ProcessTestUtils.copyTestFiles(newspaperTestProcessId, NEWSPAPER_TEST_METADATA_FILE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public void testMetadataImageComparator() {

@Test
public void testRenamingOfMultipleProcesses() throws DAOException, DataException, IOException, InterruptedException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(2);
mediaRenamingFirstProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_PROCESS_1);
ProcessTestUtils.insertDummyProcesses();
ProcessTestUtils.insertDummyProcesses(2);
mediaRenamingSecondProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_PROCESS_2);
ProcessTestUtils.copyTestFiles(mediaRenamingFirstProcessId, TEST_RENAME_MEDIA_FILE);
ProcessTestUtils.copyTestFiles(mediaRenamingSecondProcessId, TEST_RENAME_MEDIA_FILE);
Expand All @@ -163,7 +163,7 @@ public void testRenamingOfMultipleProcesses() throws DAOException, DataException
@Test
public void testRevertingOriginalFilenamesAfterRenamingError() throws DAOException, DataException, IOException,
InterruptedException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(2);
revertMediaRenamingProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_REVERT_PROCESS);
ProcessTestUtils.copyTestFiles(revertMediaRenamingProcessId, TEST_RENAME_MEDIA_FILE);
Path processScansDir = Paths.get(ConfigCore.getKitodoDataDirectory(), revertMediaRenamingProcessId
Expand Down
57 changes: 49 additions & 8 deletions Kitodo/src/test/java/org/kitodo/selenium/CalendarST.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,57 @@

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kitodo.MockDatabase;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.beans.User;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.data.ProcessService;
import org.kitodo.selenium.testframework.BaseTestSelenium;
import org.kitodo.selenium.testframework.Pages;
import org.kitodo.selenium.testframework.pages.CalendarPage;
import org.kitodo.selenium.testframework.pages.ProcessesPage;
import org.kitodo.utils.ProcessTestUtils;

public class CalendarST extends BaseTestSelenium {

private static ProcessesPage processesPage;
private static CalendarPage calendarPage;
private static List<Integer> dummyProcessIds = new LinkedList<>();
private static int newspaperTestProcessId = -1;
private static final String NEWSPAPER_TEST_METADATA_FILE = "testmetaNewspaper.xml";
private static final String NEWSPAPER_TEST_PROCESS_TITLE = "NewspaperOverallProcess";
private static final int PROJECT_ID = 1;

@BeforeClass
public static void setup() throws Exception {
processesPage = Pages.getProcessesPage();
calendarPage = Pages.getCalendarPage();
int rulesetId = MockDatabase.insertRuleset("Newspaper", "newspaper.xml", 1);
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
newspaperTestProcessId = MockDatabase.insertTestProcess(NEWSPAPER_TEST_PROCESS_TITLE, 1, 1, rulesetId);
ProcessTestUtils.copyTestMetadataFile(newspaperTestProcessId, NEWSPAPER_TEST_METADATA_FILE);
Process newsPaperProcess = ServiceManager.getProcessService().getById(newspaperTestProcessId);
// re-save process to index meta.xml including process base type!
ServiceManager.getProcessService().save(newsPaperProcess);
}

@Before
public void login() throws Exception {
User calendarUser = ServiceManager.getUserService().getByLogin("kowal");
Pages.getLoginPage().goTo().performLogin(calendarUser);
}

@After
Expand All @@ -44,14 +72,15 @@ public void logout() throws Exception {
Pages.getTopNavigation().logout();
}

@AfterClass
public static void cleanup() throws CustomResponseException, DAOException, DataException, IOException {
cleanupNewspaperProcess();
}

@Test
public void createProcessFromCalendar() throws Exception {
// add process to access calendar
MockDatabase.insertProcessForCalendarHierarchyTests();

login();
processesPage.goTo();
processesPage.goToCalendar();
processesPage.goToCalendar(newspaperTestProcessId);
calendarPage.addBlock();
calendarPage.addIssue("Morning issue");
calendarPage.addIssue("Evening issue");
Expand All @@ -63,8 +92,20 @@ public void createProcessFromCalendar() throws Exception {
assertEquals("Metadata for evening issue is incorrect", List.of("Signatur"), eveningIssueMetadata);
}

private void login() throws Exception {
User calendarUser = ServiceManager.getUserService().getByLogin("kowal");
Pages.getLoginPage().goTo().performLogin(calendarUser);
private static void deleteProcessHierarchy(Process process) throws DAOException, DataException, IOException {
for (Process childProcess : process.getChildren()) {
deleteProcessHierarchy(childProcess);
}
ProcessService.deleteProcess(process.getId());
}

private static void cleanupNewspaperProcess() throws CustomResponseException, DataException, DAOException, IOException {
for (int processId : dummyProcessIds) {
ServiceManager.getProcessService().removeFromDatabase(processId);
ServiceManager.getProcessService().removeFromIndex(processId, false);
}
if (newspaperTestProcessId > 0) {
deleteProcessHierarchy(ServiceManager.getProcessService().getById(newspaperTestProcessId));
}
}
}
15 changes: 8 additions & 7 deletions Kitodo/src/test/java/org/kitodo/selenium/MetadataST.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,29 @@ public class MetadataST extends BaseTestSelenium {
private static List<Integer> processHierarchyTestProcessIds = new LinkedList<>();
private static final String FIRST_STRUCTURE_TREE_NODE_LABEL = "1 : -";
private static final String SECOND_STRUCTURE_TREE_NODE_LABEL = "2 : -";
private static final int PROJECT_ID = 2;

private static void prepareMediaReferenceProcess() throws DAOException, DataException, IOException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
insertTestProcessForMediaReferencesTest();
copyTestFilesForMediaReferences();
}

private static void prepareMetadataLockProcess() throws DAOException, DataException, IOException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
insertTestProcessForMetadataLockTest();
ProcessTestUtils.copyTestMetadataFile(metadataLockProcessId, TEST_METADATA_LOCK_FILE);
}

private static void prepareProcessHierarchyProcesses() throws DAOException, IOException, DataException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
processHierarchyTestProcessIds = linkProcesses();
copyTestParentProcessMetadataFile();
updateChildProcessIdsInParentProcessMetadataFile();
}

private static void prepareMediaRenamingProcess() throws DAOException, DataException, IOException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
insertTestProcessForRenamingMediaFiles();
copyTestFilesForRenamingMediaFiles();
}
Expand Down Expand Up @@ -291,9 +292,9 @@ private static void insertTestProcessForRenamingMediaFiles() throws DAOException
private static List<Integer> linkProcesses() throws DAOException, DataException {
List<Integer> processIds = new LinkedList<>();
List<Process> childProcesses = new LinkedList<>();
childProcesses.add(ProcessTestUtils.addProcess(FIRST_CHILD_PROCESS_TITLE));
childProcesses.add(ProcessTestUtils.addProcess(SECOND_CHILD_PROCESS_TITLE));
Process parentProcess = ProcessTestUtils.addProcess(PARENT_PROCESS_TITLE);
childProcesses.add(ProcessTestUtils.addProcess(FIRST_CHILD_PROCESS_TITLE, PROJECT_ID));
childProcesses.add(ProcessTestUtils.addProcess(SECOND_CHILD_PROCESS_TITLE, PROJECT_ID));
Process parentProcess = ProcessTestUtils.addProcess(PARENT_PROCESS_TITLE, PROJECT_ID);
parentProcess.getChildren().addAll(childProcesses);
ServiceManager.getProcessService().save(parentProcess);
parentProcessId = parentProcess.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ public class ProcessesPage extends Page<ProcessesPage> {

private WebElement editMetadataLink;

@FindBy(xpath = "//a[@href='/kitodo/pages/calendarEdit.jsf?id=10']")
private WebElement openCalendarLink;

@SuppressWarnings("unused")
@FindBy(id = "search")
private WebElement searchForProcessesButton;
Expand Down Expand Up @@ -494,7 +491,8 @@ public void clickProcessesTableHeaderForSorting(int column) {
.until(() -> !columnHeader.getAttribute("aria-sort").equals(previousAriaSort));
}

public void goToCalendar() throws Exception {
public void goToCalendar(int processId) throws Exception {
WebElement openCalendarLink = Browser.getDriver().findElementByXPath("//a[@href='/kitodo/pages/calendarEdit.jsf?id=" + processId + "']");
if (isNotAt()) {
goTo();
}
Expand Down
8 changes: 4 additions & 4 deletions Kitodo/src/test/java/org/kitodo/utils/ProcessTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public static void copyTestMetadataFile(int processId, String filename) throws I
* @throws DAOException when adding process fails
* @throws DataException when adding process fails
*/
public static Process addProcess(String processTitle) throws DAOException, DataException {
insertDummyProcesses();
public static Process addProcess(String processTitle, int projectId) throws DAOException, DataException {
insertDummyProcesses(projectId);
return MockDatabase.addProcess(processTitle, TEST_PROJECT_ID, TEST_TEMPLATE_ID);
}

Expand All @@ -136,13 +136,13 @@ public static Process addProcess(String processTitle) throws DAOException, DataE
* @throws DAOException when retrieving existing processes from or inserting dummy processes into database fails
* @throws DataException when inserting dummy processes into database fails
*/
public static List<Integer> insertDummyProcesses() throws DAOException, DataException {
public static List<Integer> insertDummyProcesses(Integer projectId) throws DAOException, DataException {
List<Integer> dummyProcessIds = new LinkedList<>();
List<Integer> processIds = ServiceManager.getProcessService().getAll().stream().map(Process::getId)
.collect(Collectors.toList());
int id = Collections.max(processIds) + 1;
while (processDirExists(id)) {
dummyProcessIds.add(MockDatabase.insertDummyProcess(id));
dummyProcessIds.add(MockDatabase.insertDummyProcess(id, projectId));
id++;
}
return dummyProcessIds;
Expand Down

0 comments on commit d4770be

Please sign in to comment.