use of org.broadinstitute.consent.http.models.LibraryCard in project consent by DataBiosphere.
the class UserWithRolesReducer method accumulate.
@Override
public void accumulate(Map<Integer, User> map, RowView rowView) {
User user = map.computeIfAbsent(rowView.getColumn("dacuserid", Integer.class), id -> rowView.getRow(User.class));
try {
if (Objects.nonNull(rowView.getColumn("user_role_id", Integer.class))) {
UserRole ur = rowView.getRow(UserRole.class);
user.addRole(ur);
}
} catch (MappingException e) {
// Ignore any attempt to map a column that doesn't exist
}
try {
if (Objects.nonNull(rowView.getColumn("i_id", Integer.class))) {
Institution institution = rowView.getRow(Institution.class);
// There are unusual cases where we somehow create an institution with null values
if (Objects.nonNull(institution.getId())) {
user.setInstitution(institution);
}
}
} catch (MappingException e) {
// Ignore institution mapping errors, possible for new users to not have an institution
}
// Below only adds LC if not currently saved on the array
try {
if (Objects.nonNull(rowView.getColumn("lc_id", Integer.class))) {
LibraryCard lc = rowView.getRow(LibraryCard.class);
try {
if (Objects.nonNull(rowView.getColumn("lci_id", Integer.class))) {
Institution institution = rowView.getRow(Institution.class);
// There are unusual cases where we somehow create an institution with null values
if (Objects.nonNull(institution.getId())) {
lc.setInstitution(institution);
}
}
} catch (MappingException e) {
// Ignore institution mapping errors
}
if (Objects.isNull(user.getLibraryCards()) || user.getLibraryCards().stream().noneMatch(card -> card.getId().equals(lc.getId()))) {
user.addLibraryCard(lc);
}
}
} catch (MappingException e) {
// Ignore exceptions here, user may not have a library card issued under this instiution
}
try {
if (Objects.nonNull(rowView.getColumn("up_property_id", Integer.class))) {
UserProperty p = rowView.getRow(UserProperty.class);
user.addProperty(p);
// Note that the completed field is deprecated and will be removed in a future PR.
if (p.getPropertyKey().equalsIgnoreCase(UserFields.COMPLETED.getValue())) {
user.setProfileCompleted(Boolean.valueOf(p.getPropertyValue()));
}
}
} catch (MappingException e) {
// Ignore any attempt to map a column that doesn't exist
}
}
use of org.broadinstitute.consent.http.models.LibraryCard in project consent by DataBiosphere.
the class UserServiceTest method testFindUserWithPropertiesAsJsonObjectByIdNonAuthUser.
@Test
public void testFindUserWithPropertiesAsJsonObjectByIdNonAuthUser() {
User user = generateUser();
UserStatusInfo info = new UserStatusInfo().setUserEmail(user.getEmail()).setEnabled(true).setUserSubjectId("subjectId");
AuthUser authUser = new AuthUser().setEmail("not the user's email address").setAuthToken(RandomStringUtils.random(30, true, false)).setUserStatusInfo(info);
when(userDAO.findUserById(anyInt())).thenReturn(user);
when(libraryCardDAO.findLibraryCardsByUserId(anyInt())).thenReturn(List.of(new LibraryCard()));
when(userPropertyDAO.findResearcherPropertiesByUser(anyInt())).thenReturn(List.of(new UserProperty()));
initService();
JsonObject userJson = service.findUserWithPropertiesByIdAsJsonObject(authUser, user.getDacUserId());
assertNotNull(userJson);
assertTrue(userJson.get(UserService.LIBRARY_CARDS_FIELD).getAsJsonArray().isJsonArray());
assertTrue(userJson.get(UserService.RESEARCHER_PROPERTIES_FIELD).getAsJsonArray().isJsonArray());
assertNull(userJson.get(UserService.USER_STATUS_INFO_FIELD));
}
use of org.broadinstitute.consent.http.models.LibraryCard in project consent by DataBiosphere.
the class UserServiceTest method testFindUserById_HasLibraryCards.
@Test
public void testFindUserById_HasLibraryCards() {
User u = generateUser();
LibraryCard one = generateLibraryCard(u);
LibraryCard two = generateLibraryCard(u);
List<LibraryCard> cards = List.of(one, two);
when(userDAO.findUserById(any())).thenReturn(u);
when(libraryCardDAO.findLibraryCardsByUserId(any())).thenReturn(cards);
initService();
User user = service.findUserById(u.getDacUserId());
assertNotNull(user);
assertNotNull(user.getLibraryCards());
assertEquals(user.getLibraryCards().size(), 2);
assertEquals(user.getLibraryCards().get(0).getId(), one.getId());
assertEquals(user.getLibraryCards().get(1).getId(), two.getId());
}
use of org.broadinstitute.consent.http.models.LibraryCard in project consent by DataBiosphere.
the class UserServiceTest method generateLibraryCard.
private LibraryCard generateLibraryCard(User user) {
LibraryCard libraryCard = new LibraryCard();
libraryCard.setId(RandomUtils.nextInt(1, 10));
libraryCard.setUserId(user.getDacUserId());
libraryCard.setInstitutionId(RandomUtils.nextInt(1, 10));
return libraryCard;
}
use of org.broadinstitute.consent.http.models.LibraryCard in project consent by DataBiosphere.
the class LibraryCardResourceTest method testGetLibraryCardsById.
@Test
public void testGetLibraryCardsById() {
LibraryCard card = mockLibraryCardSetup();
when(libraryCardService.findLibraryCardById(anyInt())).thenReturn(card);
initResource();
Response response = resource.getLibraryCardById(authUser, 1);
String json = response.getEntity().toString();
assertEquals(HttpStatusCodes.STATUS_CODE_OK, response.getStatus());
assertNotNull(json);
}
Aggregations