UserService extends Service { @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @GenerateLink(rel = LINK_REL_USER) @ApiOperation(value = "Create a new user", response = UserDto.class) @ApiResponses({ @ApiResponse( code = 201, message = "User successfully created, response contains created entity"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 401, message = "Missed token parameter"), @ApiResponse(code = 500, message = "Couldn't create user due to internal server error") }) public Response create( @ApiParam("New user") UserDto userDto, @ApiParam("Authentication token") @QueryParam("token") String token, @ApiParam("User type") @QueryParam("temporary") @DefaultValue("false") Boolean isTemporary) throws BadRequestException, UnauthorizedException, ConflictException, ServerException { if (userDto != null) { userDto.setId(null); } final User newUser = token == null ? userDto : tokenValidator.validateToken(token); userValidator.checkUser(newUser); return Response.status(CREATED) .entity( linksInjector.injectLinks( asDto(userManager.create(newUser, isTemporary)), getServiceContext())) .build(); } @Inject  UserService(
      UserManager userManager,
      TokenValidator tokenValidator,
      UserValidator userNameValidator,
      UserLinksInjector linksInjector,
      @Named(USER_SELF_CREATION_ALLOWED) boolean userSelfCreationAllowed); @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @GenerateLink(rel = LINK_REL_USER) @ApiOperation(value = "Create a new user", response = UserDto.class) @ApiResponses({ @ApiResponse( code = 201, message = "User successfully created, response contains created entity"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 401, message = "Missed token parameter"), @ApiResponse(code = 500, message = "Couldn't create user due to internal server error") }) Response create(
      @ApiParam("New user") UserDto userDto,
      @ApiParam("Authentication token") @QueryParam("token") String token,
      @ApiParam("User type") @QueryParam("temporary") @DefaultValue("false") Boolean isTemporary); @GET @Produces(APPLICATION_JSON) @GenerateLink(rel = LINK_REL_CURRENT_USER) @ApiOperation("Get logged in user") @ApiResponses({ @ApiResponse(code = 200, message = "The response contains currently logged in user entity"), @ApiResponse(code = 500, message = "Couldn't get user due to internal server error") }) UserDto getCurrent(); @POST @Path("/password") @Consumes(APPLICATION_FORM_URLENCODED) @GenerateLink(rel = LINK_REL_CURRENT_USER_PASSWORD) @ApiOperation( value = "Update password of logged in user", notes = "Password must contain at least 8 characters, " + "passport must contain letters and digits") @ApiResponses({ @ApiResponse(code = 204, message = "Password successfully updated"), @ApiResponse( code = 400, message = "Incoming password is invalid value." + "Valid password must contain at least 8 character " + "which are letters and digits"), @ApiResponse(code = 500, message = "Couldn't update password due to internal server error") }) void updatePassword(
      @ApiParam(value = "New password", required = true) @FormParam("password") String password); @GET @Path("/{id}") @Produces(APPLICATION_JSON) @GenerateLink(rel = LINK_REL_USER) @ApiOperation("Get user by identifier") @ApiResponses({ @ApiResponse(code = 200, message = "The response contains requested user entity"), @ApiResponse(code = 404, message = "User with requested identifier not found"), @ApiResponse(code = 500, message = "Impossible to get user due to internal server error") }) UserDto getById(@ApiParam("User identifier") @PathParam("id") String id); @GET @Path("/find") @Produces(APPLICATION_JSON) @GenerateLink(rel = LINK_REL_USER) @ApiOperation("Get user by email or name") @ApiResponses({ @ApiResponse(code = 200, message = "The response contains requested user entity"), @ApiResponse(code = 404, message = "User with requested email/name not found"), @ApiResponse(code = 500, message = "Impossible to get user due to internal server error") }) UserDto find(
      @ApiParam("User email, if it is set then name shouldn't be") @QueryParam("email")
          String email,
      @ApiParam("User name, if is is set then email shouldn't be") @QueryParam("name") String name); @DELETE @Path("/{id}") @GenerateLink(rel = LINK_REL_USER) @ApiOperation("Delete user") @ApiResponses({ @ApiResponse(code = 204, message = "User successfully removed"), @ApiResponse( code = 409, message = "Couldn't remove user due to conflict(e.g. it has related entities)"), @ApiResponse(code = 500, message = "Couldn't remove user due to internal server error") }) void remove(@ApiParam("User identifier") @PathParam("id") String id); @GET @Path("/settings") @Produces(APPLICATION_JSON) Map<String, String> getSettings(); static final String USER_SELF_CREATION_ALLOWED; }
@Test public void shouldCreateUserFromToken() throws Exception { when(tokenValidator.validateToken("token_value")) .thenReturn(new UserImpl("id", "test@eclipse.org", "test")); final Response response = given() .auth() .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) .when() .contentType("application/json") .post(SECURE_PATH + "/user?token=token_value"); assertEquals(response.statusCode(), 201); verify(userManager).create(userCaptor.capture(), anyBoolean()); final User user = userCaptor.getValue(); assertEquals(user.getEmail(), "test@eclipse.org"); assertEquals(user.getName(), "test"); }
@Test public void shouldCreateUserFromEntity() throws Exception { final UserDto newUser = newDto(UserDto.class) .withName("test") .withEmail("test@codenvy.com") .withPassword("password12345"); final Response response = given() .auth() .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) .when() .body(newUser) .contentType("application/json") .post(SECURE_PATH + "/user"); assertEquals(response.statusCode(), 201); verify(userManager).create(userCaptor.capture(), anyBoolean()); final User user = userCaptor.getValue(); assertEquals(user.getEmail(), "test@codenvy.com"); assertEquals(user.getName(), "test"); assertEquals(user.getPassword(), "password12345"); }