Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ufal/be-two-clarin-licenses-are-not-imported #436

Closed
Closed
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 @@ -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;
Expand Down Expand Up @@ -72,6 +73,9 @@ public class ClarinLicenseRestRepository extends DSpaceRestRepository<ClarinLice
@Autowired
ItemService itemService;

@Autowired
ClarinUserRegistrationService clarinUserRegistrationService;

@Override
@PreAuthorize("permitAll()")
public ClarinLicenseRest findOne(Context context, Integer idValue) {
Expand Down Expand Up @@ -155,15 +159,8 @@ protected ClarinLicenseRest createAndReturn(Context context)
"license label cannot be null or empty");
}

List<ClarinUserRegistration> userRegistrations = userRegistrationService.findByEPersonUUID(context,
context.getCurrentUser().getID());
// Do not allow to create a license by the user which doesn't have data in the `user_registration` table
// because that could mean he is not logged in. That is not-logical situation, by theory it shouldn't happen.
if (CollectionUtils.isEmpty(userRegistrations)) {
throw new UnprocessableEntityException("Clarin License user registration, " +
"cannot be null");
}
ClarinUserRegistration userRegistration = userRegistrations.get(0);
// Get user registration record according to current user or create a new record if it doesn't exist yet.
ClarinUserRegistration userRegistration = getUserRegistration(context);

// create
ClarinLicense clarinLicense;
Expand Down Expand Up @@ -273,4 +270,39 @@ private ClarinLicenseLabel getClarinLicenseLabelFromRest(ClarinLicenseLabelRest
return clarinLicenseLabel;
}

/**
* The user is already authenticated, so he exists in the eperson table.
* @param context
* @return
* @throws SQLException
*/
private ClarinUserRegistration getUserRegistration(Context context) throws SQLException, AuthorizeException {
List<ClarinUserRegistration> userRegistrations = userRegistrationService.findByEPersonUUID(context,
context.getCurrentUser().getID());

// Return the user registration record if it exists.
if (!CollectionUtils.isEmpty(userRegistrations)) {
return userRegistrations.get(0);
}

// In some special case after the migration the user could be inserted into the `eperson` table,
// 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.

// 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -73,6 +75,10 @@ public class ClarinLicenseRestRepositoryIT extends AbstractControllerIntegration

@Autowired
ClarinLicenseResourceMappingService clarinLicenseResourceMappingService;

@Autowired
ClarinUserRegistrationService clarinUserRegistrationService;

ClarinLicense firstCLicense;
ClarinLicense secondCLicense;

Expand Down Expand Up @@ -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<Integer> 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<ClarinUserRegistration> 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<ClarinLicenseLabel> clarinLicenseLabelList) {
for (ClarinLicenseLabel clarinLicenseLabel : clarinLicenseLabelList) {
if (clarinLicenseLabel.isExtended()) {
Expand Down