@Test void testDeleteUserNotFound() { when(usersDao.findByEmail(EMAIL.getAddress())).thenReturn(USER); when(usersDao.delete(EMAIL.getAddress())) .thenThrow(new DatabaseException(DatabaseError.USER_NOT_FOUND)); Response response = resource.deleteUser(key, "password", EMAIL.getAddress()); assertEquals(Response.Status.NOT_FOUND, response.getStatusInfo()); }
@DELETE @Operation( summary = "Delete a user from the database", description = "Deletes a user from the database and returns the deleted user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully deleted", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "delete-requests") public Response deleteUser( @Parameter(hidden = true) @Auth Principal auth, @Parameter(description = "The password of the user, necessary if the " + "headerPasswordCheck is enabled.") @HeaderParam("password") String password, @Parameter(description = "The email address of the user to delete.", required = true) @QueryParam("email") String email) { try { requestValidator.validate(password, email, false); } catch (ValidationException e) { return Response.status(Response.Status.BAD_REQUEST) .entity(e.getMessage()).build(); } LOG.info("Attempting to delete user {}.", email); User user; try { user = usersDao.findByEmail(email); } catch (DatabaseException e) { LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } if (requestValidator.isPasswordHeaderCheckEnabled() && !hashService.isMatch(password, user.getPassword())) { LOG.error("The password for user {} was incorrect.", email); return Response.status(Response.Status.UNAUTHORIZED) .entity("Unable to validate user with provided credentials.").build(); } User result; try { result = usersDao.delete(email); } catch (DatabaseException e) { LOG.error("Error deleting user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } LOG.info("Successfully deleted user {}.", email); return Response.ok(result).build(); }
UserResource { @DELETE @Operation( summary = "Delete a user from the database", description = "Deletes a user from the database and returns the deleted user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully deleted", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "delete-requests") public Response deleteUser( @Parameter(hidden = true) @Auth Principal auth, @Parameter(description = "The password of the user, necessary if the " + "headerPasswordCheck is enabled.") @HeaderParam("password") String password, @Parameter(description = "The email address of the user to delete.", required = true) @QueryParam("email") String email) { try { requestValidator.validate(password, email, false); } catch (ValidationException e) { return Response.status(Response.Status.BAD_REQUEST) .entity(e.getMessage()).build(); } LOG.info("Attempting to delete user {}.", email); User user; try { user = usersDao.findByEmail(email); } catch (DatabaseException e) { LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } if (requestValidator.isPasswordHeaderCheckEnabled() && !hashService.isMatch(password, user.getPassword())) { LOG.error("The password for user {} was incorrect.", email); return Response.status(Response.Status.UNAUTHORIZED) .entity("Unable to validate user with provided credentials.").build(); } User result; try { result = usersDao.delete(email); } catch (DatabaseException e) { LOG.error("Error deleting user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } LOG.info("Successfully deleted user {}.", email); return Response.ok(result).build(); } }
UserResource { @DELETE @Operation( summary = "Delete a user from the database", description = "Deletes a user from the database and returns the deleted user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully deleted", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "delete-requests") public Response deleteUser( @Parameter(hidden = true) @Auth Principal auth, @Parameter(description = "The password of the user, necessary if the " + "headerPasswordCheck is enabled.") @HeaderParam("password") String password, @Parameter(description = "The email address of the user to delete.", required = true) @QueryParam("email") String email) { try { requestValidator.validate(password, email, false); } catch (ValidationException e) { return Response.status(Response.Status.BAD_REQUEST) .entity(e.getMessage()).build(); } LOG.info("Attempting to delete user {}.", email); User user; try { user = usersDao.findByEmail(email); } catch (DatabaseException e) { LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } if (requestValidator.isPasswordHeaderCheckEnabled() && !hashService.isMatch(password, user.getPassword())) { LOG.error("The password for user {} was incorrect.", email); return Response.status(Response.Status.UNAUTHORIZED) .entity("Unable to validate user with provided credentials.").build(); } User result; try { result = usersDao.delete(email); } catch (DatabaseException e) { LOG.error("Error deleting user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } LOG.info("Successfully deleted user {}.", email); return Response.ok(result).build(); } @Inject  UserResource(UsersDao usersDao,
                      RequestValidator requestValidator,
                      HashService hashService); }
UserResource { @DELETE @Operation( summary = "Delete a user from the database", description = "Deletes a user from the database and returns the deleted user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully deleted", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "delete-requests") public Response deleteUser( @Parameter(hidden = true) @Auth Principal auth, @Parameter(description = "The password of the user, necessary if the " + "headerPasswordCheck is enabled.") @HeaderParam("password") String password, @Parameter(description = "The email address of the user to delete.", required = true) @QueryParam("email") String email) { try { requestValidator.validate(password, email, false); } catch (ValidationException e) { return Response.status(Response.Status.BAD_REQUEST) .entity(e.getMessage()).build(); } LOG.info("Attempting to delete user {}.", email); User user; try { user = usersDao.findByEmail(email); } catch (DatabaseException e) { LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } if (requestValidator.isPasswordHeaderCheckEnabled() && !hashService.isMatch(password, user.getPassword())) { LOG.error("The password for user {} was incorrect.", email); return Response.status(Response.Status.UNAUTHORIZED) .entity("Unable to validate user with provided credentials.").build(); } User result; try { result = usersDao.delete(email); } catch (DatabaseException e) { LOG.error("Error deleting user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } LOG.info("Successfully deleted user {}.", email); return Response.ok(result).build(); } @Inject  UserResource(UsersDao usersDao,
                      RequestValidator requestValidator,
                      HashService hashService); @POST @Operation( summary = "Create a new user", description = "Creates a new user in the database and returns the created user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "201", description = "The user was successfully created", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The create request was malformed"), @ApiResponse(responseCode = "409", description = "The user already exists in the database"), @ApiResponse(responseCode = "500", description = "The database rejected the request for an unknown reason"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "post-requests") Response postUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @RequestBody(
          description = "The User object to create.",
          required = true,
          content = @Content(
              schema = @Schema(implementation = User.class),
              mediaType = "application/json"))
          User user); @PUT @Operation( summary = "Update an existing user", description = "Updates an existing user in the database and returns the updated user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully updated", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The update request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The existing user was not found in the database"), @ApiResponse(responseCode = "409", description = "A user with the updated email address already exists"), @ApiResponse(responseCode = "500", description = "The database rejected the request for an unknown reason"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "update-requests") Response updateUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @Parameter(description = "The password of the user, necessary if the "
          + "headerPasswordCheck is enabled.") @HeaderParam("password") String password,
      @Parameter(description = "The existing email address of the user. Only necessary if "
          + "the email address is to be changed.") @QueryParam("email") String existingEmail,
      @RequestBody(description = "The updated User object to insert.",
          required = true,
          content = @Content(
              schema = @Schema(implementation = User.class),
              mediaType = "application/json"))
          User user); @GET @Operation( summary = "Retrieve a user from the database", description = "Retrieves a user from the database and returns the user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was found and returned in the response body", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "get-requests") Response getUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @Parameter(description = "The password of the user, necessary if the "
          + "headerPasswordCheck option is enabled.") @HeaderParam("password") String password,
      @Parameter(description = "The email address of the user to retrieve.", required = true)
          @QueryParam("email") String email); @DELETE @Operation( summary = "Delete a user from the database", description = "Deletes a user from the database and returns the deleted user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully deleted", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "delete-requests") Response deleteUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @Parameter(description = "The password of the user, necessary if the "
          + "headerPasswordCheck is enabled.") @HeaderParam("password") String password,
      @Parameter(description = "The email address of the user to delete.", required = true)
          @QueryParam("email") String email); }
UserResource { @DELETE @Operation( summary = "Delete a user from the database", description = "Deletes a user from the database and returns the deleted user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully deleted", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "delete-requests") public Response deleteUser( @Parameter(hidden = true) @Auth Principal auth, @Parameter(description = "The password of the user, necessary if the " + "headerPasswordCheck is enabled.") @HeaderParam("password") String password, @Parameter(description = "The email address of the user to delete.", required = true) @QueryParam("email") String email) { try { requestValidator.validate(password, email, false); } catch (ValidationException e) { return Response.status(Response.Status.BAD_REQUEST) .entity(e.getMessage()).build(); } LOG.info("Attempting to delete user {}.", email); User user; try { user = usersDao.findByEmail(email); } catch (DatabaseException e) { LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } if (requestValidator.isPasswordHeaderCheckEnabled() && !hashService.isMatch(password, user.getPassword())) { LOG.error("The password for user {} was incorrect.", email); return Response.status(Response.Status.UNAUTHORIZED) .entity("Unable to validate user with provided credentials.").build(); } User result; try { result = usersDao.delete(email); } catch (DatabaseException e) { LOG.error("Error deleting user {} in database. Caused by: {}", email, e.getErrorKind()); return e.getErrorKind().buildResponse(email); } LOG.info("Successfully deleted user {}.", email); return Response.ok(result).build(); } @Inject  UserResource(UsersDao usersDao,
                      RequestValidator requestValidator,
                      HashService hashService); @POST @Operation( summary = "Create a new user", description = "Creates a new user in the database and returns the created user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "201", description = "The user was successfully created", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The create request was malformed"), @ApiResponse(responseCode = "409", description = "The user already exists in the database"), @ApiResponse(responseCode = "500", description = "The database rejected the request for an unknown reason"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "post-requests") Response postUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @RequestBody(
          description = "The User object to create.",
          required = true,
          content = @Content(
              schema = @Schema(implementation = User.class),
              mediaType = "application/json"))
          User user); @PUT @Operation( summary = "Update an existing user", description = "Updates an existing user in the database and returns the updated user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully updated", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The update request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The existing user was not found in the database"), @ApiResponse(responseCode = "409", description = "A user with the updated email address already exists"), @ApiResponse(responseCode = "500", description = "The database rejected the request for an unknown reason"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "update-requests") Response updateUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @Parameter(description = "The password of the user, necessary if the "
          + "headerPasswordCheck is enabled.") @HeaderParam("password") String password,
      @Parameter(description = "The existing email address of the user. Only necessary if "
          + "the email address is to be changed.") @QueryParam("email") String existingEmail,
      @RequestBody(description = "The updated User object to insert.",
          required = true,
          content = @Content(
              schema = @Schema(implementation = User.class),
              mediaType = "application/json"))
          User user); @GET @Operation( summary = "Retrieve a user from the database", description = "Retrieves a user from the database and returns the user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was found and returned in the response body", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "get-requests") Response getUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @Parameter(description = "The password of the user, necessary if the "
          + "headerPasswordCheck option is enabled.") @HeaderParam("password") String password,
      @Parameter(description = "The email address of the user to retrieve.", required = true)
          @QueryParam("email") String email); @DELETE @Operation( summary = "Delete a user from the database", description = "Deletes a user from the database and returns the deleted user.", tags = { "users" }, responses = { @ApiResponse(responseCode = "200", description = "The user was successfully deleted", content = @Content( mediaType = "application/json", schema = @Schema(implementation = User.class))), @ApiResponse(responseCode = "400", description = "The get request was malformed"), @ApiResponse(responseCode = "401", description = "The request was unauthorized"), @ApiResponse(responseCode = "404", description = "The user was not found in the database"), @ApiResponse(responseCode = "503", description = "The database is currently unavailable") }) @Metered(name = "delete-requests") Response deleteUser(
      @Parameter(hidden = true) @Auth Principal auth,
      @Parameter(description = "The password of the user, necessary if the "
          + "headerPasswordCheck is enabled.") @HeaderParam("password") String password,
      @Parameter(description = "The email address of the user to delete.", required = true)
          @QueryParam("email") String email);  }