AuthenticationController { @RequestMapping(value = "/login", method = RequestMethod.POST) @ResponseStatus( HttpStatus.CREATED ) @ResponseBody public Map<String, Object> login(@RequestBody AuthenticationRequest authenticationRequest) { logger.debug("AuthenticationController.login: login request for username: " + authenticationRequest.getUsername()); Map<String, Object> authenticationResponse = this.service.login(authenticationRequest.getUsername(), authenticationRequest.getPassword()); return authenticationResponse; }  @RequestMapping(value = "/login", method = RequestMethod.POST) @ResponseStatus( HttpStatus.CREATED ) @ResponseBody Map<String, Object> login(@RequestBody AuthenticationRequest authenticationRequest); @RequestMapping(value = "/logout/{user}", method = RequestMethod.GET) @ResponseStatus( HttpStatus.OK ) @ResponseBody void logout(@PathVariable("user") final String userId); @RequestMapping(value = "/login", method = RequestMethod.GET) @ResponseStatus( HttpStatus.METHOD_NOT_ALLOWED ) void get();  }
@Test public void doLoginPost() throws Exception { when(service.login(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.PASSWORD)).thenReturn(ServiceTestConfiguration.loginResponse()); AuthenticationRequest request = new AuthenticationRequest(); request.setPassword(ServiceTestConfiguration.PASSWORD); request.setUsername(ServiceTestConfiguration.USER_ID); mockMvc.perform(post("/login").contentType(MediaType.APPLICATION_JSON).content(convertObjectToJson(request))) .andExpect(status().isCreated()) .andExpect(jsonPath("$.authToken").value(ServiceTestConfiguration.AUTH_TOKEN)) .andExpect(jsonPath("$.accountid").value(ServiceTestConfiguration.PROFILE_ID.intValue())) .andDo(print()); }
@Test(expected=org.springframework.web.util.NestedServletException.class) public void doLoginPostBadPassword() throws Exception { when(service.login(ServiceTestConfiguration.USER_ID, ServiceTestConfiguration.BAD_PASSWORD)).thenThrow(new AuthenticationException("Login failed for user: " + ServiceTestConfiguration.USER_ID)); AuthenticationRequest request = new AuthenticationRequest(); request.setPassword(ServiceTestConfiguration.BAD_PASSWORD); request.setUsername(ServiceTestConfiguration.USER_ID); mockMvc.perform(post("/login").contentType(MediaType.APPLICATION_JSON).content(convertObjectToJson(request))) .andExpect(status().isCreated()) .andDo(print()); }