Search in sources :

Example 1 with UserProperty

use of org.broadinstitute.consent.http.models.UserProperty 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 UserProperty

use of org.broadinstitute.consent.http.models.UserProperty in project consent by DataBiosphere.

the class ResearcherServiceTest method testUpdatePropertiesWithValidation.

@Test
public void testUpdatePropertiesWithValidation() {
    List<UserProperty> props = new ArrayList<>();
    Map<String, String> propMap = new HashMap<>();
    for (UserFields researcherField : UserFields.values()) {
        if (researcherField.getRequired()) {
            String val = RandomStringUtils.random(10, true, false);
            props.add(new UserProperty(user.getDacUserId(), researcherField.getValue(), val));
            propMap.put(researcherField.getValue(), val);
        }
    }
    when(userDAO.findUserByEmail(any())).thenReturn(user);
    when(userDAO.findUserById(any())).thenReturn(user);
    when(userPropertyDAO.findResearcherPropertiesByUser(any())).thenReturn(props);
    initService();
    List<UserProperty> foundProps = service.updateProperties(propMap, authUser, true);
    Assert.assertFalse(foundProps.isEmpty());
    Assert.assertEquals(props.size(), foundProps.size());
}
Also used : UserProperty(org.broadinstitute.consent.http.models.UserProperty) UserFields(org.broadinstitute.consent.http.enumeration.UserFields) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with UserProperty

use of org.broadinstitute.consent.http.models.UserProperty in project consent by DataBiosphere.

the class ResearcherServiceTest method testUpdatePropertiesIncompleteProfile.

@Test
public void testUpdatePropertiesIncompleteProfile() throws Exception {
    List<UserProperty> props = new ArrayList<>();
    Map<String, String> propMap = new HashMap<>();
    for (UserFields researcherField : UserFields.values()) {
        if (researcherField.getRequired()) {
            String val1 = RandomStringUtils.random(10, true, false);
            String val2 = RandomStringUtils.random(10, true, false);
            props.add(new UserProperty(user.getDacUserId(), researcherField.getValue(), val1));
            propMap.put(researcherField.getValue(), val2);
        }
    }
    props.add(new UserProperty(user.getDacUserId(), UserFields.COMPLETED.getValue(), Boolean.FALSE.toString()));
    propMap.put(UserFields.COMPLETED.getValue(), Boolean.FALSE.toString());
    when(userDAO.findUserByEmail(any())).thenReturn(user);
    when(userDAO.findUserById(any())).thenReturn(user);
    when(userPropertyDAO.findResearcherPropertiesByUser(any())).thenReturn(props);
    when(userPropertyDAO.isProfileCompleted(any())).thenReturn(Boolean.FALSE.toString());
    doNothing().when(userPropertyDAO).deletePropertiesByUserAndKey(any());
    doNothing().when(userPropertyDAO).insertAll(any());
    doNothing().when(userPropertyDAO).deleteAllPropertiesByUser(any());
    doNothing().when(emailNotifierService).sendNewResearcherCreatedMessage(any(), any());
    initService();
    List<UserProperty> foundProps = service.updateProperties(propMap, authUser, true);
    Assert.assertFalse(foundProps.isEmpty());
    Assert.assertEquals(props.size(), foundProps.size());
    verify(emailNotifierService, times(0)).sendNewResearcherCreatedMessage(any(), any());
}
Also used : UserProperty(org.broadinstitute.consent.http.models.UserProperty) UserFields(org.broadinstitute.consent.http.enumeration.UserFields) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with UserProperty

use of org.broadinstitute.consent.http.models.UserProperty in project consent by DataBiosphere.

the class ResearcherServiceTest method testUpdatePropertiesMissingFields.

@Test(expected = IllegalArgumentException.class)
public void testUpdatePropertiesMissingFields() {
    UserProperty prop = new UserProperty(user.getDacUserId(), UserFields.DEPARTMENT.getValue(), RandomStringUtils.random(10, true, false));
    Map<String, String> propMap = new HashMap<>();
    propMap.put(prop.getPropertyKey(), prop.getPropertyValue());
    when(userDAO.findUserByEmail(any())).thenReturn(user);
    when(userDAO.findUserById(any())).thenReturn(user);
    initService();
    service.updateProperties(propMap, authUser, true);
}
Also used : UserProperty(org.broadinstitute.consent.http.models.UserProperty) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with UserProperty

use of org.broadinstitute.consent.http.models.UserProperty in project consent by DataBiosphere.

the class ResearcherServiceTest method testUpdatePropertiesNoValidation.

@Test
public void testUpdatePropertiesNoValidation() {
    UserProperty prop = new UserProperty(user.getDacUserId(), UserFields.DEPARTMENT.getValue(), RandomStringUtils.random(10, true, false));
    Map<String, String> propMap = new HashMap<>();
    propMap.put(prop.getPropertyKey(), prop.getPropertyValue());
    when(userDAO.findUserByEmail(any())).thenReturn(user);
    when(userDAO.findUserById(any())).thenReturn(user);
    when(userPropertyDAO.findResearcherPropertiesByUser(any())).thenReturn(Collections.singletonList(prop));
    initService();
    List<UserProperty> props = service.updateProperties(propMap, authUser, false);
    Assert.assertFalse(props.isEmpty());
    Assert.assertEquals(propMap.size(), props.size());
}
Also used : UserProperty(org.broadinstitute.consent.http.models.UserProperty) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

UserProperty (org.broadinstitute.consent.http.models.UserProperty)26 Test (org.junit.Test)17 User (org.broadinstitute.consent.http.models.User)10 HashMap (java.util.HashMap)7 AuthUser (org.broadinstitute.consent.http.models.AuthUser)7 DarCollection (org.broadinstitute.consent.http.models.DarCollection)7 ArrayList (java.util.ArrayList)6 UserFields (org.broadinstitute.consent.http.enumeration.UserFields)4 LibraryCard (org.broadinstitute.consent.http.models.LibraryCard)4 JsonObject (com.google.gson.JsonObject)3 Election (org.broadinstitute.consent.http.models.Election)3 DataAccessRequest (org.broadinstitute.consent.http.models.DataAccessRequest)2 Institution (org.broadinstitute.consent.http.models.Institution)2 Vote (org.broadinstitute.consent.http.models.Vote)2 UserStatusInfo (org.broadinstitute.consent.http.models.sam.UserStatusInfo)2 SimplifiedUser (org.broadinstitute.consent.http.service.UserService.SimplifiedUser)2 MappingException (org.jdbi.v3.core.mapper.MappingException)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Map (java.util.Map)1