UserServiceImpl extends WaspServiceImpl implements UserService { @Override public String getUniqueLoginName(final User user){ if (user == null || user.getUserId() == null){ return null; } String loginBase = user.getFirstName().substring(0, 1); String loginLastName = user.getLastName(); loginLastName = loginLastName.replaceAll(" ", ""); int posOfQuote = StringUtils.indexOf(loginLastName, "'"); int posOfHyphen = StringUtils.indexOf(loginLastName, "-"); if (posOfQuote != -1 && (posOfQuote - 1) != -1 && (posOfQuote < loginLastName.length())){ loginBase += loginLastName.substring(0, posOfQuote); loginBase += loginLastName.substring(posOfQuote + 1); } else if (posOfHyphen != -1 && (posOfHyphen - 1) != -1 && (posOfHyphen < loginLastName.length())){ loginBase += loginLastName.substring(0, posOfHyphen); loginBase += loginLastName.substring(posOfHyphen + 1); }else { loginBase += loginLastName; } loginBase = loginBase.replaceAll("[^\\w-]", "").toLowerCase(); String login = loginBase; int c = 1; while (userDao.getUserByLogin(login).getUserId() != null) { login = loginBase + c; c++; } return login; }  @Override @Autowired void setUserDao(UserDao userDao); @Override UserDao getUserDao(); void setconfirmEmailAuthDao(ConfirmEmailAuthDao confirmEmailAuthDao); @Override String getUniqueLoginName(final User user); @Override String getNewAuthcodeForUser(User user); @Override String getNewAuthcodeForUserPending(UserPending userpending); @Override void reverseSortUsersByUserId(List<User> users); @Override List<User> getFacilityTechnicians(); @Override User getUserByLogin(String login); @Override User getUserByEmail(String emailAddress); @Override List<User> getFacilityManagers(); @Override UserMetaDao getUserMetaDao(); @Override List<Lab> getLabsForUser(User user);  }
@Test public void getUniqueLoginName() { User userNew = new User(); userNew.setUserId(123); userNew.setFirstName("John"); userNew.setLastName("Greally"); User userExists = new User(); userExists.setUserId(123); userExists.setFirstName("Jane"); userExists.setLastName("Doe"); User user_apostrophe = new User(); user_apostrophe.setUserId(123); user_apostrophe.setFirstName("Jane"); user_apostrophe.setLastName("O'Brien"); User user_hyphen = new User(); user_hyphen.setUserId(123); user_hyphen.setFirstName("Jane"); user_hyphen.setLastName("Some-Lastname"); User user_apostrophe_middle = new User(); user_apostrophe_middle.setUserId(123); user_apostrophe_middle.setFirstName("Jane"); user_apostrophe_middle.setLastName("Some'Lastname"); User user_apostrophe_beg = new User(); user_apostrophe_beg.setUserId(123); user_apostrophe_beg.setFirstName("Jane"); user_apostrophe_beg.setLastName("'SomeLastname"); User userBlank = new User(); userServiceImpl.setUserDao(mockUserDao); expect(mockUserDao.getUserByLogin("jdoe")).andReturn(userExists); expect(mockUserDao.getUserByLogin("jdoe1")).andReturn(userBlank); expect(mockUserDao.getUserByLogin("jgreally")).andReturn(userBlank); expect(mockUserDao.getUserByLogin("jobrien")).andReturn(userBlank); expect(mockUserDao.getUserByLogin("jsomelastname")).andReturn(userBlank); expect(mockUserDao.getUserByLogin("jsomelastname")).andReturn(userBlank); expect(mockUserDao.getUserByLogin("jsomelastname")).andReturn(userBlank); replay(mockUserDao); Assert.assertEquals("jdoe1", userServiceImpl.getUniqueLoginName(userExists)); Assert.assertEquals("jgreally", userServiceImpl.getUniqueLoginName(userNew)); Assert.assertEquals("jobrien", userServiceImpl.getUniqueLoginName(user_apostrophe)); Assert.assertEquals("jsomelastname", userServiceImpl.getUniqueLoginName(user_apostrophe_middle)); Assert.assertEquals("jsomelastname", userServiceImpl.getUniqueLoginName(user_hyphen)); Assert.assertEquals("jsomelastname", userServiceImpl.getUniqueLoginName(user_apostrophe_beg)); verify(mockUserDao); }