UserResource { @POST @Consumes(MEDIA_TYPE) @Produces(MEDIA_TYPE) @RolesAllowed({Role.ADMIN_NAME}) @Operation(summary = "Create a new user") @ApiResponse(responseCode = "200", description = "User created", content = @Content(mediaType = MEDIA_TYPE, schema = @Schema(implementation = User.class))) @ApiResponse(responseCode = "400", description = "Invalid properties for the user or user with the given name already exists.") @RequestBody( description = "The user to be created. " + "Both the password and the name must not be empty. " + "No user must exist with the given name. " + "The id must not be set, i.e., must be null." + " The specified roles must exist.", content = @Content(schema = @Schema(implementation = User.class))) public User newUser(final User user) { if (user.getUsername() == null || user.getUsername().equals("")) { throw new BadRequestException(MSG_INVALID_USERNAME); } if (user.getPassword() == null || user.getPassword().equals("")) { throw new BadRequestException(MSG_INVALID_PASSWORD); } if (user.getId() != null) { throw new BadRequestException("Can't create user with id. Payload must not have an id."); } for (final String r : user.getRoles()) { if (!Role.exists(r)) { throw new BadRequestException(MSG_UNKOWN_ROLE + ": " + r); } } try { user.setPassword(PasswordStorage.createHash(user.getPassword())); } catch (final CannotPerformOperationException e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Could not create user due to password hashing failure: " + e.getMessage()); } throw new InternalServerErrorException(e); } try { return this.userCrudService.saveNewEntity(user); } catch (final DuplicateUserException ex) { throw new BadRequestException("User already exists", ex); } catch (final UserCrudException ex) { LOGGER.error("Error saving user", ex); throw new InternalServerErrorException(); } } @Inject  UserResource(final UserService userCrudService,
      final BatchRequestResource batchSubResource); @POST @Consumes(MEDIA_TYPE) @Produces(MEDIA_TYPE) @RolesAllowed({Role.ADMIN_NAME}) @Operation(summary = "Create a new user") @ApiResponse(responseCode = "200", description = "User created", content = @Content(mediaType = MEDIA_TYPE, schema = @Schema(implementation = User.class))) @ApiResponse(responseCode = "400", description = "Invalid properties for the user or user with the given name already exists.") @RequestBody( description = "The user to be created. " + "Both the password and the name must not be empty. " + "No user must exist with the given name. " + "The id must not be set, i.e., must be null." + " The specified roles must exist.", content = @Content(schema = @Schema(implementation = User.class))) User newUser(final User user); @PATCH @Path("{id}") @RolesAllowed({Role.ADMIN_NAME}) @Produces(MEDIA_TYPE) @Consumes(MEDIA_TYPE) @Operation(summary = "Update an existing User") @ApiResponse(responseCode = "200", description = "Updated user", content = @Content(mediaType = MEDIA_TYPE, schema = @Schema(implementation = User.class))) @ApiResponse(responseCode = "400", description = "Properties to update are invalid") User updateUser(
      @Parameter(description = "Id of the user to update") @PathParam("id") final String id,
      @Parameter(description = "Updated values") final User updatedUser); @GET @RolesAllowed({Role.ADMIN_NAME}) @Produces(MEDIA_TYPE) @Operation(description = "List all users") @Parameters({ @Parameter(in = ParameterIn.QUERY, name = "page[size]", description = "Controls the size, i.e., amount of entities, of each page."), @Parameter(in = ParameterIn.QUERY, name = "page[number]", description = "Index of the page to return."), @Parameter(in = ParameterIn.QUERY, name = "filter[roles]", description = "Restricts the result to the given role(s)."), @Parameter(in = ParameterIn.QUERY, name = "filter[batchid]", description = "Only return users that were created by the batch request " + "with the specified id.")}) @ApiResponse(responseCode = "200", description = "List of users matching the request", content = @Content(array = @ArraySchema(schema = @Schema(implementation = User.class)))) QueryResult<User> find(@Context final UriInfo uri); @GET @Path("{id}") @RolesAllowed({Role.ADMIN_NAME, Role.USER_NAME}) @Produces(MEDIA_TYPE) @Operation(summary = "Find a user by its id") @ApiResponse(responseCode = "200", description = "The requested user", content = @Content(mediaType = MEDIA_TYPE, schema = @Schema(implementation = User.class))) @ApiResponse(responseCode = "404", description = "No such user") User userById(
      @Parameter(description = "Unique id of the user to find") @PathParam("id") final String id); @DELETE @Path("{id}") @RolesAllowed({Role.ADMIN_NAME}) @Operation(summary = "Remove a user identified by its Id") @ApiResponse(responseCode = "400", description = "Attempt to delete the last existing use with the admin role, " + "which is not possible") @ApiResponse(responseCode = "204", description = "User deleted") Response removeUser(@Parameter(
      description = "Unique id of the user to delete") @PathParam("id") final String id); @DELETE @RolesAllowed({Role.ADMIN_NAME}) @Operation(summary = "Delete a list of users") @ApiResponse(responseCode = "204", description = "All user deleted") @ApiResponse(responseCode = "400", description = "Attempt to delete the last existing use with the admin role, " + "which is not possible") Response removeAll(final List<User> users);  }
@Test public void testNewUser() { final User u = new User("testuser"); u.setPassword("testPassword"); final User newUser = this.userResource.newUser(u); assertNotNull(newUser.getId()); assertTrue(Long.parseLong(newUser.getId()) > 0); }
@Test public void testInvalidUsername() { final User u = new User(""); assertThrows(BadRequestException.class, () -> this.userResource.newUser(u)); }
@Test public void testInvalidPassword() { final User u = new User(""); u.setPassword(""); assertThrows(BadRequestException.class, () -> this.userResource.newUser(u)); }
@Test public void testInvalidId() { final User u = new User("12", "name", "pw", new ArrayList<>()); assertThrows(BadRequestException.class, () -> this.userResource.newUser(u)); }
@Test public void testInvalidRoles() { final User u = new User(null, "name", "pw", Collections.singletonList("unknown")); assertThrows(BadRequestException.class, () -> this.userResource.newUser(u)); }