Skip to content

Commit

Permalink
Fix 471: add status code test cases (apache#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkeyCanCode authored Nov 28, 2024
1 parent 70b52db commit 6bb08d2
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,70 @@ public void testTokenInvalidPrincipalId() {
}
}

@Test
public void testNamespaceExistsStatus() {
// create a catalog
String catalogName = "mytablemanagecatalog";
Catalog catalog =
PolarisCatalog.builder()
.setType(Catalog.TypeEnum.INTERNAL)
.setName(catalogName)
.setStorageConfigInfo(
new AwsStorageConfigInfo(
"arn:aws:iam::012345678901:role/jdoe", StorageConfigInfo.StorageTypeEnum.S3))
.setProperties(new CatalogProperties("s3://bucket1/"))
.build();
createCatalog(catalog);

// create a namespace
String namespaceName = "c";
createNamespace(catalogName, namespaceName);

// check if a namespace existed
try (Response response =
newRequest(
"http://localhost:%d/api/catalog/v1/"
+ catalogName
+ "/namespaces/"
+ namespaceName,
userToken)
.head()) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}
}

@Test
public void testDropNamespaceStatus() {
// create a catalog
String catalogName = "mytablemanagecatalog";
Catalog catalog =
PolarisCatalog.builder()
.setType(Catalog.TypeEnum.INTERNAL)
.setName(catalogName)
.setStorageConfigInfo(
new AwsStorageConfigInfo(
"arn:aws:iam::012345678901:role/jdoe", StorageConfigInfo.StorageTypeEnum.S3))
.setProperties(new CatalogProperties("s3://bucket1/"))
.build();
createCatalog(catalog);

// create a namespace
String namespaceName = "c";
createNamespace(catalogName, namespaceName);

// drop a namespace
try (Response response =
newRequest(
"http://localhost:%d/api/catalog/v1/"
+ catalogName
+ "/namespaces/"
+ namespaceName,
userToken)
.delete()) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}
}

public static JWTCreator.Builder defaultJwt() {
Instant now = Instant.now();
return JWT.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,193 @@ public void testMultipleConflictingCommitsToSingleTableInTransaction() {
Schema latestCommittedSchema = catalog().loadTable(identifier).schema();
assertThat(latestCommittedSchema.asStruct()).isEqualTo(originalSchema.asStruct());
}

@Test
public void testTableExistsStatus() {
String tableName = "tbl1";
Namespace namespace = Namespace.of("ns1");
TableIdentifier identifier = TableIdentifier.of(namespace, tableName);

if (requiresNamespaceCreate()) {
catalog().createNamespace(namespace);
}

catalog().buildTable(identifier, SCHEMA).create();

try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/catalog/v1/%s/namespaces/%s/tables/%s",
EXT.getLocalPort(), currentCatalogName, namespace.toString(), tableName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.head()) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}
}

@Test
public void testDropTableStatus() {
String tableName = "tbl1";
Namespace namespace = Namespace.of("ns1");
TableIdentifier identifier = TableIdentifier.of(namespace, tableName);

if (requiresNamespaceCreate()) {
catalog().createNamespace(namespace);
}

catalog().buildTable(identifier, SCHEMA).create();

try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/catalog/v1/%s/namespaces/%s/tables/%s",
EXT.getLocalPort(), currentCatalogName, namespace.toString(), tableName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.delete()) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}
}

@Test
public void testViewExistsStatus() {
String tableName = "tbl1";
String viewName = "view1";
Namespace namespace = Namespace.of("ns1");
TableIdentifier identifier = TableIdentifier.of(namespace, tableName);

if (requiresNamespaceCreate()) {
catalog().createNamespace(namespace);
}

catalog().buildTable(identifier, SCHEMA).create();

restCatalog
.buildView(TableIdentifier.of(namespace, viewName))
.withSchema(SCHEMA)
.withDefaultNamespace(namespace)
.withQuery("spark", VIEW_QUERY)
.create();

try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/catalog/v1/%s/namespaces/%s/views/%s",
EXT.getLocalPort(), currentCatalogName, namespace, viewName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.head()) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}
}

@Test
public void testDropViewStatus() {
String tableName = "tbl1";
String viewName = "view1";
Namespace namespace = Namespace.of("ns1");
TableIdentifier identifier = TableIdentifier.of(namespace, tableName);

if (requiresNamespaceCreate()) {
catalog().createNamespace(namespace);
}

catalog().buildTable(identifier, SCHEMA).create();

restCatalog
.buildView(TableIdentifier.of(namespace, viewName))
.withSchema(SCHEMA)
.withDefaultNamespace(namespace)
.withQuery("spark", VIEW_QUERY)
.create();

try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/catalog/v1/%s/namespaces/%s/views/%s",
EXT.getLocalPort(), currentCatalogName, namespace, viewName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.delete()) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}
}

@Test
public void testRenameViewStatus() {
String tableName = "tbl1";
String viewName = "view1";
String newViewName = "view2";
Namespace namespace = Namespace.of("ns1");
TableIdentifier identifier = TableIdentifier.of(namespace, tableName);

if (requiresNamespaceCreate()) {
catalog().createNamespace(namespace);
}

catalog().buildTable(identifier, SCHEMA).create();

restCatalog
.buildView(TableIdentifier.of(namespace, viewName))
.withSchema(SCHEMA)
.withDefaultNamespace(namespace)
.withQuery("spark", VIEW_QUERY)
.create();

Map<String, Object> payload =
Map.of(
"source", Map.of("namespace", List.of(namespace.toString()), "name", viewName),
"destination", Map.of("namespace", List.of(namespace.toString()), "name", newViewName));

// Perform view rename
try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/catalog/v1/%s/views/rename",
EXT.getLocalPort(), currentCatalogName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.post(Entity.json(payload))) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}

// Original view should no longer exists
try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/catalog/v1/%s/namespaces/%s/views/%s",
EXT.getLocalPort(), currentCatalogName, namespace, viewName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.head()) {
assertThat(response).returns(Response.Status.NOT_FOUND.getStatusCode(), Response::getStatus);
}

// New view should exists
try (Response response =
EXT.client()
.target(
String.format(
"http://localhost:%d/api/catalog/v1/%s/namespaces/%s/views/%s",
EXT.getLocalPort(), currentCatalogName, namespace, newViewName))
.request("application/json")
.header("Authorization", "Bearer " + userToken)
.header(REALM_PROPERTY_KEY, realm)
.head()) {
assertThat(response).returns(Response.Status.NO_CONTENT.getStatusCode(), Response::getStatus);
}
}
}

0 comments on commit 6bb08d2

Please sign in to comment.