@Test public void shouldReturnTrueWhenUserDisabledSuccess() { user.setEnabled(true); User disabledUser = new User(); disabledUser.setEnabled(false); disabledUser.setUserName("user"); when(userMapper.findByUsername("user")).thenReturn(disabledUser); assertTrue(userService.disable(user)); verify(userMapper).update(user); assertFalse(userService.getByUsername("user").isEnabled()); }
@Test public void shouldReturnAllUsersWhithRoleWhenInvoked() { List<User> users = new ArrayList<User>(); users.add(user); when(userMapper.findAllUsers()).thenReturn(users); when(userRoleMapper.get(user.getId())).thenReturn(userRole); Map<User, String> result = userService.getAllUsersWithRole(); Map<User, String> expect = new HashMap<User, String>(); expect.put(user, userRole.getRoleName()); assertEquals(result, expect); verify(userMapper).findAllUsers(); verify(userRoleMapper).get(user.getId()); }
@Test public void shouldReturnFalseWhenUserDisabledFailed() { user.setEnabled(false); assertFalse("should return false when given null user", userService.disable(null)); assertFalse("should return false when given disabled user", userService.disable(user)); verify(userMapper, never()).update(user); }
@Before public void setup() { session = mock(SqlSession.class); userMapper = mock(UserMapper.class); userRoleMapper = mock(UserRoleMapper.class); sessionFactory = mock(SqlSessionFactory.class); when(sessionFactory.openSession()).thenReturn(session); when(session.getMapper(UserMapper.class)).thenReturn(userMapper); when(session.getMapper(UserRoleMapper.class)).thenReturn(userRoleMapper); userService = new UserServiceImpl(sessionFactory); user = new User(); user.setUserName("user"); user.setPasswordHash("password"); user.setId(1L); userRole = new UserRole(); userRole.setUserId(1L); userRole.setRoleName("ROLE_REGULAR"); }
@Test public void shouldChangeUserPasswordWhenAsked() { User updatedUser = new User(); updatedUser.setUserName(user.getUserName()); updatedUser.setPasswordHash("newpass"); when(userMapper.findByUsername(user.getUserName())).thenReturn(updatedUser); boolean ret = userService.password(user, "newpass"); verify(userMapper).update(user); assertEquals(userService.getByUsername(user.getUserName()).getPasswordHash(), "newpass"); assertEquals(ret, true); }
@Test public void shouldReturnTrueWhenPasswordCorrect() { String password = user.getPasswordHash(); assertEquals(userService.passwordVerify(user, password), true); }
@Test public void shouldRuturnUserRoleWhenInvoked() { userService.getUserRoleById(user.getId()); verify(userRoleMapper).get(user.getId()); }