@Test public void assignRolesTest() { final String badUsername = "badUsername"; final Long roleId = 5L; UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L); userEntity.setId(1L); Mockito.when(userRepository.findByUserName(Mockito.eq(username))).thenReturn(Optional.of(userEntity)); Mockito.when(userRepository.findByUserName(Mockito.eq(badUsername))).thenReturn(Optional.empty()); DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor); boolean assignedRoles = defaultUserAccessor.assignRoles(username, Set.of(roleId)); boolean assignedRolesFalse = defaultUserAccessor.assignRoles(badUsername, Set.of(roleId)); Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any()); Mockito.verify(roleAccessor).getRoles(Mockito.any()); assertTrue(assignedRoles); assertFalse(assignedRolesFalse); }
@Override public boolean assignRoles(String username, Set<Long> roleIds) { Optional<Long> optionalUserId = userRepository.findByUserName(username).map(UserEntity::getId); if (optionalUserId.isPresent()) { roleAccessor.updateUserRoles(optionalUserId.get(), roleAccessor.getRoles(roleIds)); return true; } return false; }
DefaultUserAccessor implements UserAccessor { @Override public boolean assignRoles(String username, Set<Long> roleIds) { Optional<Long> optionalUserId = userRepository.findByUserName(username).map(UserEntity::getId); if (optionalUserId.isPresent()) { roleAccessor.updateUserRoles(optionalUserId.get(), roleAccessor.getRoles(roleIds)); return true; } return false; } }
DefaultUserAccessor implements UserAccessor { @Override public boolean assignRoles(String username, Set<Long> roleIds) { Optional<Long> optionalUserId = userRepository.findByUserName(username).map(UserEntity::getId); if (optionalUserId.isPresent()) { roleAccessor.updateUserRoles(optionalUserId.get(), roleAccessor.getRoles(roleIds)); return true; } return false; } @Autowired  DefaultUserAccessor(UserRepository userRepository, UserRoleRepository userRoleRepository, PasswordEncoder defaultPasswordEncoder, DefaultRoleAccessor roleAccessor,
        AuthenticationTypeAccessor authenticationTypeAccessor); }
DefaultUserAccessor implements UserAccessor { @Override public boolean assignRoles(String username, Set<Long> roleIds) { Optional<Long> optionalUserId = userRepository.findByUserName(username).map(UserEntity::getId); if (optionalUserId.isPresent()) { roleAccessor.updateUserRoles(optionalUserId.get(), roleAccessor.getRoles(roleIds)); return true; } return false; } @Autowired  DefaultUserAccessor(UserRepository userRepository, UserRoleRepository userRoleRepository, PasswordEncoder defaultPasswordEncoder, DefaultRoleAccessor roleAccessor,
        AuthenticationTypeAccessor authenticationTypeAccessor); @Override List<UserModel> getUsers(); @Override Optional<UserModel> getUser(Long userId); @Override Optional<UserModel> getUser(String username); @Override UserModel addUser(String userName, String password, String emailAddress); @Override UserModel addUser(UserModel user, boolean passwordEncoded); @Override UserModel updateUser(UserModel user, boolean passwordEncoded); @Override boolean assignRoles(String username, Set<Long> roleIds); @Override boolean changeUserPassword(String username, String newPassword); @Override boolean changeUserEmailAddress(String username, String emailAddress); @Override void deleteUser(String userName); @Override void deleteUser(Long userId); }
DefaultUserAccessor implements UserAccessor { @Override public boolean assignRoles(String username, Set<Long> roleIds) { Optional<Long> optionalUserId = userRepository.findByUserName(username).map(UserEntity::getId); if (optionalUserId.isPresent()) { roleAccessor.updateUserRoles(optionalUserId.get(), roleAccessor.getRoles(roleIds)); return true; } return false; } @Autowired  DefaultUserAccessor(UserRepository userRepository, UserRoleRepository userRoleRepository, PasswordEncoder defaultPasswordEncoder, DefaultRoleAccessor roleAccessor,
        AuthenticationTypeAccessor authenticationTypeAccessor); @Override List<UserModel> getUsers(); @Override Optional<UserModel> getUser(Long userId); @Override Optional<UserModel> getUser(String username); @Override UserModel addUser(String userName, String password, String emailAddress); @Override UserModel addUser(UserModel user, boolean passwordEncoded); @Override UserModel updateUser(UserModel user, boolean passwordEncoded); @Override boolean assignRoles(String username, Set<Long> roleIds); @Override boolean changeUserPassword(String username, String newPassword); @Override boolean changeUserEmailAddress(String username, String emailAddress); @Override void deleteUser(String userName); @Override void deleteUser(Long userId);  }