@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
 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 = 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 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 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 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 registerUser_usesDAO() {
   when(userRepository.save(userA)).thenReturn(userAStored);
   communityServiceImpl.registerUser(userA);
   verify(userRepository, times(1)).save(userA);
 }