Skip to content

Commit

Permalink
[SERV-889] Handle "IIIF Parent ARK" Header in CSVs (#235)
Browse files Browse the repository at this point in the history
* [SERV-889] initial coding

* [SERV-889] adding tests

* [SERV-889] fixing method name

* [SERV-889] codacy fixes

* [SERV-889] fixes for codacy fixes
  • Loading branch information
DRickard authored Dec 1, 2023
1 parent c057535 commit 2ee3edd
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 31 deletions.
5 changes: 5 additions & 0 deletions src/main/java/edu/ucla/library/iiif/fester/CSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public final class CSV {
*/
public static final String PARENT_ARK = "Parent ARK";

/**
* Optional alternate ARK identifier for the parent object.
*/
public static final String IIIF_PARENT_ARK = "IIIF Parent ARK";

/**
* The manifest URL for works and collections.
*/
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/edu/ucla/library/iiif/fester/CsvHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public class CsvHeaders {
*/
private int myParentArkIndex = -1;

/**
* The index position for the parent IIIF ARK column.
*/
private int myIiifParentArkIndex = -1;

/**
* The index position for the title column.
*/
Expand Down Expand Up @@ -122,6 +127,9 @@ public CsvHeaders(final String[] aRow) {
case CSV.PARENT_ARK:
setParentArkIndex(index);
break;
case CSV.IIIF_PARENT_ARK:
setIiifParentArkIndex(index);
break;
case CSV.OBJECT_TYPE:
setObjectTypeIndex(index);
break;
Expand Down Expand Up @@ -273,6 +281,37 @@ public boolean hasParentArkIndex() {
return myParentArkIndex != -1;
}

/**
* Gets the IIIF Parent ARK index position.
*
* @return The Parent ARK index position
*/
@JsonGetter
public int getIiifParentArkIndex() {
return myIiifParentArkIndex;
}

/**
* Sets the IIIF Parent ARK index position.
*
* @param aIiifParentArkIndex The index position of the Parent ARK
* @return This CSV headers
*/
@JsonSetter
public CsvHeaders setIiifParentArkIndex(final int aIiifParentArkIndex) {
myIiifParentArkIndex = aIiifParentArkIndex;
return this;
}

/**
* Checks whether there is an IIIF Parent ARK index position
*
* @return True if there is an IIIF Parent ARK index position; else, false
*/
public boolean hasIiifParentArkIndex() {
return myIiifParentArkIndex != -1;
}

/**
* Gets the Title index position.
*
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/edu/ucla/library/iiif/fester/CsvParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public CsvParser parse(final Path aPath, final String aIiifVersion, final String
// Required CSV columns
if (!myCsvHeaders.hasItemArkIndex()) {
throw new CsvParsingException(MessageCodes.MFS_113);
} else if (!myCsvHeaders.hasParentArkIndex()) {
} else if (!myCsvHeaders.hasParentArkIndex() && !myCsvHeaders.hasIiifParentArkIndex()) {
throw new CsvParsingException(MessageCodes.MFS_114);
} else if (!myCsvHeaders.hasTitleIndex()) {
throw new CsvParsingException(MessageCodes.MFS_111);
Expand Down Expand Up @@ -235,7 +235,9 @@ private void extractCollectionMetadata(final String... aRow) throws CsvParsingEx
* @throws CsvParsingException If there is trouble getting the necessary info from the CSV
*/
private void extractWorkMetadata(final String... aRow) throws CsvParsingException {
final Optional<String> parentIdOpt = getMetadata(aRow, myCsvHeaders.getParentArkIndex());
final Optional<String> parentIdOpt = getMetadata(aRow,
myCsvHeaders.hasIiifParentArkIndex() ? myCsvHeaders.getIiifParentArkIndex() :
myCsvHeaders.getParentArkIndex());
final Optional<String> workIdOpt = getMetadata(aRow, myCsvHeaders.getItemArkIndex());
final Optional<String> labelOpt = getMetadata(aRow, myCsvHeaders.getTitleIndex());

Expand Down Expand Up @@ -280,7 +282,9 @@ private void extractWorkMetadata(final String... aRow) throws CsvParsingExceptio
* @throws CsvParsingException
*/
private void extractPageMetadata(final String... aRow) throws CsvParsingException {
final Optional<String> workIdOpt = getMetadata(aRow, myCsvHeaders.getParentArkIndex());
final Optional<String> workIdOpt = getMetadata(aRow,
myCsvHeaders.hasIiifParentArkIndex() ? myCsvHeaders.getIiifParentArkIndex() :
myCsvHeaders.getParentArkIndex());

if (workIdOpt.isPresent()) {
final String workID = workIdOpt.get();
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/edu/ucla/library/iiif/fester/CsvParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public final void testGetCsvHeaders() throws CsvParsingException, CsvException,
assertEquals(-1, csvHeaders.getLocalRightsStatementIndex());
assertEquals(5, csvHeaders.getObjectTypeIndex());
assertEquals(39, csvHeaders.getIiifObjectTypeIndex());
assertEquals(40, csvHeaders.getIiifParentArkIndex());
assertEquals(2, csvHeaders.getParentArkIndex());
assertEquals(20, csvHeaders.getRepositoryNameIndex());
assertEquals(17, csvHeaders.getRightsContactIndex());
Expand All @@ -113,6 +114,7 @@ public final void testGetCsvHeadersWhitespace() throws CsvParsingException, IOEx
assertEquals(2, csvHeaders.getParentArkIndex());
assertEquals(3, csvHeaders.getObjectTypeIndex());
assertEquals(24, csvHeaders.getIiifObjectTypeIndex());
assertEquals(25, csvHeaders.getIiifParentArkIndex());
assertEquals(4, csvHeaders.getFileNameIndex());
assertEquals(5, csvHeaders.getItemSequenceIndex());
assertEquals(12, csvHeaders.getTitleIndex());
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/edu/ucla/library/iiif/fester/fit/PostCsvFIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class PostCsvFIT {

private static final File IIIF_TYPES_CSV = new File(DIR, "csv/hathaway_iiif_type.csv");

private static final File IIIF_ARK_CSV = new File(DIR, "csv/hathaway_iiif_ark.csv");

private static final File WORKS_CSV_COLLECTION = new File(DIR, "csv/hathaway/batch1/works.csv");

private static final File WORKS_CSV_NO_COLLECTION = new File(DIR, "csv/hathaway/batch2/works.csv");
Expand Down Expand Up @@ -920,6 +922,51 @@ public final void testIiifObjectTypeV3(final TestContext aContext) {
}


/**
* Tests submitting a CSV with IIIF Parent ARK header with IIIF Presentation API 3 specified.
*
* @param aContext A test context
*/
@Test
public final void testIiifParentArkV3(final TestContext aContext) {
final Async asyncTask = aContext.async();

postCSV(IIIF_ARK_CSV, Constants.IIIF_API_V3, post -> {
if (post.succeeded()) {
final HttpResponse<Buffer> response = post.result();
final int statusCode = response.statusCode();
final String statusMessage = response.statusMessage();

if (statusCode == HTTP.CREATED) {
// Just check the second manifest, since another test already uses the first manifest
final Optional<JsonObject> optJsonObject = checkS3(HATHAWAY_SECOND_WORK_ARK, false);

if (optJsonObject.isPresent()) {
try {
final JsonObject expected = readJsonFile(HATHAWAY_SECOND_MANIFEST);
final JsonObject found = optJsonObject.get();

aContext.assertTrue(TestUtils.manifestsAreEffectivelyEqual(expected, found));
} catch (final IOException details) {
LOGGER.error(details, details.getMessage());
aContext.fail(details);
}
} else {
aContext.fail(LOGGER.getMessage(MessageCodes.MFS_154, ALL_IN_ONE_CSV));
}
TestUtils.complete(asyncTask);
} else {
aContext.fail(LOGGER.getMessage(MessageCodes.MFS_039, statusCode, statusMessage));
}
} else {
final Throwable exception = post.cause();

LOGGER.error(exception, exception.getMessage());
aContext.fail(exception);
}
});
}

/**
* Tests submitting a CSV using an outdated version of Festerize.
*
Expand Down
Loading

0 comments on commit 2ee3edd

Please sign in to comment.