Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.dspace.app.rest.model.BitstreamRest;
import org.dspace.app.rest.model.ClarinUserMetadataRest;
Expand Down Expand Up @@ -95,19 +94,6 @@ public ResponseEntity manageUserMetadata(@RequestParam("bitstreamUUID") UUID bit
return null;
}

// Get ClarinUserMetadataRest Array from the request body
ClarinUserMetadataRest[] clarinUserMetadataRestArray =
new ObjectMapper().readValue(request.getInputStream(), ClarinUserMetadataRest[].class);
if (ArrayUtils.isEmpty(clarinUserMetadataRestArray)) {
return null;
}

// Convert Array to the List
List<ClarinUserMetadataRest> clarinUserMetadataRestList = Arrays.asList(clarinUserMetadataRestArray);
if (CollectionUtils.isEmpty(clarinUserMetadataRestList)) {
return null;
}

// Get mapping between clarin license and the bitstream
ClarinLicenseResourceMapping clarinLicenseResourceMapping =
this.getLicenseResourceMapping(context, bitstreamUUID);
Expand All @@ -116,6 +102,17 @@ public ResponseEntity manageUserMetadata(@RequestParam("bitstreamUUID") UUID bit
" and the bitstream");
}

// Get ClarinUserMetadataRest Array from the request body
ClarinUserMetadataRest[] clarinUserMetadataRestArray =
new ObjectMapper().readValue(request.getInputStream(), ClarinUserMetadataRest[].class);
if (Objects.isNull(clarinUserMetadataRestArray)) {
throw new RuntimeException("The clarinUserMetadataRestArray cannot be null. It could be empty, but" +
" not null");
}

// Convert Array to the List
List<ClarinUserMetadataRest> clarinUserMetadataRestList = Arrays.asList(clarinUserMetadataRestArray);

// Get current user from the context to find out if the user is signed in
EPerson currentUser = context.getCurrentUser();
String downloadToken = this.generateToken();
Expand Down Expand Up @@ -359,6 +356,9 @@ private boolean shouldEmailToken(Context context, UUID bitstreamUUID,
}

// If the required info contains the key work `SEND_TOKEN` it should generate the token.
if (StringUtils.isBlank(clarinLicense.getRequiredInfo())) {
return false;
}
return clarinLicense.getRequiredInfo().contains(SEND_TOKEN);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
package org.dspace.app.rest.repository;

import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.CollectionUtils;
import org.dspace.app.rest.model.ClarinUserMetadataRest;
import org.dspace.app.rest.model.ClarinUserRegistrationRest;
import org.dspace.app.rest.projection.Projection;
import org.dspace.content.clarin.ClarinUserMetadata;
import org.dspace.content.clarin.ClarinUserRegistration;
import org.dspace.content.service.clarin.ClarinUserRegistrationService;
import org.dspace.core.Context;
Expand Down Expand Up @@ -47,12 +44,6 @@ public Page<ClarinUserMetadataRest> getUserMetadata(@Nullable HttpServletRequest
" couldn't be found");
}

List<ClarinUserMetadata> clarinUserMetadata = clarinUserRegistration.getUserMetadata();
if (CollectionUtils.isEmpty(clarinUserMetadata)) {
throw new ResourceNotFoundException("The ClarinUserMetadata for ClarinLicenseResourceUserAllowance " +
"with id: " + userRegistrationID + "doesn't exists.");
}

return converter.toRestPage(clarinUserMetadata, optionalPageable, projection);
return converter.toRestPage(clarinUserRegistration.getUserMetadata(), optionalPageable, projection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class ClarinUserMetadataRestControllerIT extends AbstractControllerIntegr
Bitstream bitstream;

// Attach ClarinLicense to the Bitstream
private void prepareEnvironment(String requiredInfo) throws Exception {
private void prepareEnvironment(String requiredInfo, Integer confirmation) throws Exception {
// 1. Create Workspace Item with uploaded file
// 2. Create Clarin License
// 3. Send request to add Clarin License to the Workspace Item
Expand All @@ -79,7 +79,7 @@ private void prepareEnvironment(String requiredInfo) throws Exception {
String clarinLicenseName = "Test Clarin License";

// 2. Create clarin license with clarin license label
clarinLicense = createClarinLicense(clarinLicenseName, "Test Def", requiredInfo, 0);
clarinLicense = createClarinLicense(clarinLicenseName, "Test Def", requiredInfo, confirmation);

// creating replace operation
Map<String, String> licenseReplaceOpValue = new HashMap<String, String>();
Expand Down Expand Up @@ -110,7 +110,7 @@ private void prepareEnvironment(String requiredInfo) throws Exception {

@Test
public void notAuthorizedUser_shouldReturnToken() throws Exception {
this.prepareEnvironment("NAME");
this.prepareEnvironment("NAME", 0);
ObjectMapper mapper = new ObjectMapper();
ClarinUserMetadataRest clarinUserMetadata1 = new ClarinUserMetadataRest();
clarinUserMetadata1.setMetadataKey("NAME");
Expand Down Expand Up @@ -142,7 +142,7 @@ public void notAuthorizedUser_shouldReturnToken() throws Exception {

@Test
public void notAuthorizedUser_shouldSendEmail() throws Exception {
this.prepareEnvironment("SEND_TOKEN");
this.prepareEnvironment("SEND_TOKEN", 0);
ObjectMapper mapper = new ObjectMapper();
ClarinUserMetadataRest clarinUserMetadata1 = new ClarinUserMetadataRest();
clarinUserMetadata1.setMetadataKey("NAME");
Expand Down Expand Up @@ -179,7 +179,7 @@ public void notAuthorizedUser_shouldSendEmail() throws Exception {

@Test
public void authorizedUserWithoutMetadata_shouldReturnToken() throws Exception {
this.prepareEnvironment("NAME");
this.prepareEnvironment("NAME", 0);
context.turnOffAuthorisationSystem();
ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder
.createClarinUserRegistration(context).withEPersonID(admin.getID()).build();
Expand Down Expand Up @@ -224,7 +224,7 @@ public void authorizedUserWithoutMetadata_shouldReturnToken() throws Exception {

@Test
public void authorizedUserWithoutMetadata_shouldSendEmail() throws Exception {
this.prepareEnvironment("SEND_TOKEN");
this.prepareEnvironment("SEND_TOKEN", 0);
context.turnOffAuthorisationSystem();
ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder
.createClarinUserRegistration(context).withEPersonID(admin.getID()).build();
Expand Down Expand Up @@ -274,7 +274,7 @@ public void authorizedUserWithoutMetadata_shouldSendEmail() throws Exception {

@Test
public void authorizedUserWithMetadata_shouldSendToken() throws Exception {
this.prepareEnvironment("NAME,ADDRESS");
this.prepareEnvironment("NAME,ADDRESS", 0);
context.turnOffAuthorisationSystem();
ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder
.createClarinUserRegistration(context).withEPersonID(admin.getID()).build();
Expand Down Expand Up @@ -323,7 +323,7 @@ public void authorizedUserWithMetadata_shouldSendToken() throws Exception {

@Test
public void authorizedUserWithMetadata_shouldSendEmail() throws Exception {
this.prepareEnvironment("SEND_TOKEN,NAME,ADDRESS");
this.prepareEnvironment("SEND_TOKEN,NAME,ADDRESS", 0);
context.turnOffAuthorisationSystem();
ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder
.createClarinUserRegistration(context).withEPersonID(admin.getID()).build();
Expand Down Expand Up @@ -375,6 +375,39 @@ public void authorizedUserWithMetadata_shouldSendEmail() throws Exception {
ClarinUserMetadataBuilder.deleteClarinUserMetadata(clarinUserRegistration.getID());
}

// Confirmation = 1
@Test
public void authorizedUserWithoutMetadata_shouldDownloadToken() throws Exception {
this.prepareEnvironment(null, 1);
context.turnOffAuthorisationSystem();
ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder
.createClarinUserRegistration(context).withEPersonID(admin.getID()).build();
context.restoreAuthSystemState();
ObjectMapper mapper = new ObjectMapper();

String adminToken = getAuthToken(admin.getEmail(), password);

// There should exist record in the UserRegistration table
getClient(adminToken).perform(get("/api/core/clarinuserregistrations")
.contentType(contentType))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page.totalElements", is(1)));

// Manage UserMetadata and get token
getClient(adminToken).perform(post("/api/core/clarinusermetadata/manage?bitstreamUUID=" + bitstream.getID())
.content(mapper.writeValueAsBytes(new ArrayList<ClarinUserMetadataRest>(0)))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$", not(CHECK_EMAIL_RESPONSE_CONTENT)));

// Get created CLRUA
getClient(adminToken).perform(get("/api/core/clarinlruallowances")
.contentType(contentType))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page.totalElements", is(1)));
}

private WorkspaceItem createWorkspaceItemWithFile() {
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
Expand Down Expand Up @@ -414,6 +447,7 @@ private ClarinLicenseLabel createClarinLicenseLabel(String label, boolean extend
return clarinLicenseLabel;
}


/**
* Create ClarinLicense object with ClarinLicenseLabel object for testing purposes.
*/
Expand Down