Search in sources :

Example 1 with LibraryCard

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
    }
}
Also used : LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) Objects(java.util.Objects) Institution(org.broadinstitute.consent.http.models.Institution) UserProperty(org.broadinstitute.consent.http.models.UserProperty) LinkedHashMapRowReducer(org.jdbi.v3.core.result.LinkedHashMapRowReducer) Map(java.util.Map) RowView(org.jdbi.v3.core.result.RowView) UserRole(org.broadinstitute.consent.http.models.UserRole) MappingException(org.jdbi.v3.core.mapper.MappingException) UserFields(org.broadinstitute.consent.http.enumeration.UserFields) User(org.broadinstitute.consent.http.models.User) LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) User(org.broadinstitute.consent.http.models.User) UserProperty(org.broadinstitute.consent.http.models.UserProperty) UserRole(org.broadinstitute.consent.http.models.UserRole) Institution(org.broadinstitute.consent.http.models.Institution) MappingException(org.jdbi.v3.core.mapper.MappingException)

Example 2 with LibraryCard

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));
}
Also used : LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) AuthUser(org.broadinstitute.consent.http.models.AuthUser) User(org.broadinstitute.consent.http.models.User) SimplifiedUser(org.broadinstitute.consent.http.service.UserService.SimplifiedUser) UserProperty(org.broadinstitute.consent.http.models.UserProperty) UserStatusInfo(org.broadinstitute.consent.http.models.sam.UserStatusInfo) JsonObject(com.google.gson.JsonObject) AuthUser(org.broadinstitute.consent.http.models.AuthUser) Test(org.junit.Test)

Example 3 with LibraryCard

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());
}
Also used : LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) AuthUser(org.broadinstitute.consent.http.models.AuthUser) User(org.broadinstitute.consent.http.models.User) SimplifiedUser(org.broadinstitute.consent.http.service.UserService.SimplifiedUser) Test(org.junit.Test)

Example 4 with LibraryCard

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;
}
Also used : LibraryCard(org.broadinstitute.consent.http.models.LibraryCard)

Example 5 with 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);
}
Also used : Response(javax.ws.rs.core.Response) LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

LibraryCard (org.broadinstitute.consent.http.models.LibraryCard)61 Test (org.junit.Test)40 User (org.broadinstitute.consent.http.models.User)33 Institution (org.broadinstitute.consent.http.models.Institution)15 AuthUser (org.broadinstitute.consent.http.models.AuthUser)13 Response (javax.ws.rs.core.Response)10 Gson (com.google.gson.Gson)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Date (java.util.Date)6 NotFoundException (javax.ws.rs.NotFoundException)4 UserProperty (org.broadinstitute.consent.http.models.UserProperty)4 UserRole (org.broadinstitute.consent.http.models.UserRole)4 SimplifiedUser (org.broadinstitute.consent.http.service.UserService.SimplifiedUser)4 JsonObject (com.google.gson.JsonObject)3 RolesAllowed (javax.annotation.security.RolesAllowed)3 BadRequestException (javax.ws.rs.BadRequestException)3 ForbiddenException (javax.ws.rs.ForbiddenException)3 Produces (javax.ws.rs.Produces)3 MappingException (org.jdbi.v3.core.mapper.MappingException)3 Consumes (javax.ws.rs.Consumes)2