mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-11 12:03:09 +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,14 +96,16 @@ 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);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
is.delete(context, item);
|
is.delete(context, item);
|
||||||
is.removeAllBundles(context, item);
|
is.removeAllBundles(context, item);
|
||||||
|
@@ -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();
|
||||||
@@ -341,34 +338,33 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
|||||||
.withMimeType("text/plain")
|
.withMimeType("text/plain")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check publicItem creation
|
// Check publicItem creation
|
||||||
getClient().perform(get("/api/core/items/" + publicItem.getID()))
|
getClient().perform(get("/api/core/items/" + publicItem.getID()))
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
|
|
||||||
// Check publicItem bitstream creatino
|
// Check publicItem bitstream creatino
|
||||||
getClient().perform(get("/api/core/items/" + publicItem.getID() + "/bitstreams"))
|
getClient().perform(get("/api/core/items/" + publicItem.getID() + "/bitstreams"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().contentType(contentType))
|
.andExpect(content().contentType(contentType))
|
||||||
.andExpect(jsonPath("$._links.self.href", Matchers
|
.andExpect(jsonPath("$._links.self.href", Matchers
|
||||||
.containsString("/api/core/items/" + publicItem.getID() + "/bitstreams")));
|
.containsString("/api/core/items/" + publicItem.getID() + "/bitstreams")));
|
||||||
|
|
||||||
String token = getAuthToken(admin.getEmail(), password);
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
|
||||||
//Delete public item
|
//Delete public item
|
||||||
getClient(token).perform(delete("/api/core/items/" + publicItem.getID()))
|
getClient(token).perform(delete("/api/core/items/" + publicItem.getID()))
|
||||||
.andExpect(status().is(204));
|
.andExpect(status().is(204));
|
||||||
|
|
||||||
//Trying to get deleted item should fail with 404
|
//Trying to get deleted item should fail with 404
|
||||||
getClient().perform(get("/api/core/items/" + publicItem.getID()))
|
getClient().perform(get("/api/core/items/" + publicItem.getID()))
|
||||||
.andExpect(status().is(404));
|
.andExpect(status().is(404));
|
||||||
|
|
||||||
//Trying to get deleted item bitstream should fail with 404
|
//Trying to get deleted item bitstream should fail with 404
|
||||||
getClient().perform(get("/api/core/biststreams/" + bitstream.getID()))
|
getClient().perform(get("/api/core/biststreams/" + bitstream.getID()))
|
||||||
.andExpect(status().is(404));
|
.andExpect(status().is(404));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void deleteOneTemplateTest() throws Exception {
|
public void deleteOneTemplateTest() throws Exception {
|
||||||
context.turnOffAuthorisationSystem();
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
@@ -382,15 +378,41 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
|||||||
|
|
||||||
//2. One template item.
|
//2. One template item.
|
||||||
Item templateItem = ItemBuilder.createTemplateItem(context, col1);
|
Item templateItem = ItemBuilder.createTemplateItem(context, col1);
|
||||||
|
|
||||||
String token = getAuthToken(admin.getEmail(), password);
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
|
||||||
//Check templateItem creation
|
//Check templateItem creation
|
||||||
getClient().perform(get("/api/core/items/" + templateItem.getID()))
|
getClient().perform(get("/api/core/items/" + templateItem.getID()))
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
|
|
||||||
//Trying to delete a templateItem should fail with 422
|
//Trying to delete a templateItem should fail with 422
|
||||||
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -36,12 +36,12 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
|||||||
ItemBuilder builder = new ItemBuilder(context);
|
ItemBuilder builder = new ItemBuilder(context);
|
||||||
return builder.create(context, col);
|
return builder.create(context, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Item createTemplateItem(final Context context, final Collection collection) {
|
public static Item createTemplateItem(final Context context, final Collection collection) {
|
||||||
ItemBuilder builder = new ItemBuilder(context);
|
ItemBuilder builder = new ItemBuilder(context);
|
||||||
return builder.create_template_item(collection);
|
return builder.create_template_item(collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Item create_template_item(final Collection collection) {
|
private Item create_template_item(final Collection collection) {
|
||||||
try {
|
try {
|
||||||
item = itemService.createTemplateItem(context, collection);
|
item = itemService.createTemplateItem(context, collection);
|
||||||
@@ -50,7 +50,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
|||||||
return handleException(e);
|
return handleException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemBuilder create(final Context context, final Collection col) {
|
private ItemBuilder create(final Context context, final Collection col) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
|||||||
item.setDiscoverable(false);
|
item.setDiscoverable(false);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemBuilder withEmbargoPeriod(String embargoPeriod) {
|
public ItemBuilder withEmbargoPeriod(String embargoPeriod) {
|
||||||
return setEmbargo(embargoPeriod, item);
|
return setEmbargo(embargoPeriod, item);
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
|||||||
return handleException(e);
|
return handleException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void cleanup() throws Exception {
|
protected void cleanup() throws Exception {
|
||||||
delete(item);
|
delete(item);
|
||||||
}
|
}
|
||||||
|
@@ -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