Skip to content

Commit

Permalink
Refactor test framework to load test resources for newspaper process …
Browse files Browse the repository at this point in the history
…generator integration test dynamically
  • Loading branch information
solth committed Dec 1, 2023
1 parent 457cc74 commit 0a82503
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 107 deletions.
65 changes: 46 additions & 19 deletions Kitodo/src/test/java/org/kitodo/MockDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -724,23 +724,24 @@ public static void removeProcessesForHierarchyTests() throws DataException {
ServiceManager.getProcessService().remove(4);
}


public static void insertProcessForCalendarHierarchyTests() throws DAOException, DataException {
Ruleset fivthRuleset = new Ruleset();
fivthRuleset.setTitle("Newspaper");
fivthRuleset.setFile("newspaper.xml");
fivthRuleset.setOrderMetadataByRuleset(false);
fivthRuleset.setClient(ServiceManager.getClientService().getById(1));
ServiceManager.getRulesetService().save(fivthRuleset);

insertPlaceholderProcesses(4, 9);

Process tenthProcess = new Process();
tenthProcess.setProject(ServiceManager.getProjectService().getById(1));
tenthProcess.setTemplate(ServiceManager.getTemplateService().getById(1));
tenthProcess.setRuleset(ServiceManager.getRulesetService().getById(5));
tenthProcess.setTitle("NewspaperOverallProcess");
ServiceManager.getProcessService().save(tenthProcess);
/**
* Insert ruleset.
* @param rulesetTitle ruleset title
* @param rulesetFilename ruleset filename
* @param clientId client id
* @return id of ruleset
* @throws DAOException when retrieving client by ID fails
* @throws DataException when saving ruleset failed
*/
public static int insertRuleset(String rulesetTitle, String rulesetFilename, int clientId) throws DAOException,
DataException {
Ruleset ruleset = new Ruleset();
ruleset.setTitle(rulesetTitle);
ruleset.setFile(rulesetFilename);
ruleset.setOrderMetadataByRuleset(false);
ruleset.setClient(ServiceManager.getClientService().getById(clientId));
ServiceManager.getRulesetService().save(ruleset);
return ruleset.getId();
}

private static void insertTemplates() throws DAOException, DataException {
Expand Down Expand Up @@ -1061,12 +1062,13 @@ private static void insertFolders() throws DAOException, DataException {
/**
* Insert dummy process into database.
* @param dummyProcessId id used in dummy process title
* @param projectId id of project to which the dummy process is added
* @return database ID of created dummy process
* @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 Expand Up @@ -1127,6 +1129,31 @@ public static int insertTestProcessIntoSecondProject(String processTitle) throws
return mediaReferencesProcess.getId();
}

/**
* Insert test process.
* @param processTitle process title of test process
* @param projectId project id of test process
* @param templateId template id of test process
* @param rulesetId ruleset id of test process
* @return id of test process
* @throws DAOException when retrieving project, template or ruleset fails
* @throws DataException when saving test process fails
*/
public static int insertTestProcess(String processTitle, int projectId, int templateId, int rulesetId)
throws DAOException, DataException {
Project project = ServiceManager.getProjectService().getById(projectId);
Template template = ServiceManager.getTemplateService().getById(templateId);
Ruleset ruleset = ServiceManager.getRulesetService().getById(rulesetId);
Process process = new Process();
process.setTitle(processTitle);
process.setProject(project);
process.setTemplate(template);
process.setRuleset(ruleset);
process.setDocket(template.getDocket());
ServiceManager.getProcessService().save(process);
return process.getId();
}

/**
* Insert folders into database and add them to second test project.
* @throws DAOException when loading project or template fails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void shouldAddLink() throws Exception {
MetadataEditor.addLink(ServiceManager.getProcessService().getById(4), "0", 7);

assertTrue("The link was not added correctly!",
isInternalMetsLink(FileUtils.readLines(metaXmlFile, StandardCharsets.UTF_8).get(37), 7));
isInternalMetsLink(FileUtils.readLines(metaXmlFile, StandardCharsets.UTF_8).get(36), 7));

FileUtils.writeLines(metaXmlFile, StandardCharsets.UTF_8.toString(), metaXmlContentBefore);
FileUtils.deleteQuietly(new File("src/test/resources/metadata/4/meta.xml.1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.MonthDay;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand All @@ -38,7 +36,6 @@
import org.kitodo.MockDatabase;
import org.kitodo.NewspaperCourse;
import org.kitodo.SecurityTestUtils;
import org.kitodo.TreeDeleter;
import org.kitodo.api.MetadataEntry;
import org.kitodo.api.dataformat.LogicalDivision;
import org.kitodo.api.dataformat.Workpiece;
Expand All @@ -48,6 +45,7 @@
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.dto.ProcessDTO;
import org.kitodo.production.helper.tasks.GeneratesNewspaperProcessesThread;
Expand All @@ -56,13 +54,19 @@
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.data.ProcessService;
import org.kitodo.production.services.dataformat.MetsService;
import org.kitodo.utils.ProcessTestUtils;

public class NewspaperProcessesGeneratorIT {
private static final ProcessService processService = ServiceManager.getProcessService();
private static final MetsService metsService = ServiceManager.getMetsService();

private static final String firstProcess = "First process";
private static final File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
private static List<Integer> dummyProcessIds = new LinkedList<>();
private static int newspaperTestProcessId = -1;
private static int rulesetId = -1;
private static final String NEWSPAPER_TEST_METADATA_FILE = "testmetaNewspaper.xml";
private static final String NEWSPAPER_TEST_PROCESS_TITLE = "NewspaperOverallProcess";

@Rule
public ExpectedException expectedEx = ExpectedException.none();
Expand All @@ -73,16 +77,17 @@ public class NewspaperProcessesGeneratorIT {
* @throws Exception
* if that does not work
*/
@Before
public void setUp() throws Exception {
@BeforeClass
public static void setUp() throws Exception {
if (!SystemUtils.IS_OS_WINDOWS) {
ExecutionPermission.setExecutePermission(script);
}
FileLoader.createConfigProjectsFileForCalendarHierarchyTests();
MockDatabase.startNode();
MockDatabase.insertProcessesFull();
MockDatabase.insertProcessForCalendarHierarchyTests();
MockDatabase.insertFoldersForSecondProject();
MockDatabase.setUpAwaitility();
rulesetId = MockDatabase.insertRuleset("Newspaper", "newspaper.xml", 1);
User userOne = ServiceManager.getUserService().getById(1);
SecurityTestUtils.addUserDataToSecurityContext(userOne, 1);
Awaitility.await().until(() -> {
Expand All @@ -94,8 +99,8 @@ public void setUp() throws Exception {
/**
* The test environment is cleaned up and the database is closed.
*/
@After
public void tearDown() throws Exception {
@AfterClass
public static void tearDown() throws Exception {
MockDatabase.stopNode();
MockDatabase.cleanDatabase();
KitodoConfigFile.PROJECT_CONFIGURATION.getFile().delete();
Expand All @@ -105,37 +110,74 @@ public void tearDown() throws Exception {
}
}

/**
* Create dummy processes and newspaper test process and copy corresponding meta.xml file.
* @throws DAOException when inserting test or dummy processes fails
* @throws DataException when inserting test or dummy processes fails
* @throws IOException when copying test metadata file fails
*/
@Before
public void prepareNewspaperProcess() throws DAOException, DataException, IOException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(2);
newspaperTestProcessId = MockDatabase.insertTestProcess(NEWSPAPER_TEST_PROCESS_TITLE, 1, 1, rulesetId);
ProcessTestUtils.copyTestFiles(newspaperTestProcessId, NEWSPAPER_TEST_METADATA_FILE);
}

/**
* Remove dummy and newspaper test processes.
* @throws DAOException when removing dummy processes from database fails
* @throws CustomResponseException when removing dummy processes from index fails
* @throws DataException when deleting newspaper test processes fails
* @throws IOException when deleting metadata test files fails
*/
@After
public void cleanupNewspaperProcess() throws DAOException, CustomResponseException, DataException, IOException {
for (int processId : dummyProcessIds) {
ServiceManager.getProcessService().removeFromDatabase(processId);
ServiceManager.getProcessService().removeFromIndex(processId, false);
}
if (newspaperTestProcessId > 0) {
deleteProcessHierarchy(ServiceManager.getProcessService().getById(newspaperTestProcessId));
}
}

private static void deleteProcessHierarchy(Process process) throws DAOException, DataException, IOException {
for (Process childProcess : process.getChildren()) {
deleteProcessHierarchy(childProcess);
}
ProcessService.deleteProcess(process.getId());
}

/**
* Perform the test. A long-running task is simulated: Progress and number
* of steps are queried and the next step is performed.
*/
@Test
public void shouldGenerateNewspaperProcesses() throws Exception {
// create backup of meta data file as this file is modified inside test
File metaFile = new File("src/test/resources/metadata/10/meta.xml");
File backupFile = new File("src/test/resources/metadata/10/meta.xml.1");
FileUtils.copyFile(metaFile, backupFile);

Process completeEdition = ServiceManager.getProcessService().getById(10);
Process completeEdition = ServiceManager.getProcessService().getById(newspaperTestProcessId);
Course course = NewspaperCourse.getCourse();
course.splitInto(Granularity.DAYS);
NewspaperProcessesGenerator underTest = new NewspaperProcessesGenerator(completeEdition, course);
while (underTest.getProgress() < underTest.getNumberOfSteps()) {
underTest.nextStep();
}
int maxId = getChildProcessWithLargestId(completeEdition, 0);
Assert.assertEquals("The newspaper processes generator has not been completed!", underTest.getNumberOfSteps(),
underTest.getProgress());
Assert.assertEquals("Process title missing in newspaper's meta.xml", "NewspaperOverallProcess",
readProcessTitleFromMetadata(10, false));
readProcessTitleFromMetadata(newspaperTestProcessId, false));
Assert.assertEquals("Process title missing in year's meta.xml", "NewspaperOverallProcess_1703",
readProcessTitleFromMetadata(11, false));
readProcessTitleFromMetadata(newspaperTestProcessId + 1, false));
Assert.assertEquals("Process title missing in issue's meta.xml", "NewspaperOverallProcess_17050127",
readProcessTitleFromMetadata(28, true));
readProcessTitleFromMetadata(maxId, true));
}

// restore backuped meta data file
FileUtils.deleteQuietly(metaFile);
FileUtils.moveFile(backupFile, metaFile);
cleanUp();
private int getChildProcessWithLargestId(Process process, int maxId) {
maxId = Math.max(maxId, process.getId());
for (Process childProcess : process.getChildren()) {
maxId = getChildProcessWithLargestId(childProcess, maxId);
}
return maxId;
}

/*
Expand All @@ -162,13 +204,7 @@ private String readProcessTitleFromMetadata(int processId, boolean issue) throws
*/
@Test
public void shouldGenerateSeasonProcesses() throws Exception {
// create backup of meta data file as this file is modified inside test
File metaFile = new File("src/test/resources/metadata/10/meta.xml");
File backupFile = new File("src/test/resources/metadata/10/meta.xml.1");
FileUtils.copyFile(metaFile, backupFile);

// Set base type in metadata/10/meta.xml to "Season"
Process seasonProcess = ServiceManager.getProcessService().getById(10);
Process seasonProcess = ServiceManager.getProcessService().getById(newspaperTestProcessId);
URI seasonUri = processService.getMetadataFileUri(seasonProcess);
Workpiece seasonMets = metsService.loadWorkpiece(seasonUri);
seasonMets.getLogicalStructure().setType("Season");
Expand Down Expand Up @@ -197,32 +233,26 @@ public void shouldGenerateSeasonProcesses() throws Exception {
*/
String twoYears = workpiece.getLogicalStructure().getOrderlabel();
List<String> years = Arrays.asList(twoYears.split("/", 2));
Assert.assertTrue("Bad season-year in " + seasonProcess + ": " + twoYears,
Integer.parseInt(years.get(0)) + 1 == Integer.parseInt(years.get(1)));
Assert.assertEquals("Bad season-year in " + seasonProcess + ": " + twoYears,
Integer.parseInt(years.get(0)) + 1, Integer.parseInt(years.get(1)));

// more tests
monthChecksOfShouldGenerateSeasonProcesses(seasonProcess, workpiece, twoYears, years);
dayChecksOfShouldGenerateSeasonProcesses(seasonProcess, workpiece);
}
}

// restore backed-up meta data file
FileUtils.deleteQuietly(metaFile);
FileUtils.moveFile(backupFile, metaFile);
cleanUp();
}

@Test
public void shouldNotGenerateDuplicateProcessTitle() throws DAOException, DataException {
Process completeEdition = ServiceManager.getProcessService().getById(10);
Process completeEdition = ServiceManager.getProcessService().getById(newspaperTestProcessId);
Course course = NewspaperCourse.getDuplicatedCourse();
course.splitInto(Granularity.DAYS);
GeneratesNewspaperProcessesThread generatesNewspaperProcessesThread = new GeneratesNewspaperProcessesThread(completeEdition, course);
generatesNewspaperProcessesThread.run();
generatesNewspaperProcessesThread.start();

ProcessDTO byId = ServiceManager.getProcessService().findById(11);
Assert.assertNull("Process should not have been created", byId.getTitle());

}

private void dayChecksOfShouldGenerateSeasonProcesses(Process seasonProcess, Workpiece seasonYearWorkpiece) {
Expand Down Expand Up @@ -291,19 +321,4 @@ private void monthChecksOfShouldGenerateSeasonProcesses(Process seasonProcess, W
previousMonthValue = monthValue;
}
}

/**
* To clean up after the end of the test. All metadata directories >10 will
* be deleted.
*/
private static void cleanUp() throws IOException {
Path dirProcesses = Paths.get(ConfigCore.getParameter(ParameterCore.DIR_PROCESSES));
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dirProcesses);
for (Path path : directoryStream) {
String fileName = path.getFileName().toString();
if (fileName.matches("\\d+") && Integer.valueOf(fileName) > 10) {
TreeDeleter.deltree(path);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ public void shouldCopyMultipleDataToChildren() throws Exception {
backupHierarchieFiles();
String metadataKey = "DigitalCollection";
HashMap<String, String> metadataSearchMap = new HashMap<>();
metadataSearchMap.put(metadataKey,"Kollektion1");
metadataSearchMap.put(metadataKey,"Kollektion2");
metadataSearchMap.put(metadataKey, "Kollektion2");

final List<ProcessDTO> processByMetadata = ServiceManager.getProcessService().findByMetadata(metadataSearchMap);
Assert.assertEquals("should not contain metadata beforehand", 1, processByMetadata.size() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class FileServiceIT {
private static final String RENAME_MEDIA_PROCESS_1 = "renameMediaProcess1";
private static final String RENAME_MEDIA_PROCESS_2 = "renameMediaProcess2";
private static final String RENAME_MEDIA_REVERT_PROCESS = "revertMediaRenamingProcess";
private static final int PROJECT_ID = 2;
private static List<Integer> dummyProcessIds = new LinkedList<>();
private static int mediaRenamingFirstProcessId = -1;
private static int mediaRenamingSecondProcessId = -1;
Expand Down Expand Up @@ -139,9 +140,9 @@ public void testMetadataImageComparator() {

@Test
public void testRenamingOfMultipleProcesses() throws DAOException, DataException, IOException, InterruptedException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
mediaRenamingFirstProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_PROCESS_1);
ProcessTestUtils.insertDummyProcesses();
ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
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 +164,7 @@ public void testRenamingOfMultipleProcesses() throws DAOException, DataException
@Test
public void testRevertingOriginalFilenamesAfterRenamingError() throws DAOException, DataException, IOException,
InterruptedException {
dummyProcessIds = ProcessTestUtils.insertDummyProcesses();
dummyProcessIds = ProcessTestUtils.insertDummyProcesses(PROJECT_ID);
revertMediaRenamingProcessId = MockDatabase.insertTestProcessIntoSecondProject(RENAME_MEDIA_REVERT_PROCESS);
ProcessTestUtils.copyTestFiles(revertMediaRenamingProcessId, TEST_RENAME_MEDIA_FILE);
Path processScansDir = Paths.get(ConfigCore.getKitodoDataDirectory(), revertMediaRenamingProcessId
Expand Down
Loading

0 comments on commit 0a82503

Please sign in to comment.