mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
Added tests and various fixes
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
|
||||
|
||||
<bean id="indexClient" class="org.dspace.discovery.IndexClient" scope="prototype">
|
||||
<property name="name" value="index-discovery"/>
|
||||
<property name="description" value="Update Discovery Solr Search Index"/>
|
||||
</bean>
|
||||
|
||||
<bean id="MockScript" class="org.dspace.scripts.impl.MockDSpaceRunnableScript" scope="prototype">
|
||||
<property name="name" value="mock-script" />
|
||||
<property name="description" value="Mocking a script for testing purposes" />
|
||||
</bean>
|
||||
</beans>
|
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.scripts.impl;
|
||||
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.apache.commons.cli.ParseException;
|
||||
import org.dspace.scripts.DSpaceRunnable;
|
||||
|
||||
public class MockDSpaceRunnableScript extends DSpaceRunnable {
|
||||
|
||||
private MockDSpaceRunnableScript() {
|
||||
Options options = constructOptions();
|
||||
this.options = options;
|
||||
}
|
||||
public void internalRun() throws Exception {
|
||||
}
|
||||
|
||||
public void setup() throws ParseException {
|
||||
if (!commandLine.hasOption("i")) {
|
||||
throw new ParseException("-i is a mandatory parameter");
|
||||
}
|
||||
}
|
||||
|
||||
private Options constructOptions() {
|
||||
Options options = new Options();
|
||||
|
||||
options.addOption("r", "remove", true, "description r");
|
||||
options.getOption("r").setType(String.class);
|
||||
options.addOption("i", "index", true, "description i");
|
||||
options.getOption("i").setType(boolean.class);
|
||||
options.getOption("i").setRequired(true);
|
||||
return options;
|
||||
}
|
||||
}
|
@@ -9,6 +9,7 @@ package org.dspace.app.rest;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.dspace.app.rest.link.HalLinkService;
|
||||
import org.dspace.app.rest.model.ProcessRest;
|
||||
import org.dspace.app.rest.model.ScriptRest;
|
||||
import org.dspace.app.rest.model.hateoas.ProcessResource;
|
||||
@@ -36,6 +37,9 @@ public class ScriptRestController {
|
||||
@Autowired
|
||||
private ScriptRestRepository scriptRestRepository;
|
||||
|
||||
@Autowired
|
||||
private HalLinkService halLinkService;
|
||||
|
||||
/**
|
||||
* This method can be called by sending a POST request to the system/scripts/{name}/processes endpoint
|
||||
* This will start a process for the script that matches the given name
|
||||
@@ -45,12 +49,14 @@ public class ScriptRestController {
|
||||
*/
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/{name}/processes")
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
public ResponseEntity<ResourceSupport> startProcess(@PathVariable(name = "name") String scriptName) throws Exception {
|
||||
public ResponseEntity<ResourceSupport> startProcess(@PathVariable(name = "name") String scriptName)
|
||||
throws Exception {
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("Starting Process for Script with name: " + scriptName);
|
||||
}
|
||||
ProcessRest processRest = scriptRestRepository.startProcess(scriptName);
|
||||
ProcessResource processResource = new ProcessResource(processRest);
|
||||
halLinkService.addLinks(processResource);
|
||||
return ControllerUtils.toResponseEntity(HttpStatus.ACCEPTED, null, processResource);
|
||||
}
|
||||
|
||||
|
@@ -20,6 +20,7 @@ import org.dspace.content.service.ProcessService;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@@ -64,7 +65,11 @@ public class ProcessRestRepository extends AbstractDSpaceRestRepository {
|
||||
public ProcessRest getProcessById(Integer processId) throws SQLException, AuthorizeException {
|
||||
Context context = obtainContext();
|
||||
Process process = processService.find(context, processId);
|
||||
if ((context.getCurrentUser() == null) || (!context.getCurrentUser().equals(process.getEPerson()) && !authorizeService.isAdmin(context))) {
|
||||
if (process == null) {
|
||||
throw new ResourceNotFoundException("The process with ID: " + processId + " wasn't found");
|
||||
}
|
||||
if ((context.getCurrentUser() == null) || (!context.getCurrentUser().equals(process.getEPerson())
|
||||
&& !authorizeService.isAdmin(context))) {
|
||||
throw new AuthorizeException("The current user is not eligible to view the process with id: " + processId);
|
||||
}
|
||||
return processConverter.fromModel(process);
|
||||
|
@@ -37,7 +37,6 @@ import org.dspace.scripts.DSpaceCommandLineParameter;
|
||||
import org.dspace.scripts.DSpaceRunnable;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -81,8 +80,9 @@ public class ScriptRestRepository extends DSpaceRestRepository<ScriptRest, Strin
|
||||
|
||||
@Override
|
||||
public Page<ScriptRest> findAll(Context context, Pageable pageable) {
|
||||
return utils.getPage(dspaceRunnables.stream().filter(dSpaceRunnable -> dSpaceRunnable.isAllowedToExecute(context)).collect(
|
||||
Collectors.toList()), pageable).map(scriptConverter);
|
||||
return utils.getPage(dspaceRunnables.stream().filter(
|
||||
dSpaceRunnable -> dSpaceRunnable.isAllowedToExecute(context)).collect(Collectors.toList()), pageable)
|
||||
.map(scriptConverter);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest;
|
||||
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
|
||||
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.content.Process;
|
||||
import org.dspace.content.ProcessStatus;
|
||||
import org.dspace.scripts.DSpaceCommandLineParameter;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ProcessRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
Process process;
|
||||
|
||||
LinkedList<DSpaceCommandLineParameter> parameters = new LinkedList<>();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws SQLException {
|
||||
parameters.add(new DSpaceCommandLineParameter("-r", "test"));
|
||||
parameters.add(new DSpaceCommandLineParameter("-i"));
|
||||
|
||||
process = ProcessBuilder.createProcess(context, admin, "mock-script", parameters).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProcessAdmin() throws Exception {
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/processes/" + process.getID()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", Matchers.is(
|
||||
ProcessMatcher.matchProcess(process.getName(), String.valueOf(process.getEPerson().getID()),
|
||||
process.getID(), parameters, ProcessStatus.SCHEDULED)))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProcessAdminEmptyParam() throws Exception {
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
Process process = ProcessBuilder.createProcess(context, admin, "mock-script", new LinkedList<>()).build();
|
||||
|
||||
getClient(token).perform(get("/api/system/processes/" + process.getID()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", Matchers.is(
|
||||
ProcessMatcher.matchProcess(process.getName(), String.valueOf(process.getEPerson().getID()),
|
||||
process.getID(), new LinkedList<>(), ProcessStatus.SCHEDULED)))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProcessAnonymousUnauthorizedException() throws Exception {
|
||||
|
||||
getClient().perform(get("/api/system/processes/" + process.getID()))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProcessForStartedUser() throws Exception {
|
||||
Process newProcess = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
|
||||
String token = getAuthToken(eperson.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/processes/" + newProcess.getID()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", Matchers.is(
|
||||
ProcessMatcher.matchProcess(newProcess.getName(),
|
||||
String.valueOf(newProcess.getEPerson().getID()),
|
||||
newProcess.getID(),
|
||||
new LinkedList<>(),
|
||||
ProcessStatus.SCHEDULED))));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProcessForDifferentUserForbiddenException() throws Exception {
|
||||
String token = getAuthToken(eperson.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/processes/" + process.getID()))
|
||||
.andExpect(status().isForbidden());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProcessNotExisting() throws Exception {
|
||||
String token = getAuthToken(eperson.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/processes/" + process.getID() * 23 + 17))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllProcessesTestAnonymous() throws Exception {
|
||||
|
||||
|
||||
getClient().perform(get("/api/system/processes/"))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getAllProcessesTestAdmin() throws Exception {
|
||||
|
||||
Process newProcess = ProcessBuilder.createProcess(context, eperson, "mock-script", parameters).build();
|
||||
Process newProcess1 = ProcessBuilder.createProcess(context, eperson, "mock-script", parameters).build();
|
||||
Process newProcess2 = ProcessBuilder.createProcess(context, eperson, "mock-script", parameters).build();
|
||||
Process newProcess3 = ProcessBuilder.createProcess(context, eperson, "mock-script", parameters).build();
|
||||
Process newProcess4 = ProcessBuilder.createProcess(context, eperson, "mock-script", parameters).build();
|
||||
Process newProcess5 = ProcessBuilder.createProcess(context, eperson, "mock-script", parameters).build();
|
||||
Process newProcess6 = ProcessBuilder.createProcess(context, eperson, "mock-script", parameters).build();
|
||||
Process newProcess7 = ProcessBuilder.createProcess(context, admin, "mock-script", parameters).build();
|
||||
Process newProcess8 = ProcessBuilder.createProcess(context, admin, "mock-script", parameters).build();
|
||||
Process newProcess9 = ProcessBuilder.createProcess(context, admin, "mock-script", parameters).build();
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/processes/"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.processes", containsInAnyOrder(
|
||||
ProcessMatcher.matchProcess(process.getName(), String.valueOf(process.getEPerson().getID()),
|
||||
process.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess.getName(),
|
||||
String.valueOf(newProcess.getEPerson().getID()),
|
||||
newProcess.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess1.getName(),
|
||||
String.valueOf(newProcess1.getEPerson().getID()),
|
||||
newProcess1.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess2.getName(),
|
||||
String.valueOf(newProcess2.getEPerson().getID()),
|
||||
newProcess2.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess3.getName(),
|
||||
String.valueOf(newProcess3.getEPerson().getID()),
|
||||
newProcess3.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess4.getName(),
|
||||
String.valueOf(newProcess4.getEPerson().getID()),
|
||||
newProcess4.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess5.getName(),
|
||||
String.valueOf(newProcess5.getEPerson().getID()),
|
||||
newProcess5.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess6.getName(),
|
||||
String.valueOf(newProcess6.getEPerson().getID()),
|
||||
newProcess6.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess7.getName(),
|
||||
String.valueOf(newProcess7.getEPerson().getID()),
|
||||
newProcess7.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess8.getName(),
|
||||
String.valueOf(newProcess8.getEPerson().getID()),
|
||||
newProcess8.getID(), parameters, ProcessStatus.SCHEDULED),
|
||||
ProcessMatcher.matchProcess(newProcess9.getName(),
|
||||
String.valueOf(newProcess9.getEPerson().getID()),
|
||||
newProcess9.getID(), parameters, ProcessStatus.SCHEDULED)
|
||||
)))
|
||||
.andExpect(jsonPath("$.page", is(
|
||||
PageMatcher.pageEntryWithTotalPagesAndElements(0, 20, 1, 11))));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllProcessesTestStartingUser() throws Exception {
|
||||
Process newProcess = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess1 = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess2 = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess3 = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess4 = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess5 = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess6 = ProcessBuilder.createProcess(context, eperson, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess7 = ProcessBuilder.createProcess(context, admin, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess8 = ProcessBuilder.createProcess(context, admin, "mock-script", new LinkedList<>()).build();
|
||||
Process newProcess9 = ProcessBuilder.createProcess(context, admin, "mock-script", new LinkedList<>()).build();
|
||||
|
||||
String token = getAuthToken(eperson.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/processes/"))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
}
|
@@ -0,0 +1,237 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest;
|
||||
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.dspace.app.rest.converter.DSpaceRunnableParameterConverter;
|
||||
import org.dspace.app.rest.matcher.PageMatcher;
|
||||
import org.dspace.app.rest.matcher.ProcessMatcher;
|
||||
import org.dspace.app.rest.matcher.ScriptMatcher;
|
||||
import org.dspace.app.rest.model.ParameterValueRest;
|
||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||
import org.dspace.content.ProcessStatus;
|
||||
import org.dspace.scripts.DSpaceCommandLineParameter;
|
||||
import org.dspace.scripts.DSpaceRunnable;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
private List<DSpaceRunnable> dSpaceRunnableList;
|
||||
|
||||
@Autowired
|
||||
private DSpaceRunnableParameterConverter dSpaceRunnableParameterConverter;
|
||||
|
||||
@Test
|
||||
public void findAllScriptsTest() throws Exception {
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/scripts"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.scripts", containsInAnyOrder(
|
||||
ScriptMatcher.matchScript(dSpaceRunnableList.get(0).getName(),
|
||||
dSpaceRunnableList.get(0).getDescription()),
|
||||
ScriptMatcher.matchScript(dSpaceRunnableList.get(1).getName(),
|
||||
dSpaceRunnableList.get(1).getDescription())
|
||||
)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findAllScriptsUnauthorizedTest() throws Exception {
|
||||
String token = getAuthToken(eperson.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/scripts"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.page",
|
||||
is(PageMatcher.pageEntryWithTotalPagesAndElements(0, 20, 0, 0))));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllScriptsPaginationTest() throws Exception {
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/scripts").param("size", "1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.scripts", hasItem(
|
||||
ScriptMatcher.matchScript(dSpaceRunnableList.get(0).getName(),
|
||||
dSpaceRunnableList.get(0).getDescription())
|
||||
)))
|
||||
.andExpect(jsonPath("$._embedded.scripts", Matchers.not(hasItem(
|
||||
ScriptMatcher.matchScript(dSpaceRunnableList.get(1).getName(),
|
||||
dSpaceRunnableList.get(1).getDescription())
|
||||
))))
|
||||
.andExpect(jsonPath("$.page",
|
||||
is(PageMatcher.pageEntry(0, 1))));
|
||||
|
||||
|
||||
getClient(token).perform(get("/api/system/scripts").param("size", "1").param("page", "1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.scripts", Matchers.not(hasItem(
|
||||
ScriptMatcher.matchScript(dSpaceRunnableList.get(0).getName(),
|
||||
dSpaceRunnableList.get(0).getDescription())
|
||||
))))
|
||||
.andExpect(jsonPath("$._embedded.scripts", hasItem(
|
||||
ScriptMatcher.matchScript(dSpaceRunnableList.get(1).getName(),
|
||||
dSpaceRunnableList.get(1).getDescription())
|
||||
)))
|
||||
.andExpect(jsonPath("$.page",
|
||||
is(PageMatcher.pageEntry(1, 1))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOneScriptByNameTest() throws Exception {
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/scripts/mock-script"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", ScriptMatcher
|
||||
.matchMockScript(dSpaceRunnableList.get(1).getOptions())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOneScriptByNameTestAccessDenied() throws Exception {
|
||||
String token = getAuthToken(eperson.getEmail(), password);
|
||||
|
||||
getClient(token).perform(get("/api/system/scripts/mock-script"))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOneScriptByInvalidNameBadRequestExceptionTest() throws Exception {
|
||||
getClient().perform(get("/api/system/scripts/mock-script-invalid"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessNonAdminAuthorizeException() throws Exception {
|
||||
|
||||
String token = getAuthToken(eperson.getEmail(), password);
|
||||
|
||||
getClient(token).perform(post("/api/system/scripts/mock-script/processes").contentType("multipart/form-data"))
|
||||
.andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessAnonymousAuthorizeException() throws Exception {
|
||||
getClient().perform(post("/api/system/scripts/mock-script/processes").contentType("multipart/form-data"))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessAdminWrongOptionsException() throws Exception {
|
||||
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(post("/api/system/scripts/mock-script/processes").contentType("multipart/form-data"))
|
||||
.andExpect(status().isAccepted())
|
||||
.andExpect(jsonPath("$", is(
|
||||
ProcessMatcher.matchProcess("mock-script",
|
||||
String.valueOf(admin.getID()), new LinkedList<>(),
|
||||
ProcessStatus.FAILED))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessAdminNoOptionsFailedStatus() throws Exception {
|
||||
|
||||
// List<ParameterValueRest> list = new LinkedList<>();
|
||||
//
|
||||
// ParameterValueRest parameterValueRest = new ParameterValueRest();
|
||||
// parameterValueRest.setName("-z");
|
||||
// parameterValueRest.setValue("test");
|
||||
// ParameterValueRest parameterValueRest1 = new ParameterValueRest();
|
||||
// parameterValueRest1.setName("-q");
|
||||
// list.add(parameterValueRest);
|
||||
// list.add(parameterValueRest1);
|
||||
|
||||
LinkedList<DSpaceCommandLineParameter> parameters = new LinkedList<>();
|
||||
|
||||
parameters.add(new DSpaceCommandLineParameter("-z", "test"));
|
||||
parameters.add(new DSpaceCommandLineParameter("-q"));
|
||||
|
||||
List<ParameterValueRest> list = parameters.stream().map(
|
||||
dSpaceCommandLineParameter -> dSpaceRunnableParameterConverter.fromModel(dSpaceCommandLineParameter))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(post("/api/system/scripts/mock-script/processes").contentType("multipart/form-data")
|
||||
.param("properties", new Gson().toJson(list)))
|
||||
.andExpect(status().isAccepted())
|
||||
.andExpect(jsonPath("$", is(
|
||||
ProcessMatcher.matchProcess("mock-script",
|
||||
String.valueOf(admin.getID()), parameters,
|
||||
ProcessStatus.FAILED))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessNonExistingScriptNameException() throws Exception {
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
getClient(token).perform(post("/api/system/scripts/mock-script-invalid/processes")
|
||||
.contentType("multipart/form-data"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessAdminWithOptionsSuccess() throws Exception {
|
||||
LinkedList<DSpaceCommandLineParameter> parameters = new LinkedList<>();
|
||||
|
||||
parameters.add(new DSpaceCommandLineParameter("-r", "test"));
|
||||
parameters.add(new DSpaceCommandLineParameter("-i"));
|
||||
|
||||
List<ParameterValueRest> list = parameters.stream().map(
|
||||
dSpaceCommandLineParameter -> dSpaceRunnableParameterConverter.fromModel(dSpaceCommandLineParameter))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
List<ProcessStatus> acceptableProcessStatuses = new LinkedList<>();
|
||||
acceptableProcessStatuses.addAll(Arrays.asList(ProcessStatus.SCHEDULED,
|
||||
ProcessStatus.RUNNING,
|
||||
ProcessStatus.COMPLETED));
|
||||
|
||||
getClient(token).perform(post("/api/system/scripts/mock-script/processes").contentType("multipart/form-data")
|
||||
.param("properties", new Gson().toJson(list)))
|
||||
.andExpect(status().isAccepted())
|
||||
.andExpect(jsonPath("$", is(
|
||||
ProcessMatcher.matchProcess("mock-script",
|
||||
String.valueOf(admin.getID()),
|
||||
parameters,
|
||||
acceptableProcessStatuses))));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessAdminWithWrongContentTypeBadRequestException() throws Exception {
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
getClient(token).perform(post("/api/system/scripts/mock-script-invalid/processes"))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
}
|
@@ -17,6 +17,7 @@ import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.authorize.service.ResourcePolicyService;
|
||||
import org.dspace.content.Bitstream;
|
||||
import org.dspace.content.factory.ContentServiceFactory;
|
||||
import org.dspace.content.factory.ProcessServiceFactory;
|
||||
import org.dspace.content.service.BitstreamFormatService;
|
||||
import org.dspace.content.service.BitstreamService;
|
||||
import org.dspace.content.service.BundleService;
|
||||
@@ -27,6 +28,7 @@ import org.dspace.content.service.InstallItemService;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.content.service.MetadataFieldService;
|
||||
import org.dspace.content.service.MetadataSchemaService;
|
||||
import org.dspace.content.service.ProcessService;
|
||||
import org.dspace.content.service.RelationshipService;
|
||||
import org.dspace.content.service.RelationshipTypeService;
|
||||
import org.dspace.content.service.SiteService;
|
||||
@@ -82,6 +84,7 @@ public abstract class AbstractBuilder<T, S> {
|
||||
static RelationshipService relationshipService;
|
||||
static RelationshipTypeService relationshipTypeService;
|
||||
static EntityTypeService entityTypeService;
|
||||
static ProcessService processService;
|
||||
|
||||
protected Context context;
|
||||
|
||||
@@ -127,6 +130,7 @@ public abstract class AbstractBuilder<T, S> {
|
||||
relationshipService = ContentServiceFactory.getInstance().getRelationshipService();
|
||||
relationshipTypeService = ContentServiceFactory.getInstance().getRelationshipTypeService();
|
||||
entityTypeService = ContentServiceFactory.getInstance().getEntityTypeService();
|
||||
processService = ProcessServiceFactory.getInstance().getProcessService();
|
||||
|
||||
// Temporarily disabled
|
||||
claimedTaskService = XmlWorkflowServiceFactory.getInstance().getClaimedTaskService();
|
||||
@@ -162,6 +166,8 @@ public abstract class AbstractBuilder<T, S> {
|
||||
relationshipService = null;
|
||||
relationshipTypeService = null;
|
||||
entityTypeService = null;
|
||||
processService = null;
|
||||
|
||||
}
|
||||
|
||||
public static void cleanupObjects() throws Exception {
|
||||
|
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest.builder;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.dspace.content.Process;
|
||||
import org.dspace.content.ProcessStatus;
|
||||
import org.dspace.content.service.ProcessService;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.eperson.EPerson;
|
||||
import org.dspace.scripts.DSpaceCommandLineParameter;
|
||||
|
||||
public class ProcessBuilder extends AbstractBuilder<Process, ProcessService> {
|
||||
|
||||
private Process process;
|
||||
|
||||
protected ProcessBuilder(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public static ProcessBuilder createProcess(Context context, EPerson ePerson, String scriptName,
|
||||
List<DSpaceCommandLineParameter> parameters)
|
||||
throws SQLException {
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(context);
|
||||
return processBuilder.create(context, ePerson, scriptName, parameters);
|
||||
}
|
||||
|
||||
private ProcessBuilder create(Context context, EPerson ePerson, String scriptName,
|
||||
List<DSpaceCommandLineParameter> parameters)
|
||||
throws SQLException {
|
||||
this.context = context;
|
||||
this.process = processService.create(context, ePerson, scriptName, parameters);
|
||||
this.process.setProcessStatus(ProcessStatus.SCHEDULED);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void cleanup() throws Exception {
|
||||
delete(process);
|
||||
}
|
||||
|
||||
public Process build() {
|
||||
try {
|
||||
processService.update(context, process);
|
||||
context.dispatchEvents();
|
||||
indexingService.commit();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return process;
|
||||
}
|
||||
|
||||
public void delete(Process dso) throws Exception {
|
||||
try (Context c = new Context()) {
|
||||
c.turnOffAuthorisationSystem();
|
||||
Process attachedDso = c.reloadEntity(dso);
|
||||
if (attachedDso != null) {
|
||||
getService().delete(c, attachedDso);
|
||||
}
|
||||
c.complete();
|
||||
}
|
||||
|
||||
indexingService.commit();
|
||||
}
|
||||
|
||||
|
||||
protected ProcessService getService() {
|
||||
return processService;
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest.matcher;
|
||||
|
||||
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
import org.apache.commons.cli.Option;
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
public class ParameterMatcher {
|
||||
|
||||
private ParameterMatcher() {
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchParameter(Option option) {
|
||||
return allOf(
|
||||
hasJsonPath("$.name", is("-" + option.getOpt())),
|
||||
hasJsonPath("$.description", is(option.getDescription())),
|
||||
hasJsonPath("$.type", is(((Class) option.getType()).getSimpleName()))
|
||||
);
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest.matcher;
|
||||
|
||||
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
public class ParameterValueMatcher {
|
||||
|
||||
private ParameterValueMatcher() {}
|
||||
|
||||
public static Matcher<? super Object> matchParameterValue(String name, String value) {
|
||||
return allOf(
|
||||
hasJsonPath("$.name", is(name)),
|
||||
hasJsonPath("$.value", is(value))
|
||||
);
|
||||
|
||||
}
|
||||
public static Matcher<? super Object> matchParameterValue(String name) {
|
||||
return allOf(
|
||||
hasJsonPath("$.name", is(name))
|
||||
);
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest.matcher;
|
||||
|
||||
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.any;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.dspace.content.ProcessStatus;
|
||||
import org.dspace.scripts.DSpaceCommandLineParameter;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
public class ProcessMatcher {
|
||||
|
||||
private ProcessMatcher() {
|
||||
}
|
||||
|
||||
private static Matcher<? super Object> matchProcess(String name, String userId) {
|
||||
return allOf(
|
||||
hasJsonPath("$.scriptName", is(name)),
|
||||
hasJsonPath("$.userId", is(userId))
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchProcess(String name, String userId, Integer processId,
|
||||
List<DSpaceCommandLineParameter> list, ProcessStatus status) {
|
||||
return allOf(
|
||||
matchProcess(name, userId, list, Collections.singletonList(status)),
|
||||
hasJsonPath("$.processId", is(processId))
|
||||
);
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchProcess(String name, String userId,
|
||||
List<DSpaceCommandLineParameter> list, ProcessStatus status) {
|
||||
return allOf(
|
||||
matchProcess(name, userId, list, Collections.singletonList(status))
|
||||
);
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchProcess(String name, String userId,
|
||||
List<DSpaceCommandLineParameter> list,
|
||||
List<ProcessStatus> statuses) {
|
||||
return allOf(
|
||||
matchProcess(name, userId),
|
||||
hasJsonPath("$.startTime", anyOf(any(String.class), nullValue())),
|
||||
hasJsonPath("$.endTime", anyOf(any(String.class), nullValue())),
|
||||
hasJsonPath("$.processStatus", Matchers.anyOf(
|
||||
statuses.stream().map(status -> is(status.toString())).collect(Collectors.toList())
|
||||
)),
|
||||
hasJsonPath("$.parameters", Matchers.containsInAnyOrder(
|
||||
list.stream().map(dSpaceCommandLineParameter -> ParameterValueMatcher
|
||||
.matchParameterValue(dSpaceCommandLineParameter.getName(), dSpaceCommandLineParameter.getValue()))
|
||||
.collect(Collectors.toList())
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest.matcher;
|
||||
|
||||
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
public class ScriptMatcher {
|
||||
|
||||
private ScriptMatcher() {
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchScript(String name, String description) {
|
||||
return allOf(
|
||||
hasJsonPath("$.name", is(name)),
|
||||
hasJsonPath("$.description", is(description))
|
||||
);
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchMockScript(Options options) {
|
||||
return allOf(
|
||||
matchScript("mock-script", "Mocking a script for testing purposes"),
|
||||
hasJsonPath("$.parameters", Matchers.containsInAnyOrder(
|
||||
ParameterMatcher.matchParameter(options.getOption("r")),
|
||||
ParameterMatcher.matchParameter(options.getOption("i"))
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.scripts.impl;
|
||||
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.apache.commons.cli.ParseException;
|
||||
import org.dspace.scripts.DSpaceRunnable;
|
||||
|
||||
public class MockDSpaceRunnableScript extends DSpaceRunnable {
|
||||
|
||||
private MockDSpaceRunnableScript() {
|
||||
Options options = constructOptions();
|
||||
this.options = options;
|
||||
}
|
||||
public void internalRun() throws Exception {
|
||||
}
|
||||
|
||||
public void setup() throws ParseException {
|
||||
if (!commandLine.hasOption("i")) {
|
||||
throw new ParseException("-i is a mandatory parameter");
|
||||
}
|
||||
}
|
||||
|
||||
private Options constructOptions() {
|
||||
Options options = new Options();
|
||||
|
||||
options.addOption("r", "remove", true, "description r");
|
||||
options.getOption("r").setType(String.class);
|
||||
options.addOption("i", "index", false, "description i");
|
||||
options.getOption("i").setType(boolean.class);
|
||||
options.getOption("i").setRequired(true);
|
||||
return options;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user