This is an automated email from the ASF dual-hosted git repository.

jshao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 1e8511ae6 [#4197] improvement(common): CatalogListResponse should 
implement the method validate (#4223)
1e8511ae6 is described below

commit 1e8511ae63df75545c4804d94eda2120ed8cfffc
Author: jingjia88 <[email protected]>
AuthorDate: Mon Jul 22 18:20:48 2024 +0800

    [#4197] improvement(common): CatalogListResponse should implement the 
method validate (#4223)
    
    ### What changes were proposed in this pull request?
    
    Add method `validate` in CatalogListResponse
    
    ### Why are the changes needed?
    
    Fix: #4197
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    Add the new ut.
---
 .../dto/responses/CatalogListResponse.java         | 26 ++++++++++++++++++++++
 .../gravitino/dto/responses/TestResponses.java     | 22 ++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git 
a/common/src/main/java/org/apache/gravitino/dto/responses/CatalogListResponse.java
 
b/common/src/main/java/org/apache/gravitino/dto/responses/CatalogListResponse.java
index dfb31417a..6d069ae46 100644
--- 
a/common/src/main/java/org/apache/gravitino/dto/responses/CatalogListResponse.java
+++ 
b/common/src/main/java/org/apache/gravitino/dto/responses/CatalogListResponse.java
@@ -19,9 +19,12 @@
 package org.apache.gravitino.dto.responses;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.ToString;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.gravitino.dto.CatalogDTO;
 
 /** Represents a response for a list of catalogs with their information. */
@@ -51,4 +54,27 @@ public class CatalogListResponse extends BaseResponse {
     super();
     this.catalogs = null;
   }
+
+  /**
+   * Validates the response data.
+   *
+   * @throws IllegalArgumentException if name, type or audit information is 
not set.
+   */
+  @Override
+  public void validate() throws IllegalArgumentException {
+    super.validate();
+
+    Preconditions.checkArgument(catalogs != null, "catalogs must be non-null");
+    Arrays.stream(catalogs)
+        .forEach(
+            catalog -> {
+              Preconditions.checkArgument(
+                  StringUtils.isNotBlank(catalog.name()),
+                  "catalog 'name' must not be null and empty");
+              Preconditions.checkArgument(
+                  catalog.type() != null, "catalog 'type' must not be null");
+              Preconditions.checkArgument(
+                  catalog.auditInfo() != null, "catalog 'audit' must not be 
null");
+            });
+  }
 }
diff --git 
a/common/src/test/java/org/apache/gravitino/dto/responses/TestResponses.java 
b/common/src/test/java/org/apache/gravitino/dto/responses/TestResponses.java
index 8d7c52808..7f9ebfeb7 100644
--- a/common/src/test/java/org/apache/gravitino/dto/responses/TestResponses.java
+++ b/common/src/test/java/org/apache/gravitino/dto/responses/TestResponses.java
@@ -146,6 +146,28 @@ public class TestResponses {
     assertThrows(IllegalArgumentException.class, () -> catalog.validate());
   }
 
+  @Test
+  void testCatalogListResponse() throws IllegalArgumentException {
+    AuditDTO audit =
+        
AuditDTO.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+    CatalogDTO catalog =
+        CatalogDTO.builder()
+            .withName("CatalogA")
+            .withComment("comment")
+            .withType(Catalog.Type.RELATIONAL)
+            .withProvider("test")
+            .withAudit(audit)
+            .build();
+    CatalogListResponse response = new CatalogListResponse(new CatalogDTO[] 
{catalog});
+    response.validate(); // No exception thrown
+  }
+
+  @Test
+  void testCatalogListException() throws IllegalArgumentException {
+    CatalogListResponse response = new CatalogListResponse();
+    assertThrows(IllegalArgumentException.class, () -> response.validate());
+  }
+
   @Test
   void testSchemaResponse() throws IllegalArgumentException {
     AuditDTO audit =

Reply via email to