mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-15 22:13:08 +00:00
Added support for the CRUD operations on the MetadataField and MetadataSchema REST endpoints
This commit is contained in:
@@ -14,6 +14,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.content.dao.MetadataSchemaDAO;
|
||||
import org.dspace.content.service.MetadataFieldService;
|
||||
import org.dspace.content.service.MetadataSchemaService;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.core.LogManager;
|
||||
@@ -33,6 +34,9 @@ public class MetadataSchemaServiceImpl implements MetadataSchemaService {
|
||||
*/
|
||||
private static Logger log = org.apache.logging.log4j.LogManager.getLogger(MetadataSchemaServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
protected MetadataFieldService metadataFieldService;
|
||||
|
||||
@Autowired(required = true)
|
||||
protected AuthorizeService authorizeService;
|
||||
|
||||
@@ -115,10 +119,14 @@ public class MetadataSchemaServiceImpl implements MetadataSchemaService {
|
||||
"Only administrators may modify the metadata registry");
|
||||
}
|
||||
|
||||
log.info(LogManager.getHeader(context, "delete_metadata_schema",
|
||||
"metadata_schema_id=" + metadataSchema.getID()));
|
||||
for (MetadataField metadataField : metadataFieldService.findAllInSchema(context, metadataSchema)) {
|
||||
metadataFieldService.delete(context, metadataField);
|
||||
}
|
||||
|
||||
metadataSchemaDAO.delete(context, metadataSchema);
|
||||
|
||||
log.info(LogManager.getHeader(context, "delete_metadata_schema",
|
||||
"metadata_schema_id=" + metadataSchema.getID()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -987,6 +987,14 @@ public class RestResourceController implements InitializingBean {
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(method = RequestMethod.PUT, value = REGEX_REQUESTMAPPING_IDENTIFIER_AS_DIGIT)
|
||||
public DSpaceResource<RestAddressableModel> put(HttpServletRequest request,
|
||||
@PathVariable String apiCategory, @PathVariable String model,
|
||||
@PathVariable Integer id,
|
||||
@RequestBody(required = true) JsonNode jsonNode) {
|
||||
return putOneInternal(request, apiCategory, model, id, jsonNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a PUT request for an entity with id of type UUID;
|
||||
*
|
||||
|
@@ -8,6 +8,7 @@
|
||||
package org.dspace.app.rest.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import org.dspace.app.rest.RestResourceController;
|
||||
|
||||
/**
|
||||
@@ -15,6 +16,7 @@ import org.dspace.app.rest.RestResourceController;
|
||||
*
|
||||
* @author Andrea Bollini (andrea.bollini at 4science.it)
|
||||
*/
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class MetadataFieldRest extends BaseObjectRest<Integer> {
|
||||
public static final String NAME = "metadatafield";
|
||||
public static final String CATEGORY = RestAddressableModel.CORE;
|
||||
|
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
package org.dspace.app.rest.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import org.dspace.app.rest.RestResourceController;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,7 @@ import org.dspace.app.rest.RestResourceController;
|
||||
*
|
||||
* @author Andrea Bollini (andrea.bollini at 4science.it)
|
||||
*/
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class MetadataSchemaRest extends BaseObjectRest<Integer> {
|
||||
public static final String NAME = "metadataschema";
|
||||
public static final String CATEGORY = RestAddressableModel.CORE;
|
||||
|
@@ -7,22 +7,37 @@
|
||||
*/
|
||||
package org.dspace.app.rest.repository;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
import static org.apache.commons.lang.StringUtils.isBlank;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import org.dspace.app.rest.Parameter;
|
||||
import org.dspace.app.rest.SearchRestMethod;
|
||||
import org.dspace.app.rest.converter.MetadataFieldConverter;
|
||||
import org.dspace.app.rest.exception.PatchBadRequestException;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.MetadataFieldRest;
|
||||
import org.dspace.app.rest.model.hateoas.MetadataFieldResource;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.MetadataField;
|
||||
import org.dspace.content.MetadataSchema;
|
||||
import org.dspace.content.NonUniqueMetadataException;
|
||||
import org.dspace.content.service.MetadataFieldService;
|
||||
import org.dspace.content.service.MetadataSchemaService;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@@ -34,7 +49,7 @@ import org.springframework.stereotype.Component;
|
||||
public class MetadataFieldRestRepository extends DSpaceRestRepository<MetadataFieldRest, Integer> {
|
||||
|
||||
@Autowired
|
||||
MetadataFieldService metaFieldService;
|
||||
MetadataFieldService metadataFieldService;
|
||||
|
||||
@Autowired
|
||||
MetadataSchemaService metadataSchemaService;
|
||||
@@ -49,7 +64,7 @@ public class MetadataFieldRestRepository extends DSpaceRestRepository<MetadataFi
|
||||
public MetadataFieldRest findOne(Context context, Integer id) {
|
||||
MetadataField metadataField = null;
|
||||
try {
|
||||
metadataField = metaFieldService.find(context, id);
|
||||
metadataField = metadataFieldService.find(context, id);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
@@ -63,7 +78,7 @@ public class MetadataFieldRestRepository extends DSpaceRestRepository<MetadataFi
|
||||
public Page<MetadataFieldRest> findAll(Context context, Pageable pageable) {
|
||||
List<MetadataField> metadataField = null;
|
||||
try {
|
||||
metadataField = metaFieldService.findAll(context);
|
||||
metadataField = metadataFieldService.findAll(context);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
@@ -81,7 +96,7 @@ public class MetadataFieldRestRepository extends DSpaceRestRepository<MetadataFi
|
||||
if (schema == null) {
|
||||
return null;
|
||||
}
|
||||
metadataFields = metaFieldService.findAllInSchema(context, schema);
|
||||
metadataFields = metadataFieldService.findAllInSchema(context, schema);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
@@ -98,4 +113,116 @@ public class MetadataFieldRestRepository extends DSpaceRestRepository<MetadataFi
|
||||
public MetadataFieldResource wrapResource(MetadataFieldRest bs, String... rels) {
|
||||
return new MetadataFieldResource(bs, utils, rels);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
protected MetadataFieldRest createAndReturn(Context context)
|
||||
throws AuthorizeException, SQLException {
|
||||
|
||||
// parse request body
|
||||
|
||||
MetadataFieldRest metadataFieldRest;
|
||||
try {
|
||||
metadataFieldRest = new ObjectMapper().readValue(
|
||||
getRequestService().getCurrentRequest().getHttpServletRequest().getInputStream(),
|
||||
MetadataFieldRest.class
|
||||
);
|
||||
} catch (IOException excIO) {
|
||||
throw new PatchBadRequestException("error parsing the body ..." + excIO.getMessage());
|
||||
}
|
||||
|
||||
// validate fields
|
||||
|
||||
String schemaId = getRequestService().getCurrentRequest().getHttpServletRequest().getParameter("schemaId");
|
||||
if (isBlank(schemaId)) {
|
||||
throw new UnprocessableEntityException("metadata schema ID can be blank not");
|
||||
}
|
||||
|
||||
MetadataSchema schema = metadataSchemaService.find(context, parseInt(schemaId));
|
||||
if (schema == null) {
|
||||
throw new UnprocessableEntityException("metadata schema is found not");
|
||||
}
|
||||
|
||||
if (isBlank(metadataFieldRest.getElement())) {
|
||||
throw new UnprocessableEntityException("metadata element can be blank not");
|
||||
}
|
||||
|
||||
// create
|
||||
|
||||
MetadataField metadataField;
|
||||
try {
|
||||
metadataField = metadataFieldService.create(context, schema,
|
||||
metadataFieldRest.getElement(), metadataFieldRest.getQualifier(), metadataFieldRest.getScopeNote());
|
||||
metadataFieldService.update(context, metadataField);
|
||||
} catch (NonUniqueMetadataException e) {
|
||||
throw new UnprocessableEntityException(
|
||||
"metadata field "
|
||||
+ schema.getName() + "." + metadataFieldRest.getElement()
|
||||
+ (metadataFieldRest.getQualifier() != null ? "." + metadataFieldRest.getQualifier() : "")
|
||||
+ " already exists"
|
||||
);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// return
|
||||
|
||||
return converter.convert(metadataField);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
protected void delete(Context context, Integer id) throws AuthorizeException {
|
||||
|
||||
try {
|
||||
MetadataField metadataField = metadataFieldService.find(context, id);
|
||||
|
||||
if (metadataField == null) {
|
||||
throw new ResourceNotFoundException("metadata field with id: " + id + " not found");
|
||||
}
|
||||
|
||||
metadataFieldService.delete(context, metadataField);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
protected MetadataFieldRest put(Context context, HttpServletRequest request, String apiCategory, String model,
|
||||
Integer id, JsonNode jsonNode) throws SQLException, AuthorizeException {
|
||||
|
||||
MetadataFieldRest metadataFieldRest = new Gson().fromJson(jsonNode.toString(), MetadataFieldRest.class);
|
||||
|
||||
if (isBlank(metadataFieldRest.getElement())) {
|
||||
throw new UnprocessableEntityException("metadata element can be blank not");
|
||||
}
|
||||
|
||||
if (!Objects.equals(id, metadataFieldRest.getId())) {
|
||||
throw new UnprocessableEntityException("body id matches path id... not");
|
||||
}
|
||||
|
||||
MetadataField metadataField = metadataFieldService.find(context, id);
|
||||
if (metadataField == null) {
|
||||
throw new ResourceNotFoundException("metadata field with id: " + id + " is found not");
|
||||
}
|
||||
|
||||
metadataField.setElement(metadataFieldRest.getElement());
|
||||
metadataField.setQualifier(metadataFieldRest.getQualifier());
|
||||
metadataField.setScopeNote(metadataFieldRest.getScopeNote());
|
||||
|
||||
try {
|
||||
metadataFieldService.update(context, metadataField);
|
||||
context.commit();
|
||||
} catch (NonUniqueMetadataException e) {
|
||||
throw new UnprocessableEntityException("metadata field "
|
||||
+ metadataField.getMetadataSchema().getName() + "." + metadataFieldRest.getElement()
|
||||
+ (metadataFieldRest.getQualifier() != null ? "." + metadataFieldRest.getQualifier() : "")
|
||||
+ " already exists");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return converter.fromModel(metadataField);
|
||||
}
|
||||
}
|
@@ -7,18 +7,32 @@
|
||||
*/
|
||||
package org.dspace.app.rest.repository;
|
||||
|
||||
import static org.apache.commons.lang.StringUtils.isBlank;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import org.dspace.app.rest.converter.MetadataSchemaConverter;
|
||||
import org.dspace.app.rest.exception.PatchBadRequestException;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.MetadataSchemaRest;
|
||||
import org.dspace.app.rest.model.hateoas.MetadataSchemaResource;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.MetadataSchema;
|
||||
import org.dspace.content.NonUniqueMetadataException;
|
||||
import org.dspace.content.service.MetadataSchemaService;
|
||||
import org.dspace.core.Context;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@@ -30,7 +44,7 @@ import org.springframework.stereotype.Component;
|
||||
public class MetadataSchemaRestRepository extends DSpaceRestRepository<MetadataSchemaRest, Integer> {
|
||||
|
||||
@Autowired
|
||||
MetadataSchemaService metaScemaService;
|
||||
MetadataSchemaService metadataSchemaService;
|
||||
|
||||
@Autowired
|
||||
MetadataSchemaConverter converter;
|
||||
@@ -42,7 +56,7 @@ public class MetadataSchemaRestRepository extends DSpaceRestRepository<MetadataS
|
||||
public MetadataSchemaRest findOne(Context context, Integer id) {
|
||||
MetadataSchema metadataSchema = null;
|
||||
try {
|
||||
metadataSchema = metaScemaService.find(context, id);
|
||||
metadataSchema = metadataSchemaService.find(context, id);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
@@ -56,7 +70,7 @@ public class MetadataSchemaRestRepository extends DSpaceRestRepository<MetadataS
|
||||
public Page<MetadataSchemaRest> findAll(Context context, Pageable pageable) {
|
||||
List<MetadataSchema> metadataSchema = null;
|
||||
try {
|
||||
metadataSchema = metaScemaService.findAll(context);
|
||||
metadataSchema = metadataSchemaService.findAll(context);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
@@ -73,4 +87,102 @@ public class MetadataSchemaRestRepository extends DSpaceRestRepository<MetadataS
|
||||
public MetadataSchemaResource wrapResource(MetadataSchemaRest bs, String... rels) {
|
||||
return new MetadataSchemaResource(bs, utils, rels);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
protected MetadataSchemaRest createAndReturn(Context context)
|
||||
throws AuthorizeException, SQLException {
|
||||
|
||||
// parse request body
|
||||
|
||||
MetadataSchemaRest metadataSchemaRest;
|
||||
try {
|
||||
metadataSchemaRest = new ObjectMapper().readValue(
|
||||
getRequestService().getCurrentRequest().getHttpServletRequest().getInputStream(),
|
||||
MetadataSchemaRest.class
|
||||
);
|
||||
} catch (IOException excIO) {
|
||||
throw new PatchBadRequestException("error parsing the body ..." + excIO.getMessage());
|
||||
}
|
||||
|
||||
// validate fields
|
||||
|
||||
if (isBlank(metadataSchemaRest.getPrefix())) {
|
||||
throw new UnprocessableEntityException("metadata schema name can be blank not");
|
||||
}
|
||||
if (isBlank(metadataSchemaRest.getNamespace())) {
|
||||
throw new UnprocessableEntityException("metadata schema namespace can be blank not");
|
||||
}
|
||||
|
||||
// create
|
||||
|
||||
MetadataSchema metadataSchema;
|
||||
try {
|
||||
metadataSchema = metadataSchemaService.create(
|
||||
context, metadataSchemaRest.getPrefix(), metadataSchemaRest.getNamespace()
|
||||
);
|
||||
metadataSchemaService.update(context, metadataSchema);
|
||||
} catch (NonUniqueMetadataException e) {
|
||||
throw new UnprocessableEntityException("metadata schema "
|
||||
+ metadataSchemaRest.getPrefix() + "." + metadataSchemaRest.getNamespace() + " already exists");
|
||||
}
|
||||
|
||||
// return
|
||||
|
||||
return converter.convert(metadataSchema);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
protected void delete(Context context, Integer id) throws AuthorizeException {
|
||||
|
||||
try {
|
||||
MetadataSchema metadataSchema = metadataSchemaService.find(context, id);
|
||||
|
||||
if (metadataSchema == null) {
|
||||
throw new ResourceNotFoundException("metadata schema with id: " + id + " not found");
|
||||
}
|
||||
|
||||
metadataSchemaService.delete(context, metadataSchema);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('ADMIN')")
|
||||
protected MetadataSchemaRest put(Context context, HttpServletRequest request, String apiCategory, String model,
|
||||
Integer id, JsonNode jsonNode) throws SQLException, AuthorizeException {
|
||||
|
||||
MetadataSchemaRest metadataSchemaRest = new Gson().fromJson(jsonNode.toString(), MetadataSchemaRest.class);
|
||||
|
||||
if (isBlank(metadataSchemaRest.getPrefix())) {
|
||||
throw new UnprocessableEntityException("metadata schema name can be blank not");
|
||||
}
|
||||
if (isBlank(metadataSchemaRest.getNamespace())) {
|
||||
throw new UnprocessableEntityException("metadata schema namespace can be blank not");
|
||||
}
|
||||
|
||||
if (!Objects.equals(id, metadataSchemaRest.getId())) {
|
||||
throw new UnprocessableEntityException("body id matches path id... not");
|
||||
}
|
||||
|
||||
MetadataSchema metadataSchema = metadataSchemaService.find(context, id);
|
||||
if (metadataSchema == null) {
|
||||
throw new ResourceNotFoundException("metadata schema with id: " + id + " not found");
|
||||
}
|
||||
|
||||
metadataSchema.setName(metadataSchemaRest.getPrefix());
|
||||
metadataSchema.setNamespace(metadataSchemaRest.getNamespace());
|
||||
|
||||
try {
|
||||
metadataSchemaService.update(context, metadataSchema);
|
||||
context.commit();
|
||||
} catch (NonUniqueMetadataException e) {
|
||||
throw new UnprocessableEntityException("metadata schema "
|
||||
+ metadataSchemaRest.getPrefix() + "." + metadataSchemaRest.getNamespace() + " already exists");
|
||||
}
|
||||
|
||||
return converter.fromModel(metadataSchema);
|
||||
}
|
||||
}
|
@@ -7,21 +7,47 @@
|
||||
*/
|
||||
package org.dspace.app.rest;
|
||||
|
||||
import static com.jayway.jsonpath.JsonPath.read;
|
||||
import static org.dspace.app.rest.matcher.MetadataschemaMatcher.matchEntry;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.dspace.app.rest.builder.MetadataSchemaBuilder;
|
||||
import org.dspace.app.rest.matcher.MetadataschemaMatcher;
|
||||
import org.dspace.app.rest.model.MetadataSchemaRest;
|
||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.MetadataSchema;
|
||||
import org.dspace.content.NonUniqueMetadataException;
|
||||
import org.dspace.content.service.MetadataSchemaService;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class MetadataSchemaRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
private static final String TEST_NAME = "testSchemaName";
|
||||
private static final String TEST_NAMESPACE = "testSchemaNameSpace";
|
||||
|
||||
private static final String TEST_NAME_UPDATED = "testSchemaNameUpdated";
|
||||
private static final String TEST_NAMESPACE_UPDATED = "testSchemaNameSpaceUpdated";
|
||||
|
||||
@Autowired
|
||||
private MetadataSchemaService metadataSchemaService;
|
||||
|
||||
@Test
|
||||
public void findAll() throws Exception {
|
||||
|
||||
@@ -33,13 +59,12 @@ public class MetadataSchemaRestRepositoryIT extends AbstractControllerIntegratio
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(contentType))
|
||||
.andExpect(jsonPath("$._embedded.metadataschemas", Matchers.hasItem(
|
||||
MetadataschemaMatcher.matchEntry()
|
||||
matchEntry()
|
||||
)))
|
||||
.andExpect(jsonPath("$._links.self.href", Matchers.containsString("/api/core/metadataschemas")))
|
||||
.andExpect(jsonPath("$.page.size", is(20)));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findOne() throws Exception {
|
||||
|
||||
@@ -50,7 +75,187 @@ public class MetadataSchemaRestRepositoryIT extends AbstractControllerIntegratio
|
||||
getClient().perform(get("/api/core/metadataschemas/" + metadataSchema.getID()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", is(
|
||||
MetadataschemaMatcher.matchEntry(metadataSchema)
|
||||
matchEntry(metadataSchema)
|
||||
)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSuccess() throws Exception {
|
||||
|
||||
MetadataSchemaRest metadataSchemaRest = new MetadataSchemaRest();
|
||||
metadataSchemaRest.setPrefix(TEST_NAME);
|
||||
metadataSchemaRest.setNamespace(TEST_NAMESPACE);
|
||||
|
||||
String authToken = getAuthToken(admin.getEmail(), password);
|
||||
AtomicReference<Integer> idRef = new AtomicReference<>();
|
||||
|
||||
try {
|
||||
|
||||
assertThat(metadataSchemaService.find(context, TEST_NAME), nullValue());
|
||||
|
||||
getClient(authToken)
|
||||
.perform(post("/api/core/metadataschemas")
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataSchemaRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isCreated())
|
||||
.andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), "$.id")));
|
||||
|
||||
MetadataSchema metadataSchema = metadataSchemaService.find(context, idRef.get());
|
||||
assertThat(metadataSchema, notNullValue());
|
||||
|
||||
assertEquals(metadataSchema.getID(), idRef.get());
|
||||
assertEquals(metadataSchema.getName(), TEST_NAME);
|
||||
assertEquals(metadataSchema.getNamespace(), TEST_NAMESPACE);
|
||||
|
||||
} finally {
|
||||
deleteMetadataSchemaIfExists(TEST_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createUnauthauthorizedTest()
|
||||
throws Exception {
|
||||
|
||||
MetadataSchemaRest metadataSchemaRest = new MetadataSchemaRest();
|
||||
metadataSchemaRest.setPrefix(TEST_NAME);
|
||||
metadataSchemaRest.setNamespace(TEST_NAMESPACE);
|
||||
|
||||
try {
|
||||
getClient()
|
||||
.perform(post("/api/core/metadataschemas")
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataSchemaRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isUnauthorized());
|
||||
|
||||
} finally {
|
||||
deleteMetadataSchemaIfExists(TEST_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteSuccess() throws Exception {
|
||||
|
||||
MetadataSchema metadataSchema = createMetadataSchema();
|
||||
|
||||
try {
|
||||
|
||||
assertThat(metadataSchemaService.find(context, metadataSchema.getID()), notNullValue());
|
||||
|
||||
getClient(getAuthToken(admin.getEmail(), password))
|
||||
.perform(delete("/api/core/metadataschemas/" + metadataSchema.getID()))
|
||||
.andExpect(status().isNoContent());
|
||||
|
||||
assertThat(metadataSchemaService.find(context, metadataSchema.getID()), nullValue());
|
||||
|
||||
} finally {
|
||||
deleteMetadataSchemaIfExists(TEST_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteUnauthorized() throws Exception {
|
||||
|
||||
MetadataSchema metadataSchema = createMetadataSchema();
|
||||
|
||||
try {
|
||||
|
||||
assertThat(metadataSchemaService.find(context, metadataSchema.getID()), notNullValue());
|
||||
|
||||
getClient()
|
||||
.perform(delete("/api/core/metadataschemas/" + metadataSchema.getID()))
|
||||
.andExpect(status().isUnauthorized());
|
||||
|
||||
assertThat(metadataSchemaService.find(context, metadataSchema.getID()), notNullValue());
|
||||
|
||||
} finally {
|
||||
deleteMetadataSchemaIfExists(TEST_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteNonExisting() throws Exception {
|
||||
|
||||
MetadataSchema metadataSchema = createMetadataSchema();
|
||||
deleteMetadataSchemaIfExists(TEST_NAME);
|
||||
|
||||
Integer id = metadataSchema.getID();
|
||||
assertThat(metadataSchemaService.find(context, id), nullValue());
|
||||
|
||||
getClient(getAuthToken(admin.getEmail(), password))
|
||||
.perform(delete("/api/core/metadataschemas/" + id))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
|
||||
MetadataSchema metadataSchema = createMetadataSchema();
|
||||
|
||||
MetadataSchemaRest metadataSchemaRest = new MetadataSchemaRest();
|
||||
metadataSchemaRest.setId(metadataSchema.getID());
|
||||
metadataSchemaRest.setPrefix(TEST_NAME_UPDATED);
|
||||
metadataSchemaRest.setNamespace(TEST_NAMESPACE_UPDATED);
|
||||
|
||||
try {
|
||||
getClient(getAuthToken(admin.getEmail(), password))
|
||||
.perform(put("/api/core/metadataschemas/" + metadataSchema.getID())
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataSchemaRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
metadataSchema = metadataSchemaService.find(context, metadataSchema.getID());
|
||||
|
||||
assertEquals(TEST_NAME_UPDATED, metadataSchema.getName());
|
||||
assertEquals(TEST_NAMESPACE_UPDATED, metadataSchema.getNamespace());
|
||||
} finally {
|
||||
deleteMetadataSchemaIfExists(metadataSchema);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUnauthorized() throws Exception {
|
||||
|
||||
MetadataSchema metadataSchema = createMetadataSchema();
|
||||
|
||||
MetadataSchemaRest metadataSchemaRest = new MetadataSchemaRest();
|
||||
metadataSchemaRest.setId(metadataSchema.getID());
|
||||
metadataSchemaRest.setPrefix(TEST_NAME_UPDATED);
|
||||
metadataSchemaRest.setNamespace(TEST_NAMESPACE_UPDATED);
|
||||
|
||||
try {
|
||||
getClient()
|
||||
.perform(put("/api/core/metadataschemas/" + metadataSchema.getID())
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataSchemaRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isUnauthorized());
|
||||
|
||||
metadataSchema = metadataSchemaService.find(context, metadataSchema.getID());
|
||||
|
||||
assertEquals(TEST_NAME, metadataSchema.getName());
|
||||
assertEquals(TEST_NAMESPACE, metadataSchema.getNamespace());
|
||||
} finally {
|
||||
deleteMetadataSchemaIfExists(metadataSchema);
|
||||
}
|
||||
}
|
||||
|
||||
private MetadataSchema createMetadataSchema() throws SQLException, AuthorizeException, NonUniqueMetadataException {
|
||||
context.turnOffAuthorisationSystem();
|
||||
MetadataSchema metadataSchema = metadataSchemaService.create(context, TEST_NAME, TEST_NAMESPACE);
|
||||
context.commit();
|
||||
return metadataSchema;
|
||||
}
|
||||
|
||||
private void deleteMetadataSchemaIfExists(String name) throws SQLException, AuthorizeException {
|
||||
|
||||
deleteMetadataSchemaIfExists(metadataSchemaService.find(context, name));
|
||||
}
|
||||
|
||||
private void deleteMetadataSchemaIfExists(MetadataSchema metadataSchema) throws SQLException, AuthorizeException {
|
||||
|
||||
if (metadataSchema != null) {
|
||||
context.turnOffAuthorisationSystem();
|
||||
metadataSchemaService.delete(context, metadataSchema);
|
||||
context.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -7,23 +7,62 @@
|
||||
*/
|
||||
package org.dspace.app.rest;
|
||||
|
||||
import static com.jayway.jsonpath.JsonPath.read;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.dspace.app.rest.builder.MetadataFieldBuilder;
|
||||
import org.dspace.app.rest.builder.MetadataSchemaBuilder;
|
||||
import org.dspace.app.rest.matcher.MetadataFieldMatcher;
|
||||
import org.dspace.app.rest.model.MetadataFieldRest;
|
||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.MetadataField;
|
||||
import org.dspace.content.MetadataFieldServiceImpl;
|
||||
import org.dspace.content.MetadataSchema;
|
||||
import org.dspace.content.NonUniqueMetadataException;
|
||||
import org.dspace.content.service.MetadataSchemaService;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class MetadatafieldRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
private static final String ELEMENT = "test element";
|
||||
private static final String QUALIFIER = "test qualifier";
|
||||
private static final String SCOPE_NOTE = "test scope_note";
|
||||
|
||||
private static final String ELEMENT_UPDATED = "test element updated";
|
||||
private static final String QUALIFIER_UPDATED = "test qualifier updated";
|
||||
private static final String SCOPE_NOTE_UPDATED = "test scope_note updated";
|
||||
|
||||
private MetadataSchema metadataSchema;
|
||||
|
||||
@Autowired
|
||||
private MetadataSchemaService metadataSchemaService;
|
||||
|
||||
@Autowired
|
||||
private MetadataFieldServiceImpl metadataFieldService;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
metadataSchema = metadataSchemaService.findAll(context).get(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAll() throws Exception {
|
||||
@@ -66,12 +105,12 @@ public class MetadatafieldRestRepositoryIT extends AbstractControllerIntegration
|
||||
@Test
|
||||
public void searchMethodsExist() throws Exception {
|
||||
getClient().perform(get("/api/core/metadatafields"))
|
||||
.andExpect(jsonPath("$._links.search.href", Matchers.notNullValue()));
|
||||
.andExpect(jsonPath("$._links.search.href", notNullValue()));
|
||||
|
||||
getClient().perform(get("/api/core/metadatafields/search"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(contentType))
|
||||
.andExpect(jsonPath("$._links.bySchema", Matchers.notNullValue()));
|
||||
.andExpect(jsonPath("$._links.bySchema", notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -124,4 +163,190 @@ public class MetadatafieldRestRepositoryIT extends AbstractControllerIntegration
|
||||
.andExpect(status().isUnprocessableEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSuccess() throws Exception {
|
||||
|
||||
MetadataFieldRest metadataFieldRest = new MetadataFieldRest();
|
||||
metadataFieldRest.setElement(ELEMENT);
|
||||
metadataFieldRest.setQualifier(QUALIFIER);
|
||||
metadataFieldRest.setScopeNote(SCOPE_NOTE);
|
||||
|
||||
String authToken = getAuthToken(admin.getEmail(), password);
|
||||
AtomicReference<Integer> idRef = new AtomicReference<>();
|
||||
|
||||
try {
|
||||
assertThat(metadataFieldService.findByElement(context, metadataSchema, ELEMENT, QUALIFIER), nullValue());
|
||||
|
||||
getClient(authToken)
|
||||
.perform(post("/api/core/metadatafields")
|
||||
.param("schemaId", metadataSchema.getID() + "")
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataFieldRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isCreated())
|
||||
.andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), "$.id")));
|
||||
|
||||
MetadataField metadataField = metadataFieldService.find(context, idRef.get());
|
||||
assertThat(metadataField, notNullValue());
|
||||
|
||||
assertEquals(metadataField.getMetadataSchema(), metadataSchema);
|
||||
assertEquals(metadataField.getElement(), ELEMENT);
|
||||
assertEquals(metadataField.getQualifier(), QUALIFIER);
|
||||
assertEquals(metadataField.getScopeNote(), SCOPE_NOTE);
|
||||
|
||||
} finally {
|
||||
deleteMetadataFieldIfExists();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createUnauthauthorized() throws Exception {
|
||||
|
||||
MetadataFieldRest metadataFieldRest = new MetadataFieldRest();
|
||||
metadataFieldRest.setElement(ELEMENT);
|
||||
metadataFieldRest.setQualifier(QUALIFIER);
|
||||
metadataFieldRest.setScopeNote(SCOPE_NOTE);
|
||||
|
||||
try {
|
||||
getClient()
|
||||
.perform(post("/api/core/metadatafields")
|
||||
.param("schemaId", metadataSchema.getID() + "")
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataFieldRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isUnauthorized());
|
||||
} finally {
|
||||
deleteMetadataFieldIfExists();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteSuccess() throws Exception {
|
||||
|
||||
MetadataField metadataField = createMetadataField();
|
||||
|
||||
try {
|
||||
|
||||
assertThat(metadataFieldService.find(context, metadataField.getID()), notNullValue());
|
||||
|
||||
getClient(getAuthToken(admin.getEmail(), password))
|
||||
.perform(delete("/api/core/metadatafields/" + metadataField.getID()))
|
||||
.andExpect(status().isNoContent());
|
||||
|
||||
assertThat(metadataFieldService.find(context, metadataField.getID()), nullValue());
|
||||
|
||||
} finally {
|
||||
deleteMetadataFieldIfExists();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteUnauthorized() throws Exception {
|
||||
|
||||
MetadataField metadataField = createMetadataField();
|
||||
|
||||
try {
|
||||
|
||||
assertThat(metadataFieldService.find(context, metadataField.getID()), notNullValue());
|
||||
|
||||
getClient()
|
||||
.perform(delete("/api/core/metadatafields/" + metadataField.getID()))
|
||||
.andExpect(status().isUnauthorized());
|
||||
|
||||
assertThat(metadataFieldService.find(context, metadataField.getID()), notNullValue());
|
||||
|
||||
} finally {
|
||||
deleteMetadataFieldIfExists();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteNonExisting() throws Exception {
|
||||
|
||||
MetadataField metadataField = createMetadataField();
|
||||
deleteMetadataFieldIfExists();
|
||||
|
||||
Integer id = metadataField.getID();
|
||||
assertThat(metadataFieldService.find(context, id), nullValue());
|
||||
|
||||
getClient(getAuthToken(admin.getEmail(), password))
|
||||
.perform(delete("/api/core/metadatafields/" + id))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void update() throws Exception {
|
||||
|
||||
MetadataField metadataField = createMetadataField();
|
||||
|
||||
MetadataFieldRest metadataFieldRest = new MetadataFieldRest();
|
||||
metadataFieldRest.setId(metadataField.getID());
|
||||
metadataFieldRest.setElement(ELEMENT_UPDATED);
|
||||
metadataFieldRest.setQualifier(QUALIFIER_UPDATED);
|
||||
metadataFieldRest.setScopeNote(SCOPE_NOTE_UPDATED);
|
||||
|
||||
try {
|
||||
getClient(getAuthToken(admin.getEmail(), password))
|
||||
.perform(put("/api/core/metadatafields/" + metadataField.getID())
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataFieldRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
metadataField = metadataFieldService.find(context, metadataField.getID());
|
||||
|
||||
assertEquals(ELEMENT_UPDATED, metadataField.getElement());
|
||||
assertEquals(QUALIFIER_UPDATED, metadataField.getQualifier());
|
||||
assertEquals(SCOPE_NOTE_UPDATED, metadataField.getScopeNote());
|
||||
} finally {
|
||||
deleteMetadataFieldIfExists(metadataField);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUnauthorized() throws Exception {
|
||||
|
||||
MetadataField metadataField = createMetadataField();
|
||||
|
||||
MetadataFieldRest metadataFieldRest = new MetadataFieldRest();
|
||||
metadataFieldRest.setId(metadataField.getID());
|
||||
metadataFieldRest.setElement(ELEMENT_UPDATED);
|
||||
metadataFieldRest.setQualifier(QUALIFIER_UPDATED);
|
||||
metadataFieldRest.setScopeNote(SCOPE_NOTE_UPDATED);
|
||||
|
||||
try {
|
||||
getClient()
|
||||
.perform(put("/api/core/metadatafields/" + metadataField.getID())
|
||||
.content(new ObjectMapper().writeValueAsBytes(metadataFieldRest))
|
||||
.contentType(contentType))
|
||||
.andExpect(status().isUnauthorized());
|
||||
|
||||
metadataField = metadataFieldService.find(context, metadataField.getID());
|
||||
|
||||
assertEquals(ELEMENT, metadataField.getElement());
|
||||
assertEquals(QUALIFIER, metadataField.getQualifier());
|
||||
assertEquals(SCOPE_NOTE, metadataField.getScopeNote());
|
||||
} finally {
|
||||
deleteMetadataFieldIfExists(metadataField);
|
||||
}
|
||||
}
|
||||
|
||||
private MetadataField createMetadataField() throws AuthorizeException, SQLException, NonUniqueMetadataException {
|
||||
context.turnOffAuthorisationSystem();
|
||||
MetadataField metadataField = metadataFieldService.create(
|
||||
context, metadataSchema, ELEMENT, QUALIFIER, SCOPE_NOTE
|
||||
);
|
||||
context.commit();
|
||||
return metadataField;
|
||||
}
|
||||
|
||||
private void deleteMetadataFieldIfExists() throws SQLException, AuthorizeException {
|
||||
|
||||
deleteMetadataFieldIfExists(metadataFieldService.findByElement(context, metadataSchema, ELEMENT, QUALIFIER));
|
||||
}
|
||||
|
||||
private void deleteMetadataFieldIfExists(MetadataField metadataField) throws SQLException, AuthorizeException {
|
||||
if (metadataField != null) {
|
||||
context.turnOffAuthorisationSystem();
|
||||
metadataFieldService.delete(context, metadataField);
|
||||
context.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -29,9 +29,13 @@ public class MetadataschemaMatcher {
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchEntry(MetadataSchema metadataSchema) {
|
||||
return matchEntry(metadataSchema.getName(), metadataSchema.getNamespace());
|
||||
}
|
||||
|
||||
public static Matcher<? super Object> matchEntry(String name, String nameSpace) {
|
||||
return allOf(
|
||||
hasJsonPath("$.prefix", is(metadataSchema.getName())),
|
||||
hasJsonPath("$.namespace", is(metadataSchema.getNamespace())),
|
||||
hasJsonPath("$.prefix", is(name)),
|
||||
hasJsonPath("$.namespace", is(nameSpace)),
|
||||
hasJsonPath("$.type", is("metadataschema")),
|
||||
hasJsonPath("$._links.self.href", Matchers.containsString("/api/core/metadataschemas"))
|
||||
);
|
||||
|
Reference in New Issue
Block a user