mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 07:23:08 +00:00
DS-3651: Check bitstream authorizatoins + tests
This commit is contained in:
@@ -20,8 +20,11 @@ import org.apache.log4j.Logger;
|
|||||||
import org.dspace.app.rest.model.BitstreamRest;
|
import org.dspace.app.rest.model.BitstreamRest;
|
||||||
import org.dspace.app.rest.utils.ContextUtil;
|
import org.dspace.app.rest.utils.ContextUtil;
|
||||||
import org.dspace.app.rest.utils.MultipartFileSender;
|
import org.dspace.app.rest.utils.MultipartFileSender;
|
||||||
|
import org.dspace.authorize.AuthorizeException;
|
||||||
|
import org.dspace.authorize.service.AuthorizeService;
|
||||||
import org.dspace.content.Bitstream;
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.content.service.BitstreamService;
|
import org.dspace.content.service.BitstreamService;
|
||||||
|
import org.dspace.core.Constants;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
import org.dspace.services.ConfigurationService;
|
import org.dspace.services.ConfigurationService;
|
||||||
import org.dspace.services.EventService;
|
import org.dspace.services.EventService;
|
||||||
@@ -51,6 +54,9 @@ public class BitstreamContentRestController implements InitializingBean {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ConfigurationService configurationService;
|
private ConfigurationService configurationService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthorizeService authorizeService;
|
||||||
|
|
||||||
private int bufferSize;
|
private int bufferSize;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,9 +70,9 @@ public class BitstreamContentRestController implements InitializingBean {
|
|||||||
|
|
||||||
Context context = ContextUtil.obtainContext(request);
|
Context context = ContextUtil.obtainContext(request);
|
||||||
|
|
||||||
Bitstream bit = bitstreamService.find(context, uuid);
|
Bitstream bit = getBitstreamIfAuthorized(context, uuid, response);
|
||||||
if (bit == null) {
|
if (bit == null) {
|
||||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
//The bitstream was not found or we're not authorized to read it.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +116,22 @@ public class BitstreamContentRestController implements InitializingBean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Bitstream getBitstreamIfAuthorized(Context context, @PathVariable UUID uuid, HttpServletResponse response) throws SQLException, IOException {
|
||||||
|
Bitstream bit = bitstreamService.find(context, uuid);
|
||||||
|
if (bit == null) {
|
||||||
|
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
authorizeService.authorizeAction(context, bit, Constants.READ);
|
||||||
|
} catch (AuthorizeException e) {
|
||||||
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
bit = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bit;
|
||||||
|
}
|
||||||
|
|
||||||
private InputStream getInputStream(Context context, Bitstream bit) {
|
private InputStream getInputStream(Context context, Bitstream bit) {
|
||||||
try {
|
try {
|
||||||
return bitstreamService.retrieve(context, bit);
|
return bitstreamService.retrieve(context, bit);
|
||||||
|
@@ -14,21 +14,25 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang3.CharEncoding;
|
import org.apache.commons.lang3.CharEncoding;
|
||||||
import org.apache.solr.client.solrj.SolrQuery;
|
import org.apache.solr.client.solrj.SolrQuery;
|
||||||
|
import org.apache.solr.client.solrj.SolrServerException;
|
||||||
import org.apache.solr.client.solrj.response.QueryResponse;
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
||||||
import org.dspace.app.rest.builder.BitstreamBuilder;
|
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.GroupBuilder;
|
||||||
import org.dspace.app.rest.builder.ItemBuilder;
|
import org.dspace.app.rest.builder.ItemBuilder;
|
||||||
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.Item;
|
import org.dspace.content.Item;
|
||||||
|
import org.dspace.eperson.Group;
|
||||||
import org.dspace.solr.MockSolrServer;
|
import org.dspace.solr.MockSolrServer;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -61,24 +65,24 @@ public class BitstreamContentRestControllerIT extends AbstractControllerIntegrat
|
|||||||
|
|
||||||
//** GIVEN **
|
//** GIVEN **
|
||||||
//1. A community-collection structure with one parent community and one collections.
|
//1. A community-collection structure with one parent community and one collections.
|
||||||
parentCommunity = new CommunityBuilder().createCommunity(context)
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
.withName("Parent Community")
|
.withName("Parent Community")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Collection col1 = new CollectionBuilder().createCollection(context, parentCommunity).withName("Collection 1").build();
|
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
|
||||||
|
|
||||||
//2. A public item with a bitstream
|
//2. A public item with a bitstream
|
||||||
String bitstreamContent = "0123456789";
|
String bitstreamContent = "0123456789";
|
||||||
|
|
||||||
try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
|
try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
|
||||||
|
|
||||||
Item publicItem1 = new ItemBuilder().createItem(context, col1)
|
Item publicItem1 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Public item 1")
|
.withTitle("Public item 1")
|
||||||
.withIssueDate("2017-10-17")
|
.withIssueDate("2017-10-17")
|
||||||
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Bitstream bitstream = new BitstreamBuilder()
|
Bitstream bitstream = BitstreamBuilder
|
||||||
.createBitstream(context, publicItem1, is)
|
.createBitstream(context, publicItem1, is)
|
||||||
.withName("Test bitstream")
|
.withName("Test bitstream")
|
||||||
.withDescription("This is a bitstream to test range requests")
|
.withDescription("This is a bitstream to test range requests")
|
||||||
@@ -109,13 +113,7 @@ public class BitstreamContentRestControllerIT extends AbstractControllerIntegrat
|
|||||||
.andExpect(status().isNotModified());
|
.andExpect(status().isNotModified());
|
||||||
|
|
||||||
//The download and head request should also be logged as a statistics record
|
//The download and head request should also be logged as a statistics record
|
||||||
mockSolrServer.getSolrServer().commit();
|
checkNumberOfStatsRecords(bitstream, 2);
|
||||||
|
|
||||||
SolrQuery query = new SolrQuery("id:\"" + bitstream.getID() + "\"")
|
|
||||||
.setRows(0)
|
|
||||||
.setStart(0);
|
|
||||||
QueryResponse queryResponse = mockSolrServer.getSolrServer().query(query);
|
|
||||||
assertEquals( 2, queryResponse.getResults().getNumFound());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,24 +123,24 @@ public class BitstreamContentRestControllerIT extends AbstractControllerIntegrat
|
|||||||
|
|
||||||
//** GIVEN **
|
//** GIVEN **
|
||||||
//1. A community-collection structure with one parent community and one collections.
|
//1. A community-collection structure with one parent community and one collections.
|
||||||
parentCommunity = new CommunityBuilder().createCommunity(context)
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
.withName("Parent Community")
|
.withName("Parent Community")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Collection col1 = new CollectionBuilder().createCollection(context, parentCommunity).withName("Collection 1").build();
|
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
|
||||||
|
|
||||||
//2. A public item with a bitstream
|
//2. A public item with a bitstream
|
||||||
String bitstreamContent = "0123456789";
|
String bitstreamContent = "0123456789";
|
||||||
|
|
||||||
try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
|
try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
|
||||||
|
|
||||||
Item publicItem1 = new ItemBuilder().createItem(context, col1)
|
Item publicItem1 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Public item 1")
|
.withTitle("Public item 1")
|
||||||
.withIssueDate("2017-10-17")
|
.withIssueDate("2017-10-17")
|
||||||
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Bitstream bitstream = new BitstreamBuilder()
|
Bitstream bitstream = BitstreamBuilder
|
||||||
.createBitstream(context, publicItem1, is)
|
.createBitstream(context, publicItem1, is)
|
||||||
.withName("Test bitstream")
|
.withName("Test bitstream")
|
||||||
.withDescription("This is a bitstream to test range requests")
|
.withDescription("This is a bitstream to test range requests")
|
||||||
@@ -192,13 +190,7 @@ public class BitstreamContentRestControllerIT extends AbstractControllerIntegrat
|
|||||||
.andExpect(content().bytes("456789".getBytes()));
|
.andExpect(content().bytes("456789".getBytes()));
|
||||||
|
|
||||||
//Check that NO statistics record was logged for the Range requests
|
//Check that NO statistics record was logged for the Range requests
|
||||||
mockSolrServer.getSolrServer().commit();
|
checkNumberOfStatsRecords(bitstream, 0);
|
||||||
|
|
||||||
SolrQuery query = new SolrQuery("id:\"" + bitstream.getID() + "\"")
|
|
||||||
.setRows(0)
|
|
||||||
.setStart(0);
|
|
||||||
QueryResponse queryResponse = mockSolrServer.getSolrServer().query(query);
|
|
||||||
assertEquals(0, queryResponse.getResults().getNumFound());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,4 +200,111 @@ public class BitstreamContentRestControllerIT extends AbstractControllerIntegrat
|
|||||||
.andExpect(status().isNotFound());
|
.andExpect(status().isNotFound());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmbargoedBitstream() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
//** GIVEN **
|
||||||
|
//1. A community-collection structure with one parent community and one collections.
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
|
||||||
|
|
||||||
|
//2. A public item with an embargoed bitstream
|
||||||
|
String bitstreamContent = "Embargoed!";
|
||||||
|
|
||||||
|
try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
|
||||||
|
|
||||||
|
Item publicItem1 = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Public item 1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Bitstream bitstream = BitstreamBuilder
|
||||||
|
.createBitstream(context, publicItem1, is)
|
||||||
|
.withName("Test Embargoed Bitstream")
|
||||||
|
.withDescription("This bitstream is embargoed")
|
||||||
|
.withMimeType("text/plain")
|
||||||
|
.withEmbargoPeriod("6 months")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//** WHEN **
|
||||||
|
//We download the bitstream
|
||||||
|
getClient().perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content"))
|
||||||
|
|
||||||
|
//** THEN **
|
||||||
|
.andExpect(status().isUnauthorized())
|
||||||
|
//The response should not contain any content.
|
||||||
|
.andExpect(content().bytes(new byte[0]));
|
||||||
|
|
||||||
|
//An unauthorized request should not log statistics
|
||||||
|
checkNumberOfStatsRecords(bitstream, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrivateBitstream() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
//** GIVEN **
|
||||||
|
//1. A community-collection structure with one parent community and one collections.
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
|
||||||
|
|
||||||
|
//2. A public item with a private bitstream
|
||||||
|
Item publicItem1 = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Public item 1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Group internalGroup = GroupBuilder.createGroup(context)
|
||||||
|
.withName("Internal Group")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String bitstreamContent = "Private!";
|
||||||
|
try(InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
|
||||||
|
|
||||||
|
Bitstream bitstream = BitstreamBuilder
|
||||||
|
.createBitstream(context, publicItem1, is)
|
||||||
|
.withName("Test Embargoed Bitstream")
|
||||||
|
.withDescription("This bitstream is embargoed")
|
||||||
|
.withMimeType("text/plain")
|
||||||
|
.withReaderGroup(internalGroup)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//** WHEN **
|
||||||
|
//We download the bitstream
|
||||||
|
getClient().perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content"))
|
||||||
|
|
||||||
|
//** THEN **
|
||||||
|
.andExpect(status().isUnauthorized())
|
||||||
|
//The response should not contain any content.
|
||||||
|
.andExpect(content().bytes(new byte[0]));
|
||||||
|
|
||||||
|
//An unauthorized request should not log statistics
|
||||||
|
checkNumberOfStatsRecords(bitstream, 0);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
//** CLEANUP **
|
||||||
|
GroupBuilder.cleaner().delete(internalGroup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkNumberOfStatsRecords(Bitstream bitstream, int expectedNumberOfStatsRecords) throws SolrServerException, IOException {
|
||||||
|
mockSolrServer.getSolrServer().commit();
|
||||||
|
|
||||||
|
SolrQuery query = new SolrQuery("id:\"" + bitstream.getID() + "\"")
|
||||||
|
.setRows(0)
|
||||||
|
.setStart(0);
|
||||||
|
QueryResponse queryResponse = mockSolrServer.getSolrServer().query(query);
|
||||||
|
assertEquals(expectedNumberOfStatsRecords, queryResponse.getResults().getNumFound());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -129,31 +129,31 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
|
|
||||||
//** GIVEN **
|
//** GIVEN **
|
||||||
//1. A community-collection structure with one parent community with sub-community and two collections.
|
//1. A community-collection structure with one parent community with sub-community and two collections.
|
||||||
parentCommunity = new CommunityBuilder().createCommunity(context)
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
.withName("Parent Community")
|
.withName("Parent Community")
|
||||||
.build();
|
.build();
|
||||||
Community child1 = new CommunityBuilder().createSubCommunity(context, parentCommunity)
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
.withName("Sub Community")
|
.withName("Sub Community")
|
||||||
.build();
|
.build();
|
||||||
Collection col1 = new CollectionBuilder().createCollection(context, child1).withName("Collection 1").build();
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
Collection col2 = new CollectionBuilder().createCollection(context, child1).withName("Collection 2").build();
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
|
||||||
//2. Three public items that are readable by Anonymous with different subjects
|
//2. Three public items that are readable by Anonymous with different subjects
|
||||||
Item publicItem1 = new ItemBuilder().createItem(context, col1)
|
Item publicItem1 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Public item 1")
|
.withTitle("Public item 1")
|
||||||
.withIssueDate("2017-10-17")
|
.withIssueDate("2017-10-17")
|
||||||
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
||||||
.withSubject("ExtraEntry")
|
.withSubject("ExtraEntry")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item publicItem2 = new ItemBuilder().createItem(context, col2)
|
Item publicItem2 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Public item 2")
|
.withTitle("Public item 2")
|
||||||
.withIssueDate("2016-02-13")
|
.withIssueDate("2016-02-13")
|
||||||
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
||||||
.withSubject("TestingForMore").withSubject("ExtraEntry")
|
.withSubject("TestingForMore").withSubject("ExtraEntry")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item publicItem3 = new ItemBuilder().createItem(context, col2)
|
Item publicItem3 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Public item 2")
|
.withTitle("Public item 2")
|
||||||
.withIssueDate("2016-02-13")
|
.withIssueDate("2016-02-13")
|
||||||
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
||||||
@@ -208,32 +208,32 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
|
|
||||||
//** GIVEN **
|
//** GIVEN **
|
||||||
//1. A community-collection structure with one parent community with sub-community and two collections.
|
//1. A community-collection structure with one parent community with sub-community and two collections.
|
||||||
parentCommunity = new CommunityBuilder().createCommunity(context)
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
.withName("Parent Community")
|
.withName("Parent Community")
|
||||||
.build();
|
.build();
|
||||||
Community child1 = new CommunityBuilder().createSubCommunity(context, parentCommunity)
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
.withName("Sub Community")
|
.withName("Sub Community")
|
||||||
.build();
|
.build();
|
||||||
Collection col1 = new CollectionBuilder().createCollection(context, child1).withName("Collection 1").build();
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
Collection col2 = new CollectionBuilder().createCollection(context, child1).withName("Collection 2").build();
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
|
||||||
//2. Two public items with the same subject and another public item that contains that same subject, but also another one
|
//2. Two public items with the same subject and another public item that contains that same subject, but also another one
|
||||||
// All of the items are readable by an Anonymous user
|
// All of the items are readable by an Anonymous user
|
||||||
Item publicItem1 = new ItemBuilder().createItem(context, col1)
|
Item publicItem1 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("zPublic item more")
|
.withTitle("zPublic item more")
|
||||||
.withIssueDate("2017-10-17")
|
.withIssueDate("2017-10-17")
|
||||||
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
||||||
.withSubject("ExtraEntry").withSubject("AnotherTest")
|
.withSubject("ExtraEntry").withSubject("AnotherTest")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item publicItem2 = new ItemBuilder().createItem(context, col2)
|
Item publicItem2 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Public item 2")
|
.withTitle("Public item 2")
|
||||||
.withIssueDate("2016-02-13")
|
.withIssueDate("2016-02-13")
|
||||||
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
||||||
.withSubject("AnotherTest")
|
.withSubject("AnotherTest")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item publicItem3 = new ItemBuilder().createItem(context, col2)
|
Item publicItem3 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Public item 3")
|
.withTitle("Public item 3")
|
||||||
.withIssueDate("2016-02-14")
|
.withIssueDate("2016-02-14")
|
||||||
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
||||||
@@ -282,24 +282,24 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
|
|
||||||
//** GIVEN **
|
//** GIVEN **
|
||||||
//1. A community-collection structure with one parent community with sub-community and two collections.
|
//1. A community-collection structure with one parent community with sub-community and two collections.
|
||||||
parentCommunity = new CommunityBuilder().createCommunity(context)
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
.withName("Parent Community")
|
.withName("Parent Community")
|
||||||
.build();
|
.build();
|
||||||
Community child1 = new CommunityBuilder().createSubCommunity(context, parentCommunity)
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
.withName("Sub Community")
|
.withName("Sub Community")
|
||||||
.build();
|
.build();
|
||||||
Collection col1 = new CollectionBuilder().createCollection(context, child1).withName("Collection 1").build();
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
Collection col2 = new CollectionBuilder().createCollection(context, child1).withName("Collection 2").build();
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
|
||||||
//2. Two public items that are readable by Anonymous
|
//2. Two public items that are readable by Anonymous
|
||||||
Item publicItem1 = new ItemBuilder().createItem(context, col1)
|
Item publicItem1 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Public item 1")
|
.withTitle("Public item 1")
|
||||||
.withIssueDate("2017-10-17")
|
.withIssueDate("2017-10-17")
|
||||||
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
.withAuthor("Smith, Donald").withAuthor("Doe, John")
|
||||||
.withSubject("Java").withSubject("Unit Testing")
|
.withSubject("Java").withSubject("Unit Testing")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item publicItem2 = new ItemBuilder().createItem(context, col2)
|
Item publicItem2 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Public item 2")
|
.withTitle("Public item 2")
|
||||||
.withIssueDate("2016-02-13")
|
.withIssueDate("2016-02-13")
|
||||||
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
.withAuthor("Smith, Maria").withAuthor("Doe, Jane")
|
||||||
@@ -307,7 +307,7 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
//3. An item that has been made private
|
//3. An item that has been made private
|
||||||
Item privateItem = new ItemBuilder().createItem(context, col1)
|
Item privateItem = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("This is a private item")
|
.withTitle("This is a private item")
|
||||||
.withIssueDate("2015-03-12")
|
.withIssueDate("2015-03-12")
|
||||||
.withAuthor("Duck, Donald")
|
.withAuthor("Duck, Donald")
|
||||||
@@ -316,7 +316,7 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
//4. An item with an item-level embargo
|
//4. An item with an item-level embargo
|
||||||
Item embargoedItem = new ItemBuilder().createItem(context, col2)
|
Item embargoedItem = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("An embargoed publication")
|
.withTitle("An embargoed publication")
|
||||||
.withIssueDate("2017-08-10")
|
.withIssueDate("2017-08-10")
|
||||||
.withAuthor("Mouse, Mickey")
|
.withAuthor("Mouse, Mickey")
|
||||||
@@ -325,11 +325,11 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
//5. An item that is only readable for an internal groups
|
//5. An item that is only readable for an internal groups
|
||||||
Group internalGroup = new GroupBuilder().createGroup(context)
|
Group internalGroup = GroupBuilder.createGroup(context)
|
||||||
.withName("Internal Group")
|
.withName("Internal Group")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item internalItem = new ItemBuilder().createItem(context, col2)
|
Item internalItem = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Internal publication")
|
.withTitle("Internal publication")
|
||||||
.withIssueDate("2016-09-19")
|
.withIssueDate("2016-09-19")
|
||||||
.withAuthor("Doe, John")
|
.withAuthor("Doe, John")
|
||||||
@@ -376,7 +376,7 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
;
|
;
|
||||||
|
|
||||||
//** CLEANUP **
|
//** CLEANUP **
|
||||||
new GroupBuilder().delete(internalGroup);
|
GroupBuilder.cleaner().delete(internalGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -385,47 +385,47 @@ public class BrowsesResourceControllerIT extends AbstractControllerIntegrationTe
|
|||||||
|
|
||||||
//** GIVEN **
|
//** GIVEN **
|
||||||
//1. A community-collection structure with one parent community with sub-community and two collections.
|
//1. A community-collection structure with one parent community with sub-community and two collections.
|
||||||
parentCommunity = new CommunityBuilder().createCommunity(context)
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
.withName("Parent Community")
|
.withName("Parent Community")
|
||||||
.build();
|
.build();
|
||||||
Community child1 = new CommunityBuilder().createSubCommunity(context, parentCommunity)
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
.withName("Sub Community")
|
.withName("Sub Community")
|
||||||
.build();
|
.build();
|
||||||
Collection col1 = new CollectionBuilder().createCollection(context, child1).withName("Collection 1").build();
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
Collection col2 = new CollectionBuilder().createCollection(context, child1).withName("Collection 2").build();
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
|
||||||
//2. 7 public items that are readable by Anonymous
|
//2. 7 public items that are readable by Anonymous
|
||||||
Item item1 = new ItemBuilder().createItem(context, col1)
|
Item item1 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Item 1")
|
.withTitle("Item 1")
|
||||||
.withIssueDate("2017-10-17")
|
.withIssueDate("2017-10-17")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item item2 = new ItemBuilder().createItem(context, col2)
|
Item item2 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Item 2")
|
.withTitle("Item 2")
|
||||||
.withIssueDate("2016-02-13")
|
.withIssueDate("2016-02-13")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item item3 = new ItemBuilder().createItem(context, col1)
|
Item item3 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Item 3")
|
.withTitle("Item 3")
|
||||||
.withIssueDate("2016-02-12")
|
.withIssueDate("2016-02-12")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item item4 = new ItemBuilder().createItem(context, col2)
|
Item item4 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Item 4")
|
.withTitle("Item 4")
|
||||||
.withIssueDate("2016-02-11")
|
.withIssueDate("2016-02-11")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item item5 = new ItemBuilder().createItem(context, col1)
|
Item item5 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Item 5")
|
.withTitle("Item 5")
|
||||||
.withIssueDate("2016-02-10")
|
.withIssueDate("2016-02-10")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item item6 = new ItemBuilder().createItem(context, col2)
|
Item item6 = ItemBuilder.createItem(context, col2)
|
||||||
.withTitle("Item 6")
|
.withTitle("Item 6")
|
||||||
.withIssueDate("2016-01-13")
|
.withIssueDate("2016-01-13")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Item item7 = new ItemBuilder().createItem(context, col1)
|
Item item7 = ItemBuilder.createItem(context, col1)
|
||||||
.withTitle("Item 7")
|
.withTitle("Item 7")
|
||||||
.withIssueDate("2016-01-12")
|
.withIssueDate("2016-01-12")
|
||||||
.build();
|
.build();
|
||||||
|
@@ -157,8 +157,10 @@ public abstract class AbstractBuilder<T extends DSpaceObject> {
|
|||||||
Context c = new Context();
|
Context c = new Context();
|
||||||
c.turnOffAuthorisationSystem();
|
c.turnOffAuthorisationSystem();
|
||||||
T attachedDso = c.reloadEntity(dso);
|
T attachedDso = c.reloadEntity(dso);
|
||||||
|
|
||||||
if(attachedDso != null) {
|
if(attachedDso != null) {
|
||||||
getDsoService().delete(c, attachedDso);
|
getDsoService().delete(c, attachedDso);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -19,6 +19,7 @@ import org.dspace.content.Bundle;
|
|||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
import org.dspace.content.service.DSpaceObjectService;
|
import org.dspace.content.service.DSpaceObjectService;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
|
import org.dspace.eperson.Group;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder class to build bitstreams in test cases
|
* Builder class to build bitstreams in test cases
|
||||||
@@ -28,9 +29,21 @@ public class BitstreamBuilder extends AbstractBuilder<Bitstream>{
|
|||||||
public static final String ORIGINAL = "ORIGINAL";
|
public static final String ORIGINAL = "ORIGINAL";
|
||||||
|
|
||||||
private Bitstream bitstream;
|
private Bitstream bitstream;
|
||||||
|
private Item item;
|
||||||
|
private Group readerGroup;
|
||||||
|
|
||||||
public BitstreamBuilder createBitstream(Context context, Item item, InputStream is) throws SQLException, AuthorizeException, IOException {
|
protected BitstreamBuilder() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BitstreamBuilder createBitstream(Context context, Item item, InputStream is) throws SQLException, AuthorizeException, IOException {
|
||||||
|
BitstreamBuilder builder = new BitstreamBuilder();
|
||||||
|
return builder.create(context, item, is);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BitstreamBuilder create(Context context, Item item, InputStream is) throws SQLException, AuthorizeException, IOException {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
this.item = item;
|
||||||
|
|
||||||
Bundle originalBundle = getOriginalBundle(item);
|
Bundle originalBundle = getOriginalBundle(item);
|
||||||
|
|
||||||
@@ -78,17 +91,37 @@ public class BitstreamBuilder extends AbstractBuilder<Bitstream>{
|
|||||||
return targetBundle;
|
return targetBundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DSpaceObjectService<Bitstream> getDsoService() {
|
public BitstreamBuilder withEmbargoPeriod(String embargoPeriod) {
|
||||||
return bitstreamService;
|
return setEmbargo(embargoPeriod, bitstream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BitstreamBuilder withReaderGroup(Group group) {
|
||||||
|
readerGroup = group;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitstream build() {
|
public Bitstream build() {
|
||||||
try {
|
try {
|
||||||
bitstreamService.update(context, bitstream);
|
bitstreamService.update(context, bitstream);
|
||||||
|
itemService.update(context, item);
|
||||||
|
|
||||||
|
//Check if we need to make this bitstream private.
|
||||||
|
if(readerGroup != null) {
|
||||||
|
setOnlyReadPermission(bitstream, readerGroup, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
context.dispatchEvents();
|
||||||
|
|
||||||
|
indexingService.commit();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bitstream;
|
return bitstream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected DSpaceObjectService<Bitstream> getDsoService() {
|
||||||
|
return bitstreamService;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,7 +20,16 @@ public class CollectionBuilder extends AbstractBuilder<Collection> {
|
|||||||
|
|
||||||
private Collection collection;
|
private Collection collection;
|
||||||
|
|
||||||
public CollectionBuilder createCollection(final Context context, final Community parent) {
|
protected CollectionBuilder() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CollectionBuilder createCollection(final Context context, final Community parent) {
|
||||||
|
CollectionBuilder builder = new CollectionBuilder();
|
||||||
|
return builder.create(context, parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CollectionBuilder create(final Context context, final Community parent) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
try {
|
try {
|
||||||
this.collection = collectionService.create(context, parent);
|
this.collection = collectionService.create(context, parent);
|
||||||
|
@@ -19,11 +19,25 @@ public class CommunityBuilder extends AbstractBuilder<Community> {
|
|||||||
|
|
||||||
private Community community;
|
private Community community;
|
||||||
|
|
||||||
public CommunityBuilder createCommunity(final Context context) {
|
protected CommunityBuilder() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CommunityBuilder createCommunity(final Context context) {
|
||||||
|
CommunityBuilder builder = new CommunityBuilder();
|
||||||
|
return builder.create(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommunityBuilder create(final Context context) {
|
||||||
return createSubCommunity(context, null);
|
return createSubCommunity(context, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommunityBuilder createSubCommunity(final Context context, final Community parent) {
|
public static CommunityBuilder createSubCommunity(final Context context, final Community parent) {
|
||||||
|
CommunityBuilder builder = new CommunityBuilder();
|
||||||
|
return builder.createSub(context, parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommunityBuilder createSub(final Context context, final Community parent) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
try {
|
try {
|
||||||
community = communityService.create(parent, context);
|
community = communityService.create(parent, context);
|
||||||
|
@@ -19,6 +19,25 @@ public class GroupBuilder extends AbstractBuilder<Group> {
|
|||||||
|
|
||||||
private Group group;
|
private Group group;
|
||||||
|
|
||||||
|
protected GroupBuilder() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GroupBuilder createGroup(final Context context) {
|
||||||
|
GroupBuilder builder = new GroupBuilder();
|
||||||
|
return builder.create(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private GroupBuilder create(final Context context) {
|
||||||
|
this.context = context;
|
||||||
|
try {
|
||||||
|
group = groupService.create(context);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return handleException(e);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected DSpaceObjectService<Group> getDsoService() {
|
protected DSpaceObjectService<Group> getDsoService() {
|
||||||
return groupService;
|
return groupService;
|
||||||
@@ -29,16 +48,6 @@ public class GroupBuilder extends AbstractBuilder<Group> {
|
|||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GroupBuilder createGroup(final Context context) {
|
|
||||||
this.context = context;
|
|
||||||
try {
|
|
||||||
group = groupService.create(context);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return handleException(e);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GroupBuilder withName(String groupName) {
|
public GroupBuilder withName(String groupName) {
|
||||||
try {
|
try {
|
||||||
groupService.setName(group, groupName);
|
groupService.setName(group, groupName);
|
||||||
@@ -65,4 +74,9 @@ public class GroupBuilder extends AbstractBuilder<Group> {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static AbstractBuilder<Group> cleaner() {
|
||||||
|
return new GroupBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -24,11 +24,20 @@ public class ItemBuilder extends AbstractBuilder<Item> {
|
|||||||
private WorkspaceItem workspaceItem;
|
private WorkspaceItem workspaceItem;
|
||||||
private Group readerGroup = null;
|
private Group readerGroup = null;
|
||||||
|
|
||||||
public ItemBuilder createItem(final Context context, final Collection col1) {
|
protected ItemBuilder() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemBuilder createItem(final Context context, final Collection col) {
|
||||||
|
ItemBuilder builder = new ItemBuilder();
|
||||||
|
return builder.create(context, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ItemBuilder create(final Context context, final Collection col) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
workspaceItem = workspaceItemService.create(context, col1, false);
|
workspaceItem = workspaceItemService.create(context, col, false);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return handleException(e);
|
return handleException(e);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user