mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-08 10:34:25 +00:00
Working Implementation
Wrote additional integration tests Created WorkspaceItemBuilder
This commit is contained in:
@@ -14,11 +14,11 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.dspace.authorize.AuthorizeException;
|
|
||||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
|
||||||
import org.dspace.app.rest.converter.ItemConverter;
|
import org.dspace.app.rest.converter.ItemConverter;
|
||||||
|
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||||
import org.dspace.app.rest.model.ItemRest;
|
import org.dspace.app.rest.model.ItemRest;
|
||||||
import org.dspace.app.rest.model.hateoas.ItemResource;
|
import org.dspace.app.rest.model.hateoas.ItemResource;
|
||||||
|
import org.dspace.authorize.AuthorizeException;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
import org.dspace.content.service.ItemService;
|
import org.dspace.content.service.ItemService;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
@@ -43,7 +43,6 @@ public class ItemRestRepository extends DSpaceRestRepository<ItemRest, UUID> {
|
|||||||
@Autowired
|
@Autowired
|
||||||
ItemConverter converter;
|
ItemConverter converter;
|
||||||
|
|
||||||
|
|
||||||
public ItemRestRepository() {
|
public ItemRestRepository() {
|
||||||
System.out.println("Repository initialized by Spring");
|
System.out.println("Repository initialized by Spring");
|
||||||
}
|
}
|
||||||
@@ -97,10 +96,12 @@ public class ItemRestRepository extends DSpaceRestRepository<ItemRest, UUID> {
|
|||||||
try {
|
try {
|
||||||
item = is.find(context, id);
|
item = is.find(context, id);
|
||||||
if (is.isInProgressSubmission(context, item)) {
|
if (is.isInProgressSubmission(context, item)) {
|
||||||
throw new UnprocessableEntityException("The item cannot be deleted. It's part of a in-progress submission.");
|
throw new UnprocessableEntityException("The item cannot be deleted. "
|
||||||
|
+ "It's part of a in-progress submission.");
|
||||||
}
|
}
|
||||||
if (item.getTemplateItemOf() != null) {
|
if (item.getTemplateItemOf() != null) {
|
||||||
throw new UnprocessableEntityException("The item cannot be deleted. It's a template for a collection");
|
throw new UnprocessableEntityException("The item cannot be deleted. "
|
||||||
|
+ "It's a template for a collection");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
@@ -23,13 +23,13 @@ import org.dspace.app.rest.builder.BitstreamBuilder;
|
|||||||
import org.dspace.app.rest.builder.CollectionBuilder;
|
import org.dspace.app.rest.builder.CollectionBuilder;
|
||||||
import org.dspace.app.rest.builder.CommunityBuilder;
|
import org.dspace.app.rest.builder.CommunityBuilder;
|
||||||
import org.dspace.app.rest.builder.ItemBuilder;
|
import org.dspace.app.rest.builder.ItemBuilder;
|
||||||
|
import org.dspace.app.rest.builder.WorkspaceItemBuilder;
|
||||||
import org.dspace.app.rest.matcher.ItemMatcher;
|
import org.dspace.app.rest.matcher.ItemMatcher;
|
||||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||||
import org.dspace.content.Bitstream;
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.content.Collection;
|
import org.dspace.content.Collection;
|
||||||
import org.dspace.content.Community;
|
import org.dspace.content.Community;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
import org.dspace.content.WorkspaceItem;
|
|
||||||
import org.hamcrest.Matchers;
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -73,7 +73,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
|||||||
.withSubject("ExtraEntry")
|
.withSubject("ExtraEntry")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
||||||
getClient().perform(get("/api/core/items"))
|
getClient().perform(get("/api/core/items"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$._embedded.items", Matchers.containsInAnyOrder(
|
.andExpect(jsonPath("$._embedded.items", Matchers.containsInAnyOrder(
|
||||||
@@ -197,7 +196,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
|||||||
.withSubject("ExtraEntry")
|
.withSubject("ExtraEntry")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
||||||
getClient().perform(get("/api/core/items/" + publicItem1.getID()))
|
getClient().perform(get("/api/core/items/" + publicItem1.getID()))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$", Matchers.is(
|
.andExpect(jsonPath("$", Matchers.is(
|
||||||
@@ -292,7 +290,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findOneTestWrongUUID() throws Exception {
|
public void findOneTestWrongUUID() throws Exception {
|
||||||
context.turnOffAuthorisationSystem();
|
context.turnOffAuthorisationSystem();
|
||||||
@@ -368,7 +365,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
|||||||
.andExpect(status().is(404));
|
.andExpect(status().is(404));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void deleteOneTemplateTest() throws Exception {
|
public void deleteOneTemplateTest() throws Exception {
|
||||||
context.turnOffAuthorisationSystem();
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
@@ -393,4 +389,30 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
|||||||
getClient(token).perform(delete("/api/core/items/" + templateItem.getID()))
|
getClient(token).perform(delete("/api/core/items/" + templateItem.getID()))
|
||||||
.andExpect(status().is(422));
|
.andExpect(status().is(422));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteOneWorkspaceTest() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
//** GIVEN **
|
||||||
|
//1. A community with one collection.
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
|
||||||
|
|
||||||
|
//2. One workspace item.
|
||||||
|
Item workspaceItem = WorkspaceItemBuilder.createWorkspaceItem(context, col1)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
|
||||||
|
//Check workspaceItem creation
|
||||||
|
getClient().perform(get("/api/core/items/" + workspaceItem.getID()))
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
|
||||||
|
//Trying to delete a workspaceItem should fail with 422
|
||||||
|
getClient(token).perform(delete("/api/core/items/" + workspaceItem.getID()))
|
||||||
|
.andExpect(status().is(422));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,69 @@
|
|||||||
|
/**
|
||||||
|
* 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 org.dspace.content.Collection;
|
||||||
|
import org.dspace.content.Item;
|
||||||
|
import org.dspace.content.WorkspaceItem;
|
||||||
|
import org.dspace.content.service.DSpaceObjectService;
|
||||||
|
import org.dspace.core.Context;
|
||||||
|
import org.dspace.eperson.Group;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builder to construct Item objects
|
||||||
|
*
|
||||||
|
* @author Tom Desair (tom dot desair at atmire dot com)
|
||||||
|
* @author Raf Ponsaerts (raf dot ponsaerts at atmire dot com)
|
||||||
|
*/
|
||||||
|
public class WorkspaceItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
||||||
|
|
||||||
|
private WorkspaceItem workspaceItem;
|
||||||
|
private Item item;
|
||||||
|
private Group readerGroup = null;
|
||||||
|
|
||||||
|
protected WorkspaceItemBuilder(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WorkspaceItemBuilder createWorkspaceItem(final Context context, final Collection col) {
|
||||||
|
WorkspaceItemBuilder builder = new WorkspaceItemBuilder(context);
|
||||||
|
return builder.create(context, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
private WorkspaceItemBuilder create(final Context context, final Collection col) {
|
||||||
|
this.context = context;
|
||||||
|
|
||||||
|
try {
|
||||||
|
workspaceItem = workspaceItemService.create(context, col, false);
|
||||||
|
item = workspaceItem.getItem();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return handleException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item build() {
|
||||||
|
try {
|
||||||
|
return item;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return handleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void cleanup() throws Exception {
|
||||||
|
delete(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected DSpaceObjectService<Item> getService() {
|
||||||
|
return itemService;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user