Fixes #1333 - Databases create API documentation has no information about the valid service type (#2668)

This commit is contained in:
Suresh Srinivas 2022-02-07 23:17:20 -08:00 committed by GitHub
parent 9b0c9fa24b
commit f9b5901d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 10 deletions

View File

@ -57,7 +57,7 @@ public final class CatalogExceptionMessage {
return String.format("%s instance for %s and version %s not found", entityType, id, version);
}
public static String invalidServiceEntity(String serviceEntity, String entityType) {
return String.format("Invalid service entity type %s for %s", serviceEntity, entityType);
public static String invalidServiceEntity(String serviceType, String entityType, String expected) {
return String.format("Invalid service type `%s` for %s. Expected %s.", serviceType, entityType, expected);
}
}

View File

@ -113,7 +113,8 @@ public class DashboardRepository extends EntityRepository<Dashboard> {
if (entityType.equalsIgnoreCase(Entity.DASHBOARD_SERVICE)) {
return daoCollection.dashboardServiceDAO().findEntityById(serviceId);
}
throw new IllegalArgumentException(CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.DASHBOARD));
throw new IllegalArgumentException(
CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.DASHBOARD, Entity.DASHBOARD_SERVICE));
}
public void setService(Dashboard dashboard, EntityReference service) {

View File

@ -193,7 +193,8 @@ public class DatabaseRepository extends EntityRepository<Database> {
if (entityType.equalsIgnoreCase(Entity.DATABASE_SERVICE)) {
return daoCollection.dbServiceDAO().findEntityById(serviceId);
}
throw new IllegalArgumentException(CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.DATABASE));
throw new IllegalArgumentException(
CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.DATABASE, Entity.DATABASE_SERVICE));
}
@Transaction

View File

@ -170,7 +170,8 @@ public class LocationRepository extends EntityRepository<Location> {
if (entityType.equalsIgnoreCase(Entity.STORAGE_SERVICE)) {
return daoCollection.storageServiceDAO().findEntityById(serviceId);
}
throw new IllegalArgumentException(CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.LOCATION));
throw new IllegalArgumentException(
CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.LOCATION, STORAGE_SERVICE));
}
@Override
@ -231,7 +232,7 @@ public class LocationRepository extends EntityRepository<Location> {
return daoCollection.storageServiceDAO().findEntityReferenceById(service.getId());
}
throw new IllegalArgumentException(
CatalogExceptionMessage.invalidServiceEntity(service.getType(), Entity.LOCATION));
CatalogExceptionMessage.invalidServiceEntity(service.getType(), Entity.LOCATION, STORAGE_SERVICE));
}
public void setService(Location location, EntityReference service) throws IOException {

View File

@ -118,7 +118,8 @@ public class MetricsRepository extends EntityRepository<Metrics> {
if (service.getType().equalsIgnoreCase(Entity.DASHBOARD_SERVICE)) {
return daoCollection.dbServiceDAO().findEntityReferenceById(service.getId());
}
throw new IllegalArgumentException(CatalogExceptionMessage.invalidServiceEntity(service.getType(), Entity.METRICS));
throw new IllegalArgumentException(
CatalogExceptionMessage.invalidServiceEntity(service.getType(), Entity.METRICS, DASHBOARD_SERVICE));
}
static class MetricsEntityInterface implements EntityInterface<Metrics> {

View File

@ -163,7 +163,8 @@ public class PipelineRepository extends EntityRepository<Pipeline> {
if (entityType.equalsIgnoreCase(Entity.PIPELINE_SERVICE)) {
return daoCollection.pipelineServiceDAO().findEntityById(serviceId);
}
throw new IllegalArgumentException(CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.PIPELINE));
throw new IllegalArgumentException(
CatalogExceptionMessage.invalidServiceEntity(entityType, Entity.PIPELINE, PIPELINE_SERVICE));
}
public static class PipelineEntityInterface implements EntityInterface<Pipeline> {

View File

@ -25,6 +25,7 @@ import static org.openmetadata.catalog.util.TestUtils.ADMIN_AUTH_HEADERS;
import static org.openmetadata.catalog.util.TestUtils.UpdateType.MINOR_UPDATE;
import static org.openmetadata.catalog.util.TestUtils.assertListNotNull;
import static org.openmetadata.catalog.util.TestUtils.assertListNull;
import static org.openmetadata.catalog.util.TestUtils.assertResponse;
import static org.openmetadata.catalog.util.TestUtils.assertResponseContains;
import java.io.IOException;
@ -108,8 +109,10 @@ public class DashboardResourceTest extends EntityResourceTest<Dashboard, CreateD
CreateDashboard create = createRequest(test).withService(SUPERSET_INVALID_SERVICE_REFERENCE);
HttpResponseException exception =
assertThrows(HttpResponseException.class, () -> createEntity(create, ADMIN_AUTH_HEADERS));
assertResponseContains(
exception, BAD_REQUEST, invalidServiceEntity(SUPERSET_INVALID_SERVICE_REFERENCE.getType(), Entity.DASHBOARD));
assertResponse(
exception,
BAD_REQUEST,
invalidServiceEntity(SUPERSET_INVALID_SERVICE_REFERENCE.getType(), Entity.DASHBOARD, Entity.DASHBOARD_SERVICE));
}
@Test

View File

@ -34,6 +34,7 @@ import org.junit.jupiter.api.TestInstance;
import org.openmetadata.catalog.Entity;
import org.openmetadata.catalog.api.data.CreateDatabase;
import org.openmetadata.catalog.entity.data.Database;
import org.openmetadata.catalog.exception.CatalogExceptionMessage;
import org.openmetadata.catalog.jdbi3.DatabaseRepository.DatabaseEntityInterface;
import org.openmetadata.catalog.resources.EntityResourceTest;
import org.openmetadata.catalog.resources.databases.DatabaseResource.DatabaseList;
@ -63,6 +64,18 @@ public class DatabaseResourceTest extends EntityResourceTest<Database, CreateDat
super.setup(test);
}
@Test
void post_databaseWithInvalidServiceType_4xx(TestInfo test) {
// Create a database with entity reference to databaseServiceType having invalid serviceType
CreateDatabase create = createRequest(test);
EntityReference invalidService = new EntityReference().withId(SNOWFLAKE_REFERENCE.getId()).withType("invalid");
create.withService(invalidService);
TestUtils.assertResponse(
() -> createEntity(create, ADMIN_AUTH_HEADERS),
BAD_REQUEST,
CatalogExceptionMessage.invalidServiceEntity("invalid", Entity.DATABASE, Entity.DATABASE_SERVICE));
}
@Test
void post_validDatabases_as_admin_200_OK(TestInfo test) throws IOException {
// Create team with different optional fields