RegistrationController { @RequestMapping(method = RequestMethod.POST) public String submitRegistrationForm(Player player, BindingResult result, HttpServletRequest request) { if (result.hasErrors()) { return "register"; } if (playerService.alreadyRegistered(player.getName())) { playerService.updatePlayer(player); return "redirect:/board/" + player.getName(); } if (gameSettingsService.getCurentProtocol().equals(TetrisPlayerService.WS_PROTOCOL)) { player.setCallbackUrl(request.getRemoteAddr()); } playerService.addNewPlayer(player.getName(), player.getCallbackUrl(), gameSettingsService.getCurentProtocol()); return "redirect:/board/" + player.getName(); } RegistrationController();   RegistrationController(TetrisPlayerService playerService, GameSettings gameSettingsService); @RequestMapping(method = RequestMethod.GET) String openRegistrationForm(HttpServletRequest request, Model model); @RequestMapping(params = "remove_me", method = RequestMethod.GET) String removeUserFromGame(HttpServletRequest request, Model model); @RequestMapping(method = RequestMethod.POST) String submitRegistrationForm(Player player, BindingResult result, HttpServletRequest request);  }
@Test public void shouldUpdatePlayerWhenExistsOnSubmitRegistrationForm() { when(bindingResult.hasErrors()).thenReturn(false); when(playerService.alreadyRegistered("vasia")).thenReturn(true); Player player = new Player("vasia", "URL", null, null, null); String jsp = controller.submitRegistrationForm(player, bindingResult, request); assertEquals("redirect:/board/vasia", jsp); verify(playerService).updatePlayer(player); }
@Test public void shouldCreatePlayerWhenNotExistsOnSubmitRegistrationForm() { when(bindingResult.hasErrors()).thenReturn(false); when(playerService.alreadyRegistered("vasia")).thenReturn(false); Player player = new Player("vasia", "URL", null, null, null); String jsp = controller.submitRegistrationForm(player, bindingResult, request); assertEquals("redirect:/board/vasia", jsp); verify(playerService).addNewPlayer("vasia", "URL", "http"); }
@Test public void shouldStayOnRegistrationFormWhenRegisterLocalhostUser() { when(bindingResult.hasErrors()).thenReturn(false); when(playerService.alreadyRegistered("vasia")).thenReturn(false); Player player = new Player("vasia", "http: String jsp = controller.submitRegistrationForm(player, bindingResult, request); assertEquals("register", jsp); verify(playerService).addNewPlayer("vasia", "http: }
@Test public void shouldStayOnRegistrationFormWhenError() { when(bindingResult.hasErrors()).thenReturn(true); String jsp = controller.submitRegistrationForm(null, bindingResult, request); assertEquals("register", jsp); verifyNoMoreInteractions(playerService); }