[Task 71405] fixed the Process Files endpoints and added two tests for them

This commit is contained in:
Raf Ponsaerts
2020-06-16 09:46:01 +02:00
parent b32c461006
commit 31c87c2ba1
5 changed files with 56 additions and 15 deletions

View File

@@ -8,6 +8,7 @@
package org.dspace.scripts;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -176,6 +177,9 @@ public class Process implements ReloadableEntity<Integer> {
* @return The Bitstreams that are used or created by the process
*/
public List<Bitstream> getBitstreams() {
if (bitstreams == null) {
bitstreams = new LinkedList<>();
}
return bitstreams;
}

View File

@@ -206,13 +206,15 @@ public class ProcessServiceImpl implements ProcessService {
if (type == null) {
return null;
} else {
if (allBitstreams != null) {
for (Bitstream bitstream : allBitstreams) {
if (StringUtils.equals(bitstreamService.getMetadata(bitstream, Process.BITSTREAM_TYPE_METADATAFIELD),
type)) {
if (StringUtils.equals(bitstreamService.getMetadata(bitstream,
Process.BITSTREAM_TYPE_METADATAFIELD), type)) {
return bitstream;
}
}
}
}
return null;
}

View File

@@ -73,7 +73,7 @@ public class ProcessFilesLinkRepository extends AbstractDSpaceRestRepository imp
* @throws AuthorizeException If something goes wrong
*/
@PreAuthorize("hasPermission(#processId, 'PROCESS', 'READ')")
public BitstreamRest getFileFromProcessByType(HttpServletRequest request, String processId, String fileType,
public BitstreamRest getResource(HttpServletRequest request, String processId, String fileType,
Pageable pageable, Projection projection)
throws SQLException, AuthorizeException {
if (log.isTraceEnabled()) {

View File

@@ -13,16 +13,17 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.LinkedList;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.io.IOUtils;
import org.dspace.app.rest.builder.ProcessBuilder;
import org.dspace.app.rest.matcher.PageMatcher;
import org.dspace.app.rest.matcher.ProcessMatcher;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.ProcessStatus;
import org.dspace.scripts.DSpaceCommandLineParameter;
import org.dspace.scripts.Process;
@@ -203,15 +204,48 @@ public class ProcessRestRepositoryIT extends AbstractControllerIntegrationTest {
.andExpect(status().isForbidden());
}
@Test
public void getProcessFiles() throws Exception {
Process newProcess = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
try (InputStream is = IOUtils.toInputStream("Test File For Process", CharEncoding.UTF_8)) {
processService.appendFile(context, process, is, "inputfile", "test.csv");
}
Bitstream bitstream = processService.getBitstream(context, process, "inputfile");
String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/system/processes/" + process.getID() + "/files"))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.inputfile[0].name", is("test.csv")))
.andExpect(jsonPath("$._embedded.inputfile[0].uuid", is(bitstream.getID().toString())))
.andExpect(jsonPath("$._embedded.inputfile[0].metadata['dspace.process.filetype']" +
"[0].value", is("inputfile")));
}
@Test
public void getProcessFilesByFileType() throws Exception {
Process newProcess = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
try (InputStream is = IOUtils.toInputStream("Test File For Process", CharEncoding.UTF_8)) {
processService.appendFile(context, process, is, "inputfile", "test.csv");
}
Bitstream bitstream = processService.getBitstream(context, process, "inputfile");
String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/system/processes/" + process.getID() + "/files/inputfile"))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.bitstreams[0].name", is("test.csv")))
.andExpect(jsonPath("$._embedded.bitstreams[0].uuid", is(bitstream.getID().toString())))
.andExpect(jsonPath("$._embedded.bitstreams[0].metadata['dspace.process.filetype']" +
"[0].value", is("inputfile")));
}
@After
public void destroy() throws Exception {
CollectionUtils.emptyIfNull(processService.findAll(context)).stream().forEach(process -> {
try {
processService.delete(context, process);
} catch (SQLException | IOException | AuthorizeException e) {
throw new RuntimeException(e);
}
});
super.destroy();
}
}

View File

@@ -43,6 +43,7 @@ public class ProcessBuilder extends AbstractBuilder<Process, ProcessService> {
return this;
}
@Override
public void cleanup() throws Exception {
try (Context c = new Context()) {
c.turnOffAuthorisationSystem();