Skip to content

Commit

Permalink
imageFormats constant, add image formats, lowercase file extension, a…
Browse files Browse the repository at this point in the history
…llowedActions return list, add test, remove jackson.version
  • Loading branch information
krwong committed Aug 29, 2024
1 parent 7d99adf commit f080b96
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
3 changes: 0 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<junit.jupiter.version>5.9.2</junit.jupiter.version>
<solr.solrj.version>8.10.1</solr.solrj.version>
<embedded-redis.version>0.7.3</embedded-redis.version>
<jackson.version>2.17.2</jackson.version>
</properties>

<build>
Expand Down Expand Up @@ -142,12 +141,10 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Service for listing all chompb projects in a given directory
Expand All @@ -34,6 +37,12 @@ public class ListProjectsService {
public static final String PROJECT_PATH = "projectPath";
public static final String STATUS = "status";
public static final String ALLOWED_ACTIONS = "allowedActions";
private static final Set<String> IMAGE_FORMATS = new HashSet<>();
// accepted file types are listed in imageFormats below
static {
IMAGE_FORMATS.addAll(Arrays.asList("tif", "tiff", "jpeg", "jpg", "png", "gif", "pict", "bmp",
"psd", "jp2", "nef", "crw", "cr2", "dng", "raf"));
}

/**
* List projects in given directory
Expand All @@ -56,14 +65,14 @@ public JsonNode listProjects(Path directory) throws Exception {

Path projectPath = directory.toAbsolutePath();
String projectStatus = status(project);
String allowedActions = allowedActions(project);
List<String> allowedActions = allowedActions(project);
JsonNode projectProperties = mapper.readTree(project.getProjectPropertiesPath().toFile());

// add project info to JSON
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put(PROJECT_PATH, projectPath.toString());
objectNode.put(STATUS, projectStatus);
objectNode.put(ALLOWED_ACTIONS, allowedActions);
objectNode.put(ALLOWED_ACTIONS, String.valueOf(allowedActions));
objectNode.set("projectProperties", projectProperties);
arrayNode.add(objectNode);
}
Expand Down Expand Up @@ -105,16 +114,15 @@ private String status(MigrationProject project) {
* crop_color_bars: populated if source files are mapped and if project contains any images (based off source file extensions)
* @return allowed_actions
*/
private String allowedActions(MigrationProject project) throws Exception {
String allowedActions = null;
List<String> imageFormats = Arrays.asList("tif", "jpeg", "png", "gif", "pict", "bmp",
"psd", "jp2", "nef", "crw", "cr2", "dng", "raf");
private List<String> allowedActions(MigrationProject project) throws Exception {
List<String> allowedActions = new ArrayList<>();

if (project.getProjectProperties().getSourceFilesUpdatedDate() != null) {
SourceFilesInfo info = sourceFileService.loadMappings();
if (info.getMappings().stream().map(entry ->
FilenameUtils.getExtension(entry.getFirstSourcePath().toString())).anyMatch(imageFormats::contains)) {
allowedActions = "crop_color_bars";
FilenameUtils.getExtension(entry.getFirstSourcePath().toString().toLowerCase()))
.anyMatch(IMAGE_FORMATS::contains)) {
allowedActions.add("crop_color_bars");
}
}
return allowedActions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void listProjectsTest() throws Exception {

assertOutputContains("\"" + ListProjectsService.PROJECT_PATH + "\" : \"" + baseDir + "\"");
assertOutputContains("\"" + ListProjectsService.STATUS + "\" : \"initialized\"");
assertOutputContains("\"" + ListProjectsService.ALLOWED_ACTIONS + "\" : null");
assertOutputContains("\"" + ListProjectsService.ALLOWED_ACTIONS + "\" : \"[]\"");
assertOutputContains("\"name\" : \"" + PROJECT_ID + "\"");
}

Expand All @@ -55,7 +55,7 @@ public void listMultipleProjectsTest() throws Exception {

assertOutputContains("\"" + ListProjectsService.PROJECT_PATH + "\" : \"" + baseDir + "\"");
assertOutputContains("\""+ ListProjectsService.STATUS + "\" : \"initialized\"");
assertOutputContains("\"" + ListProjectsService.ALLOWED_ACTIONS + "\" : null");
assertOutputContains("\"" + ListProjectsService.ALLOWED_ACTIONS + "\" : \"[]\"");
assertOutputContains("\"name\" : \"" + PROJECT_ID + "\"");
assertOutputContains("\"name\" : \"" + PROJECT_ID_2 + "\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import com.fasterxml.jackson.databind.JsonNode;
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.model.SourceFilesInfo;
import edu.unc.lib.boxc.migration.cdm.test.BxcEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.CdmEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.SipServiceHelper;
import edu.unc.lib.boxc.migration.cdm.util.ProjectPropertiesSerialization;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Collections;
Expand Down Expand Up @@ -73,13 +77,24 @@ public void invalidDirectoryTest() throws Exception {
}
}

@Test
public void allowedActionTest() throws Exception {
writeSourceFilesCsv(mappingBody("test,," + Path.of("test.tif") + ","));
JsonNode list = service.listProjects(tmpFolder);

assertEquals(tmpFolder.toString(), list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("sources_mapped", list.findValue(ListProjectsService.STATUS).asText());
assertEquals("[crop_color_bars]", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals(PROJECT_NAME, list.findValue("name").asText());
}

@Test
public void listProjectsInitializedTest() throws Exception {
JsonNode list = service.listProjects(tmpFolder);

assertEquals(tmpFolder.toString(), list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("initialized", list.findValue(ListProjectsService.STATUS).asText());
assertEquals("null", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals("[]", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals(PROJECT_NAME, list.findValue("name").asText());
}

Expand All @@ -91,7 +106,7 @@ public void listProjectsIndexedTest() throws Exception {

assertEquals(tmpFolder.toString(), list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("indexed", list.findValue(ListProjectsService.STATUS).asText());
assertEquals("null", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals("[]", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals(PROJECT_NAME, list.findValue("name").asText());
}

Expand All @@ -103,7 +118,7 @@ public void listProjectsSourcesMappedTest() throws Exception {

assertEquals(tmpFolder.toString(), list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("sources_mapped", list.findValue(ListProjectsService.STATUS).asText());
assertEquals("null", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals("[]", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals(PROJECT_NAME, list.findValue("name").asText());
}

Expand All @@ -116,7 +131,7 @@ public void listProjectsSipsGeneratedTest() throws Exception {

assertEquals(tmpFolder.toString(), list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("sips_generated", list.findValue(ListProjectsService.STATUS).asText());
assertEquals("null", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals("[]", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals(PROJECT_NAME, list.findValue("name").asText());
}

Expand All @@ -129,7 +144,7 @@ public void listProjectsSipsSubmittedTest() throws Exception {

assertEquals(tmpFolder.toString(), list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("ingested", list.findValue(ListProjectsService.STATUS).asText());
assertEquals("null", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals("[]", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals(PROJECT_NAME, list.findValue("name").asText());
}

Expand All @@ -150,8 +165,20 @@ public void listProjectsMultipleProjectsTest() throws Exception {
assertEquals(tmpFolder.toString(), list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertTrue(list.findValues(ListProjectsService.STATUS).toString().contains("initialized"));
assertTrue(list.findValues(ListProjectsService.STATUS).toString().contains("ingested"));
assertEquals("null", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertEquals("[]", list.findValue(ListProjectsService.ALLOWED_ACTIONS).asText());
assertTrue(list.findValues("name").toString().contains(PROJECT_NAME_2));
assertTrue(list.findValues("name").toString().contains(PROJECT_NAME));
}

private String mappingBody(String... rows) {
return String.join(",", SourceFilesInfo.CSV_HEADERS) + "\n"
+ String.join("\n", rows);
}

private void writeSourceFilesCsv(String mappingBody) throws IOException {
FileUtils.write(project.getSourceFilesMappingPath().toFile(),
mappingBody, StandardCharsets.UTF_8);
project.getProjectProperties().setSourceFilesUpdatedDate(Instant.now());
ProjectPropertiesSerialization.write(project);
}
}

0 comments on commit f080b96

Please sign in to comment.