70506: CC license (REST): Patch submission (Remove)

This commit is contained in:
Yana De Pauw
2020-04-22 18:00:43 +02:00
parent 09c0f62561
commit a27b64c88f
4 changed files with 167 additions and 3 deletions

View File

@@ -0,0 +1,50 @@
/**
* 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.submit.factory.impl;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.core.Context;
import org.dspace.license.service.CreativeCommonsService;
import org.dspace.services.model.Request;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Submission "remove" PATCH operation
*
* To remove the Creative Commons License of a workspace item.
*
* Example: <code>
* curl -X PATCH http://${dspace.server.url}/api/submission/workspaceitems/31599 -H "Content-Type:
* application/json" -d '[{ "op": "remove", "path": "/sections/cclicense/uri"}]'
* </code>
*/
public class CCLicenseRemovePatchOperation extends RemovePatchOperation<String> {
@Autowired
CreativeCommonsService creativeCommonsService;
@Override
protected Class<String[]> getArrayClassForEvaluation() {
return String[].class;
}
@Override
protected Class<String> getClassForEvaluation() {
return String.class;
}
@Override
void remove(Context context, Request currentRequest, InProgressSubmission source, String path, Object value)
throws Exception {
Item item = source.getItem();
creativeCommonsService.removeLicense(context, item);
}
}

View File

@@ -81,6 +81,10 @@
<bean <bean
class="org.dspace.app.rest.submit.factory.impl.BitstreamResourcePolicyRemovePatchOperation"/> class="org.dspace.app.rest.submit.factory.impl.BitstreamResourcePolicyRemovePatchOperation"/>
</entry> </entry>
<entry key="cclicense/uri">
<bean
class="org.dspace.app.rest.submit.factory.impl.CCLicenseRemovePatchOperation"/>
</entry>
</map> </map>
</entry> </entry>
<entry key="replace"> <entry key="replace">

View File

@@ -7,7 +7,11 @@
*/ */
package org.dspace.app.rest; package org.dspace.app.rest;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList; import java.util.ArrayList;
@@ -26,7 +30,7 @@ import org.dspace.content.WorkspaceItem;
import org.junit.Test; import org.junit.Test;
/** /**
* Class to the methods from the CCLicenseAddPatchOperation * Class to test the methods from the CCLicenseAddPatchOperation
* Since the CC Licenses are obtained from the CC License API, a mock service has been implemented * Since the CC Licenses are obtained from the CC License API, a mock service has been implemented
* This mock service will return a fixed set of CC Licenses using a similar structure to the ones obtained from the * This mock service will return a fixed set of CC Licenses using a similar structure to the ones obtained from the
* CC License API. * CC License API.
@@ -54,7 +58,8 @@ public class CCLicenseAddPatchOperationIT extends AbstractControllerIntegrationT
String adminToken = getAuthToken(admin.getEmail(), password); String adminToken = getAuthToken(admin.getEmail(), password);
List<Operation> ops = new ArrayList<Operation>(); List<Operation> ops = new ArrayList<Operation>();
AddOperation addOperation = new AddOperation("/sections/cclicense/uri", "license-uri"); AddOperation addOperation = new AddOperation("/sections/cclicense/uri",
"http://creativecommons.org/licenses/by-nc-sa/4.0/");
ops.add(addOperation); ops.add(addOperation);
String patchBody = getPatchContent(ops); String patchBody = getPatchContent(ops);
@@ -63,7 +68,13 @@ public class CCLicenseAddPatchOperationIT extends AbstractControllerIntegrationT
getClient(adminToken).perform(patch("/api/submission/workspaceitems/" + workspaceItem.getID()) getClient(adminToken).perform(patch("/api/submission/workspaceitems/" + workspaceItem.getID())
.content(patchBody) .content(patchBody)
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON)) .contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk()); .andExpect(status().isOk())
.andExpect(jsonPath("$.sections.cclicense", allOf(
hasJsonPath("$.uri", is("http://creativecommons.org/licenses/by-nc-sa/4.0/")),
hasJsonPath("$.rights",
is("Attribution-NonCommercial-ShareAlike 4.0 International")),
hasJsonPath("$.file.name", is("license_rdf"))
)));
} }
@Test @Test

View File

@@ -0,0 +1,99 @@
/**
* 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.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.MediaType;
import org.dspace.app.rest.builder.CollectionBuilder;
import org.dspace.app.rest.builder.CommunityBuilder;
import org.dspace.app.rest.builder.WorkspaceItemBuilder;
import org.dspace.app.rest.model.patch.AddOperation;
import org.dspace.app.rest.model.patch.Operation;
import org.dspace.app.rest.model.patch.RemoveOperation;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.WorkspaceItem;
import org.junit.Test;
/**
* Class to test the methods from the CCLicenseRemovePatchOperation
* Since the CC Licenses are obtained from the CC License API, a mock service has been implemented
* This mock service will return a fixed set of CC Licenses using a similar structure to the ones obtained from the
* CC License API.
* Refer to {@link org.dspace.license.MockCCLicenseConnectorServiceImpl} for more information
*/
public class CCLicenseRemovePatchOperationIT extends AbstractControllerIntegrationTest {
@Test
public void patchRemoveSubmissionCCLicense() throws Exception {
context.turnOffAuthorisationSystem();
Community community = CommunityBuilder.createCommunity(context)
.withName("Community")
.build();
Collection collection = CollectionBuilder.createCollection(context, community)
.withName("Collection")
.build();
WorkspaceItem workspaceItem = WorkspaceItemBuilder.createWorkspaceItem(context, collection)
.withTitle("Workspace Item")
.build();
String adminToken = getAuthToken(admin.getEmail(), password);
// First add a license and verify it is added
List<Operation> ops = new ArrayList<Operation>();
AddOperation addOperation = new AddOperation("/sections/cclicense/uri",
"http://creativecommons.org/licenses/by-nc-sa/4.0/");
ops.add(addOperation);
String patchBody = getPatchContent(ops);
getClient(adminToken).perform(patch("/api/submission/workspaceitems/" + workspaceItem.getID())
.content(patchBody)
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.sections.cclicense", allOf(
hasJsonPath("$.uri", is("http://creativecommons.org/licenses/by-nc-sa/4.0/")),
hasJsonPath("$.rights",
is("Attribution-NonCommercial-ShareAlike 4.0 International")),
hasJsonPath("$.file.name", is("license_rdf"))
)));
// Remove the license again and verify it is removed
List<Operation> removeOps = new ArrayList<Operation>();
RemoveOperation removeOperation = new RemoveOperation("/sections/cclicense/uri");
removeOps.add(removeOperation);
String removePatch = getPatchContent(removeOps);
getClient(adminToken).perform(patch("/api/submission/workspaceitems/" + workspaceItem.getID())
.content(removePatch)
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.sections", not(hasJsonPath("cclicense"))));
}
}