@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);
 }
 @Test
 public void shouldNotAddEmailAddressIfInMap() {
   String expectedAttribute = "My Object";
   modelMap.addAttribute(PlayerProfileController.EMAIL_ADDRESS_OBJECT_KEY, expectedAttribute);
   setUpPlayerProfile();
   underTest.getMainProfilePage(modelMap, request, response, true);
   assertEquals(expectedAttribute, modelMap.get(PlayerProfileController.EMAIL_ADDRESS_OBJECT_KEY));
 }
 @Test
 public void shouldNotAddDisplayNameIfInMap() {
   String expectedAttribute = "My Object";
   modelMap.addAttribute(PlayerProfileController.DISPLAY_NAME_OBJECT_KEY, expectedAttribute);
   setUpPlayerProfile();
   underTest.getMainProfilePage(modelMap, request, response, true);
   assertEquals(expectedAttribute, modelMap.get(PlayerProfileController.DISPLAY_NAME_OBJECT_KEY));
 }
 @Test
 public void souldReturnCorrectPartialValueInModel() {
   setUpPlayerProfile();
   boolean expectedPartialValue = true;
   final ModelAndView mainProfilePage =
       underTest.getMainProfilePage(modelMap, request, response, expectedPartialValue);
   assertModelMapContainsPartialValue(expectedPartialValue, mainProfilePage.getModelMap());
 }
 @Test
 public void shouldOnErrorsForUpdatePasswordReturnMainFormAgain() {
   PlayerProfile userProfile = setUpPlayerProfile();
   PasswordChangeRequest passwordChangeForm = setUpPasswordUpdate();
   when(bindingResult.hasErrors()).thenReturn(true);
   assertMainProfilePageViewName(
       underTest.updatedPassword(
           request, response, false, passwordChangeForm, bindingResult, modelMap));
 }
 @Test
 public void shouldOnErrorsForUpdateEmailAddressReturnMainFormAgain() {
   EmailAddress expectedEmailAddress = new EmailAddress("invalid");
   setupUpdateEmailAddress(expectedEmailAddress.getEmailAddress());
   when(bindingResult.hasErrors()).thenReturn(true);
   assertMainProfilePageViewName(
       underTest.updateCommunicationEmailAddress(
           request, response, false, expectedEmailAddress, bindingResult, modelMap));
 }
 @Test
 public void shouldGetCorrectDateYearList() {
   Map<String, String> map = underTest.populateMonthsYears();
   Calendar calendar = Calendar.getInstance();
   int year = calendar.get(Calendar.YEAR);
   assertEquals(101, map.size());
   assertEquals(Integer.toString(year), map.values().toArray()[0]);
   assertEquals(Integer.toString(year - 100), map.values().toArray()[100]);
 }
 @Test
 public void shouldOnEmailAddressNotUpdateAnythingIfErrors() {
   EmailAddress expectedEmailAddress = new EmailAddress("invalid");
   setupUpdateEmailAddress(expectedEmailAddress.getEmailAddress());
   when(bindingResult.hasErrors()).thenReturn(true);
   underTest.updateCommunicationEmailAddress(
       request, response, false, expectedEmailAddress, bindingResult, modelMap);
   verifyNoUpdatesDone();
 }
  @Test
  public void shouldReturnDefaultFormOnErrorWhenUpdatingAvatarUrl() throws Exception {
    setUpPlayerProfile();
    MultipartFile multipartFile = mock(MultipartFile.class);
    when(multipartFile.getBytes()).thenThrow(new IOException());

    assertMainProfilePageViewName(
        underTest.updateUserAvatar(request, response, false, multipartFile, modelMap));
  }
 @Test
 public void shouldOnUpdateDisplayNameNotUpdateAnythingIfErrors() {
   DisplayName expectedDisplayName = new DisplayName("Super Cool New Name");
   setUpPlayerProfile();
   when(bindingResult.hasErrors()).thenReturn(true);
   underTest.updateDisplayName(
       request, response, false, expectedDisplayName, bindingResult, modelMap);
   verifyNoUpdatesDone();
 }
 @Test
 public void shouldOnErrorsForDisplayNameReturnPlayerObjectAgain() {
   DisplayName expectedDisplayName = new DisplayName("Super Cool New Name");
   PlayerProfile expectedUserProfile = setUpPlayerProfile();
   when(bindingResult.hasErrors()).thenReturn(true);
   underTest.updateDisplayName(
       request, response, false, expectedDisplayName, bindingResult, modelMap);
   assertModelMapContainsPlayerObject(expectedUserProfile);
 }
 @Test
 public void shouldOnErrorsForDisplayNameReturnMainFormAgain() {
   DisplayName expectedDisplayName = new DisplayName("Super Cool New Name");
   setUpPlayerProfile();
   when(bindingResult.hasErrors()).thenReturn(true);
   assertMainProfilePageViewName(
       underTest.updateDisplayName(
           request, response, false, expectedDisplayName, bindingResult, modelMap));
 }
 @Test
 public void shouldOnUpdateDisplayNameReturnCorrectPartialValue() {
   DisplayName expectedDisplayName = new DisplayName("Super Cool New Name");
   setUpPlayerProfile();
   when(bindingResult.hasErrors()).thenReturn(false);
   ModelAndView modelAndView =
       underTest.updateDisplayName(
           request, response, true, expectedDisplayName, bindingResult, modelMap);
   assertTrue(((RedirectView) modelAndView.getView()).getUrl().endsWith("?partial=true"));
 }
  @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 shouldOnUpdatePasswordReturnCorrectPartialValue() {
   PlayerProfile userProfile = setUpPlayerProfile();
   PasswordChangeRequest passwordChangeForm = setUpPasswordUpdate();
   when(bindingResult.hasErrors()).thenReturn(false);
   ModelAndView modelAndView =
       underTest.updatedPassword(
           request, response, true, passwordChangeForm, bindingResult, modelMap);
   assertTrue(((RedirectView) modelAndView.getView()).getUrl().endsWith("?partial=true"));
 }
 @Test
 public void shouldOnUpdateCommunicationEmailReturnCorrectPartialValue() {
   EmailAddress expectedEmailAddress = new EmailAddress("dont@@hit.me");
   setUpPlayerProfile();
   when(bindingResult.hasErrors()).thenReturn(false);
   ModelAndView modelAndView =
       underTest.updateCommunicationEmailAddress(
           request, response, true, expectedEmailAddress, bindingResult, modelMap);
   assertTrue(((RedirectView) modelAndView.getView()).getUrl().endsWith("?partial=true"));
 }
  @Test
  public void shouldUpdateDisplayNameForUserAndRedirectToOriginalForm() {
    DisplayName expectedDisplayName = new DisplayName("Super Cool New Name");
    setupUpdateDisplayName(expectedDisplayName.getDisplayName());

    assertRedirectToMainProfilePage(
        underTest.updateDisplayName(
            request, response, false, expectedDisplayName, bindingResult, modelMap));
    assertModelMapIsEmptyToAvoidQueryStringOnRedirect(modelMap);
  }
  @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 shouldOnUpdateAvatarNotHavingFileNameReturnDefaultInfo() {
    setUpPlayerProfile();

    MultipartFile multipartFile = mock(MultipartFile.class);
    when(multipartFile.getOriginalFilename()).thenReturn("");

    underTest.updateUserAvatar(request, response, true, multipartFile, modelMap);
    verify(playerProfileService, never()).updateAvatar(any(BigDecimal.class), any(Avatar.class));
  }
 @Test
 public void shouldRedirectOnSuccessfulUpdateOfPassword() {
   PlayerProfile userProfile = setUpPlayerProfile();
   PasswordChangeRequest passwordChangeForm = setUpPasswordUpdate();
   when(bindingResult.hasErrors()).thenReturn(false);
   assertRedirectToMainProfilePage(
       underTest.updatedPassword(
           request, response, false, passwordChangeForm, bindingResult, modelMap));
   assertModelMapIsEmptyToAvoidQueryStringOnRedirect(modelMap);
 }
 @Test
 public void shouldOnErrorsForUpdateEmailAddressReturnPlayerObjectAgain() {
   EmailAddress expectedEmailAddress = new EmailAddress("dont@@hit.me");
   PlayerProfile expectedUserProfile =
       setupUpdateEmailAddress(expectedEmailAddress.getEmailAddress());
   when(bindingResult.hasErrors()).thenReturn(true);
   underTest.updateCommunicationEmailAddress(
       request, response, false, expectedEmailAddress, bindingResult, modelMap);
   assertModelMapContainsPlayerObject(expectedUserProfile);
 }
 @Test
 public void shouldGetCorrectListOfMonths() {
   DateUtils dateUtils = new DateUtils();
   final String[] months = dateUtils.getShortFormMonthsOfYear();
   final String[] keys =
       new String[] {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
   Map<String, String> actualMonths = underTest.populateMonths();
   assertEquals("expect size to be 12", 12, actualMonths.size());
   assertArrayEquals(months, actualMonths.values().toArray());
   assertArrayEquals(keys, actualMonths.keySet().toArray());
 }
  @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());
  }
  @Test
  public void resolveExceptionShouldSetCountries() {
    long maxUploadSize = 1000;
    setUpPlayerProfile();

    final ModelAndView modelAndViewUnderTest =
        underTest.resolveException(
            request, response, null, new MaxUploadSizeExceededException(maxUploadSize));
    assertSame(
        countryRepository.getCountries(), modelAndViewUnderTest.getModelMap().get("countries"));
  }
  @Test
  public void resolveExceptionShouldSetGender() {
    long maxUploadSize = 1000;
    setUpPlayerProfile();

    final ModelAndView modelAndViewUnderTest =
        underTest.resolveException(
            request, response, null, new MaxUploadSizeExceededException(maxUploadSize));
    assertThat(
        genderRepository.getGenders(),
        is(equalTo(modelAndViewUnderTest.getModelMap().get("genders"))));
  }
  @Test
  public void resolveExceptionShouldSetPartial() {
    long maxUploadSize = 1000;
    setUpPlayerProfile();

    Boolean expectedPartialValue = Boolean.TRUE;
    request.setParameter(AbstractProfileController.PARTIAL_URI_FLAG, "true");
    ModelAndView modelAndViewUnderTest =
        underTest.resolveException(
            request, response, null, new MaxUploadSizeExceededException(maxUploadSize));

    assertEquals(
        expectedPartialValue,
        modelAndViewUnderTest.getModel().get(AbstractProfileController.PARTIAL_URI_FLAG));

    request.setParameter(AbstractProfileController.PARTIAL_URI_FLAG, "false");
    modelAndViewUnderTest =
        underTest.resolveException(
            request, response, null, new MaxUploadSizeExceededException(maxUploadSize));
    ProfileTestHelper.assertPartialFlagSetCorrectly(modelAndViewUnderTest, false);
  }
 @Test
 public void shouldOnUpdatePasswordNotUpdateAnythingIfErrors() {
   PlayerProfile userProfile = setUpPlayerProfile();
   PasswordChangeRequest passwordChangeForm = setUpPasswordUpdate();
   passwordChangeForm =
       new PasswordChangeFormTestBuilder(passwordChangeForm).withConfirmNewPassword("bob").build();
   when(bindingResult.hasErrors()).thenReturn(true);
   underTest.updatedPassword(
       request, response, false, passwordChangeForm, bindingResult, modelMap);
   verify(passwordChangeFormValidator).validate(passwordChangeForm, bindingResult);
   verify(playerProfileService, never())
       .updatePlayerInfo(any(BigDecimal.class), any(PlayerProfileSummary.class));
 }
  @Test
  public void shouldReturnDefaultFormOnErrorWhenAvatarHasAnInvalidExtension() throws Exception {
    setUpPlayerProfile();
    MultipartFile multipartFile = mock(MultipartFile.class);
    when(multipartFile.getBytes()).thenReturn("file".getBytes());
    when(multipartFile.getOriginalFilename()).thenReturn("aFile.pdf");
    when(avatarRepository.storeAvatar("aFile.pdf", "file".getBytes()))
        .thenThrow(new IllegalArgumentException("Invalid extension"));

    assertMainProfilePageViewName(
        underTest.updateUserAvatar(request, response, false, multipartFile, modelMap));
    assertThat((String) modelMap.get("avatarHasErrors"), is(equalTo("true")));
  }
 @Test
 public void shouldOnErrorsForPlayerInfoReturnMainFormAgain() {
   PlayerProfile expectedUserProfile = setUpPlayerProfile();
   when(bindingResult.hasErrors()).thenReturn(true);
   assertMainProfilePageViewName(
       underTest.updateUserProfileInfo(
           request,
           response,
           false,
           new PlayerProfileSummaryBuilder(expectedUserProfile).build(),
           bindingResult,
           modelMap));
 }
 @Test
 public void shouldOnErrorsForPlayerInfoReturnPlayerObjectAgain() {
   PlayerProfile expectedUserProfile = setUpPlayerProfile();
   when(bindingResult.hasErrors()).thenReturn(true);
   underTest.updateUserProfileInfo(
       request,
       response,
       false,
       new PlayerProfileSummaryBuilder(expectedUserProfile).build(),
       bindingResult,
       modelMap);
   assertModelMapContainsPlayerObject(expectedUserProfile);
 }