Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#8196]Improvement(TableDto): validation in TableDTO Builder (#8247)

Sambhavi Pandey45759ad112f8
diff --git a/common/src/main/java/org/apache/gravitino/dto/rel/TableDTO.java b/common/src/main/java/org/apache/gravitino/dto/rel/TableDTO.java
index 9a6843a3fd..d029c609b9 100644
--- a/common/src/main/java/org/apache/gravitino/dto/rel/TableDTO.java
+++ b/common/src/main/java/org/apache/gravitino/dto/rel/TableDTO.java
@@ -296,6 +296,8 @@ public class TableDTO implements Table {
public TableDTO build() {
Preconditions.checkArgument(name != null && !name.isEmpty(), "name cannot be null or empty");
Preconditions.checkArgument(audit != null, "audit cannot be null");
+ Preconditions.checkArgument(
+ columns != null && columns.length > 0, "columns cannot be null or empty");
return new TableDTO(
name,
diff --git a/common/src/test/java/org/apache/gravitino/dto/rel/TestTableDTO.java b/common/src/test/java/org/apache/gravitino/dto/rel/TestTableDTO.java
new file mode 100644
index 0000000000..5f0733c5ea
--- /dev/null
+++ b/common/src/test/java/org/apache/gravitino/dto/rel/TestTableDTO.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.dto.rel;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.time.Instant;
+import org.apache.gravitino.dto.AuditDTO;
+import org.apache.gravitino.rel.types.Types;
+import org.junit.jupiter.api.Test;
+
+public class TestTableDTO {
+ @Test
+ public void testBuildWithoutColumns() {
+ AuditDTO audit =
+ AuditDTO.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+ TableDTO.Builder<?> builder = TableDTO.builder().withName("t1").withAudit(audit);
+ assertThrows(IllegalArgumentException.class, builder::build);
+ }
+
+ @Test
+ public void testBuildWithEmptyColumns() {
+ AuditDTO audit =
+ AuditDTO.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+ TableDTO.Builder<?> builder =
+ TableDTO.builder().withName("t1").withAudit(audit).withColumns(new ColumnDTO[0]);
+ assertThrows(IllegalArgumentException.class, builder::build);
+ }
+
+ @Test
+ public void testBuildWithColumns() {
+ AuditDTO audit =
+ AuditDTO.builder().withCreator("creator").withCreateTime(Instant.now()).build();
+ ColumnDTO column =
+ ColumnDTO.builder().withName("c1").withDataType(Types.IntegerType.get()).build();
+ TableDTO.Builder<?> builder =
+ TableDTO.builder().withName("t1").withAudit(audit).withColumns(new ColumnDTO[] {column});
+ assertDoesNotThrow(builder::build);
+ }
+}