mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-12 20:43:18 +00:00
Implemented feedback and testing on item bitstreams post endpoint
This commit is contained in:
@@ -1,13 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
package org.dspace.app.rest;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.BadRequestException;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@@ -16,18 +20,19 @@ import org.apache.logging.log4j.Logger;
|
|||||||
import org.dspace.app.rest.converter.BitstreamConverter;
|
import org.dspace.app.rest.converter.BitstreamConverter;
|
||||||
import org.dspace.app.rest.converter.MetadataConverter;
|
import org.dspace.app.rest.converter.MetadataConverter;
|
||||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||||
import org.dspace.app.rest.model.PropertiesRest;
|
import org.dspace.app.rest.model.BitstreamPropertiesRest;
|
||||||
import org.dspace.app.rest.model.hateoas.BitstreamResource;
|
import org.dspace.app.rest.model.hateoas.BitstreamResource;
|
||||||
import org.dspace.app.rest.utils.ContextUtil;
|
import org.dspace.app.rest.utils.ContextUtil;
|
||||||
import org.dspace.app.rest.utils.Utils;
|
import org.dspace.app.rest.utils.Utils;
|
||||||
import org.dspace.authorize.AuthorizeException;
|
import org.dspace.authorize.AuthorizeException;
|
||||||
import org.dspace.content.Bitstream;
|
import org.dspace.content.Bitstream;
|
||||||
import org.dspace.content.Item;
|
import org.dspace.content.Item;
|
||||||
import org.dspace.content.MetadataValue;
|
|
||||||
import org.dspace.content.service.BitstreamService;
|
import org.dspace.content.service.BitstreamService;
|
||||||
import org.dspace.content.service.ItemService;
|
import org.dspace.content.service.ItemService;
|
||||||
import org.dspace.core.Context;
|
import org.dspace.core.Context;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
@@ -57,6 +62,7 @@ public class ItemUploadController {
|
|||||||
private MetadataConverter metadataConverter;
|
private MetadataConverter metadataConverter;
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "/bitstreams", headers = "content-type=multipart/form-data")
|
@RequestMapping(method = RequestMethod.POST, value = "/bitstreams", headers = "content-type=multipart/form-data")
|
||||||
|
@PreAuthorize("hasPermission(#uuid, 'ITEM', 'WRITE')")
|
||||||
public BitstreamResource uploadBitstream(HttpServletRequest request, @PathVariable UUID uuid,
|
public BitstreamResource uploadBitstream(HttpServletRequest request, @PathVariable UUID uuid,
|
||||||
@RequestParam("file") MultipartFile uploadfile,
|
@RequestParam("file") MultipartFile uploadfile,
|
||||||
@RequestParam(value = "properties", required = false) String properties) {
|
@RequestParam(value = "properties", required = false) String properties) {
|
||||||
@@ -70,15 +76,14 @@ public class ItemUploadController {
|
|||||||
log.error("Something went wrong trying to find the Item with uuid: " + uuid, e);
|
log.error("Something went wrong trying to find the Item with uuid: " + uuid, e);
|
||||||
}
|
}
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
throw new BadRequestException("The given uuid did not resolve to an Item on the server: " + uuid);
|
throw new ResourceNotFoundException("The given uuid did not resolve to an Item on the server: " + uuid);
|
||||||
}
|
}
|
||||||
String fileName = uploadfile.getName();
|
|
||||||
InputStream fileInputStream = null;
|
InputStream fileInputStream = null;
|
||||||
try {
|
try {
|
||||||
fileInputStream = uploadfile.getInputStream();
|
fileInputStream = uploadfile.getInputStream();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Something went wrong when trying to read the inputstream from the given file in the request");
|
log.error("Something went wrong when trying to read the inputstream from the given file in the request");
|
||||||
throw new BadRequestException("The InputStream from the file couldn't be read");
|
throw new UnprocessableEntityException("The InputStream from the file couldn't be read");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
bitstream = processBitstreamCreation(context, item, fileInputStream, properties);
|
bitstream = processBitstreamCreation(context, item, fileInputStream, properties);
|
||||||
@@ -86,7 +91,8 @@ public class ItemUploadController {
|
|||||||
context.commit();
|
context.commit();
|
||||||
} catch (AuthorizeException | IOException | SQLException e) {
|
} catch (AuthorizeException | IOException | SQLException e) {
|
||||||
log.error(
|
log.error(
|
||||||
"Something went wrong with trying to create the single bitstream for file with filename: " + fileName
|
"Something went wrong with trying to create the single bitstream for file with filename: " + uploadfile
|
||||||
|
.getOriginalFilename()
|
||||||
+ " for item with uuid: " + uuid + " and possible properties: " + properties);
|
+ " for item with uuid: " + uuid + " and possible properties: " + properties);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -100,26 +106,29 @@ public class ItemUploadController {
|
|||||||
Bitstream bitstream = null;
|
Bitstream bitstream = null;
|
||||||
if (StringUtils.isNotBlank(properties)) {
|
if (StringUtils.isNotBlank(properties)) {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
PropertiesRest propertiesRest = null;
|
BitstreamPropertiesRest bitstreamPropertiesRest = null;
|
||||||
try {
|
try {
|
||||||
propertiesRest = mapper.readValue(properties, PropertiesRest.class);
|
bitstreamPropertiesRest = mapper.readValue(properties, BitstreamPropertiesRest.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new UnprocessableEntityException("The properties parameter was incorrect: " + properties);
|
throw new UnprocessableEntityException("The properties parameter was incorrect: " + properties);
|
||||||
}
|
}
|
||||||
String bundleName = propertiesRest.getBundleName();
|
String bundleName = bitstreamPropertiesRest.getBundleName();
|
||||||
if (StringUtils.isBlank(bundleName)) {
|
if (StringUtils.isBlank(bundleName)) {
|
||||||
throw new BadRequestException("Properties without a bundleName is not allowed");
|
throw new UnprocessableEntityException("Properties without a bundleName is not allowed");
|
||||||
}
|
}
|
||||||
bitstream = itemService.createSingleBitstream(context, fileInputStream, item, bundleName);
|
bitstream = itemService.createSingleBitstream(context, fileInputStream, item, bundleName);
|
||||||
metadataConverter.setMetadata(context, bitstream, propertiesRest.getMetadata());
|
if (bitstreamPropertiesRest.getMetadata() != null) {
|
||||||
String name = propertiesRest.getName();
|
metadataConverter.setMetadata(context, bitstream, bitstreamPropertiesRest.getMetadata());
|
||||||
|
}
|
||||||
|
String name = bitstreamPropertiesRest.getName();
|
||||||
if (StringUtils.isNotBlank(name)) {
|
if (StringUtils.isNotBlank(name)) {
|
||||||
bitstream.setName(context, name);
|
bitstream.setName(context, name);
|
||||||
}
|
}
|
||||||
String sequenceId = propertiesRest.getSequenceId();
|
Integer sequenceId = bitstreamPropertiesRest.getSequenceId();
|
||||||
if (StringUtils.isNotBlank(sequenceId)) {
|
if (sequenceId != null) {
|
||||||
bitstream.setSequenceID(Integer.parseInt(sequenceId));
|
bitstream.setSequenceID(sequenceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
bitstream = itemService.createSingleBitstream(context, fileInputStream, item);
|
bitstream = itemService.createSingleBitstream(context, fileInputStream, item);
|
||||||
|
|
||||||
|
@@ -38,6 +38,7 @@ public class BitstreamConverter
|
|||||||
@Override
|
@Override
|
||||||
public BitstreamRest fromModel(org.dspace.content.Bitstream obj) {
|
public BitstreamRest fromModel(org.dspace.content.Bitstream obj) {
|
||||||
BitstreamRest b = super.fromModel(obj);
|
BitstreamRest b = super.fromModel(obj);
|
||||||
|
b.setSequenceId(obj.getSequenceID());
|
||||||
List<Bundle> bundles = null;
|
List<Bundle> bundles = null;
|
||||||
try {
|
try {
|
||||||
bundles = obj.getBundles();
|
bundles = obj.getBundles();
|
||||||
|
@@ -1,10 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* 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.model;
|
package org.dspace.app.rest.model;
|
||||||
|
|
||||||
public class PropertiesRest {
|
public class BitstreamPropertiesRest {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private String bundleName;
|
private String bundleName;
|
||||||
private String sequenceId;
|
private Integer sequenceId;
|
||||||
private MetadataRest metadata;
|
private MetadataRest metadata;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -31,11 +38,11 @@ public class PropertiesRest {
|
|||||||
this.metadata = metadata;
|
this.metadata = metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSequenceId() {
|
public Integer getSequenceId() {
|
||||||
return sequenceId;
|
return sequenceId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSequenceId(String sequenceId) {
|
public void setSequenceId(Integer sequenceId) {
|
||||||
this.sequenceId = sequenceId;
|
this.sequenceId = sequenceId;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -16,7 +16,8 @@ public enum DSpaceRestPermission {
|
|||||||
|
|
||||||
READ(Constants.READ),
|
READ(Constants.READ),
|
||||||
WRITE(Constants.WRITE),
|
WRITE(Constants.WRITE),
|
||||||
DELETE(Constants.DELETE);
|
DELETE(Constants.DELETE),
|
||||||
|
ADD(Constants.ADD);
|
||||||
|
|
||||||
private int dspaceApiActionId;
|
private int dspaceApiActionId;
|
||||||
|
|
||||||
|
@@ -0,0 +1,498 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
|
||||||
|
import static org.hamcrest.core.IsNull.notNullValue;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
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.matcher.MetadataMatcher;
|
||||||
|
import org.dspace.app.rest.model.BitstreamPropertiesRest;
|
||||||
|
import org.dspace.app.rest.model.MetadataRest;
|
||||||
|
import org.dspace.app.rest.model.MetadataValueRest;
|
||||||
|
import org.dspace.app.rest.test.AbstractEntityIntegrationTest;
|
||||||
|
import org.dspace.authorize.service.AuthorizeService;
|
||||||
|
import org.dspace.content.Collection;
|
||||||
|
import org.dspace.content.Community;
|
||||||
|
import org.dspace.content.Item;
|
||||||
|
import org.dspace.core.Constants;
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
|
||||||
|
public class ItemUploadControllerIT extends AbstractEntityIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthorizeService authorizeService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadBitstreamAllPossibleFieldsProperties() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
|
||||||
|
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
|
||||||
|
bitstreamPropertiesRest.setBundleName("ORIGINAL");
|
||||||
|
bitstreamPropertiesRest.setName("testing");
|
||||||
|
bitstreamPropertiesRest.setSequenceId(123456);
|
||||||
|
|
||||||
|
MetadataRest metadataRest = new MetadataRest();
|
||||||
|
|
||||||
|
MetadataValueRest description = new MetadataValueRest();
|
||||||
|
description.setValue("description");
|
||||||
|
metadataRest.put("dc.description", description);
|
||||||
|
|
||||||
|
MetadataValueRest contents = new MetadataValueRest();
|
||||||
|
contents.setValue("News");
|
||||||
|
metadataRest.put("dc.description.tableofcontents", contents);
|
||||||
|
|
||||||
|
MetadataValueRest copyright = new MetadataValueRest();
|
||||||
|
copyright.setValue("Custom Copyright Text");
|
||||||
|
metadataRest.put("dc.rights", copyright);
|
||||||
|
|
||||||
|
MetadataValueRest title = new MetadataValueRest();
|
||||||
|
title.setValue("Title Text");
|
||||||
|
metadataRest.put("dc.title", title);
|
||||||
|
|
||||||
|
bitstreamPropertiesRest.setMetadata(metadataRest);
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file)
|
||||||
|
.param("properties", mapper
|
||||||
|
.writeValueAsString(bitstreamPropertiesRest)))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.name", Matchers.is("testing")))
|
||||||
|
.andExpect(jsonPath("$.bundleName", Matchers.is("ORIGINAL")))
|
||||||
|
.andExpect(jsonPath("$.sequenceId", Matchers.is(Integer.parseInt("123456"))))
|
||||||
|
.andExpect(jsonPath("$", Matchers.allOf(
|
||||||
|
hasJsonPath("$.metadata", Matchers.allOf(
|
||||||
|
MetadataMatcher.matchMetadata("dc.description",
|
||||||
|
"description"),
|
||||||
|
MetadataMatcher.matchMetadata("dc.description.tableofcontents",
|
||||||
|
"News"),
|
||||||
|
MetadataMatcher.matchMetadata("dc.rights",
|
||||||
|
"Custom Copyright Text"),
|
||||||
|
MetadataMatcher.matchMetadata("dc.title",
|
||||||
|
"testing")
|
||||||
|
)))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadBitstreamNoProperties() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file))
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadBitstreamNoPropertiesUserWithItemAddAndWriteRights() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
context.setCurrentUser(eperson);
|
||||||
|
authorizeService.addPolicy(context, item, Constants.ADD, eperson);
|
||||||
|
authorizeService.addPolicy(context, item, Constants.WRITE, eperson);
|
||||||
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.uuid", notNullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadBitstreamNoRights() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
context.setCurrentUser(eperson);
|
||||||
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file))
|
||||||
|
.andExpect(status().isForbidden());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadBitstreamAnonymous() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
context.setCurrentUser(eperson);
|
||||||
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient().perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file))
|
||||||
|
.andExpect(status().isUnauthorized());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadBitstreamMinimalProperties() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
context.setCurrentUser(eperson);
|
||||||
|
authorizeService.addPolicy(context, item, Constants.ADD, eperson);
|
||||||
|
authorizeService.addPolicy(context, item, Constants.WRITE, eperson);
|
||||||
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
|
||||||
|
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
|
||||||
|
String originalBundle = "ORIGINAL";
|
||||||
|
bitstreamPropertiesRest.setBundleName(originalBundle);
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file)
|
||||||
|
.param("properties", mapper
|
||||||
|
.writeValueAsString(bitstreamPropertiesRest)))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.bundleName", Matchers.is(originalBundle)))
|
||||||
|
.andExpect(jsonPath("$.uuid", notNullValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO This test just fails to run entirely because we cannot pass 'null' to a file upload
|
||||||
|
// Should we support this test case differently and if so, how?
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void uploadBitstreamNoFileUnprocessableEntityException() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(null))
|
||||||
|
.andExpect(status().isUnprocessableEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO This test doesn't work either as it seems that only the first file is ever transfered into the request
|
||||||
|
// Thus we cannot check for this and we have no way of knowing how many files we gave to the request
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void uploadBitstreamMultipleFiles() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello1.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
MockMultipartFile file2 = new MockMultipartFile("file", "hello2.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file).file(file2))
|
||||||
|
.andExpect(status().isUnprocessableEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uploadBitstreamNoBundleNameInPropertiesUnprocessableException() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
context.setCurrentUser(eperson);
|
||||||
|
authorizeService.addPolicy(context, item, Constants.ADD, eperson);
|
||||||
|
authorizeService.addPolicy(context, item, Constants.WRITE, eperson);
|
||||||
|
String token = getAuthToken(eperson.getEmail(), password);
|
||||||
|
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
|
||||||
|
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file)
|
||||||
|
.param("properties", mapper
|
||||||
|
.writeValueAsString(bitstreamPropertiesRest)))
|
||||||
|
.andExpect(status().isUnprocessableEntity());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//TODO This check on sequence ID doesn't happen
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void uploadBitstreamSameSequenceIdTwiceUnprocessableEntityException() throws Exception {
|
||||||
|
context.turnOffAuthorisationSystem();
|
||||||
|
|
||||||
|
|
||||||
|
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||||
|
.withName("Parent Community")
|
||||||
|
.build();
|
||||||
|
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
|
||||||
|
.withName("Sub Community")
|
||||||
|
.build();
|
||||||
|
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
|
||||||
|
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();
|
||||||
|
Collection col3 = CollectionBuilder.createCollection(context, child1).withName("OrgUnits").build();
|
||||||
|
|
||||||
|
Item item = ItemBuilder.createItem(context, col1)
|
||||||
|
.withTitle("Author1")
|
||||||
|
.withIssueDate("2017-10-17")
|
||||||
|
.withAuthor("Smith, Donald")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String token = getAuthToken(admin.getEmail(), password);
|
||||||
|
String input = "Hello, World!";
|
||||||
|
|
||||||
|
MockMultipartFile file = new MockMultipartFile("file", "hello.txt", MediaType.TEXT_PLAIN_VALUE,
|
||||||
|
input.getBytes());
|
||||||
|
|
||||||
|
BitstreamPropertiesRest bitstreamPropertiesRest = new BitstreamPropertiesRest();
|
||||||
|
bitstreamPropertiesRest.setBundleName("ORIGINAL");
|
||||||
|
bitstreamPropertiesRest.setName("testing");
|
||||||
|
bitstreamPropertiesRest.setSequenceId(123456);
|
||||||
|
|
||||||
|
MetadataRest metadataRest = new MetadataRest();
|
||||||
|
|
||||||
|
MetadataValueRest description = new MetadataValueRest();
|
||||||
|
description.setValue("description");
|
||||||
|
metadataRest.put("dc.description", description);
|
||||||
|
|
||||||
|
MetadataValueRest contents = new MetadataValueRest();
|
||||||
|
contents.setValue("News");
|
||||||
|
metadataRest.put("dc.description.tableofcontents", contents);
|
||||||
|
|
||||||
|
MetadataValueRest copyright = new MetadataValueRest();
|
||||||
|
copyright.setValue("Custom Copyright Text");
|
||||||
|
metadataRest.put("dc.rights", copyright);
|
||||||
|
|
||||||
|
MetadataValueRest title = new MetadataValueRest();
|
||||||
|
title.setValue("Title Text");
|
||||||
|
metadataRest.put("dc.title", title);
|
||||||
|
|
||||||
|
bitstreamPropertiesRest.setMetadata(metadataRest);
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
context.restoreAuthSystemState();
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file)
|
||||||
|
.param("properties", mapper
|
||||||
|
.writeValueAsString(bitstreamPropertiesRest)))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.name", Matchers.is("testing")))
|
||||||
|
.andExpect(jsonPath("$.bundleName", Matchers.is("ORIGINAL")))
|
||||||
|
.andExpect(jsonPath("$.sequenceId", Matchers.is(Integer.parseInt("123456"))))
|
||||||
|
.andExpect(jsonPath("$", Matchers.allOf(
|
||||||
|
hasJsonPath("$.metadata", Matchers.allOf(
|
||||||
|
MetadataMatcher.matchMetadata("dc.description",
|
||||||
|
"description"),
|
||||||
|
MetadataMatcher.matchMetadata("dc.description.tableofcontents",
|
||||||
|
"News"),
|
||||||
|
MetadataMatcher.matchMetadata("dc.rights",
|
||||||
|
"Custom Copyright Text"),
|
||||||
|
MetadataMatcher.matchMetadata("dc.title",
|
||||||
|
"testing")
|
||||||
|
)))));
|
||||||
|
|
||||||
|
getClient(token).perform(MockMvcRequestBuilders.fileUpload("/api/core/items/" + item.getID() + "/bitstreams")
|
||||||
|
.file(file)
|
||||||
|
.param("properties", mapper
|
||||||
|
.writeValueAsString(bitstreamPropertiesRest)))
|
||||||
|
.andExpect(status().isUnprocessableEntity());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user