From f07ccc78503e51d4aa4eabbbe8e78d89bb546b02 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Tue, 26 Sep 2023 15:59:31 +0200 Subject: [PATCH] Refactoring and created test --- .../ClarinUserMetadataRestController.java | 35 ++--------- .../ClarinUserMetadataImportControllerIT.java | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+), 31 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestController.java index 105381f0eb23..4494349ccb82 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestController.java @@ -223,35 +223,10 @@ public List processSignedInUser(Context context, EPerson cur currentUser.getID() + " is null."); } - List currentClarinUserMetadataList = clarinUserRegistration.getUserMetadata(); - List newClarinUserMetadataList = new ArrayList<>(currentClarinUserMetadataList); - // If exists ClarinResourceUserAllowance - Clrua record in the table, create a new clrua with current - // resource mapping, user metadata, user registration -// if (CollectionUtils.isEmpty(currentClarinUserMetadataList)) { -// // The current user doesn't fill in any user metadata, create a new UserMetadata objects -// newClarinUserMetadataList = this.createUserMetadataFromRequest(context, -// clarinUserMetadataRestList); -// } else { - // The current user has signed some user metadata, update actual UserMetadata objects and create - // the new one. - // Compare the old metadata value with the new one and if the value is changed or missing, create/update - // the metadata value. -// newClarinUserMetadataList = new ArrayList<>(); + // Copy current user_metadata records into a list and append it by a new ones. + List newClarinUserMetadataList = new ArrayList<>(clarinUserRegistration.getUserMetadata()); + for (ClarinUserMetadataRest clarinUserMetadataRest : clarinUserMetadataRestList) { -// boolean shouldCreate = true; -// for (ClarinUserMetadata clarinUserMetadata: currentClarinUserMetadataList) { -// if (StringUtils.equals(clarinUserMetadataRest.getMetadataKey(), -// clarinUserMetadata.getMetadataKey())) { -// shouldCreate = false; -// // Set metadata value -// clarinUserMetadata.setMetadataValue(clarinUserMetadataRest.getMetadataValue()); -// // Update the user metadata record -// clarinUserMetadataService.update(context, clarinUserMetadata); -// // Add userMetadata to the list of the new user metadata -// newClarinUserMetadataList.add(clarinUserMetadata); -// } -// } -// if (shouldCreate) { ClarinUserMetadata clarinUserMetadata = this.clarinUserMetadataService.create(context); clarinUserMetadata.setMetadataKey(clarinUserMetadataRest.getMetadataKey()); clarinUserMetadata.setMetadataValue(clarinUserMetadataRest.getMetadataValue()); @@ -259,8 +234,6 @@ public List processSignedInUser(Context context, EPerson cur clarinUserMetadataService.update(context, clarinUserMetadata); // Add userMetadata to the list of the new user metadata newClarinUserMetadataList.add(clarinUserMetadata); -// } -// } } // Process clrua with the new clarin user metadata @@ -308,7 +281,7 @@ public List processNonSignedInUser(Context context, List clarinUserMetadataList = this.createUserMetadataFromRequest(context, clarinUserMetadataRestList); - // Get anonymous user registration + // Get anonymous user registration - user metadata should not have `user_registration_id = null` ClarinUserRegistration clarinUserRegistration = null; List clarinUserRegistrationList = clarinUserRegistrationService .findByEmail(context, ANONYMOUS_USER_REGISTRATION); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataImportControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataImportControllerIT.java index 1db39d2b8d81..7360c9edcb05 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataImportControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataImportControllerIT.java @@ -211,6 +211,66 @@ public void importUserMetadataWithoutEpersonTest() throws Exception { ClarinUserMetadataBuilder.deleteClarinUserMetadata(clarinUserRegistration.getID()); } + // The user metadata shouldn't be updated, but it should be added + @Test + public void importTwoTimesUserMetadataWithEpersonTest() throws Exception { + this.prepareEnvironment("NAME"); + context.turnOffAuthorisationSystem(); + ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder + .createClarinUserRegistration(context).withEPersonID(admin.getID()).build(); + context.restoreAuthSystemState(); + ObjectMapper mapper = new ObjectMapper(); + ClarinUserMetadataRest clarinUserMetadata1 = new ClarinUserMetadataRest(); + clarinUserMetadata1.setMetadataKey("NAME"); + clarinUserMetadata1.setMetadataValue("Test"); + + List clarinUserMetadataRestList = new ArrayList<>(); + clarinUserMetadataRestList.add(clarinUserMetadata1); + + 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/clarin/import/usermetadata") + .content(mapper.writeValueAsBytes(clarinUserMetadataRestList.toArray())) + .contentType(MediaType.APPLICATION_JSON) + .param("userRegistrationId", clarinUserRegistration.getID().toString()) + .param("bitstreamUUID", bitstream.getID().toString()) + .param("createdOn", "2012-09-19T10:30:03.741633") + .param("token", "111")) + .andExpect(status().isOk()); + + getClient(adminToken).perform(post("/api/clarin/import/usermetadata") + .content(mapper.writeValueAsBytes(clarinUserMetadataRestList.toArray())) + .contentType(MediaType.APPLICATION_JSON) + .param("userRegistrationId", clarinUserRegistration.getID().toString()) + .param("bitstreamUUID", bitstream.getID().toString()) + .param("createdOn", "2012-09-19T10:30:03.741633") + .param("token", "111")) + .andExpect(status().isOk()); + + List allUserMetadata = clarinUserMetadataService.findAll(context); + // UserMetadata should be created a not updated + assertEquals(2, allUserMetadata.size()); + + // get first created data and check it + ClarinUserMetadata clarinUserMetadata = allUserMetadata.get(0); + assertEquals(clarinUserMetadata.getMetadataKey(), "NAME"); + assertEquals(clarinUserMetadata.getMetadataValue(), "Test"); + assertEquals(clarinUserMetadata.getEperson().getPersonID(), admin.getID()); + assertEquals(clarinUserMetadata.getTransaction().getCreatedOn().getTime(), + getDateFromString("2012-09-19T10:30:03.741633").getTime()); + assertEquals(clarinUserMetadata.getTransaction().getToken(), "111"); + + //clean all + ClarinUserMetadataBuilder.deleteClarinUserMetadata(clarinUserRegistration.getID()); + } + /** * Create Workspace item with file. */