All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmetadata.service.resources.dqtests.TestSuiteResource Maven / Gradle / Ivy

There is a newer version: 1.5.11
Show newest version
package org.openmetadata.service.resources.dqtests;

import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty;
import static org.openmetadata.schema.type.Include.ALL;

import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.json.JsonPatch;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.schema.EntityInterface;
import org.openmetadata.schema.api.data.RestoreEntity;
import org.openmetadata.schema.api.tests.CreateTestSuite;
import org.openmetadata.schema.entity.data.Table;
import org.openmetadata.schema.tests.DataQualityReport;
import org.openmetadata.schema.tests.TestSuite;
import org.openmetadata.schema.tests.type.TestSummary;
import org.openmetadata.schema.type.EntityHistory;
import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.schema.type.Include;
import org.openmetadata.schema.type.MetadataOperation;
import org.openmetadata.service.Entity;
import org.openmetadata.service.jdbi3.ListFilter;
import org.openmetadata.service.jdbi3.TestSuiteRepository;
import org.openmetadata.service.limits.Limits;
import org.openmetadata.service.resources.Collection;
import org.openmetadata.service.resources.EntityResource;
import org.openmetadata.service.search.SearchListFilter;
import org.openmetadata.service.search.SearchSortFilter;
import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.security.policyevaluator.OperationContext;
import org.openmetadata.service.security.policyevaluator.ResourceContext;
import org.openmetadata.service.util.EntityUtil;
import org.openmetadata.service.util.RestUtil;
import org.openmetadata.service.util.ResultList;

@Slf4j
@Path("/v1/dataQuality/testSuites")
@Tag(
    name = "Test Suites",
    description = "`TestSuite` is a set of test cases grouped together to capture data quality.")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Collection(name = "TestSuites")
public class TestSuiteResource extends EntityResource {
  public static final String COLLECTION_PATH = "/v1/dataQuality/testSuites";
  public static final String EXECUTABLE_TEST_SUITE_DELETION_ERROR =
      "Cannot delete logical test suite. To delete logical test suite, use DELETE /v1/dataQuality/testSuites/<...>";
  public static final String NON_EXECUTABLE_TEST_SUITE_DELETION_ERROR =
      "Cannot delete executable test suite. To delete executable test suite, use DELETE /v1/dataQuality/testSuites/executable/<...>";

  static final String FIELDS = "owners,tests,summary";
  static final String SEARCH_FIELDS_EXCLUDE = "table,database,databaseSchema,service";

  public TestSuiteResource(Authorizer authorizer, Limits limits) {
    super(Entity.TEST_SUITE, authorizer, limits);
  }

  @Override
  protected List getEntitySpecificOperations() {
    addViewOperation("tests", MetadataOperation.VIEW_BASIC);
    return null;
  }

  public static class TestSuiteList extends ResultList {
    /* Required for serde */
  }

  @GET
  @Operation(
      operationId = "listTestSuites",
      summary = "List test suites",
      description =
          "Get a list of test suites. Use `fields` "
              + "parameter to get only necessary fields. Use cursor-based pagination to limit the number "
              + "entries in the list using `limit` and `before` or `after` query params.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "List of test definitions",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuiteResource.TestSuiteList.class)))
      })
  public ResultList list(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(
              description = "Fields requested in the returned resource",
              schema = @Schema(type = "string", example = FIELDS))
          @QueryParam("fields")
          String fieldsParam,
      @Parameter(
              description =
                  "Limit the number test definitions returned. (1 to 1000000, default = 10)")
          @DefaultValue("10")
          @QueryParam("limit")
          @Min(0)
          @Max(1000000)
          int limitParam,
      @Parameter(
              description =
                  "Returns executable or logical test suites. If omitted, returns all test suites.",
              schema = @Schema(type = "string", example = "executable"))
          @QueryParam("testSuiteType")
          String testSuiteType,
      @Parameter(
              description = "Include empty test suite in the response.",
              schema = @Schema(type = "boolean", example = "true"))
          @QueryParam("includeEmptyTestSuites")
          @DefaultValue("true")
          Boolean includeEmptyTestSuites,
      @Parameter(
              description = "Returns list of test definitions before this cursor",
              schema = @Schema(type = "string"))
          @QueryParam("before")
          String before,
      @Parameter(
              description = "Returns list of test definitions after this cursor",
              schema = @Schema(type = "string"))
          @QueryParam("after")
          String after,
      @Parameter(
              description = "Include all, deleted, or non-deleted entities.",
              schema = @Schema(implementation = Include.class))
          @QueryParam("include")
          @DefaultValue("non-deleted")
          Include include) {
    ListFilter filter = new ListFilter(include);
    filter.addQueryParam("testSuiteType", testSuiteType);
    filter.addQueryParam("includeEmptyTestSuites", includeEmptyTestSuites);
    EntityUtil.Fields fields = getFields(fieldsParam);

    ResourceContext resourceContext = getResourceContext();
    OperationContext operationContext =
        new OperationContext(Entity.TABLE, MetadataOperation.VIEW_TESTS);

    return super.listInternal(
        uriInfo,
        securityContext,
        fields,
        filter,
        limitParam,
        before,
        after,
        operationContext,
        resourceContext);
  }

  @GET
  @Path("/search/list")
  @Operation(
      operationId = "listTestSuiteFromSearchService",
      summary = "List test suite using search service",
      description =
          "Get a list of test suite using the search service. Use `fields` "
              + "parameter to get only necessary fields. Use offset/limit pagination to limit the number "
              + "entries in the list using `limit` and `offset` query params."
              + "Use the `tests` field to get the test cases linked to the test suite "
              + "and/or use the `summary` field to get a summary of test case executions.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "List of test suites",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuiteList.class)))
      })
  public ResultList listFromSearch(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(
              description = "Fields requested in the returned resource",
              schema = @Schema(type = "string", example = FIELDS))
          @QueryParam("fields")
          String fieldsParam,
      @Parameter(description = "Limit the number test suite returned. (1 to 1000000, default = 10)")
          @DefaultValue("10")
          @QueryParam("limit")
          @Min(0)
          @Max(1000000)
          int limit,
      @Parameter(
              description = "Returns list of test suite after this offset (default = 0)",
              schema = @Schema(type = "string"))
          @QueryParam("offset")
          @DefaultValue("0")
          @Min(0)
          int offset,
      @Parameter(
              description =
                  "Returns executable or logical test suites. If omitted, returns all test suites.",
              schema = @Schema(type = "string", example = "executable"))
          @QueryParam("testSuiteType")
          String testSuiteType,
      @Parameter(
              description = "Include empty test suite in the response.",
              schema = @Schema(type = "boolean", example = "true"))
          @QueryParam("includeEmptyTestSuites")
          @DefaultValue("true")
          Boolean includeEmptyTestSuites,
      @Parameter(
              description = "Filter a test suite by fully qualified name.",
              schema = @Schema(type = "string"))
          @QueryParam("fullyQualifiedName")
          String fullyQualifiedName,
      @Parameter(description = "Filter test suites by owner.", schema = @Schema(type = "string"))
          @QueryParam("owner")
          String owner,
      @Parameter(
              description = "Include all, deleted, or non-deleted entities.",
              schema = @Schema(implementation = Include.class))
          @QueryParam("include")
          @DefaultValue("non-deleted")
          Include include,
      @Parameter(
              description = "Field used to sort the test cases listing",
              schema = @Schema(type = "string"))
          @QueryParam("sortField")
          String sortField,
      @Parameter(
              description =
                  "Set this field if your mapping is nested and you want to sort on a nested field",
              schema = @Schema(type = "string"))
          @QueryParam("sortNestedPath")
          String sortNestedPath,
      @Parameter(
              description =
                  "Set this field if your mapping is nested and you want to sort on a nested field",
              schema = @Schema(type = "string", example = "min,max,avg,sum,median"))
          @QueryParam("sortNestedMode")
          String sortNestedMode,
      @Parameter(
              description = "Sort type",
              schema =
                  @Schema(
                      type = "string",
                      allowableValues = {"asc", "desc"}))
          @QueryParam("sortType")
          @DefaultValue("desc")
          String sortType,
      @Parameter(
              description = "search query term to use in list",
              schema = @Schema(type = "string"))
          @QueryParam("q")
          String q)
      throws IOException {
    SearchSortFilter searchSortFilter =
        new SearchSortFilter(sortField, sortType, sortNestedPath, sortNestedMode);
    SearchListFilter searchListFilter = new SearchListFilter(include);
    searchListFilter.addQueryParam("testSuiteType", testSuiteType);
    searchListFilter.addQueryParam("includeEmptyTestSuites", includeEmptyTestSuites);
    searchListFilter.addQueryParam("fullyQualifiedName", fullyQualifiedName);
    searchListFilter.addQueryParam("excludeFields", SEARCH_FIELDS_EXCLUDE);
    if (!nullOrEmpty(owner)) {
      EntityInterface entity;
      try {
        entity = Entity.getEntityByName(Entity.USER, owner, "", ALL);
      } catch (Exception e) {
        // If the owner is not a user, then we'll try to geta team
        entity = Entity.getEntityByName(Entity.TEAM, owner, "", ALL);
      }
      searchListFilter.addQueryParam("owners", entity.getId().toString());
    }

    EntityUtil.Fields fields = getFields(fieldsParam);

    ResourceContext resourceContext = getResourceContext();
    OperationContext operationContext =
        new OperationContext(Entity.TABLE, MetadataOperation.VIEW_TESTS);

    return super.listInternalFromSearch(
        uriInfo,
        securityContext,
        fields,
        searchListFilter,
        limit,
        offset,
        searchSortFilter,
        q,
        operationContext,
        resourceContext);
  }

  @GET
  @Path("/{id}/versions")
  @Operation(
      operationId = "listAllTestSuiteVersion",
      summary = "List test suite versions",
      description = "Get a list of all the versions of a test suite identified by `id`",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "List of test suite versions",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = EntityHistory.class)))
      })
  public EntityHistory listVersions(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(description = "Id of the test suite", schema = @Schema(type = "UUID"))
          @PathParam("id")
          UUID id) {
    return super.listVersionsInternal(securityContext, id);
  }

  @GET
  @Path("/{id}")
  @Operation(
      summary = "Get a test suite by Id",
      description = "Get a Test Suite by `Id`.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "The Test suite",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class))),
        @ApiResponse(
            responseCode = "404",
            description = "Test Suite for instance {id} is not found")
      })
  public TestSuite get(
      @Context UriInfo uriInfo,
      @Parameter(description = "Id of the test suite", schema = @Schema(type = "UUID"))
          @PathParam("id")
          UUID id,
      @Context SecurityContext securityContext,
      @Parameter(
              description = "Fields requested in the returned resource",
              schema = @Schema(type = "string", example = FIELDS))
          @QueryParam("fields")
          String fieldsParam,
      @Parameter(
              description = "Include all, deleted, or non-deleted entities.",
              schema = @Schema(implementation = Include.class))
          @QueryParam("include")
          @DefaultValue("non-deleted")
          Include include) {
    return getInternal(uriInfo, securityContext, id, fieldsParam, include);
  }

  @GET
  @Path("/name/{name}")
  @Operation(
      operationId = "getTestSuiteByName",
      summary = "Get a test suite by name",
      description = "Get a test suite by  name.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "The test suite",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class))),
        @ApiResponse(
            responseCode = "404",
            description = "Test Suite for instance {name} is not found")
      })
  public TestSuite getByName(
      @Context UriInfo uriInfo,
      @Parameter(description = "Name of the test suite", schema = @Schema(type = "string"))
          @PathParam("name")
          String name,
      @Context SecurityContext securityContext,
      @Parameter(
              description = "Fields requested in the returned resource",
              schema = @Schema(type = "string", example = FIELDS))
          @QueryParam("fields")
          String fieldsParam,
      @Parameter(
              description = "Include all, deleted, or non-deleted entities.",
              schema = @Schema(implementation = Include.class))
          @QueryParam("include")
          @DefaultValue("non-deleted")
          Include include) {
    return getByNameInternal(uriInfo, securityContext, name, fieldsParam, include);
  }

  @GET
  @Path("/{id}/versions/{version}")
  @Operation(
      operationId = "getSpecificTestSuiteVersion",
      summary = "Get a version of the test suite",
      description = "Get a version of the test suite by given `id`",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "TestSuite",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class))),
        @ApiResponse(
            responseCode = "404",
            description = "Test Suite for instance {id} and version {version} is not found")
      })
  public TestSuite getVersion(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(description = "Id of the test suite", schema = @Schema(type = "UUID"))
          @PathParam("id")
          UUID id,
      @Parameter(
              description = "Test Suite version number in the form `major`.`minor`",
              schema = @Schema(type = "string", example = "0.1 or 1.1"))
          @PathParam("version")
          String version) {
    return super.getVersionInternal(securityContext, id, version);
  }

  @GET
  @Path("/executionSummary")
  @Operation(
      operationId = "getExecutionSummaryOfTestSuites",
      summary = "Get the execution summary of test suites",
      description = "Get the execution summary of test suites.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "Tests Execution Summary",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSummary.class)))
      })
  public TestSummary getTestsExecutionSummary(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Context HttpServletResponse response,
      @Parameter(
              description = "get summary for a specific test suite",
              schema = @Schema(type = "String", format = "uuid"))
          @QueryParam("testSuiteId")
          UUID testSuiteId) {
    ResourceContext resourceContext = getResourceContext();
    OperationContext operationContext =
        new OperationContext(Entity.TABLE, MetadataOperation.VIEW_TESTS);
    authorizer.authorize(securityContext, operationContext, resourceContext);
    // Set the deprecation header based on draft specification from IETF
    // https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-deprecation-header-02
    response.setHeader("Deprecation", "Monday, October 30, 2024");
    response.setHeader(
        "Link", "api/v1/dataQuality/testSuites/dataQualityReport; rel=\"alternate\"");
    return repository.getTestSummary(testSuiteId);
  }

  @GET
  @Path("/dataQualityReport")
  @Operation(
      operationId = "getDataQualityReport",
      summary = "Get Data Quality Report",
      description =
          """
            Use the search service to perform data quality aggregation. You can use the `q` parameter to filter the results.
            the `aggregationQuery` is of the form `bucketName=:aggType=:field=`. You can sperate aggregation
            query with a comma `,` to perform nested aggregations.
            For example, `bucketName=table:aggType=terms:field=databaseName,bucketName=:aggType=:field=`
            """,
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "Data Quality Report Results",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = DataQualityReport.class)))
      })
  public DataQualityReport getDataQualityReport(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(
              description = "Search query to filter the aggregation results",
              schema = @Schema(type = "String"))
          @QueryParam("q")
          String query,
      @Parameter(
              description = "Aggregation query to perform aggregation on the search results",
              schema = @Schema(type = "String"))
          @QueryParam("aggregationQuery")
          String aggregationQuery,
      @Parameter(
              description = "Index to perform the aggregation against",
              schema = @Schema(type = "String"))
          @QueryParam("index")
          String index)
      throws IOException {
    ResourceContext resourceContext = getResourceContext();
    OperationContext operationContext =
        new OperationContext(entityType, MetadataOperation.VIEW_TESTS);
    authorizer.authorize(securityContext, operationContext, resourceContext);
    if (nullOrEmpty(aggregationQuery) || nullOrEmpty(index)) {
      throw new IllegalArgumentException("aggregationQuery and index are required parameters");
    }
    return repository.getDataQualityReport(query, aggregationQuery, index);
  }

  @POST
  @Operation(
      operationId = "createLogicalTestSuite",
      summary = "Create a logical test suite",
      description = "Create a logical test suite.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "The test suite",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class))),
        @ApiResponse(responseCode = "400", description = "Bad request")
      })
  public Response create(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Valid CreateTestSuite create) {
    create =
        create.withExecutableEntityReference(
            null); // entity reference is not applicable for logical test suites
    TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName());
    testSuite.setExecutable(false);
    return create(uriInfo, securityContext, testSuite);
  }

  @POST
  @Path("/executable")
  @Operation(
      operationId = "createExecutableTestSuite",
      summary = "Create an executable test suite",
      description = "Create an executable test suite.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "Executable test suite",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class))),
        @ApiResponse(responseCode = "400", description = "Bad request")
      })
  public Response createExecutable(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Valid CreateTestSuite create) {
    TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName());
    testSuite.setExecutable(true);
    return create(uriInfo, securityContext, testSuite);
  }

  @PATCH
  @Path("/{id}")
  @Operation(
      operationId = "patchTestSuite",
      summary = "Update a test suite",
      description = "Update an existing testSuite using JsonPatch.",
      externalDocs =
          @ExternalDocumentation(
              description = "JsonPatch RFC",
              url = "https://tools.ietf.org/html/rfc6902"))
  @Consumes(MediaType.APPLICATION_JSON_PATCH_JSON)
  public Response updateDescription(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(description = "Id of the test suite", schema = @Schema(type = "UUID"))
          @PathParam("id")
          UUID id,
      @RequestBody(
              description = "JsonPatch with array of operations",
              content =
                  @Content(
                      mediaType = MediaType.APPLICATION_JSON_PATCH_JSON,
                      examples = {
                        @ExampleObject("[{op:remove, path:/a},{op:add, path: /b, value: val}]")
                      }))
          JsonPatch patch) {
    return patchInternal(uriInfo, securityContext, id, patch);
  }

  @PUT
  @Operation(
      operationId = "createOrUpdateLogicalTestSuite",
      summary = "Update logical test suite",
      description =
          "Create a logical TestSuite, if it does not exist or update an existing test suite.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "The updated test definition ",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class)))
      })
  public Response createOrUpdate(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Valid CreateTestSuite create) {
    create =
        create.withExecutableEntityReference(
            null); // entity reference is not applicable for logical test suites
    TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName());
    testSuite.setExecutable(false);
    return createOrUpdate(uriInfo, securityContext, testSuite);
  }

  @PUT
  @Path("/executable")
  @Operation(
      operationId = "createOrUpdateExecutableTestSuite",
      summary = "Create or Update Executable test suite",
      description =
          "Create an Executable TestSuite if it does not exist or update an existing one.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "The updated test definition ",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class)))
      })
  public Response createOrUpdateExecutable(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Valid CreateTestSuite create) {
    TestSuite testSuite = getTestSuite(create, securityContext.getUserPrincipal().getName());
    testSuite.setExecutable(true);
    return createOrUpdate(uriInfo, securityContext, testSuite);
  }

  @DELETE
  @Path("/{id}")
  @Operation(
      operationId = "deleteLogicalTestSuite",
      summary = "Delete a logical test suite",
      description = "Delete a logical test suite by `id`.",
      responses = {
        @ApiResponse(responseCode = "200", description = "OK"),
        @ApiResponse(
            responseCode = "404",
            description = "Logical test suite for instance {id} is not found")
      })
  public Response delete(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(description = "Hard delete the logical entity. (Default = `false`)")
          @QueryParam("hardDelete")
          @DefaultValue("false")
          boolean hardDelete,
      @Parameter(description = "Id of the logical test suite", schema = @Schema(type = "UUID"))
          @PathParam("id")
          UUID id) {
    OperationContext operationContext = new OperationContext(entityType, MetadataOperation.DELETE);
    authorizer.authorize(securityContext, operationContext, getResourceContextById(id));
    TestSuite testSuite = Entity.getEntity(Entity.TEST_SUITE, id, "*", ALL);
    if (Boolean.TRUE.equals(testSuite.getExecutable())) {
      throw new IllegalArgumentException(NON_EXECUTABLE_TEST_SUITE_DELETION_ERROR);
    }
    RestUtil.DeleteResponse response =
        repository.deleteLogicalTestSuite(securityContext, testSuite, hardDelete);
    repository.deleteFromSearch(response.entity(), response.changeType());
    addHref(uriInfo, response.entity());
    return response.toResponse();
  }

  @DELETE
  @Path("/name/{name}")
  @Operation(
      operationId = "deleteLogicalTestSuite",
      summary = "Delete a logical test suite",
      description = "Delete a logical test suite by `name`.",
      responses = {
        @ApiResponse(responseCode = "200", description = "OK"),
        @ApiResponse(
            responseCode = "404",
            description = "Logical Test suite for instance {name} is not found")
      })
  public Response delete(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(description = "Hard delete the logical entity. (Default = `false`)")
          @QueryParam("hardDelete")
          @DefaultValue("false")
          boolean hardDelete,
      @Parameter(description = "FQN of the logical test suite", schema = @Schema(type = "String"))
          @PathParam("name")
          String name) {
    OperationContext operationContext = new OperationContext(entityType, MetadataOperation.DELETE);
    authorizer.authorize(securityContext, operationContext, getResourceContextByName(name));
    TestSuite testSuite = Entity.getEntityByName(Entity.TEST_SUITE, name, "*", ALL);
    if (Boolean.TRUE.equals(testSuite.getExecutable())) {
      throw new IllegalArgumentException(NON_EXECUTABLE_TEST_SUITE_DELETION_ERROR);
    }
    RestUtil.DeleteResponse response =
        repository.deleteLogicalTestSuite(securityContext, testSuite, hardDelete);
    addHref(uriInfo, response.entity());
    return response.toResponse();
  }

  @DELETE
  @Path("/executable/name/{name}")
  @Operation(
      operationId = "deleteTestSuiteByName",
      summary = "Delete a test suite",
      description = "Delete a test suite by `name`.",
      responses = {
        @ApiResponse(responseCode = "200", description = "OK"),
        @ApiResponse(
            responseCode = "404",
            description = "Test suite for instance {name} is not found")
      })
  public Response deleteExecutable(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(
              description = "Recursively delete this entity and it's children. (Default `false`)")
          @DefaultValue("false")
          @QueryParam("recursive")
          boolean recursive,
      @Parameter(description = "Hard delete the entity. (Default = `false`)")
          @QueryParam("hardDelete")
          @DefaultValue("false")
          boolean hardDelete,
      @Parameter(description = "Name of the test suite", schema = @Schema(type = "string"))
          @PathParam("name")
          String name) {
    OperationContext operationContext = new OperationContext(entityType, MetadataOperation.DELETE);
    authorizer.authorize(securityContext, operationContext, getResourceContextByName(name));
    TestSuite testSuite = Entity.getEntityByName(Entity.TEST_SUITE, name, "*", ALL);
    if (Boolean.FALSE.equals(testSuite.getExecutable())) {
      throw new IllegalArgumentException(EXECUTABLE_TEST_SUITE_DELETION_ERROR);
    }
    return deleteByName(uriInfo, securityContext, name, recursive, hardDelete);
  }

  @DELETE
  @Path("/executable/{id}")
  @Operation(
      operationId = "deleteTestSuite",
      summary = "Delete a test suite",
      description = "Delete a test suite by `Id`.",
      responses = {
        @ApiResponse(responseCode = "200", description = "OK"),
        @ApiResponse(
            responseCode = "404",
            description = "Test suite for instance {id} is not found")
      })
  public Response deleteExecutable(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Parameter(
              description = "Recursively delete this entity and it's children. (Default `false`)")
          @DefaultValue("false")
          @QueryParam("recursive")
          boolean recursive,
      @Parameter(description = "Hard delete the entity. (Default = `false`)")
          @QueryParam("hardDelete")
          @DefaultValue("false")
          boolean hardDelete,
      @Parameter(description = "Id of the test suite", schema = @Schema(type = "UUID"))
          @PathParam("id")
          UUID id) {
    OperationContext operationContext = new OperationContext(entityType, MetadataOperation.DELETE);
    authorizer.authorize(securityContext, operationContext, getResourceContextById(id));
    TestSuite testSuite = Entity.getEntity(Entity.TEST_SUITE, id, "*", ALL);
    if (Boolean.FALSE.equals(testSuite.getExecutable())) {
      throw new IllegalArgumentException(EXECUTABLE_TEST_SUITE_DELETION_ERROR);
    }
    return delete(uriInfo, securityContext, id, recursive, hardDelete);
  }

  @PUT
  @Path("/restore")
  @Operation(
      operationId = "restore",
      summary = "Restore a soft deleted test suite",
      description = "Restore a soft deleted test suite.",
      responses = {
        @ApiResponse(
            responseCode = "200",
            description = "Successfully restored the TestSuite.",
            content =
                @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TestSuite.class)))
      })
  public Response restoreTestSuite(
      @Context UriInfo uriInfo,
      @Context SecurityContext securityContext,
      @Valid RestoreEntity restore) {
    return restoreEntity(uriInfo, securityContext, restore.getId());
  }

  private TestSuite getTestSuite(CreateTestSuite create, String user) {
    TestSuite testSuite =
        repository
            .copy(new TestSuite(), create, user)
            .withDescription(create.getDescription())
            .withDisplayName(create.getDisplayName())
            .withName(create.getName());
    if (create.getExecutableEntityReference() != null) {
      Table table =
          Entity.getEntityByName(Entity.TABLE, create.getExecutableEntityReference(), null, null);
      EntityReference entityReference =
          new EntityReference()
              .withId(table.getId())
              .withFullyQualifiedName(table.getFullyQualifiedName())
              .withName(table.getName())
              .withType(Entity.TABLE);
      testSuite.setExecutableEntityReference(entityReference);
    }
    return testSuite;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy