@Test
 public void shouldOnPasswordUpdateReplacePlayerId() {
   PlayerProfile userProfile = setUpPlayerProfile();
   PasswordChangeRequest passwordChangeForm = setUpPasswordUpdate();
   when(bindingResult.hasErrors()).thenReturn(false);
   underTest.updatedPassword(
       request, response, false, passwordChangeForm, bindingResult, modelMap);
   PasswordChangeRequest expectedPasswordChangeForm =
       new PasswordChangeFormTestBuilder(passwordChangeForm)
           .withPlayerId(userProfile.getPlayerId())
           .build();
   verify(playerProfileService)
       .updatePassword(userProfile.getPlayerId(), expectedPasswordChangeForm);
 }
 @Test
 public void shouldOnUpdateFbSyncUpdateUpdateFbSyncForPlayer() {
   PlayerProfile userProfile = setUpPlayerProfile();
   boolean expectedFbSync = true;
   underTest.updateFbSync(request, response, false, expectedFbSync, modelMap);
   verify(playerProfileService, times(1)).updateSyncFor(userProfile.getPlayerId(), expectedFbSync);
 }
 private PlayerProfile setupUpdateEmailAddress(String emailAddress) {
   PlayerProfile userProfile = getUserProfile();
   when(lobbySessionCache.getActiveSession(request)).thenReturn(getLobbySession(userProfile));
   PlayerProfile finalUserProfile = getUserProfileWithEmailAddress(emailAddress);
   when(lobbySessionCache.getActiveSession(request)).thenReturn(getLobbySession(finalUserProfile));
   when(playerProfileService.findByPlayerId(userProfile.getPlayerId()))
       .thenReturn(finalUserProfile);
   return finalUserProfile;
 }
 private PlayerProfile setupUpdateDisplayName(String displayName) {
   PlayerProfile userProfile = getUserProfile();
   when(lobbySessionCache.getActiveSession(request)).thenReturn(getLobbySession(userProfile));
   PlayerProfile finalUserProfile = getUserProfileWithDisplayName(displayName);
   when(lobbySessionCache.getActiveSession(request)).thenReturn(getLobbySession(finalUserProfile));
   when(playerProfileService.findByPlayerId(userProfile.getPlayerId()))
       .thenReturn(finalUserProfile);
   return finalUserProfile;
 }
  @Test
  public void shouldClearPlayerProfileCacheOnUpdateEmailAddressForUser() {
    String expectedEmailAddress = "*****@*****.**";

    PlayerProfile expectedUserProfile = setupUpdateEmailAddress(expectedEmailAddress);
    underTest.updateCommunicationEmailAddress(
        request, response, false, new EmailAddress(expectedUserProfile), bindingResult, modelMap);

    verify(playerProfileCacheRemovalListener).playerUpdated(expectedUserProfile.getPlayerId());
  }
  @Test
  public void shouldClearPlayerProfileCacheOnUpdateOfDisplayNameForUser() {
    final DisplayName expectedDisplayName = new DisplayName("Super Cool New Name");
    final PlayerProfile userProfile = setupUpdateDisplayName(expectedDisplayName.getDisplayName());

    underTest.updateDisplayName(
        request, response, false, expectedDisplayName, bindingResult, modelMap);

    verify(playerProfileCacheRemovalListener).playerUpdated(userProfile.getPlayerId());
  }
  @Test
  public void shouldUpdateDisplayNameForUser() {
    DisplayName expectedDisplayName = new DisplayName("Super Cool New Name");
    PlayerProfile userProfile = setupUpdateDisplayName(expectedDisplayName.getDisplayName());

    underTest.updateDisplayName(
        request, response, false, expectedDisplayName, bindingResult, modelMap);

    verify(playerProfileService)
        .updateDisplayName(userProfile.getPlayerId(), expectedDisplayName.getDisplayName());
  }
 private LobbySession getLobbySession(PlayerProfile userProfile) {
   return new LobbySession(
       BigDecimal.valueOf(3141592),
       userProfile.getPlayerId(),
       userProfile.getDisplayName(),
       "session",
       Partner.YAZINO,
       "anAvatarUrl",
       userProfile.getEmailAddress(),
       null,
       false,
       WEB,
       AuthProvider.YAZINO);
 }
  @Test
  public void shouldClearPlayerProfileCacheOnUpdateUserProfileInfoForUser() {
    String expectedAvatarUrl = "http://lol/avatar";
    String expectedGender = "M";
    String expectedCountry = "Brerea";
    DateTime expectedDateOfBirth = new DateTime(System.currentTimeMillis());

    PlayerProfile expectedUserProfile =
        setUpUserProfileInfo(
            expectedAvatarUrl, expectedGender, expectedCountry, expectedDateOfBirth);
    PlayerProfileSummary expectedUserProfileInfo =
        new PlayerProfileSummaryBuilder(expectedUserProfile).build();
    underTest.updateUserProfileInfo(
        request, response, false, expectedUserProfileInfo, bindingResult, modelMap);

    verify(playerProfileCacheRemovalListener).playerUpdated(expectedUserProfile.getPlayerId());
  }
  @Test
  public void shouldClearPlayerProfileCacheOnAvatarUpdate() throws Exception {
    final PlayerProfile playerProfile = setUpPlayerProfile();

    final MultipartFile multipartFile = mock(MultipartFile.class);
    final String expectedOriginalFilename = "WORD";
    final byte[] expectedBytes = expectedOriginalFilename.getBytes();

    when(multipartFile.getBytes()).thenReturn(expectedBytes);
    when(multipartFile.getOriginalFilename()).thenReturn(expectedOriginalFilename);

    when(avatarRepository.storeAvatar(expectedOriginalFilename, expectedBytes))
        .thenReturn(new Avatar("one", "two"));

    underTest.updateUserAvatar(request, response, true, multipartFile, modelMap);

    verify(playerProfileCacheRemovalListener).playerUpdated(playerProfile.getPlayerId());
  }
  @Test
  public void shouldUpdateAvatarUrl() throws Exception {
    PlayerProfile userProfile = setUpPlayerProfile();

    MultipartFile multipartFile = mock(MultipartFile.class);
    String expectedOriginalFilename = "WORD";
    byte[] expectedBytes = expectedOriginalFilename.getBytes();

    when(multipartFile.getBytes()).thenReturn(expectedBytes);
    when(multipartFile.getOriginalFilename()).thenReturn(expectedOriginalFilename);

    Avatar expectedAvatar = new Avatar("one", "two");
    when(avatarRepository.storeAvatar(expectedOriginalFilename, expectedBytes))
        .thenReturn(expectedAvatar);

    underTest.updateUserAvatar(request, response, false, multipartFile, modelMap);

    verify(playerProfileService).updateAvatar(userProfile.getPlayerId(), expectedAvatar);
  }
  private PlayerProfile setUpUserProfileInfo(
      final String avatarUrl,
      final String gender,
      final String country,
      final DateTime dateOfBirth) {

    PlayerProfile userProfile = getUserProfile();
    when(lobbySessionCache.getActiveSession(request)).thenReturn(getLobbySession(userProfile));
    PlayerProfile finalUserProfile =
        PlayerProfile.copy(userProfile)
            .withGender(Gender.getById(gender))
            .withCountry(country)
            .withDateOfBirth(dateOfBirth)
            .asProfile();

    when(lobbySessionCache.getActiveSession(request)).thenReturn(getLobbySession(finalUserProfile));
    when(playerProfileService.findByPlayerId(userProfile.getPlayerId()))
        .thenReturn(finalUserProfile);
    return finalUserProfile;
  }
 private PlayerProfile setUpPlayerProfile() {
   PlayerProfile userProfile = getUserProfile();
   when(lobbySessionCache.getActiveSession(request)).thenReturn(getLobbySession(userProfile));
   when(playerProfileService.findByPlayerId(userProfile.getPlayerId())).thenReturn(userProfile);
   return userProfile;
 }