World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#71] feat(client): Add client side catalog manipulation support (#189)
Jerry Shao4eb173cf52bb
diff --git a/api/src/main/java/com/datastrato/graviton/CatalogChange.java b/api/src/main/java/com/datastrato/graviton/CatalogChange.javaindex 7f28e69d2f..f2da3411e0 100644--- a/api/src/main/java/com/datastrato/graviton/CatalogChange.java+++ b/api/src/main/java/com/datastrato/graviton/CatalogChange.java@@ -71,6 +71,7 @@ public interface CatalogChange {}@Getter+ @EqualsAndHashCodefinal class SetProperty implements CatalogChange {private final String property;private final String value;@@ -82,6 +83,7 @@ public interface CatalogChange {}@Getter+ @EqualsAndHashCodefinal class RemoveProperty implements CatalogChange {private final String property;diff --git a/client-java/build.gradle.kts b/client-java/build.gradle.ktsindex da260eeaad..2404de9ce2 100644--- a/client-java/build.gradle.kts+++ b/client-java/build.gradle.kts@@ -28,6 +28,7 @@ dependencies {implementation(libs.guava)implementation(libs.bundles.log4j)implementation(libs.httpclient5)+ implementation(libs.commons.lang3)compileOnly(libs.lombok)annotationProcessor(libs.lombok)diff --git a/client-java/src/main/java/com/datastrato/graviton/DTOConverters.java b/client-java/src/main/java/com/datastrato/graviton/DTOConverters.javadeleted file mode 100644index d2a31f9e50..0000000000--- a/client-java/src/main/java/com/datastrato/graviton/DTOConverters.java+++ /dev/null@@ -1,64 +0,0 @@-/*- * Copyright 2023 Datastrato.- * This software is licensed under the Apache License version 2.- */--package com.datastrato.graviton;--import com.datastrato.graviton.client.RESTClient;-import com.datastrato.graviton.dto.AuditDTO;-import com.datastrato.graviton.dto.MetalakeDTO;-import com.datastrato.graviton.dto.requests.MetalakeUpdateRequest;--/** Utility class for converting between DTO objects and GravitonMetaLake objects. */-class DTOConverters {- private DTOConverters() {}-- /**- * Converts a MetalakeDTO object to a GravitonMetaLake object.- *- * @param metalake The Metalake DTO to be converted.- * @param client The REST client associated with the GravitonMetaLake.- * @return A GravitonMetaLake object representing the converted MetalakeDTO.- */- static GravitonMetaLake toMetaLake(MetalakeDTO metalake, RESTClient client) {- return new GravitonMetaLake.Builder()- .withName(metalake.name())- .withComment(metalake.comment())- .withProperties(metalake.properties())- .withAudit((AuditDTO) metalake.auditInfo())- .withRestClient(client)- .build();- }-- /**- * Converts a MetalakeChange object to a MetalakeUpdateRequest object.- *- * @param change The Metalake change to be converted.- * @return A MetalakeUpdateRequest object representing the converted Metalake change.- * @throws IllegalArgumentException If the provided Metalake change is of an unknown type.- */- static MetalakeUpdateRequest toMetalakeUpdateRequest(MetalakeChange change) {- if (change instanceof MetalakeChange.RenameMetalake) {- return new MetalakeUpdateRequest.RenameMetalakeRequest(- ((MetalakeChange.RenameMetalake) change).getNewName());-- } else if (change instanceof MetalakeChange.UpdateMetalakeComment) {- return new MetalakeUpdateRequest.UpdateMetalakeCommentRequest(- ((MetalakeChange.UpdateMetalakeComment) change).getNewComment());-- } else if (change instanceof MetalakeChange.SetProperty) {- return new MetalakeUpdateRequest.SetMetalakePropertyRequest(- ((MetalakeChange.SetProperty) change).getProperty(),- ((MetalakeChange.SetProperty) change).getValue());-- } else if (change instanceof MetalakeChange.RemoveProperty) {- return new MetalakeUpdateRequest.RemoveMetalakePropertyRequest(- ((MetalakeChange.RemoveProperty) change).getProperty());-- } else {- throw new IllegalArgumentException(- "Unknown change type: " + change.getClass().getSimpleName());- }- }-}diff --git a/client-java/src/main/java/com/datastrato/graviton/GravitonMetaLake.java b/client-java/src/main/java/com/datastrato/graviton/GravitonMetaLake.javadeleted file mode 100644index 861e54948b..0000000000--- a/client-java/src/main/java/com/datastrato/graviton/GravitonMetaLake.java+++ /dev/null@@ -1,144 +0,0 @@-/*- * Copyright 2023 Datastrato.- * This software is licensed under the Apache License version 2.- */--package com.datastrato.graviton;--import com.datastrato.graviton.client.RESTClient;-import com.datastrato.graviton.dto.AuditDTO;-import com.datastrato.graviton.dto.MetalakeDTO;-import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;-import com.datastrato.graviton.exceptions.NoSuchCatalogException;-import com.datastrato.graviton.exceptions.NoSuchMetalakeException;-import com.google.common.base.Preconditions;-import java.util.Map;--/**- * GravitonMetaLake is a specific implementation of MetalakeDTO that also supports Catalog- * operations.- */-public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {-- private final RESTClient restClient;-- /**- * Constructs a new Graviton MetaLake instance with the provided details.- *- * @param name The name of the Graviton MetaLake.- * @param comment The comment for the Graviton MetaLake.- * @param properties The properties of the Graviton MetaLake.- * @param auditDTO The audit information for the Graviton MetaLake.- * @param restClient The REST client used for API communication.- */- GravitonMetaLake(- String name,- String comment,- Map<String, String> properties,- AuditDTO auditDTO,- RESTClient restClient) {- super(name, comment, properties, auditDTO);- this.restClient = restClient;- }-- /**- * Retrieves an array of Catalog names associated with this Graviton MetaLake.- *- * @param namespace The namespace to filter Catalogs.- * @return An array of name identifier's representing the Catalogs.- * @throws NoSuchMetalakeException If the Graviton MetaLake does not exist.- */- @Override- public NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeException {- throw new UnsupportedOperationException("Not implemented yet");- }-- /**- * Loads a specific Catalog associated with this Graviton MetaLake.- *- * @param ident The identifier of the Catalog to be loaded.- * @return A Catalog instance representing the loaded Catalog.- * @throws NoSuchCatalogException If the specified Catalog does not exist.- */- @Override- public Catalog loadCatalog(NameIdentifier ident) throws NoSuchCatalogException {- throw new UnsupportedOperationException("Not implemented yet");- }-- /**- * Creates a new Catalog associated with this Graviton MetaLake.- *- * @param ident The identifier of the new Catalog.- * @param type The type of the new Catalog.- * @param comment The comment for the new Catalog.- * @param properties The properties of the new Catalog.- * @return A Catalog instance representing the newly created Catalog.- * @throws NoSuchMetalakeException If the Graviton MetaLake does not exist.- * @throws CatalogAlreadyExistsException If a Catalog with the specified identifier already- * exists.- */- @Override- public Catalog createCatalog(- NameIdentifier ident, Catalog.Type type, String comment, Map<String, String> properties)- throws NoSuchMetalakeException, CatalogAlreadyExistsException {- throw new UnsupportedOperationException("Not implemented yet");- }-- /**- * Alters a specific Catalog associated with this GravitonMetaLake.- *- * @param ident The identifier of the Catalog to be altered.- * @param changes The changes to be applied to the Catalog.- * @return A Catalog instance representing the updated Catalog.- * @throws NoSuchCatalogException If the specified Catalog does not exist.- * @throws IllegalArgumentException If the provided changes are invalid.- */- @Override- public Catalog alterCatalog(NameIdentifier ident, CatalogChange... changes)- throws NoSuchCatalogException, IllegalArgumentException {- throw new UnsupportedOperationException("Not implemented yet");- }-- /**- * Drops a specific Catalog associated with this Graviton MetaLake.- *- * @param ident The identifier of the Catalog to be dropped.- * @return True if the drop operation was successful, false otherwise.- */- @Override- public boolean dropCatalog(NameIdentifier ident) {- throw new UnsupportedOperationException("Not implemented yet");- }-- /** The Builder class for GravitonMetaLake. */- static class Builder extends MetalakeDTO.Builder<Builder> {- private RESTClient restClient;-- /**- * Sets the RESTClient for the GravitonMetaLake being built.- *- * @param restClient The REST client for API communication.- * @return The Builder instance with the updated REST client.- */- Builder withRestClient(RESTClient restClient) {- this.restClient = restClient;- return this;- }-- /**- * Builds the GravitonMetaLake instance.- *- * @return A new Graviton MetaLake instance with the specified details.- * @throws NullPointerException If the REST client is not set.- * @throws IllegalArgumentException If the name is null or empty, or the audit is null.- */- @Override- public GravitonMetaLake build() {- Preconditions.checkNotNull(restClient, "restClient must be set");- Preconditions.checkArgument(- name != null && !name.isEmpty(), "name must not be null or empty");- Preconditions.checkArgument(audit != null, "audit must be non-null");- return new GravitonMetaLake(name, comment, properties, audit, restClient);- }- }-}diff --git a/client-java/src/main/java/com/datastrato/graviton/client/DTOConverters.java b/client-java/src/main/java/com/datastrato/graviton/client/DTOConverters.javanew file mode 100644index 0000000000..bf26345c22--- /dev/null+++ b/client-java/src/main/java/com/datastrato/graviton/client/DTOConverters.java@@ -0,0 +1,95 @@+/*+ * Copyright 2023 Datastrato.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.graviton.client;++import com.datastrato.graviton.Catalog;+import com.datastrato.graviton.CatalogChange;+import com.datastrato.graviton.MetalakeChange;+import com.datastrato.graviton.dto.AuditDTO;+import com.datastrato.graviton.dto.CatalogDTO;+import com.datastrato.graviton.dto.MetalakeDTO;+import com.datastrato.graviton.dto.requests.CatalogUpdateRequest;+import com.datastrato.graviton.dto.requests.MetalakeUpdateRequest;++class DTOConverters {+ private DTOConverters() {}++ static GravitonMetaLake toMetaLake(MetalakeDTO metalake, RESTClient client) {+ return new GravitonMetaLake.Builder()+ .withName(metalake.name())+ .withComment(metalake.comment())+ .withProperties(metalake.properties())+ .withAudit((AuditDTO) metalake.auditInfo())+ .withRestClient(client)+ .build();+ }++ static MetalakeUpdateRequest toMetalakeUpdateRequest(MetalakeChange change) {+ if (change instanceof MetalakeChange.RenameMetalake) {+ return new MetalakeUpdateRequest.RenameMetalakeRequest(+ ((MetalakeChange.RenameMetalake) change).getNewName());++ } else if (change instanceof MetalakeChange.UpdateMetalakeComment) {+ return new MetalakeUpdateRequest.UpdateMetalakeCommentRequest(+ ((MetalakeChange.UpdateMetalakeComment) change).getNewComment());++ } else if (change instanceof MetalakeChange.SetProperty) {+ return new MetalakeUpdateRequest.SetMetalakePropertyRequest(+ ((MetalakeChange.SetProperty) change).getProperty(),+ ((MetalakeChange.SetProperty) change).getValue());++ } else if (change instanceof MetalakeChange.RemoveProperty) {+ return new MetalakeUpdateRequest.RemoveMetalakePropertyRequest(+ ((MetalakeChange.RemoveProperty) change).getProperty());++ } else {+ throw new IllegalArgumentException(+ "Unknown change type: " + change.getClass().getSimpleName());+ }+ }++ static Catalog toCatalog(CatalogDTO catalog, RESTClient client) {+ switch (catalog.type()) {+ case RELATIONAL:+ return new RelationalCatalog.Builder()+ .withName(catalog.name())+ .withType(catalog.type())+ .withComment(catalog.comment())+ .withProperties(catalog.properties())+ .withAudit((AuditDTO) catalog.auditInfo())+ .withRestClient(client)+ .build();++ case FILE:+ case STREAM:+ default:+ throw new UnsupportedOperationException("Unsupported catalog type: " + catalog.type());+ }+ }++ static CatalogUpdateRequest toCatalogUpdateRequest(CatalogChange change) {+ if (change instanceof CatalogChange.RenameCatalog) {+ return new CatalogUpdateRequest.RenameCatalogRequest(+ ((CatalogChange.RenameCatalog) change).getNewName());++ } else if (change instanceof CatalogChange.UpdateCatalogComment) {+ return new CatalogUpdateRequest.UpdateCatalogCommentRequest(+ ((CatalogChange.UpdateCatalogComment) change).getNewComment());++ } else if (change instanceof CatalogChange.SetProperty) {+ return new CatalogUpdateRequest.SetCatalogPropertyRequest(+ ((CatalogChange.SetProperty) change).getProperty(),+ ((CatalogChange.SetProperty) change).getValue());++ } else if (change instanceof CatalogChange.RemoveProperty) {+ return new CatalogUpdateRequest.RemoveCatalogPropertyRequest(+ ((CatalogChange.RemoveProperty) change).getProperty());++ } else {+ throw new IllegalArgumentException(+ "Unknown change type: " + change.getClass().getSimpleName());+ }+ }+}diff --git a/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java b/client-java/src/main/java/com/datastrato/graviton/client/ErrorHandlers.javasimilarity index 68%rename from client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.javarename to client-java/src/main/java/com/datastrato/graviton/client/ErrorHandlers.javaindex 71bc120833..ccf7985e2c 100644--- a/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java+++ b/client-java/src/main/java/com/datastrato/graviton/client/ErrorHandlers.java@@ -2,13 +2,15 @@* Copyright 2023 Datastrato.* This software is licensed under the Apache License version 2.*/-package com.datastrato.graviton;+package com.datastrato.graviton.client;-import com.datastrato.graviton.client.ErrorHandler;import com.datastrato.graviton.dto.responses.ErrorConstants;import com.datastrato.graviton.dto.responses.ErrorResponse;+import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;import com.datastrato.graviton.exceptions.MetalakeAlreadyExistsException;+import com.datastrato.graviton.exceptions.NoSuchCatalogException;import com.datastrato.graviton.exceptions.NoSuchMetalakeException;+import com.datastrato.graviton.exceptions.NotFoundException;import com.datastrato.graviton.exceptions.RESTException;import com.fasterxml.jackson.databind.ObjectMapper;import com.google.common.base.Joiner;@@ -35,6 +37,10 @@ public class ErrorHandlers {return MetalakeErrorHandler.INSTANCE;}+ public static Consumer<ErrorResponse> catalogErrorHandler() {+ return CatalogErrorHandler.INSTANCE;+ }+/*** Creates a generic error handler for REST requests.*@@ -77,6 +83,36 @@ public class ErrorHandlers {}}+ /** Error handler specific to Catalog operations. */+ private static class CatalogErrorHandler extends RestErrorHandler {+ private static final ErrorHandler INSTANCE = new CatalogErrorHandler();++ @Override+ public void accept(ErrorResponse errorResponse) {+ switch (errorResponse.getCode()) {+ case ErrorConstants.ILLEGAL_ARGUMENTS_CODE:+ throw new IllegalArgumentException(formatErrorMessage(errorResponse));++ case ErrorConstants.NOT_FOUND_CODE:+ if (errorResponse.getType().equals(NoSuchMetalakeException.class.getSimpleName())) {+ throw new NoSuchMetalakeException(formatErrorMessage(errorResponse));+ } else if (errorResponse.getType().equals(NoSuchCatalogException.class.getSimpleName())) {+ throw new NoSuchCatalogException(formatErrorMessage(errorResponse));+ } else {+ throw new NotFoundException(formatErrorMessage(errorResponse));+ }++ case ErrorConstants.ALREADY_EXISTS_CODE:+ throw new CatalogAlreadyExistsException(formatErrorMessage(errorResponse));++ case ErrorConstants.INTERNAL_ERROR_CODE:+ throw new RuntimeException(formatErrorMessage(errorResponse));+ }++ super.accept(errorResponse);+ }+ }+/** Error handler specific to Metalake operations. */private static class MetalakeErrorHandler extends RestErrorHandler {private static final ErrorHandler INSTANCE = new MetalakeErrorHandler();@@ -86,10 +122,15 @@ public class ErrorHandlers {switch (errorResponse.getCode()) {case ErrorConstants.ILLEGAL_ARGUMENTS_CODE:throw new IllegalArgumentException(formatErrorMessage(errorResponse));+case ErrorConstants.NOT_FOUND_CODE:throw new NoSuchMetalakeException(formatErrorMessage(errorResponse));+case ErrorConstants.ALREADY_EXISTS_CODE:throw new MetalakeAlreadyExistsException(formatErrorMessage(errorResponse));++ case ErrorConstants.INTERNAL_ERROR_CODE:+ throw new RuntimeException(formatErrorMessage(errorResponse));}super.accept(errorResponse);@@ -114,7 +155,7 @@ public class ErrorHandlers {@Overridepublic void accept(ErrorResponse errorResponse) {- throw new RESTException("Unable to process: %s", errorResponse.getMessage());+ throw new RESTException("Unable to process: %s", formatErrorMessage(errorResponse));}}}diff --git a/client-java/src/main/java/com/datastrato/graviton/GravitonClient.java b/client-java/src/main/java/com/datastrato/graviton/client/GravitonClient.javasimilarity index 95%rename from client-java/src/main/java/com/datastrato/graviton/GravitonClient.javarename to client-java/src/main/java/com/datastrato/graviton/client/GravitonClient.javaindex c5aaf0bcea..f9665bffe2 100644--- a/client-java/src/main/java/com/datastrato/graviton/GravitonClient.java+++ b/client-java/src/main/java/com/datastrato/graviton/client/GravitonClient.java@@ -3,10 +3,11 @@* This software is licensed under the Apache License version 2.*/-package com.datastrato.graviton;+package com.datastrato.graviton.client;-import com.datastrato.graviton.client.HTTPClient;-import com.datastrato.graviton.client.RESTClient;+import com.datastrato.graviton.MetalakeChange;+import com.datastrato.graviton.NameIdentifier;+import com.datastrato.graviton.SupportsMetalakes;import com.datastrato.graviton.dto.requests.MetalakeCreateRequest;import com.datastrato.graviton.dto.requests.MetalakeUpdateRequest;import com.datastrato.graviton.dto.requests.MetalakeUpdatesRequest;@@ -88,7 +89,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {MetalakeResponse resp =restClient.get(- "api/metalakes/" + ident.toString(),+ "api/metalakes/" + ident.name(),MetalakeResponse.class,Collections.emptyMap(),ErrorHandlers.metalakeErrorHandler());@@ -116,7 +117,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {!ident.hasNamespace(),"Metalake name identifier is a top-level identifier, namespace should be empty");- MetalakeCreateRequest req = new MetalakeCreateRequest(ident.toString(), comment, properties);+ MetalakeCreateRequest req = new MetalakeCreateRequest(ident.name(), comment, properties);req.validate();MetalakeResponse resp =@@ -157,7 +158,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {MetalakeResponse resp =restClient.put(- "api/metalakes/" + ident.toString(),+ "api/metalakes/" + ident.name(),updatesRequest,MetalakeResponse.class,Collections.emptyMap(),@@ -183,7 +184,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {try {DropResponse resp =restClient.delete(- "api/metalakes/" + ident.toString(),+ "api/metalakes/" + ident.name(),DropResponse.class,Collections.emptyMap(),ErrorHandlers.metalakeErrorHandler());diff --git a/client-java/src/main/java/com/datastrato/graviton/client/GravitonMetaLake.java b/client-java/src/main/java/com/datastrato/graviton/client/GravitonMetaLake.javanew file mode 100644index 0000000000..d671a819dc--- /dev/null+++ b/client-java/src/main/java/com/datastrato/graviton/client/GravitonMetaLake.java@@ -0,0 +1,173 @@+/*+ * Copyright 2023 Datastrato.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.graviton.client;++import com.datastrato.graviton.Catalog;+import com.datastrato.graviton.CatalogChange;+import com.datastrato.graviton.NameIdentifier;+import com.datastrato.graviton.Namespace;+import com.datastrato.graviton.SupportsCatalogs;+import com.datastrato.graviton.dto.AuditDTO;+import com.datastrato.graviton.dto.MetalakeDTO;+import com.datastrato.graviton.dto.requests.CatalogCreateRequest;+import com.datastrato.graviton.dto.requests.CatalogUpdateRequest;+import com.datastrato.graviton.dto.requests.CatalogUpdatesRequest;+import com.datastrato.graviton.dto.responses.CatalogResponse;+import com.datastrato.graviton.dto.responses.DropResponse;+import com.datastrato.graviton.dto.responses.EntityListResponse;+import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;+import com.datastrato.graviton.exceptions.NoSuchCatalogException;+import com.datastrato.graviton.exceptions.NoSuchMetalakeException;+import com.google.common.base.Preconditions;+import java.util.Arrays;+import java.util.Collections;+import java.util.List;+import java.util.Map;+import java.util.stream.Collectors;+import org.apache.commons.lang3.StringUtils;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {++ private static final Logger LOG = LoggerFactory.getLogger(GravitonMetaLake.class);++ private final RESTClient restClient;++ GravitonMetaLake(+ String name,+ String comment,+ Map<String, String> properties,+ AuditDTO auditDTO,+ RESTClient restClient) {+ super(name, comment, properties, auditDTO);+ this.restClient = restClient;+ }++ @Override+ public NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeException {+ validateCatalogNamespace(namespace);++ EntityListResponse resp =+ restClient.get(+ String.format("api/metalakes/%s/catalogs", namespace.level(0)),+ EntityListResponse.class,+ Collections.emptyMap(),+ ErrorHandlers.catalogErrorHandler());+ resp.validate();++ return resp.identifiers();+ }++ @Override+ public Catalog loadCatalog(NameIdentifier ident) throws NoSuchCatalogException {+ validateCatalogIdentifier(ident);++ CatalogResponse resp =+ restClient.get(+ String.format("api/metalakes/%s/catalogs/%s", ident.namespace().level(0), ident.name()),+ CatalogResponse.class,+ Collections.emptyMap(),+ ErrorHandlers.catalogErrorHandler());+ resp.validate();++ return DTOConverters.toCatalog(resp.getCatalog(), restClient);+ }++ @Override+ public Catalog createCatalog(+ NameIdentifier ident, Catalog.Type type, String comment, Map<String, String> properties)+ throws NoSuchMetalakeException, CatalogAlreadyExistsException {+ validateCatalogIdentifier(ident);++ CatalogCreateRequest req = new CatalogCreateRequest(ident.name(), type, comment, properties);+ req.validate();++ CatalogResponse resp =+ restClient.post(+ String.format("api/metalakes/%s/catalogs", ident.namespace().level(0)),+ req,+ CatalogResponse.class,+ Collections.emptyMap(),+ ErrorHandlers.catalogErrorHandler());+ resp.validate();++ return DTOConverters.toCatalog(resp.getCatalog(), restClient);+ }++ @Override+ public Catalog alterCatalog(NameIdentifier ident, CatalogChange... changes)+ throws NoSuchCatalogException, IllegalArgumentException {+ validateCatalogIdentifier(ident);++ List<CatalogUpdateRequest> reqs =+ Arrays.stream(changes)+ .map(DTOConverters::toCatalogUpdateRequest)+ .collect(Collectors.toList());+ CatalogUpdatesRequest updatesRequest = new CatalogUpdatesRequest(reqs);+ updatesRequest.validate();++ CatalogResponse resp =+ restClient.put(+ String.format("api/metalakes/%s/catalogs/%s", ident.namespace().level(0), ident.name()),+ updatesRequest,+ CatalogResponse.class,+ Collections.emptyMap(),+ ErrorHandlers.catalogErrorHandler());+ resp.validate();++ return DTOConverters.toCatalog(resp.getCatalog(), restClient);+ }++ @Override+ public boolean dropCatalog(NameIdentifier ident) {+ validateCatalogIdentifier(ident);++ try {+ DropResponse resp =+ restClient.delete(+ String.format(+ "api/metalakes/%s/catalogs/%s", ident.namespace().level(0), ident.name()),+ DropResponse.class,+ Collections.emptyMap(),+ ErrorHandlers.catalogErrorHandler());+ resp.validate();+ return resp.dropped();++ } catch (Exception e) {+ LOG.warn("Failed to drop catalog {}", ident, e);+ return false;+ }+ }++ private static void validateCatalogNamespace(Namespace ns) {+ Preconditions.checkArgument(+ ns != null && ns.length() == 1, "namespace must not be null and have exactly one level");+ }++ private static void validateCatalogIdentifier(NameIdentifier ident) {+ validateCatalogNamespace(ident.namespace());+ Preconditions.checkArgument(+ StringUtils.isNotBlank(ident.name()), "name must not be null or empty");+ }++ static class Builder extends MetalakeDTO.Builder<Builder> {+ private RESTClient restClient;++ Builder withRestClient(RESTClient restClient) {+ this.restClient = restClient;+ return this;+ }++ @Override+ public GravitonMetaLake build() {+ Preconditions.checkNotNull(restClient, "restClient must be set");+ Preconditions.checkArgument(StringUtils.isNotBlank(name), "name must not be null or empty");+ Preconditions.checkArgument(audit != null, "audit must not be null");++ return new GravitonMetaLake(name, comment, properties, audit, restClient);+ }+ }+}diff --git a/client-java/src/main/java/com/datastrato/graviton/client/RelationalCatalog.java b/client-java/src/main/java/com/datastrato/graviton/client/RelationalCatalog.javanew file mode 100644index 0000000000..250424c835--- /dev/null+++ b/client-java/src/main/java/com/datastrato/graviton/client/RelationalCatalog.java@@ -0,0 +1,126 @@+/*+ * Copyright 2023 Datastrato.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.graviton.client;++import com.datastrato.graviton.NameIdentifier;+import com.datastrato.graviton.Namespace;+import com.datastrato.graviton.dto.AuditDTO;+import com.datastrato.graviton.dto.CatalogDTO;+import com.datastrato.graviton.exceptions.NoSuchCatalogException;+import com.datastrato.graviton.exceptions.NoSuchSchemaException;+import com.datastrato.graviton.exceptions.NoSuchTableException;+import com.datastrato.graviton.exceptions.NonEmptySchemaException;+import com.datastrato.graviton.exceptions.SchemaAlreadyExistsException;+import com.datastrato.graviton.exceptions.TableAlreadyExistsException;+import com.datastrato.graviton.rel.Column;+import com.datastrato.graviton.rel.Schema;+import com.datastrato.graviton.rel.SchemaChange;+import com.datastrato.graviton.rel.SupportsSchemas;+import com.datastrato.graviton.rel.Table;+import com.datastrato.graviton.rel.TableCatalog;+import com.datastrato.graviton.rel.TableChange;+import com.google.common.base.Preconditions;+import java.util.Map;++public class RelationalCatalog extends CatalogDTO implements TableCatalog, SupportsSchemas {++ private final RESTClient restClient;++ RelationalCatalog(+ String name,+ Type type,+ String comment,+ Map<String, String> properties,+ AuditDTO auditDTO,+ RESTClient restClient) {+ super(name, type, comment, properties, auditDTO);+ this.restClient = restClient;+ }++ @Override+ public SupportsSchemas asSchemas() {+ return this;+ }++ @Override+ public TableCatalog asTableCatalog() {+ return this;+ }++ @Override+ public NameIdentifier[] listTables(Namespace namespace) throws NoSuchCatalogException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public Table loadTable(NameIdentifier ident) throws NoSuchTableException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public Table createTable(+ NameIdentifier ident, Column[] columns, String comment, Map<String, String> properties)+ throws NoSuchSchemaException, TableAlreadyExistsException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public Table alterTable(NameIdentifier ident, TableChange... changes)+ throws NoSuchTableException, IllegalArgumentException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public boolean dropTable(NameIdentifier ident) {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchCatalogException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)+ throws NoSuchCatalogException, SchemaAlreadyExistsException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public Schema loadSchema(NameIdentifier ident) throws NoSuchSchemaException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public Schema alterSchema(NameIdentifier ident, SchemaChange... changes)+ throws NoSuchSchemaException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ @Override+ public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmptySchemaException {+ throw new UnsupportedOperationException("Not implemented yet");+ }++ static class Builder extends CatalogDTO.Builder<Builder> {+ private RESTClient restClient;++ Builder withRestClient(RESTClient restClient) {+ this.restClient = restClient;+ return this;+ }++ @Override+ public RelationalCatalog build() {+ Preconditions.checkArgument(restClient != null, "restClient must be set");+ Preconditions.checkArgument(+ name != null && !name.isEmpty(), "name must not be null or empty");+ Preconditions.checkArgument(type != null, "type must not be null");+ Preconditions.checkArgument(audit != null, "audit must not be null");++ return new RelationalCatalog(name, type, comment, properties, audit, restClient);+ }+ }+}diff --git a/client-java/src/test/java/com/datastrato/graviton/client/TestBase.java b/client-java/src/test/java/com/datastrato/graviton/client/TestBase.javanew file mode 100644index 0000000000..c13e65833a--- /dev/null+++ b/client-java/src/test/java/com/datastrato/graviton/client/TestBase.java@@ -0,0 +1,58 @@+/*+ * Copyright 2023 Datastrato.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.graviton.client;++import com.datastrato.graviton.json.JsonUtils;+import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.databind.ObjectMapper;+import org.apache.hc.core5.http.Method;+import org.junit.jupiter.api.AfterAll;+import org.junit.jupiter.api.BeforeAll;+import org.mockserver.integration.ClientAndServer;+import org.mockserver.matchers.Times;+import org.mockserver.model.HttpRequest;+import org.mockserver.model.HttpResponse;++public abstract class TestBase {++ private static final ObjectMapper MAPPER = JsonUtils.objectMapper();++ private static ClientAndServer mockServer;++ protected static GravitonClient client;++ @BeforeAll+ public static void setUp() throws Exception {+ mockServer = ClientAndServer.startClientAndServer(0);+ int port = mockServer.getLocalPort();+ client = GravitonClient.builder("http://127.0.0.1:" + port).build();+ }++ @AfterAll+ public static void tearDown() {+ mockServer.stop();+ client.close();+ }++ protected static <T, R> void buildMockResource(+ Method method, String path, T reqBody, R respBody, int statusCode)+ throws JsonProcessingException {+ HttpRequest mockRequest = HttpRequest.request(path).withMethod(method.name());+ if (reqBody != null) {+ String reqJson = MAPPER.writeValueAsString(reqBody);+ mockRequest = mockRequest.withBody(reqJson);+ }++ HttpResponse mockResponse = HttpResponse.response().withStatusCode(statusCode);+ if (respBody != null) {+ String respJson = MAPPER.writeValueAsString(respBody);+ mockResponse = mockResponse.withBody(respJson);+ }++ // Using Times.exactly(1) will only match once for the request, so we could set difference+ // responses for the same request and path.+ mockServer.when(mockRequest, Times.exactly(1)).respond(mockResponse);+ }+}diff --git a/client-java/src/test/java/com/datastrato/graviton/TestGravitonClient.java b/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonClient.javasimilarity index 85%rename from client-java/src/test/java/com/datastrato/graviton/TestGravitonClient.javarename to client-java/src/test/java/com/datastrato/graviton/client/TestGravitonClient.javaindex 99c5068146..ac6bf62de3 100644--- a/client-java/src/test/java/com/datastrato/graviton/TestGravitonClient.java+++ b/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonClient.java@@ -2,9 +2,10 @@* Copyright 2023 Datastrato.* This software is licensed under the Apache License version 2.*/+package com.datastrato.graviton.client;-package com.datastrato.graviton;-+import com.datastrato.graviton.MetalakeChange;+import com.datastrato.graviton.NameIdentifier;import com.datastrato.graviton.dto.AuditDTO;import com.datastrato.graviton.dto.MetalakeDTO;import com.datastrato.graviton.dto.requests.MetalakeCreateRequest;@@ -16,42 +17,17 @@ import com.datastrato.graviton.dto.responses.MetalakeResponse;import com.datastrato.graviton.exceptions.MetalakeAlreadyExistsException;import com.datastrato.graviton.exceptions.NoSuchMetalakeException;import com.datastrato.graviton.exceptions.RESTException;-import com.datastrato.graviton.json.JsonUtils;import com.fasterxml.jackson.core.JsonProcessingException;-import com.fasterxml.jackson.databind.ObjectMapper;import java.time.Instant;import java.util.Arrays;import java.util.Collections;import java.util.stream.Collectors;import org.apache.hc.core5.http.HttpStatus;import org.apache.hc.core5.http.Method;-import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.Assertions;-import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;-import org.mockserver.integration.ClientAndServer;-import org.mockserver.matchers.Times;-import org.mockserver.model.HttpRequest;-import org.mockserver.model.HttpResponse;--public class TestGravitonClient {-- private static final ObjectMapper MAPPER = JsonUtils.objectMapper();- private static ClientAndServer mockServer;- private static GravitonClient client;-- @BeforeAll- public static void setUp() {- mockServer = ClientAndServer.startClientAndServer(0);- int port = mockServer.getLocalPort();- client = GravitonClient.builder("http://127.0.0.1:" + port).build();- }- @AfterAll- public static void tearDown() {- mockServer.stop();- client.close();- }+public class TestGravitonClient extends TestBase {@Testpublic void testListMetalakes() throws JsonProcessingException {@@ -97,7 +73,7 @@ public class TestGravitonClient {ErrorResponse errorResp = ErrorResponse.internalError("mock error");buildMockResource(Method.GET, "/api/metalakes", null, errorResp, HttpStatus.SC_INTERNAL_SERVER_ERROR);- Throwable excep = Assertions.assertThrows(RESTException.class, () -> client.listMetalakes());+ Throwable excep = Assertions.assertThrows(RuntimeException.class, () -> client.listMetalakes());Assertions.assertTrue(excep.getMessage().contains("mock error"));// Test return unparsed system error@@ -259,24 +235,4 @@ public class TestGravitonClient {() -> client.dropMetalake(NameIdentifier.parse("mock.mock")));Assertions.assertTrue(excep1.getMessage().contains("namespace should be empty"));}-- private static <T, R> void buildMockResource(- Method method, String path, T reqBody, R respBody, int statusCode)- throws JsonProcessingException {- HttpRequest mockRequest = HttpRequest.request(path).withMethod(method.name());- if (reqBody != null) {- String reqJson = MAPPER.writeValueAsString(reqBody);- mockRequest = mockRequest.withBody(reqJson);- }-- HttpResponse mockResponse = HttpResponse.response().withStatusCode(statusCode);- if (respBody != null) {- String respJson = MAPPER.writeValueAsString(respBody);- mockResponse = mockResponse.withBody(respJson);- }-- // Using Times.exactly(1) will only match once for the request, so we could set difference- // responses for the same request and path.- mockServer.when(mockRequest, Times.exactly(1)).respond(mockResponse);- }}diff --git a/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonMetalake.java b/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonMetalake.javanew file mode 100644index 0000000000..623a792198--- /dev/null+++ b/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonMetalake.java@@ -0,0 +1,363 @@+/*+ * Copyright 2023 Datastrato.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.graviton.client;++import com.datastrato.graviton.Catalog;+import com.datastrato.graviton.CatalogChange;+import com.datastrato.graviton.NameIdentifier;+import com.datastrato.graviton.Namespace;+import com.datastrato.graviton.dto.AuditDTO;+import com.datastrato.graviton.dto.CatalogDTO;+import com.datastrato.graviton.dto.MetalakeDTO;+import com.datastrato.graviton.dto.requests.CatalogCreateRequest;+import com.datastrato.graviton.dto.requests.CatalogUpdateRequest;+import com.datastrato.graviton.dto.requests.CatalogUpdatesRequest;+import com.datastrato.graviton.dto.requests.MetalakeCreateRequest;+import com.datastrato.graviton.dto.responses.CatalogResponse;+import com.datastrato.graviton.dto.responses.DropResponse;+import com.datastrato.graviton.dto.responses.EntityListResponse;+import com.datastrato.graviton.dto.responses.ErrorResponse;+import com.datastrato.graviton.dto.responses.MetalakeResponse;+import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;+import com.datastrato.graviton.exceptions.NoSuchCatalogException;+import com.datastrato.graviton.exceptions.NoSuchMetalakeException;+import com.datastrato.graviton.exceptions.RESTException;+import com.fasterxml.jackson.core.JsonProcessingException;+import java.time.Instant;+import java.util.Arrays;+import java.util.Collections;+import java.util.List;+import java.util.stream.Collectors;+import org.apache.hc.core5.http.HttpStatus;+import org.apache.hc.core5.http.Method;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++public class TestGravitonMetalake extends TestBase {++ private static GravitonMetaLake metalake;++ private static final String metalakeName = "test";++ @BeforeAll+ public static void setUp() throws Exception {+ TestBase.setUp();++ MetalakeDTO mockMetalake =+ new MetalakeDTO.Builder()+ .withName(metalakeName)+ .withComment("comment")+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ MetalakeCreateRequest req =+ new MetalakeCreateRequest(metalakeName, "comment", Collections.emptyMap());+ MetalakeResponse resp = new MetalakeResponse(mockMetalake);+ buildMockResource(Method.POST, "/api/metalakes", req, resp, HttpStatus.SC_OK);++ metalake =+ client.createMetalake(+ NameIdentifier.parse(metalakeName), "comment", Collections.emptyMap());+ }++ @Test+ public void testListCatalogs() throws JsonProcessingException {+ String path = "/api/metalakes/" + metalakeName + "/catalogs";++ CatalogDTO mockCatalog =+ new CatalogDTO.Builder()+ .withName("mock")+ .withComment("comment")+ .withType(Catalog.Type.RELATIONAL)+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ CatalogDTO mockCatalog2 =+ new CatalogDTO.Builder()+ .withName("mock2")+ .withComment("comment")+ .withType(Catalog.Type.RELATIONAL)+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ NameIdentifier ident1 = NameIdentifier.of(metalakeName, "mock");+ NameIdentifier ident2 = NameIdentifier.of(metalakeName, "mock2");++ EntityListResponse resp = new EntityListResponse(new NameIdentifier[] {ident1, ident2});+ buildMockResource(Method.GET, path, null, resp, HttpStatus.SC_OK);+ NameIdentifier[] catalogs = metalake.listCatalogs(Namespace.of(metalakeName));++ Assertions.assertEquals(2, catalogs.length);+ Assertions.assertEquals(ident1, catalogs[0]);+ Assertions.assertEquals(ident2, catalogs[1]);++ // Test return empty catalog list+ EntityListResponse resp1 = new EntityListResponse(new NameIdentifier[] {});+ buildMockResource(Method.GET, path, null, resp1, HttpStatus.SC_OK);+ NameIdentifier[] catalogs1 = metalake.listCatalogs(Namespace.of(metalakeName));+ Assertions.assertEquals(0, catalogs1.length);++ // Test return internal error+ ErrorResponse errorResp = ErrorResponse.internalError("mock error");+ buildMockResource(Method.GET, path, null, errorResp, HttpStatus.SC_INTERNAL_SERVER_ERROR);+ Throwable ex =+ Assertions.assertThrows(+ RuntimeException.class, () -> metalake.listCatalogs(Namespace.of(metalakeName)));+ Assertions.assertTrue(ex.getMessage().contains("mock error"));++ // Test return unparsed system error+ buildMockResource(Method.GET, path, null, "mock error", HttpStatus.SC_CONFLICT);+ Throwable ex1 =+ Assertions.assertThrows(+ RESTException.class, () -> metalake.listCatalogs(Namespace.of(metalakeName)));+ Assertions.assertTrue(ex1.getMessage().contains("Error code: " + HttpStatus.SC_CONFLICT));+ }++ @Test+ public void testLoadCatalog() throws JsonProcessingException {+ String catalogName = "mock";+ String path = "/api/metalakes/" + metalakeName + "/catalogs/" + catalogName;++ CatalogDTO mockCatalog =+ new CatalogDTO.Builder()+ .withName("mock")+ .withComment("comment")+ .withType(Catalog.Type.RELATIONAL)+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ CatalogResponse resp = new CatalogResponse(mockCatalog);++ buildMockResource(Method.GET, path, null, resp, HttpStatus.SC_OK);+ Catalog catalog = metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName));++ Assertions.assertEquals(catalogName, catalog.name());+ Assertions.assertEquals("comment", catalog.comment());+ Assertions.assertEquals(Catalog.Type.RELATIONAL, catalog.type());++ // Test return not found+ ErrorResponse errorResponse =+ ErrorResponse.notFound(NoSuchCatalogException.class.getSimpleName(), "mock error");+ buildMockResource(Method.GET, path, null, errorResponse, HttpStatus.SC_NOT_FOUND);+ Throwable ex =+ Assertions.assertThrows(+ NoSuchCatalogException.class,+ () -> metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName)));+ Assertions.assertTrue(ex.getMessage().contains("mock error"));++ // Test return unsupported catalog type+ CatalogDTO mockCatalog1 =+ new CatalogDTO.Builder()+ .withName("mock")+ .withComment("comment")+ .withType(Catalog.Type.FILE)+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ CatalogResponse resp1 = new CatalogResponse(mockCatalog1);+ buildMockResource(Method.GET, path, null, resp1, HttpStatus.SC_OK);+ Assertions.assertThrows(+ UnsupportedOperationException.class,+ () -> metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName)));++ // Test return internal error+ ErrorResponse errorResp = ErrorResponse.internalError("mock error");+ buildMockResource(Method.GET, path, null, errorResp, HttpStatus.SC_INTERNAL_SERVER_ERROR);+ Throwable ex1 =+ Assertions.assertThrows(+ RuntimeException.class,+ () -> metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName)));+ Assertions.assertTrue(ex1.getMessage().contains("mock error"));++ // Test return unparsed system error+ buildMockResource(Method.GET, path, null, "mock error", HttpStatus.SC_CONFLICT);+ Throwable ex2 =+ Assertions.assertThrows(+ RESTException.class,+ () -> metalake.loadCatalog(NameIdentifier.of(metalakeName, catalogName)));+ Assertions.assertTrue(ex2.getMessage().contains("Error code: " + HttpStatus.SC_CONFLICT));+ }++ @Test+ public void testCreateCatalog() throws JsonProcessingException {+ String catalogName = "mock";+ String path = "/api/metalakes/" + metalakeName + "/catalogs";++ CatalogDTO mockCatalog =+ new CatalogDTO.Builder()+ .withName(catalogName)+ .withComment("comment")+ .withType(Catalog.Type.RELATIONAL)+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ CatalogCreateRequest req =+ new CatalogCreateRequest(+ catalogName, Catalog.Type.RELATIONAL, "comment", Collections.emptyMap());+ CatalogResponse resp = new CatalogResponse(mockCatalog);+ buildMockResource(Method.POST, path, req, resp, HttpStatus.SC_OK);++ Catalog catalog =+ metalake.createCatalog(+ NameIdentifier.of(metalakeName, catalogName),+ Catalog.Type.RELATIONAL,+ "comment",+ Collections.emptyMap());+ Assertions.assertEquals(catalogName, catalog.name());+ Assertions.assertEquals("comment", catalog.comment());+ Assertions.assertEquals(Catalog.Type.RELATIONAL, catalog.type());++ // Test return unsupported catalog type+ CatalogDTO mockCatalog1 =+ new CatalogDTO.Builder()+ .withName("mock")+ .withComment("comment")+ .withType(Catalog.Type.FILE)+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ CatalogCreateRequest req1 =+ new CatalogCreateRequest(catalogName, Catalog.Type.FILE, "comment", Collections.emptyMap());+ CatalogResponse resp1 = new CatalogResponse(mockCatalog1);+ buildMockResource(Method.POST, path, req1, resp1, HttpStatus.SC_OK);+ Assertions.assertThrows(+ UnsupportedOperationException.class,+ () ->+ metalake.createCatalog(+ NameIdentifier.of(metalakeName, catalogName),+ Catalog.Type.FILE,+ "comment",+ Collections.emptyMap()));++ // Test return NoSuchMetalakeException+ ErrorResponse errorResponse =+ ErrorResponse.notFound(NoSuchMetalakeException.class.getSimpleName(), "mock error");+ buildMockResource(Method.POST, path, req, errorResponse, HttpStatus.SC_NOT_FOUND);+ Throwable ex =+ Assertions.assertThrows(+ NoSuchMetalakeException.class,+ () ->+ metalake.createCatalog(+ NameIdentifier.of(metalakeName, catalogName),+ Catalog.Type.RELATIONAL,+ "comment",+ Collections.emptyMap()));+ Assertions.assertTrue(ex.getMessage().contains("mock error"));++ // Test return CatalogAlreadyExistsException+ ErrorResponse errorResponse1 =+ ErrorResponse.alreadyExists(+ CatalogAlreadyExistsException.class.getSimpleName(), "mock error");+ buildMockResource(Method.POST, path, req, errorResponse1, HttpStatus.SC_CONFLICT);+ Throwable ex1 =+ Assertions.assertThrows(+ CatalogAlreadyExistsException.class,+ () ->+ metalake.createCatalog(+ NameIdentifier.of(metalakeName, catalogName),+ Catalog.Type.RELATIONAL,+ "comment",+ Collections.emptyMap()));+ Assertions.assertTrue(ex1.getMessage().contains("mock error"));++ // Test return internal error+ ErrorResponse errorResp = ErrorResponse.internalError("mock error");+ buildMockResource(Method.POST, path, req, errorResp, HttpStatus.SC_INTERNAL_SERVER_ERROR);+ Throwable ex2 =+ Assertions.assertThrows(+ RuntimeException.class,+ () ->+ metalake.createCatalog(+ NameIdentifier.of(metalakeName, catalogName),+ Catalog.Type.RELATIONAL,+ "comment",+ Collections.emptyMap()));+ Assertions.assertTrue(ex2.getMessage().contains("mock error"));+ }++ @Test+ public void testAlterCatalog() throws JsonProcessingException {+ String catalogName = "mock";+ String path = "/api/metalakes/" + metalakeName + "/catalogs/" + catalogName;++ CatalogDTO mockCatalog =+ new CatalogDTO.Builder()+ .withName("mock1")+ .withComment("comment1")+ .withType(Catalog.Type.RELATIONAL)+ .withAudit(+ new AuditDTO.Builder().withCreator("creator").withCreateTime(Instant.now()).build())+ .build();+ CatalogResponse resp = new CatalogResponse(mockCatalog);++ CatalogChange change1 = CatalogChange.rename("mock1");+ CatalogChange change2 = CatalogChange.updateComment("comment1");+ List<CatalogUpdateRequest> reqs =+ Arrays.asList(change1, change2).stream()+ .map(DTOConverters::toCatalogUpdateRequest)+ .collect(Collectors.toList());+ CatalogUpdatesRequest updatesRequest = new CatalogUpdatesRequest(reqs);++ buildMockResource(Method.PUT, path, updatesRequest, resp, HttpStatus.SC_OK);+ Catalog catalog =+ metalake.alterCatalog(NameIdentifier.of(metalakeName, catalogName), change1, change2);+ Assertions.assertEquals("mock1", catalog.name());+ Assertions.assertEquals("comment1", catalog.comment());+ Assertions.assertEquals(Catalog.Type.RELATIONAL, catalog.type());++ // Test return NoSuchCatalogException+ ErrorResponse errorResponse =+ ErrorResponse.notFound(NoSuchCatalogException.class.getSimpleName(), "mock error");+ buildMockResource(Method.PUT, path, updatesRequest, errorResponse, HttpStatus.SC_NOT_FOUND);+ Throwable ex =+ Assertions.assertThrows(+ NoSuchCatalogException.class,+ () ->+ metalake.alterCatalog(+ NameIdentifier.of(metalakeName, catalogName), change1, change2));+ Assertions.assertTrue(ex.getMessage().contains("mock error"));++ // Test return IllegalArgumentException+ ErrorResponse errorResponse1 = ErrorResponse.illegalArguments("mock error");+ buildMockResource(Method.PUT, path, updatesRequest, errorResponse1, HttpStatus.SC_BAD_REQUEST);+ Throwable ex1 =+ Assertions.assertThrows(+ IllegalArgumentException.class,+ () ->+ metalake.alterCatalog(+ NameIdentifier.of(metalakeName, catalogName), change1, change2));+ Assertions.assertTrue(ex1.getMessage().contains("mock error"));++ // Test return internal error+ ErrorResponse errorResp = ErrorResponse.internalError("mock error");+ buildMockResource(+ Method.PUT, path, updatesRequest, errorResp, HttpStatus.SC_INTERNAL_SERVER_ERROR);+ Throwable ex2 =+ Assertions.assertThrows(+ RuntimeException.class,+ () ->+ metalake.alterCatalog(+ NameIdentifier.of(metalakeName, catalogName), change1, change2));+ Assertions.assertTrue(ex2.getMessage().contains("mock error"));+ }++ @Test+ public void testDropCatalog() throws JsonProcessingException {+ String catalogName = "mock";+ String path = "/api/metalakes/" + metalakeName + "/catalogs/" + catalogName;++ DropResponse resp = new DropResponse(true);+ buildMockResource(Method.DELETE, path, null, resp, HttpStatus.SC_OK);+ boolean dropped = metalake.dropCatalog(NameIdentifier.of(metalakeName, catalogName));+ Assertions.assertTrue(dropped);++ // Test return false+ DropResponse resp1 = new DropResponse(false);+ buildMockResource(Method.DELETE, path, null, resp1, HttpStatus.SC_OK);+ boolean dropped1 = metalake.dropCatalog(NameIdentifier.of(metalakeName, catalogName));+ Assertions.assertFalse(dropped1);+ }+}diff --git a/common/src/main/java/com/datastrato/graviton/dto/CatalogDTO.java b/common/src/main/java/com/datastrato/graviton/dto/CatalogDTO.javaindex db80dff761..6f26f2d3b1 100644--- a/common/src/main/java/com/datastrato/graviton/dto/CatalogDTO.java+++ b/common/src/main/java/com/datastrato/graviton/dto/CatalogDTO.java@@ -35,9 +35,9 @@ public class CatalogDTO implements Catalog {@JsonProperty("audit")private AuditDTO audit;- private CatalogDTO() {}+ protected CatalogDTO() {}- private CatalogDTO(+ protected CatalogDTO(String name, Type type, String comment, Map<String, String> properties, AuditDTO audit) {this.name = name;this.type = type;@@ -71,15 +71,6 @@ public class CatalogDTO implements Catalog {return audit;}- /**- * Creates a new Builder for constructing a Catalog DTO.- *- * @return A new Builder instance.- */- public static Builder builder() {- return new Builder();- }-/*** Builder class for constructing a CatalogDTO instance.*@@ -133,7 +124,7 @@ public class CatalogDTO implements Catalog {* @param properties The properties of the catalog.* @return The builder instance.*/- public Builder withProperties(Map<String, String> properties) {+ public S withProperties(Map<String, String> properties) {this.properties = properties;return (S) this;}@@ -144,7 +135,7 @@ public class CatalogDTO implements Catalog {* @param audit The audit information of the catalog.* @return The builder instance.*/- public Builder withAudit(AuditDTO audit) {+ public S withAudit(AuditDTO audit) {this.audit = audit;return (S) this;}diff --git a/common/src/test/java/com/datastrato/graviton/json/TestDTOJsonSerDe.java b/common/src/test/java/com/datastrato/graviton/json/TestDTOJsonSerDe.javaindex e3619e794f..7a706a2b2a 100644--- a/common/src/test/java/com/datastrato/graviton/json/TestDTOJsonSerDe.java+++ b/common/src/test/java/com/datastrato/graviton/json/TestDTOJsonSerDe.java@@ -124,7 +124,7 @@ public class TestDTOJsonSerDe {AuditDTO audit =AuditDTO.builder().withCreator("creator").withCreateTime(Instant.now()).build();CatalogDTO catalog =- CatalogDTO.builder()+ new CatalogDTO.Builder().withName("catalog").withType(Catalog.Type.RELATIONAL).withComment("comment")@@ -138,7 +138,7 @@ public class TestDTOJsonSerDe {// test with optional fieldsCatalogDTO catalog1 =- CatalogDTO.builder()+ new CatalogDTO.Builder().withName("catalog").withType(Catalog.Type.RELATIONAL).withAudit(audit)