@Test(expected = ConstraintViolationException.class)
 public void createUser_byRoles_throwsForConflict() {
   when(userRepository.save(userA)).thenReturn(userA);
   when(userRepository.findOne(USER_A_NAME))
       .thenThrow(new ConstraintViolationException("", null, null));
   communityServiceImpl.createUser(userA, Role.ROLE_BASIC);
 }
 @Test(expected = ConstraintViolationException.class)
 public void createUserWithBalance_throwsForConflict() {
   when(userRepository.save(userA)).thenReturn(userA);
   when(userRepository.findOne(USER_A_NAME))
       .thenThrow(new ConstraintViolationException("", null, null));
   Role[] roles = {Role.ROLE_BASIC, Role.ROLE_OAUTH2};
   BigDecimal balance = new BigDecimal(123);
   communityServiceImpl.createUserWithBalance(userA, roles, balance);
 }
 @Test
 public void findAll_usesDAO() {
   when(userRepository.findAll(userPageable)).thenReturn(userResultsPage);
   Page<User> userPage = communityServiceImpl.findAll(userPageable);
   assertEquals(userResultsPage, userPage);
   verify(userRepository, times(1)).findAll(userPageable);
 }
 @Test
 public void findOne_usesDAO() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userA);
   User user = communityServiceImpl.findOne(USER_A_NAME);
   assertEquals(userA, user);
   verify(userRepository, times(1)).findOne(USER_A_NAME);
 }
 @Test
 public void identifyUser_usesDAO() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userAStored);
   when(passwordEncoder.matches(USER_A_PASSWORD, USER_A_PASSWORD_ENCODED)).thenReturn(true);
   communityServiceImpl.identifyUser(userA);
   verify(userRepository, times(1)).findOne(USER_A_NAME);
 }
 @Test(expected = IllegalArgumentException.class)
 public void identifyUser_throwsIllegalArgumentException_002() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userAStored);
   when(passwordEncoder.matches(USER_A_PASSWORD, USER_A_PASSWORD_ENCODED)).thenReturn(false);
   userA.setPassword("");
   communityServiceImpl.identifyUser(userA);
 }
 @Test
 public void getUserByEmail_usesDAO() {
   when(userRepository.findByEmail(USER_A_EMAIL)).thenReturn(Sets.newHashSet(userAStored));
   User user = communityServiceImpl.getUserByEmail(userA.getEmail());
   verify(userRepository, times(1)).findByEmail(USER_A_EMAIL);
   assertEquals(user, userAStored);
 }
 @Test
 public void getLeaders_setFilteredAttributes() {
   when(userRepository.findAll(userPageable)).thenReturn(userResultsPage);
   Page<UserDTO> page = communityServiceImpl.getLeaders(userPageable);
   assertDTOWithHiddenValues(page.getContent().get(0), userA);
   assertDTOWithHiddenValues(page.getContent().get(1), userB);
 }
 @Test
 public void createUser_usesDAO() {
   when(userRepository.save(userA)).thenReturn(userA);
   User user = communityServiceImpl.createUser(userA, Role.ROLE_BASIC);
   assertEquals(userA, user);
   verify(userRepository, times(1)).save(userA);
 }
  @Test(expected = BadCredentialsException.class)
  public void updateUser_throwsUnAuthorized() {
    when(userRepository.save(userA)).thenReturn(userA);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    when(authentication.getPrincipal()).thenReturn(userB);

    communityServiceImpl.updateUser(userA);
  }
  @Test(expected = ResourceAccessException.class)
  public void loadUserByUsername_throwsResourceAccessException() {
    when(userRepository.findOne(USER_A_NAME)).thenReturn(null);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    when(authentication.getPrincipal()).thenReturn(null);

    communityServiceImpl.loadUserByUsername(USER_A_NAME);
    verify(securityContext, times(1)).getAuthentication();
    verify(authentication, times(1)).getPrincipal();
  }
  @Test
  public void updateUser_setsAttributes() {
    when(userRepository.save(userA)).thenReturn(userA);
    when(passwordEncoder.encode(USER_A_PASSWORD)).thenReturn(USER_A_PASSWORD_ENCODED);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    when(authentication.getPrincipal()).thenReturn(userA);

    User user = communityServiceImpl.updateUser(userA);
    assertEquals(USER_A_PASSWORD_ENCODED, user.getPassword());
    verify(passwordEncoder, times(1)).encode(USER_A_PASSWORD);
  }
  @Test
  public void loadUserByUsername_usesSecurityContext() {
    when(userRepository.findOne(USER_A_NAME)).thenReturn(null);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    when(authentication.getPrincipal()).thenReturn(userA);

    UserDetails result = communityServiceImpl.loadUserByUsername(USER_A_NAME);
    verify(securityContext, times(1)).getAuthentication();
    verify(authentication, times(1)).getPrincipal();
    assertEquals(USER_A_NAME, result.getUsername());
  }
 @Test
 public void createUser_setsAttributes() {
   when(userRepository.save(userB)).thenReturn(userB);
   when(passwordEncoder.encode(USER_B_PASSWORD)).thenReturn(USER_B_PASSWORD_ENCODED);
   User user = communityServiceImpl.createUser(userB, Role.ROLE_BASIC);
   assertEquals(1, user.getAuthorities().size());
   assertEquals(Role.ROLE_BASIC.name(), user.getAuthorities().iterator().next().getAuthority());
   assertEquals(1, user.getActions().size());
   assertEquals(UserActivityType.REGISTER, user.getActions().iterator().next().getType());
   assertEquals(USER_B_PASSWORD_ENCODED, user.getPassword());
   verify(passwordEncoder, times(1)).encode(USER_B_PASSWORD);
 }
  @Test
  public void updateUser_usesDAO_001() {
    when(userRepository.save(userA)).thenReturn(userA);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    when(authentication.getPrincipal()).thenReturn(userA);

    User user = communityServiceImpl.updateUser(userA);
    assertEquals(userA, user);

    verify(userRepository, times(1)).save(userA);
    verify(securityContext, times(1)).getAuthentication();
    verify(authentication, times(1)).getPrincipal();
  }
  @Test
  public void search_usesDAO() {
    Specification<User> searchUserSpec = userSpecifications.idStartsWith(USER_A_NAME);
    userList =
        userList
            .stream()
            .filter(u -> u.getId().startsWith(USER_A_NAME))
            .collect(Collectors.toList());
    userResultsPage = new PageImpl<>(userList, userPageable, userList.size());

    when(userRepository.findAll(searchUserSpec, userPageable)).thenReturn(userResultsPage);
    communityServiceImpl.search(searchUserSpec, userPageable);
    verify(userRepository, times(1)).findAll(searchUserSpec, userPageable);
  }
 @Test
 public void createUser_byRoles_setsAttributes() {
   when(userRepository.save(userB)).thenReturn(userB);
   when(passwordEncoder.encode(USER_B_PASSWORD)).thenReturn(USER_B_PASSWORD_ENCODED);
   Role[] roles = {Role.ROLE_BASIC, Role.ROLE_OAUTH2};
   User user = communityServiceImpl.createUser(userB, roles);
   assertEquals(2, user.getAuthorities().size());
   Iterator<? extends GrantedAuthority> authoritiesIterator = user.getAuthorities().iterator();
   assertEquals(Role.ROLE_BASIC.name(), authoritiesIterator.next().getAuthority());
   assertEquals(Role.ROLE_OAUTH2.name(), authoritiesIterator.next().getAuthority());
   assertEquals(1, user.getActions().size());
   assertEquals(UserActivityType.REGISTER, user.getActions().iterator().next().getType());
   assertEquals(USER_B_PASSWORD_ENCODED, user.getPassword());
   verify(passwordEncoder, times(1)).encode(USER_B_PASSWORD);
 }
  @Test
  public void search_setFilteredAttributes() {
    Specification<User> searchUserSpec = userSpecifications.idStartsWith(USER_A_NAME);
    userList =
        userList
            .stream()
            .filter(u -> u.getId().startsWith(USER_A_NAME))
            .collect(Collectors.toList());
    userResultsPage = new PageImpl<>(userList, userPageable, userList.size());

    when(userRepository.findAll(searchUserSpec, userPageable)).thenReturn(userResultsPage);
    Page<UserDTO> page = communityServiceImpl.search(searchUserSpec, userPageable);
    assertEquals(1, page.getContent().size());
    assertDTOWithHiddenValues(page.getContent().get(0), userA);
  }
 @Test
 public void getLeaders_usesDAO() {
   when(userRepository.findAll(userPageable)).thenReturn(userResultsPage);
   communityServiceImpl.getLeaders(userPageable);
   verify(userRepository, times(1)).findAll(userPageable);
 }
 @Test
 public void loadUserByUsername_usesDAO() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userAStored);
   communityServiceImpl.loadUserByUsername(USER_A_NAME);
   verify(userRepository, times(1)).findOne(USER_A_NAME);
 }
 @Test
 public void registerUser_usesDAO() {
   when(userRepository.save(userA)).thenReturn(userAStored);
   communityServiceImpl.registerUser(userA);
   verify(userRepository, times(1)).save(userA);
 }
 @Test
 public void findByLogin_setsFilteredAttributes() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userAStored);
   UserDTO userDTO = communityServiceImpl.findByLogin(USER_A_NAME);
   assertDTOWithHiddenValues(userDTO, userA);
 }
 @Test
 public void findByLogin_usesDAO() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userAStored);
   communityServiceImpl.findByLogin(USER_A_NAME);
   verify(userRepository, times(1)).findOne(USER_A_NAME);
 }
 @Test(expected = BadCredentialsException.class)
 public void identifyUser_throwsBadCredentialsException() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userAStored);
   when(passwordEncoder.matches(USER_A_PASSWORD, USER_A_PASSWORD_ENCODED)).thenReturn(false);
   communityServiceImpl.identifyUser(userA);
 }
 @Test
 public void getUser_usesDAO() {
   when(userRepository.findOne(USER_A_NAME)).thenReturn(userA);
   communityServiceImpl.getUser(USER_A_NAME);
   verify(userRepository, times(1)).findOne(USER_A_NAME);
 }