diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinLicenseImportController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinLicenseImportController.java new file mode 100644 index 000000000000..b9a8344af145 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinLicenseImportController.java @@ -0,0 +1,78 @@ +/** + * 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 org.dspace.app.rest.utils.ContextUtil.obtainContext; + +import java.sql.SQLException; +import java.util.Objects; +import java.util.UUID; +import javax.servlet.http.HttpServletRequest; + +import org.apache.logging.log4j.Logger; +import org.dspace.app.rest.model.ClarinLicenseRest; +import org.dspace.app.rest.repository.ClarinLicenseRestRepository; +import org.dspace.authorize.AuthorizeException; +import org.dspace.core.Context; +import org.dspace.eperson.EPerson; +import org.dspace.eperson.service.EPersonService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * Controller for import licenses into database. + * Endpoint: /api/licenses/import/{value} + * This controller can: + * - import labels in json format into database (POST /api/licenses/import/labels) + * - import extended mapping in json format - create mapped dictionary (POST /api/licenses/import/extendedMapping) + * - import licenses in json format into database (POST /api/licenses/import/licenses) + * + * @author Michaela Paurikova (michaela.paurikova at dataquest.sk) + */ +@RestController +@RequestMapping("/api/clarin/import") +public class ClarinLicenseImportController { + private static final Logger log = org.apache.logging.log4j.LogManager + .getLogger(ClarinLicenseImportController.class); + + @Autowired + private EPersonService ePersonService; + @Autowired + private ClarinLicenseRestRepository clarinLicenseRestRepository; + + @PreAuthorize("hasAuthority('ADMIN')") + @RequestMapping(method = RequestMethod.POST, path = "/license") + public ClarinLicenseRest importLicense(HttpServletRequest request) + throws AuthorizeException, SQLException { + Context context = obtainContext(request); + if (Objects.isNull(context)) { + throw new RuntimeException("Context is null!"); + } + //get param + UUID epersonUUID = UUID.fromString(request.getParameter("eperson")); + EPerson ePerson = ePersonService.find(context, epersonUUID); + + //set current user to eperson and create license + EPerson currUser = context.getCurrentUser(); + context.setCurrentUser(ePerson); + //turn off authorization + context.turnOffAuthorisationSystem(); + ClarinLicenseRest clarinLicenseRest = clarinLicenseRestRepository.createAndReturn(); + context.restoreAuthSystemState(); + //set back current use + context.setCurrentUser(currUser); + + context.complete(); + return clarinLicenseRest; + } + + +} \ No newline at end of file diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinLicenseImportRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinLicenseImportRestController.java deleted file mode 100644 index e0e925219645..000000000000 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ClarinLicenseImportRestController.java +++ /dev/null @@ -1,327 +0,0 @@ -/** - * 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 org.dspace.app.rest.utils.ContextUtil.obtainContext; - -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Dictionary; -import java.util.HashSet; -import java.util.Hashtable; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.BadRequestException; - -import com.fasterxml.jackson.databind.JsonNode; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.logging.log4j.Logger; -import org.dspace.authorize.AuthorizeException; -import org.dspace.content.clarin.ClarinLicense; -import org.dspace.content.clarin.ClarinLicenseLabel; -import org.dspace.content.clarin.ClarinUserRegistration; -import org.dspace.content.service.clarin.ClarinLicenseLabelService; -import org.dspace.content.service.clarin.ClarinLicenseService; -import org.dspace.content.service.clarin.ClarinUserRegistrationService; -import org.dspace.core.Context; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -/** - * Controller for import licenses into database. - * Endpoint: /api/licenses/import/{value} - * This controller can: - * - import labels in json format into database (POST /api/licenses/import/labels) - * - import extended mapping in json format - create mapped dictionary (POST /api/licenses/import/extendedMapping) - * - import licenses in json format into database (POST /api/licenses/import/licenses) - * - * @author Michaela Paurikova (michaela.paurikova at dataquest.sk) - */ -@RestController -@RequestMapping("/api/licenses/import") -public class ClarinLicenseImportRestController { - private static final Logger log = org.apache.logging.log4j.LogManager - .getLogger(ClarinLicenseImportRestController.class); - private Dictionary licenseLabelsIds = new Hashtable<>(); - private Dictionary> licenseToLicenseLabel = new Hashtable<>(); - @Autowired - private ClarinLicenseLabelService clarinLicenseLabelService; - @Autowired - private ClarinLicenseService clarinLicenseService; - @Autowired - private ClarinUserRegistrationService clarinUserRegistrationService; - - /** - * This method import labels in json format into database. - * - * @param licenseLabels Array of json nodes - * @param request The response object - * @param request The request object - * @return Response entity with status - * @throws SQLException - * @throws AuthorizeException - */ - @RequestMapping(method = RequestMethod.POST, value = "/labels") - @PreAuthorize("hasAuthority('ADMIN')") - public ResponseEntity importLicenseLabels(@RequestBody(required = false) List licenseLabels, - HttpServletRequest request, HttpServletResponse response) - throws SQLException, AuthorizeException { - - if (Objects.isNull(licenseLabels) || CollectionUtils.isEmpty(licenseLabels)) { - throw new BadRequestException("The new license labels should be included as " + - "json in the body of this request"); - } - - Context context = obtainContext(request); - if (Objects.isNull(context)) { - return new ResponseEntity<>("Context is null", HttpStatus.INTERNAL_SERVER_ERROR); - } - - List errors = new ArrayList<>(); - ClarinLicenseLabel licenseLabel; - - for (JsonNode jsonLicenseLabel : licenseLabels) { - if (jsonLicenseLabel.has("label_id") && !jsonLicenseLabel.get("label_id").isNull()) { - if (jsonLicenseLabel.has("label") && jsonLicenseLabel.has("title") - && jsonLicenseLabel.has("is_extended")) { - - Integer id = jsonLicenseLabel.get("label_id").asInt(); - String label = jsonLicenseLabel.get("label").isNull() ? - null : jsonLicenseLabel.get("label").asText(); - String title = jsonLicenseLabel.get("title").isNull() ? - null : jsonLicenseLabel.get("title").asText(); - boolean is_extended = jsonLicenseLabel.get("is_extended").asBoolean(); - // create - licenseLabel = clarinLicenseLabelService.create(context); - licenseLabel.setLabel(label); - licenseLabel.setTitle(title); - licenseLabel.setExtended(is_extended); - - clarinLicenseLabelService.update(context, licenseLabel); - this.licenseLabelsIds.put(id, licenseLabel.getID()); - } else { - //if any argument is missing, we create log and move to the next label - errors.add(jsonLicenseLabel.get("label_id").asInt()); - } - } else { - return new ResponseEntity<>("Label id has to be entered and it cannot be null!", - HttpStatus.UNPROCESSABLE_ENTITY); - } - } - - context.commit(); - - if (errors.isEmpty()) { - return new ResponseEntity<>("Import License labels were successful", HttpStatus.OK); - } - - for (Integer id: errors) { - log.warn("The license label with id: " + id + " had incorrect inputted arguments!"); - } - return new ResponseEntity<>("License label extended mappings were imported partially!", - HttpStatus.CONFLICT); - } - - /** - * This method mapping extended mappings into dictionary. - * - * @param licenseLabelExtendedMappings Array of json nodes - * @param request The response object - * @param request The request object - * @return Response entity with status - */ - @RequestMapping(method = RequestMethod.POST, value = "/extendedMapping") - @PreAuthorize("hasAuthority('ADMIN')") - public ResponseEntity importLicenseLabelExtendedMapping(@RequestBody(required = false) List - licenseLabelExtendedMappings, - HttpServletRequest request, HttpServletResponse response) { - - if (Objects.isNull(licenseLabelExtendedMappings) || CollectionUtils.isEmpty(licenseLabelExtendedMappings)) { - throw new BadRequestException("The new license label extended mappings should be included as " + - "json in the body of this request"); - } - - Context context = obtainContext(request); - if (Objects.isNull(context)) { - return new ResponseEntity<>("Context is null", HttpStatus.INTERNAL_SERVER_ERROR); - } - - List errors = new ArrayList<>(); - - for (JsonNode jsonLicenseLabelExtendedMapping : licenseLabelExtendedMappings) { - if (jsonLicenseLabelExtendedMapping.has("license_id") && - !jsonLicenseLabelExtendedMapping.get("license_id").isNull() && - jsonLicenseLabelExtendedMapping.has("label_id") && - !jsonLicenseLabelExtendedMapping.get("label_id").isNull()) { - - Set licenseLabels = this.licenseToLicenseLabel.get( - jsonLicenseLabelExtendedMapping.get("license_id").asInt()); - if (Objects.isNull(licenseLabels)) { - licenseLabels = new HashSet<>(); - this.licenseToLicenseLabel.put(jsonLicenseLabelExtendedMapping.get( - "license_id").asInt(), licenseLabels); - } - ClarinLicenseLabel clarinLicenseLabel; - try { - Integer licenseLabelID = this.licenseLabelsIds.get(jsonLicenseLabelExtendedMapping.get( - "label_id").asInt()); - if (Objects.isNull(licenseLabelID)) { - //if label_id doesn't exist, we create log and move to the next extended mapping - errors.add(jsonLicenseLabelExtendedMapping.get("label_id").asInt()); - continue; - } - clarinLicenseLabel = clarinLicenseLabelService.find(context,licenseLabelID); - if (Objects.isNull(clarinLicenseLabel)) { - //if label_id doesn't exist, we create log and move to the next extended mapping - errors.add(licenseLabelID); - continue; - } - licenseLabels.add(clarinLicenseLabel); - } catch (SQLException e) { - e.printStackTrace(); - } - } else { - return new ResponseEntity<>("Label id and license id have to be entered and it cannot be null!", - HttpStatus.UNPROCESSABLE_ENTITY); - } - } - - if (errors.isEmpty()) { - return new ResponseEntity<>("Import License label extended mappings were successful!", HttpStatus.OK); - } - - for (Integer id: errors) { - log.warn("The extended mapping with label id: " + id + " was not imported! There was not find " + - "corresponded label in database!"); - } - return new ResponseEntity<>("License label extended mappings were imported partially!", - HttpStatus.CONFLICT); - } - - /** - * This method import licenses into database. - * - * @param licenses Array of json nodes - * @param request The response object - * @param request The request object - * @return Response entity with status - * @throws SQLException - * @throws AuthorizeException - */ - @RequestMapping(method = RequestMethod.POST, value = "/licenses") - @PreAuthorize("hasAuthority('ADMIN')") - public ResponseEntity importLicenses(@RequestBody(required = false) List licenses, - HttpServletRequest request, HttpServletResponse response) - throws SQLException, AuthorizeException { - - if (Objects.isNull(licenses) || CollectionUtils.isEmpty(licenses)) { - throw new BadRequestException("The new licenses should be included as json in the body of this request"); - } - - Context context = obtainContext(request); - if (Objects.isNull(context)) { - return new ResponseEntity<>("Context is null", HttpStatus.INTERNAL_SERVER_ERROR); - } - - ClarinLicense license; - List errors = new ArrayList<>(); - - for (JsonNode jsonLicense : licenses) { - if (jsonLicense.has("license_id") && !jsonLicense.get("license_id").isNull()) { - if (jsonLicense.has("name") && jsonLicense.has("definition") - //&& jsonLicense.has("eperson_id") - && jsonLicense.has("label_id") && - jsonLicense.has("confirmation") && jsonLicense.has("required_info")) { - - Integer id = jsonLicense.get("license_id").asInt(); - //the name has to be unique too - String name = jsonLicense.get("name").isNull() ? null : jsonLicense.get("name").asText(); - String definition = jsonLicense.get("definition").isNull() ? - null : jsonLicense.get("definition").asText(); - Integer label_id = jsonLicense.get("label_id").isNull() ? - null : jsonLicense.get("label_id").asInt(); - Integer confirmation = jsonLicense.get("confirmation").isNull() ? - null : jsonLicense.get("confirmation").asInt(); - String required_info = jsonLicense.get("required_info").isNull() ? - null : jsonLicense.get("required_info").asText(); - //eperson_id is only require if the license iswas reated by an andmin and not imported - String epersonIdString = jsonLicense.get("eperson_id").asText(); - UUID epersonId = UUID.fromString(epersonIdString); - List userRegistrations = clarinUserRegistrationService.findByEPersonUUID( - context, epersonId); - ClarinUserRegistration userRegistration = userRegistrations.size() > 0 ? - userRegistrations.get(0) : null; - //TODO - //String createdOnString = jsonLicense.get("created_on").asText(); - - if (Objects.nonNull(clarinLicenseService.findByName(context, name))) { - errors.add(label_id); - continue; - } - - ClarinLicenseLabel label = null; - if (Objects.nonNull(label_id) && Objects.nonNull(this.licenseLabelsIds.get(label_id))) { - label = this.clarinLicenseLabelService.find(context, this.licenseLabelsIds.get(label_id)); - label_id = this.licenseLabelsIds.get(label_id); - } - - if (Objects.isNull(label_id) || Objects.isNull(label)) { - //if label_id doesn't exist, we create log and move to the next extended mapping - errors.add(label_id); - continue; - } - - Set licenseLabels = this.licenseToLicenseLabel.get(id); - if (Objects.isNull(licenseLabels)) { - licenseLabels = new HashSet<>(); - } - licenseLabels.add(label); - - license = clarinLicenseService.create(context); - license.setName(name); - license.setLicenseLabels(licenseLabels); - license.setDefinition(definition); - license.setEperson(userRegistration); - license.setConfirmation(confirmation); - license.setRequiredInfo(required_info); - - clarinLicenseService.update(context, license); - } else { - //if any argument is missing, we create log and move to the next label - errors.add(jsonLicense.get("license_id").asInt()); - } - } else { - return new ResponseEntity<>("License id has to be entered and it cannot be null!" + - " Name has to be unique!", - HttpStatus.UNPROCESSABLE_ENTITY); - } - } - - context.commit(); - - if (errors.isEmpty()) { - return new ResponseEntity<>("Import licenses were successful", HttpStatus.OK); - } - for (Integer id: errors) { - log.warn("The license with label, which is mapping to label id: " + id + " was not imported! " + - "There is not corresponded label in database!"); - } - return new ResponseEntity<>("License label extended mappings were imported partially!", - HttpStatus.CONFLICT); - } -} \ No newline at end of file diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java index f3c928d75bfb..061ef5807529 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinLicenseLabelRestRepository.java @@ -15,7 +15,6 @@ import java.util.Objects; import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.lang3.ArrayUtils; import org.dspace.app.rest.exception.DSpaceBadRequestException; import org.dspace.app.rest.exception.UnprocessableEntityException; import org.dspace.app.rest.model.ClarinLicenseLabelRest; @@ -82,8 +81,7 @@ protected ClarinLicenseLabelRest createAndReturn(Context context) } // validate fields - if (isBlank(clarinLicenseLabelRest.getLabel()) || isBlank(clarinLicenseLabelRest.getTitle()) || - ArrayUtils.isEmpty(clarinLicenseLabelRest.getIcon())) { + if (isBlank(clarinLicenseLabelRest.getLabel()) || isBlank(clarinLicenseLabelRest.getTitle())) { throw new UnprocessableEntityException("CLARIN License Label title, label, icon cannot be null or empty"); } 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 8056454cc4aa..2ef00709a512 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 @@ -35,9 +35,11 @@ import org.dspace.content.WorkspaceItem; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.clarin.ClarinUserRegistration; import org.dspace.content.service.ItemService; import org.dspace.content.service.WorkspaceItemService; import org.dspace.content.service.clarin.ClarinLicenseService; +import org.dspace.content.service.clarin.ClarinUserRegistrationService; import org.dspace.core.Context; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -61,6 +63,8 @@ public class ClarinLicenseRestRepository extends DSpaceRestRepository 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); + // create ClarinLicense clarinLicense; clarinLicense = clarinLicenseService.create(context); @@ -160,6 +174,7 @@ protected ClarinLicenseRest createAndReturn(Context context) clarinLicense.setDefinition(clarinLicenseRest.getDefinition()); clarinLicense.setConfirmation(clarinLicenseRest.getConfirmation()); clarinLicense.setRequiredInfo(clarinLicenseRest.getRequiredInfo()); + clarinLicense.setEperson(userRegistration); clarinLicenseService.update(context, clarinLicense); // return diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseImportControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseImportControllerIT.java index 0b9d38d3dd7b..92ee88fa21fb 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseImportControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseImportControllerIT.java @@ -7,38 +7,35 @@ */ package org.dspace.app.rest; +import static com.jayway.jsonpath.JsonPath.read; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import java.io.BufferedReader; -import java.io.FileReader; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Dictionary; import java.util.HashSet; -import java.util.Hashtable; -import java.util.List; import java.util.Objects; -import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.JsonNodeFactory; -import com.fasterxml.jackson.databind.node.ObjectNode; import org.dspace.app.rest.converter.ClarinLicenseConverter; import org.dspace.app.rest.converter.ClarinLicenseLabelConverter; +import org.dspace.app.rest.model.ClarinLicenseLabelRest; +import org.dspace.app.rest.model.ClarinLicenseRest; +import org.dspace.app.rest.projection.Projection; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; -import org.dspace.authorize.AuthorizeException; +import org.dspace.builder.ClarinLicenseBuilder; +import org.dspace.builder.ClarinLicenseLabelBuilder; import org.dspace.builder.ClarinUserRegistrationBuilder; +import org.dspace.builder.EPersonBuilder; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; import org.dspace.content.clarin.ClarinUserRegistration; import org.dspace.content.service.clarin.ClarinLicenseLabelService; import org.dspace.content.service.clarin.ClarinLicenseService; -import org.dspace.core.Context; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.junit.Assert; +import org.dspace.eperson.EPerson; +import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -50,20 +47,6 @@ */ public class ClarinLicenseImportControllerIT extends AbstractControllerIntegrationTest { - private final static String LICENSE_LABELS = "test/json_data_import_licenses/jm.license_label.json"; - private final static String EXTENDED_MAPPINGS = - "test/json_data_import_licenses/jm.license_label_extended_mapping.json"; - private final static String LICENSES = "test/json_data_import_licenses/jm.license_definition.json"; - private final static String LICENSE_LABELS_TEST = "test/json_data_import_licenses/jm.license_label_test.json"; - private final static String EXTENDED_MAPPINGS_TEST = - "test/json_data_import_licenses/jm.license_label_extended_mapping_test.json"; - private final static String LICENSES_TEST = "test/json_data_import_licenses/jm.license_definition_test.json"; - - private final static String URL_LICENSE_LABEL = "/api/licenses/import/labels"; - private final static String URL_EXTENDED_MAPPING = "/api/licenses/import/extendedMapping"; - private final static String URL_LICENSE = "/api/licenses/import/licenses"; - private JsonNodeFactory jsonNodeFactory = new JsonNodeFactory(true); - @Autowired private ClarinLicenseLabelService clarinLicenseLabelService; @@ -76,474 +59,115 @@ public class ClarinLicenseImportControllerIT extends AbstractControllerIntegrati @Autowired private ClarinLicenseLabelConverter clarinLicenseLabelConverter; - private Dictionary licenseLabelDictionary = new Hashtable<>(); - private Dictionary licenseLabelIDDictionary = new Hashtable<>(); - private Dictionary licenseDictionary = new Hashtable<>(); - private Dictionary> extendedMappingDictionary = new Hashtable<>(); + ClarinLicenseLabel firstCLicenseLabel; + ClarinLicenseLabel secondCLicenseLabel; + ClarinLicense firstCLicense; - @Test - public void importLicensesTest() throws Exception { + @Before + public void setup() throws Exception { context.turnOffAuthorisationSystem(); - ClarinUserRegistration userRegistration = ClarinUserRegistrationBuilder - .createClarinUserRegistration(context).withEPersonID(admin.getID()).build(); - context.restoreAuthSystemState(); - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSE_LABELS) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - ClarinLicenseLabel clarinLicenseLabel; - List nodes = new ArrayList<>(); - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - node.set("label", jsonNodeFactory.textNode(jsonObject.get("label").toString())); - node.set("title", jsonNodeFactory.textNode(jsonObject.get("title").toString())); - node.set("is_extended", jsonNodeFactory.textNode(jsonObject.get("is_extended").toString())); - nodes.add(node); - - //for test control - clarinLicenseLabel = new ClarinLicenseLabel(); - clarinLicenseLabel.setId(Integer.parseInt(jsonObject.get("label_id").toString())); - clarinLicenseLabel.setLabel(jsonObject.get("label").toString()); - clarinLicenseLabel.setTitle(jsonObject.get("title").toString()); - clarinLicenseLabel.setExtended(Boolean.parseBoolean(jsonObject.get("is_extended").toString())); - licenseLabelDictionary.put(clarinLicenseLabel.getLabel(), clarinLicenseLabel); - licenseLabelIDDictionary.put(clarinLicenseLabel.getID(), clarinLicenseLabel); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE_LABEL) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().isOk()); - - //extendedMapping - bufferReader = new BufferedReader(new FileReader(getClass().getResource(EXTENDED_MAPPINGS).getFile())); - nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("mapping_id", jsonNodeFactory.textNode(jsonObject.get("mapping_id").toString())); - node.set("license_id", jsonNodeFactory.textNode(jsonObject.get("license_id").toString())); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - if (Objects.isNull(extendedMappingDictionary.get(Integer.parseInt(jsonObject.get("license_id") - .toString())))) { - extendedMappingDictionary.put(Integer.parseInt(jsonObject.get("license_id").toString()), - new HashSet<>()); - } - extendedMappingDictionary.get(Integer.parseInt(jsonObject.get("license_id").toString())).add( - Integer.parseInt(jsonObject.get("label_id").toString())); - nodes.add(node); - } - - getClient(adminToken).perform(post(URL_EXTENDED_MAPPING) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().isOk()); - - //licenses - bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSES).getFile())); - ClarinLicense license; - nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("license_id", jsonNodeFactory.textNode(jsonObject.get("license_id").toString())); - node.set("name", jsonNodeFactory.textNode(jsonObject.get("name").toString())); - node.set("definition", jsonNodeFactory.textNode(jsonObject.get("definition").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(admin.getID().toString())); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - node.set("confirmation", jsonNodeFactory.textNode(jsonObject.get("confirmation").toString())); - node.set("required_info", jsonNodeFactory.textNode(Objects.isNull(jsonObject.get("required_info")) ? - null : jsonObject.get("required_info").toString())); - nodes.add(node); - - //for test control - license = new ClarinLicense(); - license.setId(Integer.parseInt(jsonObject.get("license_id").toString())); - license.setName(jsonObject.get("name").toString()); - license.setDefinition(jsonObject.get("definition").toString()); - license.setEperson(userRegistration); - Set labels = new HashSet<>(); - labels.add(this.licenseLabelIDDictionary.get(Integer.parseInt(jsonObject.get("label_id").toString()))); - license.setLicenseLabels(labels); - license.setConfirmation(Integer.parseInt(jsonObject.get("confirmation").toString())); - license.setRequiredInfo(Objects.isNull(jsonObject.get("required_info")) ? - null : jsonObject.get("required_info").toString()); - licenseDictionary.put(license.getName(), license); - if (Objects.isNull(extendedMappingDictionary.get(license.getID()))) { - extendedMappingDictionary.put(license.getID(), new HashSet<>()); - } - extendedMappingDictionary.get(license.getID()).add(license.getLicenseLabels().get(0).getID()); - } - - getClient(adminToken).perform(post(URL_LICENSE) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().isOk()); - - //check - context.turnOffAuthorisationSystem(); - List clarinLicenses = this.clarinLicenseService.findAll(context); - Assert.assertEquals(clarinLicenses.size(), licenseDictionary.size()); - //control of the license mapping - for (ClarinLicense clarinLicense: clarinLicenses) { - ClarinLicense oldLicense = licenseDictionary.get(clarinLicense.getName()); - Assert.assertNotNull(oldLicense); - Assert.assertEquals(clarinLicense.getConfirmation(), oldLicense.getConfirmation()); - Assert.assertEquals(clarinLicense.getDefinition(), oldLicense.getDefinition()); - Assert.assertEquals(clarinLicense.getRequiredInfo(), oldLicense.getRequiredInfo()); - Assert.assertEquals(clarinLicense.getLicenseLabels().size(), extendedMappingDictionary.get( - oldLicense.getID()).size()); - Assert.assertEquals(clarinLicense.getEperson().getID(), userRegistration.getID()); - List clarinLicenseLabels = clarinLicense.getLicenseLabels(); - for (ClarinLicenseLabel label: clarinLicenseLabels) { - ClarinLicenseLabel oldLabel = licenseLabelDictionary.get(label.getLabel()); - Assert.assertEquals(label.getLabel(), oldLabel.getLabel()); - Assert.assertEquals(label.getTitle(), oldLabel.getTitle()); - Assert.assertEquals(label.isExtended(), oldLabel.isExtended()); - } - } + // create LicenseLabels + firstCLicenseLabel = ClarinLicenseLabelBuilder.createClarinLicenseLabel(context).build(); + firstCLicenseLabel.setLabel("CC"); + firstCLicenseLabel.setExtended(true); + firstCLicenseLabel.setTitle("CLL Title1"); + clarinLicenseLabelService.update(context, firstCLicenseLabel); + + secondCLicenseLabel = ClarinLicenseLabelBuilder.createClarinLicenseLabel(context).build(); + secondCLicenseLabel.setLabel("CCC"); + secondCLicenseLabel.setExtended(false); + secondCLicenseLabel.setTitle("CLL Title2"); + clarinLicenseLabelService.update(context, secondCLicenseLabel); + + // create ClarinLicenses + firstCLicense = ClarinLicenseBuilder.createClarinLicense(context).build(); + firstCLicense.setName("CL Name1"); + firstCLicense.setConfirmation(0); + firstCLicense.setDefinition("CL Definition1"); + firstCLicense.setRequiredInfo("CL Req1"); + // add ClarinLicenseLabels to the ClarinLicense + HashSet firstClarinLicenseLabels = new HashSet<>(); + firstClarinLicenseLabels.add(firstCLicenseLabel); + firstClarinLicenseLabels.add(secondCLicenseLabel); + firstCLicense.setLicenseLabels(firstClarinLicenseLabels); + clarinLicenseService.update(context, firstCLicense); - //control of the license label mapping - List clarinLicenseLabels = this.clarinLicenseLabelService.findAll(context); - Assert.assertEquals(clarinLicenseLabels.size(), licenseLabelDictionary.size()); - for (ClarinLicenseLabel label: clarinLicenseLabels) { - ClarinLicenseLabel oldLabel = licenseLabelDictionary.get(label.getLabel()); - Assert.assertNotNull(oldLabel); - Assert.assertEquals(label.getTitle(), oldLabel.getTitle()); - Assert.assertEquals(label.getIcon(), oldLabel.getIcon()); - } context.restoreAuthSystemState(); - this.cleanAll(context); - System.out.println(); - } - @Test - public void labelIDoesntExist() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSE_LABELS_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("label", jsonNodeFactory.textNode(jsonObject.get("label").toString())); - node.set("title", jsonNodeFactory.textNode(jsonObject.get("title").toString())); - node.set("is_extended", jsonNodeFactory.textNode(jsonObject.get("is_extended").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE_LABEL) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(422)); - this.cleanAll(context); } @Test - public void labelIDisNull() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSE_LABELS_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("license_id", jsonNodeFactory.textNode(null)); - node.set("label", jsonNodeFactory.textNode(jsonObject.get("label").toString())); - node.set("title", jsonNodeFactory.textNode(jsonObject.get("title").toString())); - node.set("is_extended", jsonNodeFactory.textNode(jsonObject.get("is_extended").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE_LABEL) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(422)); - this.cleanAll(context); - } - - @Test - public void labelIncorrectArgument() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSE_LABELS_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while (Objects.nonNull((line = bufferReader.readLine()))) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - node.set("title", jsonNodeFactory.textNode(jsonObject.get("title").toString())); - node.set("is_extended", jsonNodeFactory.textNode(jsonObject.get("is_extended").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE_LABEL) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(409)); - this.cleanAll(context); - } - - @Test - public void extendedMappingDoesntExist() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(EXTENDED_MAPPINGS_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("mapping_id", jsonNodeFactory.textNode(jsonObject.get("mapping_id").toString())); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_EXTENDED_MAPPING) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(422)); - this.cleanAll(context); - } - - @Test - public void extendedMappingLabelDoesntExist() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(EXTENDED_MAPPINGS_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("mapping_id", jsonNodeFactory.textNode(jsonObject.get("mapping_id").toString())); - node.set("license_id", jsonNodeFactory.textNode(jsonObject.get("license_id").toString())); - node.set("label_id", jsonNodeFactory.textNode("1000")); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_EXTENDED_MAPPING) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(409)); - this.cleanAll(context); - } - - @Test - public void licenseIDDoesntExist() throws Exception { + public void importLicenseTest() throws Exception { context.turnOffAuthorisationSystem(); - ClarinUserRegistration userRegistration = ClarinUserRegistrationBuilder - .createClarinUserRegistration(context).withEPersonID(admin.getID()).build(); + EPerson ePerson = EPersonBuilder.createEPerson(context) + .withNameInMetadata("John", "Doe") + .withEmail("Johndoe@example.com") + .build(); + ClarinUserRegistration userRegistration = ClarinUserRegistrationBuilder.createClarinUserRegistration(context) + .withEPersonID(ePerson.getID()) + .build(); context.restoreAuthSystemState(); - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSES_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("name", jsonNodeFactory.textNode(jsonObject.get("name").toString())); - node.set("definition", jsonNodeFactory.textNode(jsonObject.get("definition").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(jsonObject.get("eperson_id").toString())); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(admin.getID().toString())); - node.set("confirmation", jsonNodeFactory.textNode(jsonObject.get("confirmation").toString())); - node.set("required_info", jsonNodeFactory.textNode(Objects.isNull(jsonObject.get("required_info")) ? - null : jsonObject.get("required_info").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(422)); - this.cleanAll(context); - } - - @Test - public void licenseIDisNull() throws Exception { - context.turnOffAuthorisationSystem(); - ClarinUserRegistration userRegistration = ClarinUserRegistrationBuilder - .createClarinUserRegistration(context).withEPersonID(admin.getID()).build(); - context.restoreAuthSystemState(); - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSES_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("name", jsonNodeFactory.textNode(jsonObject.get("name").toString())); - node.set("license_id", jsonNodeFactory.textNode(null)); - node.set("definition", jsonNodeFactory.textNode(jsonObject.get("definition").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(jsonObject.get("eperson_id").toString())); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(admin.getID().toString())); - node.set("confirmation", jsonNodeFactory.textNode(jsonObject.get("confirmation").toString())); - node.set("required_info", jsonNodeFactory.textNode(Objects.isNull(jsonObject.get("required_info")) ? - null : jsonObject.get("required_info").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(422)); - this.cleanAll(context); - } - - @Test - public void licenseIncorrectArgument() throws Exception { - context.turnOffAuthorisationSystem(); - ClarinUserRegistration userRegistration = ClarinUserRegistrationBuilder - .createClarinUserRegistration(context).withEPersonID(admin.getID()).build(); - context.restoreAuthSystemState(); - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSES_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while ((line = bufferReader.readLine()) != null) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("license_id", jsonNodeFactory.textNode(jsonObject.get("license_id").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(jsonObject.get("eperson_id").toString())); - node.set("label_id", jsonNodeFactory.textNode(jsonObject.get("label_id").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(admin.getID().toString())); - node.set("confirmation", jsonNodeFactory.textNode(jsonObject.get("confirmation").toString())); - node.set("required_info", jsonNodeFactory.textNode(Objects.isNull(jsonObject.get("required_info")) ? - null : jsonObject.get("required_info").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(409)); - this.cleanAll(context); - } - - public void licenseLabelDoesntExist() throws Exception { - context.turnOffAuthorisationSystem(); - ClarinUserRegistration userRegistration = ClarinUserRegistrationBuilder - .createClarinUserRegistration(context).withEPersonID(admin.getID()).build(); - context.restoreAuthSystemState(); - ObjectMapper mapper = new ObjectMapper(); - JSONParser parser = new JSONParser(); - BufferedReader bufferReader = new BufferedReader(new FileReader(getClass().getResource(LICENSES_TEST) - .getFile())); - Object obj; - String line; - ObjectNode node; - JSONObject jsonObject; - List nodes = new ArrayList<>(); - - while (Objects.nonNull(line = bufferReader.readLine())) { - obj = parser.parse(line); - jsonObject = (JSONObject)obj; - node = jsonNodeFactory.objectNode(); - node.set("name", jsonNodeFactory.textNode(jsonObject.get("name").toString())); - node.set("license_id", jsonNodeFactory.textNode(jsonObject.get("license_id").toString())); - node.set("eperson_id", jsonNodeFactory.textNode(jsonObject.get("eperson_id").toString())); - node.set("label_id", jsonNodeFactory.textNode("1000")); - node.set("eperson_id", jsonNodeFactory.textNode(admin.getID().toString())); - node.set("confirmation", jsonNodeFactory.textNode(jsonObject.get("confirmation").toString())); - node.set("required_info", jsonNodeFactory.textNode(Objects.isNull(jsonObject.get("required_info")) ? - null : jsonObject.get("required_info").toString())); - nodes.add(node); - } - - String adminToken = getAuthToken(admin.getEmail(), password); - getClient(adminToken).perform(post(URL_LICENSE) - .content(mapper.writeValueAsBytes(nodes)) - .contentType(contentType)) - .andExpect(status().is(409)); - this.cleanAll(context); - } - - private void cleanAll(Context context) throws SQLException, AuthorizeException { - context.turnOffAuthorisationSystem(); - List licenses = clarinLicenseService.findAll(context); - List labels = clarinLicenseLabelService.findAll(context); - for (ClarinLicense license: licenses) { - clarinLicenseService.delete(context, license); - } - for (ClarinLicenseLabel label: labels) { - clarinLicenseLabelService.delete(context, label); + 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/clarin/import/license") + .content(new ObjectMapper().writeValueAsBytes(clarinLicenseRest)) + .contentType(org.springframework.http.MediaType.APPLICATION_JSON) + .param("eperson", ePerson.getID().toString())) + .andExpect(status().isOk()) + .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"))); + + //control of user registration + ClarinLicense license = clarinLicenseService.find(context, idRef.get()); + assertEquals(license.getEperson().getID(), userRegistration.getID()); + } finally { + if (Objects.nonNull(idRef.get())) { + // remove created clarin license + ClarinLicenseBuilder.deleteClarinLicense(idRef.get()); + } } - context.restoreAuthSystemState(); } } \ No newline at end of file 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 7a84b036b002..7f07464b456a 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 @@ -36,6 +36,8 @@ import org.dspace.builder.BitstreamBuilder; import org.dspace.builder.ClarinLicenseBuilder; import org.dspace.builder.ClarinLicenseLabelBuilder; +import org.dspace.builder.ClarinUserMetadataBuilder; +import org.dspace.builder.ClarinUserRegistrationBuilder; import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; import org.dspace.builder.ItemBuilder; @@ -43,6 +45,7 @@ import org.dspace.content.Item; import org.dspace.content.clarin.ClarinLicense; import org.dspace.content.clarin.ClarinLicenseLabel; +import org.dspace.content.clarin.ClarinUserRegistration; import org.dspace.content.service.clarin.ClarinLicenseLabelService; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.content.service.clarin.ClarinLicenseService; @@ -265,7 +268,10 @@ public void create() throws Exception { Projection.DEFAULT); clarinLicenseConverter.setClarinLicenseLabel(clarinLicenseRest, firstCLicense.getLicenseLabels(), Projection.DEFAULT); - + context.turnOffAuthorisationSystem(); + ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder + .createClarinUserRegistration(context).withEPersonID(admin.getID()).build(); + context.restoreAuthSystemState(); // id of created clarin license AtomicReference idRef = new AtomicReference<>(); String authTokenAdmin = getAuthToken(admin.getEmail(), password); @@ -311,6 +317,7 @@ public void create() throws Exception { ClarinLicenseBuilder.deleteClarinLicense(idRef.get()); } } + ClarinUserMetadataBuilder.deleteClarinUserMetadata(clarinUserRegistration.getID()); } // Edit