diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java index f9762913a5c1..807c3fd5f282 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseRestRepository.java @@ -41,6 +41,7 @@ import org.dspace.content.service.clarin.ClarinLicenseService; import org.dspace.content.service.clarin.ClarinUserRegistrationService; import org.dspace.core.Context; +import org.dspace.eperson.EPerson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -72,6 +73,9 @@ public class ClarinLicenseRestRepository extends DSpaceRestRepository userRegistrations = userRegistrationService.findByEPersonUUID(context, context.getCurrentUser().getID()); @@ -275,11 +286,23 @@ private ClarinUserRegistration getUserRegistration(Context context) throws SQLEx } // In some special case after the migration the user could be inserted into the `eperson` table, - // but it is not in the `user_registration` table. Maybe the user was signed in via Shibboleth. + // but it is not inserted in the `user_registration` table. Maybe the user was signed in via Shibboleth. // Create a new ClarinUserRegistration record in this case. - throw new UnprocessableEntityException("Clarin License user registration, " + - "cannot be null"); -// ClarinUserRegistration userRegistration = userRegistrations.get(0); + + // Get current user + EPerson ePerson = context.getCurrentUser(); + if (Objects.isNull(ePerson)) { + throw new UnprocessableEntityException("Cannot create a ClarinUserRegistration because the " + + "current user from the context is null."); + } + + ClarinUserRegistration clarinUserRegistration = new ClarinUserRegistration(); + clarinUserRegistration.setConfirmation(true); + clarinUserRegistration.setEmail(ePerson.getEmail()); + clarinUserRegistration.setPersonID(ePerson.getID()); + clarinUserRegistration.setOrganization(ePerson.getNetid()); + clarinUserRegistrationService.create(context, clarinUserRegistration); + return clarinUserRegistration; } } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java index 7f07464b456a..eb3f574eec02 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseRestRepositoryIT.java @@ -49,11 +49,13 @@ import org.dspace.content.service.clarin.ClarinLicenseLabelService; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; +import org.dspace.content.service.clarin.ClarinUserRegistrationService; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.CollectionUtils; /** * Integration tests for the Clarin License Rest Repository @@ -73,6 +75,10 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration @Autowired ClarinLicenseResourceMappingService clarinLicenseResourceMappingService; + + @Autowired + ClarinUserRegistrationService clarinUserRegistrationService; + ClarinLicense firstCLicense; ClarinLicense secondCLicense; @@ -494,6 +500,73 @@ public void findAllBitstreamsAttachedToLicense() throws Exception { .andExpect(jsonPath("$.bitstreams", is(2))); } + @Test + public void createUserRegistrationIfItDoesntExist() throws Exception { + context.setCurrentUser(admin); + ClarinLicenseRest clarinLicenseRest = new ClarinLicenseRest(); + clarinLicenseRest.setName("name"); + clarinLicenseRest.setBitstreams(0); + clarinLicenseRest.setConfirmation(4); + clarinLicenseRest.setRequiredInfo("Not required"); + clarinLicenseRest.setDefinition("definition"); + clarinLicenseConverter.setExtendedClarinLicenseLabels(clarinLicenseRest, firstCLicense.getLicenseLabels(), + Projection.DEFAULT); + clarinLicenseConverter.setClarinLicenseLabel(clarinLicenseRest, firstCLicense.getLicenseLabels(), + Projection.DEFAULT); + // id of created clarin license + AtomicReference idRef = new AtomicReference<>(); + String authTokenAdmin = getAuthToken(admin.getEmail(), password); + try { + getClient(authTokenAdmin).perform(post("/api/core/clarinlicenses") + .content(new ObjectMapper().writeValueAsBytes(clarinLicenseRest)) + .contentType(org.springframework.http.MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.name", is(clarinLicenseRest.getName()))) + .andExpect(jsonPath("$.definition", + is(clarinLicenseRest.getDefinition()))) + .andExpect(jsonPath("$.confirmation", + is(clarinLicenseRest.getConfirmation()))) + .andExpect(jsonPath("$.requiredInfo", + is(clarinLicenseRest.getRequiredInfo()))) + .andExpect(jsonPath("$.bitstreams", + is(clarinLicenseRest.getBitstreams()))) + .andExpect(jsonPath("$.type", + is(ClarinLicenseRest.NAME))) + + .andExpect(jsonPath("$.clarinLicenseLabel.label", + is(clarinLicenseRest.getClarinLicenseLabel().getLabel()))) + .andExpect(jsonPath("$.clarinLicenseLabel.title", + is(clarinLicenseRest.getClarinLicenseLabel().getTitle()))) + .andExpect(jsonPath("$.clarinLicenseLabel.extended", + is(clarinLicenseRest.getClarinLicenseLabel().isExtended()))) + .andExpect(jsonPath("$.clarinLicenseLabel.type", + is(ClarinLicenseLabelRest.NAME))) + + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].label", + is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).getLabel()))) + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].title", + is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).getTitle()))) + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].extended", + is(clarinLicenseRest.getExtendedClarinLicenseLabels().get(0).isExtended()))) + .andExpect(jsonPath("$.extendedClarinLicenseLabels[0].type", + is(ClarinLicenseLabelRest.NAME))) + .andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), + "$.id"))); + } finally { + if (Objects.nonNull(idRef.get())) { + // remove created clarin license + ClarinLicenseBuilder.deleteClarinLicense(idRef.get()); + } + } + + List userRegistrations = clarinUserRegistrationService.findByEPersonUUID(context, + context.getCurrentUser().getID()); + + Assert.assertFalse(CollectionUtils.isEmpty(userRegistrations)); + // Remove the user registration record - it was created in the `getUserRegistration` method. + ClarinUserMetadataBuilder.deleteClarinUserMetadata(userRegistrations.get(0).getID()); + } + private ClarinLicenseLabel getNonExtendedLicenseLabel(List clarinLicenseLabelList) { for (ClarinLicenseLabel clarinLicenseLabel : clarinLicenseLabelList) { if (clarinLicenseLabel.isExtended()) {