UserDAO { @RequiresSession @Deprecated @Nullable public UserEntity findSingleUserByName(String userName) { TypedQuery<UserEntity> query = entityManagerProvider.get().createNamedQuery("userByName", UserEntity.class); query.setParameter("username", userName.toLowerCase()); List<UserEntity> resultList = query.getResultList(); switch (resultList.size()) { case 0: return null; case 1: return resultList.get(0); default: ImmutableMap.Builder<UserType, UserEntity> mapBuilder = ImmutableMap.<UserType, UserEntity>builder(); for (UserEntity user: resultList) { mapBuilder.put(user.getUserType(), user); } ImmutableMap<UserType, UserEntity> usersByType = mapBuilder.build(); UserEntity user = usersByType.containsKey(UserType.LOCAL) ? usersByType.get(UserType.LOCAL) : usersByType.containsKey(UserType.LOCAL.LDAP) ? usersByType.get(UserType.LDAP) : usersByType.containsKey(UserType.JWT) ? usersByType.get(UserType.JWT) : usersByType.get(UserType.PAM); return user; } }  @RequiresSession UserEntity findByPK(Integer userPK); @RequiresSession List<UserEntity> findAll(); @RequiresSession @Deprecated UserEntity findUserByName(String userName); @RequiresSession @Deprecated @Nullable UserEntity findSingleUserByName(String userName); @RequiresSession UserEntity findUserByNameAndType(String userName, UserType userType); @RequiresSession UserEntity findLocalUserByName(String userName); @RequiresSession UserEntity findLdapUserByName(String userName); @RequiresSession List<UserEntity> findUsersByPrincipal(List<PrincipalEntity> principalList); @RequiresSession UserEntity findUserByPrincipal(PrincipalEntity principal); @Transactional void create(UserEntity user); @Transactional void create(Set<UserEntity> users); @Transactional UserEntity merge(UserEntity user); @Transactional void merge(Set<UserEntity> users); @Transactional void remove(UserEntity user); @Transactional void remove(Set<UserEntity> users); @Transactional void removeByPK(Integer userPK);  }
@Test public void testFindSingleUserByName_NoUsers() { init(); assertNull(userDAO.findSingleUserByName(SERVICEOP_USER_NAME)); }
@Test public void testFindSingleUserByName_SingleUser() { init(user(UserType.PAM)); assertEquals(UserType.PAM, userDAO.findSingleUserByName(SERVICEOP_USER_NAME).getUserType()); }
@Test public void testFindSingleUserByName_LocalIsFirstPrecedence() { init(user(UserType.LOCAL), user(UserType.LDAP), user(UserType.JWT), user(UserType.PAM)); assertEquals(UserType.LOCAL, userDAO.findSingleUserByName(SERVICEOP_USER_NAME).getUserType()); }
@Test public void testFindSingleUserByName_LdapIsSecondPrecedence() { init(user(UserType.LDAP), user(UserType.JWT), user(UserType.PAM)); assertEquals(UserType.LDAP, userDAO.findSingleUserByName(SERVICEOP_USER_NAME).getUserType()); }
@Test public void testFindSingleUserByName_JwtIsThirdPrecedence() { init(user(UserType.JWT), user(UserType.PAM)); assertEquals(UserType.JWT, userDAO.findSingleUserByName(SERVICEOP_USER_NAME).getUserType()); }