Merge pull request #2429 from 4Science/DS-4210

DS-4210 Deposit on collection without workflow seems to fail (405 but succeed)
This commit is contained in:
Tim Donohue
2019-07-10 23:20:45 +02:00
committed by GitHub
6 changed files with 118 additions and 11 deletions

View File

@@ -441,15 +441,9 @@ public class RestResourceController implements InitializingBean {
throws HttpRequestMethodNotSupportedException {
checkModelPluralForm(apiCategory, model);
DSpaceRestRepository<RestAddressableModel, ID> repository = utils.getResourceRepository(apiCategory, model);
RestAddressableModel modelObject = null;
try {
modelObject = repository.createAndReturn();
} catch (ClassCastException e) {
log.error(e.getMessage(), e);
return ControllerUtils.toEmptyResponse(HttpStatus.INTERNAL_SERVER_ERROR);
}
RestAddressableModel modelObject = repository.createAndReturn();
if (modelObject == null) {
throw new HttpRequestMethodNotSupportedException(RequestMethod.POST.toString());
return ControllerUtils.toEmptyResponse(HttpStatus.CREATED);
}
DSpaceResource result = repository.wrapResource(modelObject);
linkService.addLinks(result);
@@ -482,7 +476,7 @@ public class RestResourceController implements InitializingBean {
return ControllerUtils.toEmptyResponse(HttpStatus.INTERNAL_SERVER_ERROR);
}
if (modelObject == null) {
throw new HttpRequestMethodNotSupportedException(RequestMethod.POST.toString());
return ControllerUtils.toEmptyResponse(HttpStatus.CREATED);
}
DSpaceResource result = repository.wrapResource(modelObject);
linkService.addLinks(result);

View File

@@ -657,6 +657,55 @@ public class WorkflowItemRestRepositoryIT extends AbstractControllerIntegrationT
}
@Test
/**
* Test the deposit of a workspaceitem POSTing to the resource workflowitems collection endpoint a workspaceitem (as
* uri-list) in a collection without workflow. This corresponds to the deposit action done by the submitter.
*
* @throws Exception
*/
public void depositWorkspaceItemWithoutWorkflowTest() throws Exception {
context.turnOffAuthorisationSystem();
//** GIVEN **
//1. create a normal user to use as submitter
EPerson submitter = EPersonBuilder.createEPerson(context)
.withEmail("submitter@example.com")
.withPassword(password)
.build();
//2. A community with one collection.
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection 1")
.withSubmitterGroup(submitter)
.build();
context.setCurrentUser(submitter);
//3. a workspace item ready to go
InputStream pdf = getClass().getResourceAsStream("simple-article.pdf");
WorkspaceItem wsitem = WorkspaceItemBuilder.createWorkspaceItem(context, col1)
.withTitle("Submission Item")
.withIssueDate("2017-10-17")
.withFulltext("simple-article.pdf", "/local/path/simple-article.pdf", pdf)
.grantLicense()
.build();
context.restoreAuthSystemState();
String authToken = getAuthToken(submitter.getEmail(), password);
// submit the workspaceitem to complete the deposit (as there is no workflow configured)
getClient(authToken)
.perform(post(BASE_REST_SERVER_URL + "/api/workflow/workflowitems")
.content("/api/submission/workspaceitems/" + wsitem.getID())
.contentType(textUriContentType))
.andExpect(status().isCreated());
}
@Test
/**
* Test the creation of workflowitem POSTing to the resource workflowitems collection endpoint a workspaceitem (as

View File

@@ -72,6 +72,23 @@ public class CollectionBuilder extends AbstractDSpaceObjectBuilder<Collection> {
return this;
}
/**
* Create a submitter group for the collection with the specified members
*
* @param members epersons to add to the submitter group
* @return this builder
* @throws SQLException
* @throws AuthorizeException
*/
public CollectionBuilder withSubmitterGroup(EPerson... members) throws SQLException, AuthorizeException {
Group g = collectionService.createSubmitters(context, collection);
for (EPerson e : members) {
groupService.addMember(context, g, e);
}
groupService.update(context, g);
return this;
}
public CollectionBuilder withWorkflowGroup(int step, EPerson... members) throws SQLException, AuthorizeException {
Group g = collectionService.createWorkflowGroup(context, collection, step);
for (EPerson e : members) {

View File

@@ -31,6 +31,9 @@ import org.dspace.xmlworkflow.storedcomponents.service.XmlWorkflowItemService;
**/
public class WorkflowItemBuilder extends AbstractBuilder<XmlWorkflowItem, XmlWorkflowItemService> {
/** Keep a reference to the underlying Item for cleanup **/
private Item item;
private WorkspaceItem workspaceItem;
private XmlWorkflowItem workflowItem;
@@ -60,6 +63,7 @@ public class WorkflowItemBuilder extends AbstractBuilder<XmlWorkflowItem, XmlWor
try {
workspaceItem = workspaceItemService.create(context, col, false);
item = workspaceItem.getItem();
} catch (Exception e) {
return handleException(e);
}
@@ -88,6 +92,7 @@ public class WorkflowItemBuilder extends AbstractBuilder<XmlWorkflowItem, XmlWor
XmlWorkflowItem attachedDso = c.reloadEntity(dso);
if (attachedDso != null) {
getService().delete(c, attachedDso);
item = null;
}
c.complete();
}
@@ -101,6 +106,7 @@ public class WorkflowItemBuilder extends AbstractBuilder<XmlWorkflowItem, XmlWor
WorkspaceItem attachedDso = c.reloadEntity(dso);
if (attachedDso != null) {
workspaceItemService.deleteAll(c, attachedDso);
item = null;
}
c.complete();
}
@@ -108,6 +114,25 @@ public class WorkflowItemBuilder extends AbstractBuilder<XmlWorkflowItem, XmlWor
indexingService.commit();
}
private void deleteItem(Item dso) throws Exception {
try (Context c = new Context()) {
c.turnOffAuthorisationSystem();
Item attachedDso = c.reloadEntity(dso);
if (attachedDso != null) {
// if we still have a reference to an item it could be an approved workflow or a rejected one. In the
// last case we need to remove the "new" workspaceitem
WorkspaceItem wi = workspaceItemService.findByItem(c, item);
if (wi != null) {
workspaceItemService.deleteAll(c, wi);
} else {
itemService.delete(c, attachedDso);
}
}
c.complete();
}
indexingService.commit();
}
@Override
public void cleanup() throws Exception {
@@ -117,6 +142,9 @@ public class WorkflowItemBuilder extends AbstractBuilder<XmlWorkflowItem, XmlWor
if (workflowItem != null) {
delete(workflowItem);
}
if (item != null) {
deleteItem(item);
}
}
@Override

View File

@@ -26,6 +26,9 @@ import org.dspace.eperson.EPerson;
**/
public class WorkspaceItemBuilder extends AbstractBuilder<WorkspaceItem, WorkspaceItemService> {
/** Keep a reference to the underlying Item for cleanup **/
private Item item;
private WorkspaceItem workspaceItem;
protected WorkspaceItemBuilder(Context context) {
@@ -42,6 +45,7 @@ public class WorkspaceItemBuilder extends AbstractBuilder<WorkspaceItem, Workspa
try {
workspaceItem = workspaceItemService.create(context, col, false);
item = workspaceItem.getItem();
} catch (Exception e) {
return handleException(e);
}
@@ -61,6 +65,19 @@ public class WorkspaceItemBuilder extends AbstractBuilder<WorkspaceItem, Workspa
}
private void deleteItem(Item dso) throws Exception {
try (Context c = new Context()) {
c.turnOffAuthorisationSystem();
Item attachedDso = c.reloadEntity(dso);
if (attachedDso != null) {
itemService.delete(c, attachedDso);
}
c.complete();
}
indexingService.commit();
}
@Override
public void delete(WorkspaceItem dso) throws Exception {
try (Context c = new Context()) {
@@ -68,6 +85,7 @@ public class WorkspaceItemBuilder extends AbstractBuilder<WorkspaceItem, Workspa
WorkspaceItem attachedDso = c.reloadEntity(dso);
if (attachedDso != null) {
getService().deleteAll(c, attachedDso);
item = null;
}
c.complete();
}
@@ -78,6 +96,9 @@ public class WorkspaceItemBuilder extends AbstractBuilder<WorkspaceItem, Workspa
@Override
public void cleanup() throws Exception {
delete(workspaceItem);
if (item != null) {
deleteItem(item);
}
}
@Override

View File

@@ -673,8 +673,6 @@ event.consumer.discovery.filters = Community|Collection|Item|Bundle+Add|Create|M
event.consumer.eperson.class = org.dspace.eperson.EPersonConsumer
event.consumer.eperson.filters = EPerson+Create
# consumer to update metadata of DOIs
event.consumer.doi.class = org.dspace.identifier.doi.DOIConsumer
event.consumer.doi.filters = Item+Modify_Metadata