AccountResource { @POST @Path("/create") @ApiOperation(value = "Create a new account", response = AccountDTO.class, authorizations = {}) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of created AccountDTO. Response will contain authentication token."), @ApiResponse(code = 202, message = "Account creation successful, but not yet enabled"), @ApiResponse(code = 400, message = "Bad request, read response message for more details"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) public Response createAccount( @Context HttpServletRequest request, @Context HttpServletResponse response, @ApiParam(required = true, value = "Account to create") AccountDTO accountDTO) throws EntityAlreadyExistsException, CreationException { Account account = accountManager.createAccount(accountDTO.getLogin(), accountDTO.getName(), accountDTO.getEmail(), accountDTO.getLanguage(), accountDTO.getNewPassword(), accountDTO.getTimeZone()); HttpSession session = request.getSession(); if (account.isEnabled()) { String login = account.getLogin(); try { LOGGER.log(Level.INFO, "Authenticating response"); request.authenticate(response); } catch (IOException | ServletException e) { LOGGER.log(Level.WARNING, "Request.authenticate failed", e); return Response.status(Response.Status.FORBIDDEN).entity(e.getMessage()).build(); } session.setAttribute("login", login); session.setAttribute("groups", UserGroupMapping.REGULAR_USER_ROLE_ID); Response.ResponseBuilder responseBuilder = Response.ok() .entity(mapper.map(account, AccountDTO.class)); if (authConfig.isJwtEnabled()) { responseBuilder.header("jwt", JWTokenFactory.createAuthToken(authConfig.getJWTKey(), new UserGroupMapping(login, UserGroupMapping.REGULAR_USER_ROLE_ID))); } return responseBuilder .build(); } else { session.invalidate(); return Response.status(Response.Status.ACCEPTED).build(); } } AccountResource(); @PostConstruct void init(); @GET @Path("/me") @ApiOperation(value = "Get authenticated user's account", response = AccountDTO.class, authorizations = {@Authorization(value = "authorization")}) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of AccountDTO"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) AccountDTO getAccount(); @PUT @Path("/me") @ApiOperation(value = "Update user's account", response = AccountDTO.class, authorizations = {@Authorization(value = "authorization")}) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of updated AccountDTO"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) Response updateAccount(
            @ApiParam(hidden = true) @HeaderParam("Authorization") String authorizationString,
            @ApiParam(required = true, value = "Updated account") AccountDTO accountDTO); @POST @Path("/create") @ApiOperation(value = "Create a new account", response = AccountDTO.class, authorizations = {}) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of created AccountDTO. Response will contain authentication token."), @ApiResponse(code = 202, message = "Account creation successful, but not yet enabled"), @ApiResponse(code = 400, message = "Bad request, read response message for more details"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) Response createAccount(
            @Context HttpServletRequest request,
            @Context HttpServletResponse response,
            @ApiParam(required = true, value = "Account to create") AccountDTO accountDTO); @GET @Path("/workspaces") @ApiOperation(value = "Get workspaces where authenticated user is active", response = WorkspaceDTO.class, responseContainer = "List", authorizations = {@Authorization(value = "authorization")}) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of Workspaces. It can be an empty list."), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) Response getWorkspaces(); @PUT @Path("gcm") @ApiOperation(value = "Update GCM account for authenticated user", response = Response.class, authorizations = {@Authorization(value = "authorization")}) @ApiResponses(value = { @ApiResponse(code = 204, message = "Successful retrieval of created GCMAccount."), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 500, message = "Internal server error") }) @Consumes(MediaType.APPLICATION_JSON) Response setGCMAccount(
            @ApiParam(required = true, value = "GCM account to set") GCMAccountDTO data); @DELETE @Path("gcm") @ApiOperation(value = "Update GCM account for authenticated user", response = Response.class, authorizations = {@Authorization(value = "authorization")}) @ApiResponses(value = { @ApiResponse(code = 204, message = "Successful delete of GCMAccount"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 500, message = "Internal server error") }) Response deleteGCMAccount();  }
@Test public void createAccountTest() throws ApplicationException, IOException, ServletException { Key key = new HmacKey("verySecretPhrase".getBytes("UTF-8")); Mockito.when(authConfig.getJWTKey()).thenReturn(key); HttpServletRequest mockedRequest = Mockito.mock(HttpServletRequest.class); HttpServletResponse mockedResponse = Mockito.mock(HttpServletResponse.class); HttpSession mockedSession = Mockito.mock(HttpSession.class); Mockito.when(mockedRequest.getSession()).thenReturn(mockedSession); AccountDTO accountDTO = new AccountDTO(); Account account = new Account(); Mockito.when(accountManager.createAccount(Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyString(), Matchers.anyString())) .thenReturn(account); Response res = accountResource.createAccount(mockedRequest, mockedResponse, accountDTO); Assert.assertEquals(Response.Status.ACCEPTED.getStatusCode(), res.getStatus()); account.setEnabled(true); res = accountResource.createAccount(mockedRequest, mockedResponse, accountDTO); Assert.assertEquals(Response.Status.OK.getStatusCode(), res.getStatus()); Mockito.when(authConfig.isJwtEnabled()).thenReturn(true); res = accountResource.createAccount(mockedRequest, mockedResponse, accountDTO); Assert.assertNotNull(res.getHeaderString("jwt")); Mockito.when(authConfig.isJwtEnabled()).thenReturn(false); res = accountResource.createAccount(mockedRequest, mockedResponse, accountDTO); Assert.assertNull(res.getHeaderString("jwt")); Mockito.when(mockedRequest.authenticate(mockedResponse)) .thenThrow(new IOException("Mocked exception")); res = accountResource.createAccount(mockedRequest, mockedResponse, accountDTO); Assert.assertEquals(Response.Status.FORBIDDEN.getStatusCode(), res.getStatus()); }