From ec2a2e5652c65d4f2aa24c8215f736dba2d911e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6hr?= Date: Mon, 18 Mar 2024 17:59:16 +0100 Subject: [PATCH 1/3] Add tests for CalendarService --- .../calendar/CalendarServiceTest.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java diff --git a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java new file mode 100644 index 00000000000..d649996cdd0 --- /dev/null +++ b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java @@ -0,0 +1,92 @@ +/* + * (c) Kitodo. Key to digital objects e. V. + * + * This file is part of the Kitodo project. + * + * It is licensed under GNU General Public License version 3 or later. + * + * For the full copyright and license information, please read the + * GPL3-License.txt file that was distributed with this source code. + */ + +package org.kitodo.production.services.calendar; + +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; +import org.junit.Test; +import org.kitodo.production.model.bibliography.course.Block; +import org.kitodo.production.model.bibliography.course.Course; +import org.kitodo.production.model.bibliography.course.Issue; + +import java.time.LocalDate; +import java.time.Month; + +public class CalendarServiceTest { + + @Test + public void shouldGetIndividualIssues() { + Course course = new Course(); + Block block = new Block(course); + Issue firstIssue = block.addIssue(); + Issue secondIssue = block.addIssue(); + firstIssue.setMonday(true); + firstIssue.setTuesday(true); + secondIssue.setSaturday(true); + Assert.assertEquals(0, CalendarService.getIndividualIssues(block).size()); + + block.setFirstAppearance(LocalDate.of(2024, Month.MARCH, 1)); + block.setLastAppearance(LocalDate.of(2024, Month.MARCH, 15)); + Assert.assertEquals(6, CalendarService.getIndividualIssues(block).size()); + + firstIssue.addExclusion(LocalDate.of(2024, Month.MARCH, 11)); + firstIssue.addExclusion(LocalDate.of(2024, Month.MARCH, 12)); + Assert.assertEquals(4, CalendarService.getIndividualIssues(block).size()); + + secondIssue.addAddition(LocalDate.of(2024, Month.MARCH, 1)); + Assert.assertEquals(5, CalendarService.getIndividualIssues(block).size()); + } + + @Test + public void shouldGetAddableMetadata() { + // TODO implement + } + + @Test + public void shouldGetMetadataTranslation() { + // TODO implement + } + + @Test + public void shouldGetMetadata() { + // TODO implement + } + + @Test + public void shouldGetMetadataSummary() { + // TODO implement + } + + @Test + public void shouldConvertDateIssueToString() { + Course course = new Course(); + LocalDate date = LocalDate.of(2024, Month.MARCH, 15); + Issue issue = new Issue(course, "Issue One"); + + Pair pairOne = Pair.of(date, issue); + Pair pairTwo = Pair.of(date, null); + Pair pairThree = Pair.of(null, issue); + Pair pairFour = Pair.of(null, null); + Pair pairFive = null; + + Assert.assertEquals("2024-03-15, Issue One", CalendarService.dateIssueToString(pairOne)); + Assert.assertEquals("2024-03-15", CalendarService.dateIssueToString(pairTwo)); + Assert.assertEquals("", CalendarService.dateIssueToString(pairThree)); + Assert.assertEquals("", CalendarService.dateIssueToString(pairFour)); + Assert.assertEquals("", CalendarService.dateIssueToString(pairFive)); + } + + @Test + public void shouldGetAddableMetadataTable() { + // TODO implement + } +} From e9451f9f936daf9a5ec9804c612d9dc798165a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6hr?= Date: Fri, 7 Jun 2024 09:02:36 +0200 Subject: [PATCH 2/3] Add tests for CalendarService --- .../services/calendar/CalendarServiceIT.java | 148 ++++++++++++++++++ .../calendar/CalendarServiceTest.java | 31 +--- 2 files changed, 151 insertions(+), 28 deletions(-) create mode 100644 Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java diff --git a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java new file mode 100644 index 00000000000..d30fe040ce1 --- /dev/null +++ b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java @@ -0,0 +1,148 @@ +/* + * (c) Kitodo. Key to digital objects e. V. + * + * This file is part of the Kitodo project. + * + * It is licensed under GNU General Public License version 3 or later. + * + * For the full copyright and license information, please read the + * GPL3-License.txt file that was distributed with this source code. + */ +package org.kitodo.production.services.calendar; + +import java.io.IOException; +import java.time.LocalDate; +import java.time.Month; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.tuple.Pair; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.kitodo.MockDatabase; +import org.kitodo.SecurityTestUtils; +import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewInterface; +import org.kitodo.data.database.beans.Process; +import org.kitodo.data.database.exceptions.DAOException; +import org.kitodo.data.exceptions.DataException; +import org.kitodo.production.forms.createprocess.ProcessDetail; +import org.kitodo.production.model.bibliography.course.Block; +import org.kitodo.production.model.bibliography.course.Course; +import org.kitodo.production.model.bibliography.course.Issue; +import org.kitodo.production.model.bibliography.course.metadata.CountableMetadata; +import org.kitodo.production.services.ServiceManager; +import org.kitodo.test.utils.ProcessTestUtils; + +public class CalendarServiceIT { + + private static int newspaperTestProcessId = -1; + private static final String NEWSPAPER_TEST_METADATA_FILE = "testmetaNewspaper.xml"; + private static final String NEWSPAPER_TEST_PROCESS_TITLE = "NewspaperOverallProcess"; + + + @BeforeClass + public static void prepareDatabase() throws Exception { + MockDatabase.startNode(); + MockDatabase.insertProcessesFull(); + int rulesetId = MockDatabase.insertRuleset("Newspaper", "newspaper.xml", 1); + newspaperTestProcessId = MockDatabase.insertTestProcess(NEWSPAPER_TEST_PROCESS_TITLE, 1, 1, rulesetId); + ProcessTestUtils.copyTestMetadataFile(newspaperTestProcessId, NEWSPAPER_TEST_METADATA_FILE); + SecurityTestUtils.addUserDataToSecurityContext(ServiceManager.getUserService().getById(1), 1); + } + + @AfterClass + public static void cleanDatabase() throws Exception { + ProcessTestUtils.removeTestProcess(newspaperTestProcessId); + MockDatabase.stopNode(); + MockDatabase.cleanDatabase(); + } + + @Test + public void shouldGetAddableMetadata() throws DAOException, DataException, IOException { + Process process = ServiceManager.getProcessService().getById(newspaperTestProcessId); + List actualMetadata = CalendarService.getAddableMetadata(process); + + List expectedMetadata = Arrays.asList("ORDERLABEL", "LABEL", "CONTENTIDS", "ProcessTitle", "ShelfMark"); + Assert.assertEquals(expectedMetadata, actualMetadata.stream() + .map(MetadataViewInterface::getId) + .collect(Collectors.toList())); + } + + @Test + public void shouldGetMetadata() { + Course course = new Course(); + Block block = new Block(course); + Issue firstIssue = block.addIssue(); + Issue secondIssue = block.addIssue(); + firstIssue.setMonday(true); + firstIssue.setTuesday(true); + secondIssue.setSaturday(true); + + block.setFirstAppearance(LocalDate.of(2024, Month.MARCH, 1)); + block.setLastAppearance(LocalDate.of(2024, Month.MARCH, 15)); + + Pair firstIssueFirstAppearance = Pair.of(LocalDate.of(2024, Month.MARCH, 4), firstIssue); + CountableMetadata processTitle = new CountableMetadata(block, firstIssueFirstAppearance); + processTitle.setMetadataType("ProcessTitle"); + block.addMetadata(processTitle); + + Pair secondIssueFirstAppearance = Pair.of(LocalDate.of(2024, Month.MARCH, 5), secondIssue); + CountableMetadata shelfMark = new CountableMetadata(block, secondIssueFirstAppearance); + shelfMark.setMetadataType("ShelfMark"); + block.addMetadata(shelfMark); + + Assert.assertEquals(1, + CalendarService.getMetadata(block, LocalDate.of(2024, Month.MARCH, 4), firstIssue).size()); + Assert.assertEquals(2, + CalendarService.getMetadata(block, LocalDate.of(2024, Month.MARCH, 5), secondIssue).size()); + Assert.assertEquals(2, + CalendarService.getMetadata(block, LocalDate.of(2024, Month.MARCH, 12), secondIssue).size()); + } + + @Test + public void shouldGetMetadataSummary() throws DAOException, DataException, IOException { + Process process = ServiceManager.getProcessService().getById(newspaperTestProcessId); + List addableMetadata = CalendarService.getAddableMetadataTable(process); + Course course = new Course(); + Block block = new Block(course); + Issue firstIssue = block.addIssue(); + Issue secondIssue = block.addIssue(); + firstIssue.setMonday(true); + firstIssue.setTuesday(true); + secondIssue.setSaturday(true); + + block.setFirstAppearance(LocalDate.of(2024, Month.MARCH, 1)); + block.setLastAppearance(LocalDate.of(2024, Month.MARCH, 15)); + + Pair firstIssueFirstAppearance = Pair.of(LocalDate.of(2024, Month.MARCH, 4), firstIssue); + CountableMetadata processTitle = new CountableMetadata(block, firstIssueFirstAppearance); + processTitle.setMetadataDetail(addableMetadata.get(3)); + processTitle.setStartValue("Test Process Title"); + block.addMetadata(processTitle); + + Pair secondIssueFirstAppearance = Pair.of(LocalDate.of(2024, Month.MARCH, 5), secondIssue); + CountableMetadata shelfMark = new CountableMetadata(block, secondIssueFirstAppearance); + shelfMark.setMetadataDetail(addableMetadata.get(4)); + shelfMark.setStartValue("Test Shelf Mark"); + block.addMetadata(shelfMark); + + List actualMetadataSummary = CalendarService.getMetadataSummary(block).stream() + .map(entry -> entry.getKey().getLabel() + " - " + entry.getValue().toString()) + .collect(Collectors.toList()); + List expectedMetadataSummary = Arrays.asList("Process title - 2024-03-04", "Signatur - 2024-03-05"); + Assert.assertEquals(expectedMetadataSummary, actualMetadataSummary); + } + + @Test + public void shouldGetAddableMetadataTable() throws DAOException, DataException, IOException { + Process process = ServiceManager.getProcessService().getById(newspaperTestProcessId); + List actualMetadata = CalendarService.getAddableMetadataTable(process); + + List expectedMetadata = Arrays.asList( + "METS Reihenfolge-Etikett", "METS-Beschriftung", "METS-Inhalts-ID", "Process title", "Signatur"); + Assert.assertEquals(expectedMetadata, actualMetadata.stream().map(ProcessDetail::getLabel).collect(Collectors.toList())); + } +} diff --git a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java index d649996cdd0..ee478c7786c 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java @@ -11,6 +11,9 @@ package org.kitodo.production.services.calendar; +import java.time.LocalDate; +import java.time.Month; + import org.apache.commons.lang3.tuple.Pair; import org.junit.Assert; import org.junit.Test; @@ -18,9 +21,6 @@ import org.kitodo.production.model.bibliography.course.Course; import org.kitodo.production.model.bibliography.course.Issue; -import java.time.LocalDate; -import java.time.Month; - public class CalendarServiceTest { @Test @@ -46,26 +46,6 @@ public void shouldGetIndividualIssues() { Assert.assertEquals(5, CalendarService.getIndividualIssues(block).size()); } - @Test - public void shouldGetAddableMetadata() { - // TODO implement - } - - @Test - public void shouldGetMetadataTranslation() { - // TODO implement - } - - @Test - public void shouldGetMetadata() { - // TODO implement - } - - @Test - public void shouldGetMetadataSummary() { - // TODO implement - } - @Test public void shouldConvertDateIssueToString() { Course course = new Course(); @@ -84,9 +64,4 @@ public void shouldConvertDateIssueToString() { Assert.assertEquals("", CalendarService.dateIssueToString(pairFour)); Assert.assertEquals("", CalendarService.dateIssueToString(pairFive)); } - - @Test - public void shouldGetAddableMetadataTable() { - // TODO implement - } } From 3a5e20e771d75894c343b3f8264edde3ced1e32a Mon Sep 17 00:00:00 2001 From: Arved Solth Date: Mon, 10 Jun 2024 09:55:20 +0200 Subject: [PATCH 3/3] Use version 5 of JUnit --- .../services/calendar/CalendarServiceIT.java | 25 ++++++++++--------- .../calendar/CalendarServiceTest.java | 23 +++++++++-------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java index d30fe040ce1..206801b08f8 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceIT.java @@ -10,6 +10,8 @@ */ package org.kitodo.production.services.calendar; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.io.IOException; import java.time.LocalDate; import java.time.Month; @@ -18,10 +20,9 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.tuple.Pair; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.kitodo.MockDatabase; import org.kitodo.SecurityTestUtils; import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewInterface; @@ -43,7 +44,7 @@ public class CalendarServiceIT { private static final String NEWSPAPER_TEST_PROCESS_TITLE = "NewspaperOverallProcess"; - @BeforeClass + @BeforeAll public static void prepareDatabase() throws Exception { MockDatabase.startNode(); MockDatabase.insertProcessesFull(); @@ -53,7 +54,7 @@ public static void prepareDatabase() throws Exception { SecurityTestUtils.addUserDataToSecurityContext(ServiceManager.getUserService().getById(1), 1); } - @AfterClass + @AfterAll public static void cleanDatabase() throws Exception { ProcessTestUtils.removeTestProcess(newspaperTestProcessId); MockDatabase.stopNode(); @@ -66,7 +67,7 @@ public void shouldGetAddableMetadata() throws DAOException, DataException, IOExc List actualMetadata = CalendarService.getAddableMetadata(process); List expectedMetadata = Arrays.asList("ORDERLABEL", "LABEL", "CONTENTIDS", "ProcessTitle", "ShelfMark"); - Assert.assertEquals(expectedMetadata, actualMetadata.stream() + assertEquals(expectedMetadata, actualMetadata.stream() .map(MetadataViewInterface::getId) .collect(Collectors.toList())); } @@ -94,11 +95,11 @@ public void shouldGetMetadata() { shelfMark.setMetadataType("ShelfMark"); block.addMetadata(shelfMark); - Assert.assertEquals(1, + assertEquals(1, CalendarService.getMetadata(block, LocalDate.of(2024, Month.MARCH, 4), firstIssue).size()); - Assert.assertEquals(2, + assertEquals(2, CalendarService.getMetadata(block, LocalDate.of(2024, Month.MARCH, 5), secondIssue).size()); - Assert.assertEquals(2, + assertEquals(2, CalendarService.getMetadata(block, LocalDate.of(2024, Month.MARCH, 12), secondIssue).size()); } @@ -133,7 +134,7 @@ public void shouldGetMetadataSummary() throws DAOException, DataException, IOExc .map(entry -> entry.getKey().getLabel() + " - " + entry.getValue().toString()) .collect(Collectors.toList()); List expectedMetadataSummary = Arrays.asList("Process title - 2024-03-04", "Signatur - 2024-03-05"); - Assert.assertEquals(expectedMetadataSummary, actualMetadataSummary); + assertEquals(expectedMetadataSummary, actualMetadataSummary); } @Test @@ -143,6 +144,6 @@ public void shouldGetAddableMetadataTable() throws DAOException, DataException, List expectedMetadata = Arrays.asList( "METS Reihenfolge-Etikett", "METS-Beschriftung", "METS-Inhalts-ID", "Process title", "Signatur"); - Assert.assertEquals(expectedMetadata, actualMetadata.stream().map(ProcessDetail::getLabel).collect(Collectors.toList())); + assertEquals(expectedMetadata, actualMetadata.stream().map(ProcessDetail::getLabel).collect(Collectors.toList())); } } diff --git a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java index ee478c7786c..acad9286c51 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/calendar/CalendarServiceTest.java @@ -11,12 +11,13 @@ package org.kitodo.production.services.calendar; +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.time.LocalDate; import java.time.Month; import org.apache.commons.lang3.tuple.Pair; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.kitodo.production.model.bibliography.course.Block; import org.kitodo.production.model.bibliography.course.Course; import org.kitodo.production.model.bibliography.course.Issue; @@ -32,18 +33,18 @@ public void shouldGetIndividualIssues() { firstIssue.setMonday(true); firstIssue.setTuesday(true); secondIssue.setSaturday(true); - Assert.assertEquals(0, CalendarService.getIndividualIssues(block).size()); + assertEquals(0, CalendarService.getIndividualIssues(block).size()); block.setFirstAppearance(LocalDate.of(2024, Month.MARCH, 1)); block.setLastAppearance(LocalDate.of(2024, Month.MARCH, 15)); - Assert.assertEquals(6, CalendarService.getIndividualIssues(block).size()); + assertEquals(6, CalendarService.getIndividualIssues(block).size()); firstIssue.addExclusion(LocalDate.of(2024, Month.MARCH, 11)); firstIssue.addExclusion(LocalDate.of(2024, Month.MARCH, 12)); - Assert.assertEquals(4, CalendarService.getIndividualIssues(block).size()); + assertEquals(4, CalendarService.getIndividualIssues(block).size()); secondIssue.addAddition(LocalDate.of(2024, Month.MARCH, 1)); - Assert.assertEquals(5, CalendarService.getIndividualIssues(block).size()); + assertEquals(5, CalendarService.getIndividualIssues(block).size()); } @Test @@ -58,10 +59,10 @@ public void shouldConvertDateIssueToString() { Pair pairFour = Pair.of(null, null); Pair pairFive = null; - Assert.assertEquals("2024-03-15, Issue One", CalendarService.dateIssueToString(pairOne)); - Assert.assertEquals("2024-03-15", CalendarService.dateIssueToString(pairTwo)); - Assert.assertEquals("", CalendarService.dateIssueToString(pairThree)); - Assert.assertEquals("", CalendarService.dateIssueToString(pairFour)); - Assert.assertEquals("", CalendarService.dateIssueToString(pairFive)); + assertEquals("2024-03-15, Issue One", CalendarService.dateIssueToString(pairOne)); + assertEquals("2024-03-15", CalendarService.dateIssueToString(pairTwo)); + assertEquals("", CalendarService.dateIssueToString(pairThree)); + assertEquals("", CalendarService.dateIssueToString(pairFour)); + assertEquals("", CalendarService.dateIssueToString(pairFive)); } }