Working Implementation

Wrote additional integration tests
Created WorkspaceItemBuilder
This commit is contained in:
Pablo Prieto
2018-06-29 16:51:11 -05:00
parent a487fb7d58
commit fe2030d024
4 changed files with 118 additions and 26 deletions

View File

@@ -14,11 +14,11 @@ import java.util.Iterator;
import java.util.List;
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.exception.UnprocessableEntityException;
import org.dspace.app.rest.model.ItemRest;
import org.dspace.app.rest.model.hateoas.ItemResource;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Item;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
@@ -43,7 +43,6 @@ public class ItemRestRepository extends DSpaceRestRepository<ItemRest, UUID> {
@Autowired
ItemConverter converter;
public ItemRestRepository() {
System.out.println("Repository initialized by Spring");
}
@@ -97,14 +96,16 @@ public class ItemRestRepository extends DSpaceRestRepository<ItemRest, UUID> {
try {
item = is.find(context, id);
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) {
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) {
throw new RuntimeException(e.getMessage(), e);
}
}
try {
is.delete(context, item);
is.removeAllBundles(context, item);

View File

@@ -23,13 +23,13 @@ import org.dspace.app.rest.builder.BitstreamBuilder;
import org.dspace.app.rest.builder.CollectionBuilder;
import org.dspace.app.rest.builder.CommunityBuilder;
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.test.AbstractControllerIntegrationTest;
import org.dspace.content.Bitstream;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.Item;
import org.dspace.content.WorkspaceItem;
import org.hamcrest.Matchers;
import org.junit.Test;
@@ -73,7 +73,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
.withSubject("ExtraEntry")
.build();
getClient().perform(get("/api/core/items"))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.items", Matchers.containsInAnyOrder(
@@ -197,7 +196,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
.withSubject("ExtraEntry")
.build();
getClient().perform(get("/api/core/items/" + publicItem1.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.is(
@@ -292,7 +290,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
;
}
@Test
public void findOneTestWrongUUID() throws Exception {
context.turnOffAuthorisationSystem();
@@ -341,34 +338,33 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
.withMimeType("text/plain")
.build();
}
// Check publicItem creation
getClient().perform(get("/api/core/items/" + publicItem.getID()))
.andExpect(status().isOk());
// Check publicItem bitstream creatino
getClient().perform(get("/api/core/items/" + publicItem.getID() + "/bitstreams"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._links.self.href", Matchers
.containsString("/api/core/items/" + publicItem.getID() + "/bitstreams")));
String token = getAuthToken(admin.getEmail(), password);
//Delete public item
getClient(token).perform(delete("/api/core/items/" + publicItem.getID()))
.andExpect(status().is(204));
//Trying to get deleted item should fail with 404
getClient().perform(get("/api/core/items/" + publicItem.getID()))
.andExpect(status().is(404));
//Trying to get deleted item bitstream should fail with 404
getClient().perform(get("/api/core/biststreams/" + bitstream.getID()))
.andExpect(status().is(404));
}
public void deleteOneTemplateTest() throws Exception {
context.turnOffAuthorisationSystem();
@@ -382,15 +378,41 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
//2. One template item.
Item templateItem = ItemBuilder.createTemplateItem(context, col1);
String token = getAuthToken(admin.getEmail(), password);
//Check templateItem creation
getClient().perform(get("/api/core/items/" + templateItem.getID()))
.andExpect(status().isOk());
//Trying to delete a templateItem should fail with 422
getClient(token).perform(delete("/api/core/items/" + templateItem.getID()))
.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));
}
}

View File

@@ -36,12 +36,12 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
ItemBuilder builder = new ItemBuilder(context);
return builder.create(context, col);
}
public static Item createTemplateItem(final Context context, final Collection collection) {
ItemBuilder builder = new ItemBuilder(context);
return builder.create_template_item(collection);
}
private Item create_template_item(final Collection collection) {
try {
item = itemService.createTemplateItem(context, collection);
@@ -50,7 +50,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
return handleException(e);
}
}
private ItemBuilder create(final Context context, final Collection col) {
this.context = context;
@@ -84,7 +84,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
item.setDiscoverable(false);
return this;
}
public ItemBuilder withEmbargoPeriod(String embargoPeriod) {
return setEmbargo(embargoPeriod, item);
}
@@ -113,7 +113,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
return handleException(e);
}
}
protected void cleanup() throws Exception {
delete(item);
}

View File

@@ -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;
}
}