Skip to content

Commit

Permalink
Merge pull request kitodo#6097 from effective-webwork/countable-metad…
Browse files Browse the repository at this point in the history
…ata-test

Add test for CountableMetadata
  • Loading branch information
solth authored May 31, 2024
2 parents 8bfc23a + 291bf9a commit 746fe09
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class CalendarForm implements Serializable {
* done once on form creation.
*/
private final LocalDate today = LocalDate.now();
private static Integer parentId;
private Integer parentId;

private String activeIndexes = "0";

Expand Down Expand Up @@ -195,7 +195,7 @@ public void setParentId(Integer parentId) {
*
* @return value of parentId
*/
public static Integer getParentId() {
public Integer getParentId() {
return parentId;
}

Expand Down Expand Up @@ -721,8 +721,9 @@ public void addMetadata(Issue issue, boolean onlyThisIssue) {
if (!selectedBlock.getIssues().isEmpty() && Objects.nonNull(selectedIssue)) {
CountableMetadata metadata = new CountableMetadata(selectedBlock,
Triple.of(selectedIssue.getDate(), selectedIssue.getIssue(), onlyThisIssue));
if (!metadata.getAllMetadataTypes().isEmpty()) {
metadata.setMetadataDetail(metadata.getAllMetadataTypes().get(0));
List<ProcessDetail> metadataTypes = metadata.getAllMetadataTypes(getParentId());
if (!metadataTypes.isEmpty()) {
metadata.setMetadataDetail(metadataTypes.get(0));
}
selectedBlock.addMetadata(metadata);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.InvalidMetadataValueException;
import org.kitodo.production.forms.CalendarForm;
import org.kitodo.production.forms.createprocess.ProcessDetail;
import org.kitodo.production.forms.createprocess.ProcessTextMetadata;
import org.kitodo.production.helper.Helper;
Expand All @@ -51,13 +50,13 @@ public class CountableMetadata {
* access to the other issues, which—together with the start value and step
* size—define the value of the counter on a given day.
*/
private Block block;
private final Block block;

/**
* Date and issue this counter appears the first time,
* boolean to check if this metadata counter is only associated to issue or not.
*/
private Triple<LocalDate, Issue, Boolean> create;
private final Triple<LocalDate, Issue, Boolean> create;

/**
* Date and issue this counter does no longer appear on. May be null, indicating that no end issue has been set.
Expand Down Expand Up @@ -211,7 +210,7 @@ public String getValue(Pair<LocalDate, Issue> selectedIssue, MonthDay yearStart)
assert new IssueComparator(block).compare(selectedIssue, Pair.of(this.create.getLeft(), this.create.getMiddle())) >= 0;
Paginator values = new Paginator(startValue);
int breakMark = 0;
for (LocalDate i = create.getLeft(); i.compareTo(selectedIssue.getLeft()) <= 0; i = i.plusDays(1)) {
for (LocalDate i = create.getLeft(); !i.isAfter(selectedIssue.getLeft()); i = i.plusDays(1)) {
boolean first = i.equals(create.getLeft());
for (IndividualIssue issue : block.getIndividualIssues(i)) {
if (first && block.getIssueIndex(issue.getIssue()) < block.getIssueIndex(create.getMiddle())) {
Expand Down Expand Up @@ -329,12 +328,14 @@ public void setMetadataDetail(ProcessDetail metadataDetail) {
/**
* Returns the list of selectable metadata types.
*
* @return the map of metadata types
* @param processId ID of process for which metadata types are determined
*
* @return the list of metadata types
*/
public List<ProcessDetail> getAllMetadataTypes() {
public List<ProcessDetail> getAllMetadataTypes(Integer processId) {
if (Objects.isNull(allMetadataTypes)) {
try {
Process process = ServiceManager.getProcessService().getById(CalendarForm.getParentId());
Process process = ServiceManager.getProcessService().getById(processId);
allMetadataTypes = new ArrayList<>(CalendarService.getAddableMetadataTable(process));

} catch (DAOException | DataException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@
converter="#{processDetailConverter}"
value="#{metadata.metadataDetail}"
title="#{msgs.type}">
<f:selectItems value="#{metadata.allMetadataTypes}"
<f:selectItems value="#{metadata.getAllMetadataTypes(CalendarForm.parentId)}"
var="metadataType"
itemValue="#{metadataType}"
itemLabel="#{metadataType.label}"/>
<f:attribute name="allMetadataTypes"
value="#{metadata.allMetadataTypes}"/>
value="#{metadata.getAllMetadataTypes(CalendarForm.parentId)}"/>
<p:ajax event="change" update="calendarDayForm"/>
</p:selectOneMenu>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* 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.model.bibliography.course.metadata;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kitodo.MockDatabase;
import org.kitodo.SecurityTestUtils;
import org.kitodo.data.database.beans.User;
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.Granularity;
import org.kitodo.production.model.bibliography.course.Issue;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.test.utils.ProcessTestUtils;
import org.kitodo.test.utils.TestConstants;

public class CountableMetadataIT {

private Issue issue;
private CountableMetadata countableMetadata;
private static int processId = -1;
private static final int EXPECTED_NUMBER_OF_METADATA_TYPES = 12;
private static final int PUBLICATION_YEAR = 1990;
private static final Month PUBLICATION_MONTH = Month.MARCH;
private static final LocalDate BLOCK_START = LocalDate.of(PUBLICATION_YEAR, PUBLICATION_MONTH, 1);
private static final LocalDate BLOCK_END = LocalDate.of(PUBLICATION_YEAR, PUBLICATION_MONTH, 30);
private static final LocalDate HIDDEN_DATE = LocalDate.of(PUBLICATION_YEAR, PUBLICATION_MONTH, 5);
private static final LocalDate DEFINE_DATE = LocalDate.of(PUBLICATION_YEAR, PUBLICATION_MONTH, 12);
private static final LocalDate CONTINUE_DATE = LocalDate.of(PUBLICATION_YEAR, PUBLICATION_MONTH, 19);
private static final LocalDate DELETE_DATE = LocalDate.of(PUBLICATION_YEAR, PUBLICATION_MONTH, 26);
private static final MonthDay YEAR_START = MonthDay.of(Month.JANUARY, 1);
private static final String ISSUE_HEADING = "Abendausgabe";
private static final String PROCESS_TITLE = "Countable_Metadata_Test_Process";
private static final String METADATA_START_VALUE = "Ausgabe 1";

@BeforeAll
public static void setup() throws Exception {
MockDatabase.startNode();
MockDatabase.insertProcessesFull();
MockDatabase.setUpAwaitility();
User userOne = ServiceManager.getUserService().getById(1);
SecurityTestUtils.addUserDataToSecurityContext(userOne, 1);
}

@AfterAll
public static void tearDown() throws Exception {
MockDatabase.stopNode();
MockDatabase.cleanDatabase();
}

@BeforeEach
public void init() {
Course course = new Course();
issue = new Issue(course);
issue.setHeading(ISSUE_HEADING);
issue.setMonday(true);
Pair<LocalDate, Issue> pair = new ImmutablePair<>(DEFINE_DATE, issue);
Block block = new Block(course);
block.setPublicationPeriod(BLOCK_START, BLOCK_END);
block.addIssue(issue);
countableMetadata = new CountableMetadata(block, pair);
countableMetadata.setStepSize(Granularity.ISSUES);
countableMetadata.setDelete(new ImmutablePair<>(DELETE_DATE, issue));
}

@AfterEach
public void removeTestProcess() throws DAOException {
if (processId > 0) {
ProcessTestUtils.removeTestProcess(processId);
processId = -1;
}
}

@Test
public void shouldGetEditMode() {
Pair<LocalDate, Issue> priorPair = new ImmutablePair<>(HIDDEN_DATE, issue);
MetadataEditMode shouldBeHiddenMode = countableMetadata.getEditMode(priorPair);
assertEquals(MetadataEditMode.HIDDEN, shouldBeHiddenMode);

Pair<LocalDate, Issue> concurrentPair = new ImmutablePair<>(DEFINE_DATE, issue);
MetadataEditMode shouldBeDefineMode = countableMetadata.getEditMode(concurrentPair);
assertEquals(MetadataEditMode.DEFINE, shouldBeDefineMode);

Pair<LocalDate, Issue> laterPair = new ImmutablePair<>(CONTINUE_DATE, issue);
MetadataEditMode shouldBeContinueMode = countableMetadata.getEditMode(laterPair);
assertEquals(MetadataEditMode.CONTINUE, shouldBeContinueMode);

Pair<LocalDate, Issue> deletePair = new ImmutablePair<>(DELETE_DATE, issue);
MetadataEditMode shouldBeDeleteMode = countableMetadata.getEditMode(deletePair);
assertEquals(MetadataEditMode.DELETE, shouldBeDeleteMode);
}

@Test
public void shouldGetValue() throws DAOException, DataException {
List<ProcessDetail> metadataTypes = getMetadataTypes();
countableMetadata.setMetadataDetail(metadataTypes.get(0));
countableMetadata.setStartValue(METADATA_START_VALUE);
Pair<LocalDate, Issue> issuePair = new ImmutablePair<>(DEFINE_DATE.plusWeeks(1), issue);
String value = countableMetadata.getValue(issuePair, YEAR_START);
assertTrue(StringUtils.isNotBlank(value));
}

@Test
public void shouldMatch() throws DAOException, DataException {
List<ProcessDetail> metadataTypes = getMetadataTypes();
ProcessDetail firstMetadataType = metadataTypes.get(0);
countableMetadata.setMetadataDetail(firstMetadataType);
countableMetadata.setStartValue(METADATA_START_VALUE);
Pair<LocalDate, Issue> deletePair = new ImmutablePair<>(DELETE_DATE, issue);
assertTrue(countableMetadata.matches(firstMetadataType.getMetadataID(), deletePair, false));
assertFalse(countableMetadata.matches(firstMetadataType.getMetadataID(), deletePair, true));
}

@Test
public void shouldGetAllMetadataTypes() throws DAOException, DataException {
List<ProcessDetail> allMetadataTypes = getMetadataTypes();
assertEquals(EXPECTED_NUMBER_OF_METADATA_TYPES, allMetadataTypes.size());
List<String> labels = allMetadataTypes.stream().map(ProcessDetail::getLabel).collect(Collectors.toList());
assertTrue(labels.contains(TestConstants.TITLE_MAIN));
assertTrue(labels.contains(TestConstants.METS_LABEL));
}

private List<ProcessDetail> getMetadataTypes() throws DAOException, DataException {
processId = MockDatabase.insertTestProcess(PROCESS_TITLE, 1, 1, 1);
List<ProcessDetail> allMetadataTypes = countableMetadata.getAllMetadataTypes(processId);
assertTrue(Objects.nonNull(allMetadataTypes));
assertFalse(allMetadataTypes.isEmpty());
return allMetadataTypes;
}
}
2 changes: 2 additions & 0 deletions Kitodo/src/test/java/org/kitodo/test/utils/TestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public final class TestConstants {
public static final String VALUE = "value";
public static final String TEST_RULESET = "src/test/resources/rulesets/ruleset_test.xml";
public static final String TITLE_DOC_MAIN = "TitleDocMain";
public static final String TITLE_MAIN = "HauptTitel";
public static final String METS_LABEL = "METS-Anordnungsbeschriftung";
}

0 comments on commit 746fe09

Please sign in to comment.