Skip to content

Commit

Permalink
Refactoring and created test
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Sep 26, 2023
1 parent c5a2df1 commit f07ccc7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,44 +223,17 @@ public List<ClarinUserMetadata> processSignedInUser(Context context, EPerson cur
currentUser.getID() + " is null.");
}

List<ClarinUserMetadata> currentClarinUserMetadataList = clarinUserRegistration.getUserMetadata();
List<ClarinUserMetadata> 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<ClarinUserMetadata> 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());
clarinUserMetadata.setEperson(clarinUserRegistration);
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
Expand Down Expand Up @@ -308,7 +281,7 @@ public List<ClarinUserMetadata> processNonSignedInUser(Context context,
List<ClarinUserMetadata> 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<ClarinUserRegistration> clarinUserRegistrationList = clarinUserRegistrationService
.findByEmail(context, ANONYMOUS_USER_REGISTRATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClarinUserMetadataRest> 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<ClarinUserMetadata> 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.
*/
Expand Down

0 comments on commit f07ccc7

Please sign in to comment.