LoginServiceImpl implements LoginService { @Override public UserLogin addUser(Login user, long role, String firstName, String middleName, String lastName, String transRoleDesc) throws UserLoginException { boolean isCreateUser; String passwordHash; byte[] saltValue; try { saltValue = credentialService.generateRandomSalt(); passwordHash = new String(SHA2PasswordUtil.calculateHash(saltValue, user.getPassword().getBytes())); } catch (UtilException e) { throw new UserLoginException("Error while calculating hash.", e); } UserLogin userLoginEntity = new UserLogin(); userLoginEntity.setUserName(user.getUserName()); userLoginEntity.setSha2(passwordHash); userLoginEntity.setSalt(new String(saltValue)); userLoginEntity.setFirstName(firstName); userLoginEntity.setMiddleName(middleName); userLoginEntity.setLastName(lastName); userLoginEntity.setTransactionRole(getUserRoleCode(transRoleDesc)); userLoginEntity.setTransactionRoleDesc(transRoleDesc); UserRole userRole = getUserRole(role); if (userRole != null) { userRole.addLogin(userLoginEntity); } userLoginEntity.setUserRole(userRole); isCreateUser = userLoginDAO.createUser(userLoginEntity); if (isCreateUser) { return userLoginEntity; } else { return null; } } LoginServiceImpl(); @Override UserLogin login(Login login); @Override UserLogin addUser(Login user, long role, String firstName, String middleName,
            String lastName, String transRoleDesc); @Override List<UserLogin> getAllUsers(); @Override void deleteUser(UserLogin user); @Override Properties getUserRoleList(); static final String CONNECT_ADMIN_USER; }
@Test public void testAdduser_Pass() throws UserLoginException, UtilException { Login login = new Login(); login.setUserName("ABCD"); login.setPassword("ABCDEFGH"); Mockito.when(userDAO.createUser(Mockito.any(UserLogin.class))).thenReturn(true); UserLogin addedUser = loginService.addUser(login, 1, "firstName", "middleName", "lastName", "role description"); assertNotNull(addedUser); Mockito.verify(passwordService).generateRandomSalt(); String expectedHash = new String(SHA2PasswordUtil.calculateHash(salt.getBytes(), login.getPassword().getBytes())); assertEquals(expectedHash, addedUser.getSha2()); assertEquals("firstName", addedUser.getFirstName()); assertEquals("middleName", addedUser.getMiddleName()); assertEquals("lastName", addedUser.getLastName()); assertEquals(salt, addedUser.getSalt()); }