Skip to content

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

[#7310] Improvement(core):Call isPresent() first in PropertiesMetadata (#7310)

lsyulong521688ae42ec
diff --git a/core/src/main/java/org/apache/gravitino/connector/PropertiesMetadata.java b/core/src/main/java/org/apache/gravitino/connector/PropertiesMetadata.java
index ad40589cb3..196fbb53aa 100644
--- a/core/src/main/java/org/apache/gravitino/connector/PropertiesMetadata.java
+++ b/core/src/main/java/org/apache/gravitino/connector/PropertiesMetadata.java
@@ -149,11 +149,19 @@ public interface PropertiesMetadata {
if (!containsProperty(propertyName)) {
throw new IllegalArgumentException("Property is not defined: " + propertyName);
}
- return getNonPrefixEntry(propertyName).isPresent()
- ? getNonPrefixEntry(propertyName).get()
- : getPropertyPrefixEntry(propertyName).get();
- }
+ Optional<PropertyEntry<?>> nonPrefixEntry = getNonPrefixEntry(propertyName);
+ if (nonPrefixEntry.isPresent()) {
+ return nonPrefixEntry.get();
+ }
+
+ Optional<PropertyEntry<?>> prefixEntry = getPropertyPrefixEntry(propertyName);
+ if (prefixEntry.isPresent()) {
+ return prefixEntry.get();
+ }
+
+ throw new IllegalArgumentException("Property is not defined: " + propertyName);
+ }
/**
* Get the property prefix entry of the property. If there are multiple property prefix entries
* matching the property name, the longest prefix entry will be returned.
diff --git a/core/src/test/java/org/apache/gravitino/TestBasePropertiesMetadata.java b/core/src/test/java/org/apache/gravitino/TestBasePropertiesMetadata.java
index c613d53c47..44281d7067 100644
--- a/core/src/test/java/org/apache/gravitino/TestBasePropertiesMetadata.java
+++ b/core/src/test/java/org/apache/gravitino/TestBasePropertiesMetadata.java
@@ -18,12 +18,18 @@
*/
package org.apache.gravitino;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import org.apache.gravitino.connector.BasePropertiesMetadata;
import org.apache.gravitino.connector.PropertyEntry;
+import org.junit.jupiter.api.Test;
public class TestBasePropertiesMetadata extends BasePropertiesMetadata {
@@ -51,4 +57,40 @@ public class TestBasePropertiesMetadata extends BasePropertiesMetadata {
protected Map<String, PropertyEntry<?>> specificPropertyEntries() {
return TEST_BASE_PROPERTY;
}
+
+ @Test
+ public void testGetPropertyEntryWithExistingProperty() {
+ TestBasePropertiesMetadata metadata = new TestBasePropertiesMetadata();
+
+ PropertyEntry<?> entry =
+ metadata.getPropertyEntry(TestBasePropertiesMetadata.TEST_REQUIRED_KEY);
+ assertNotNull(entry);
+ assertEquals(TestBasePropertiesMetadata.TEST_REQUIRED_KEY, entry.getName());
+ assertEquals("test required k1 property", entry.getDescription());
+ assertTrue(entry.isRequired());
+ }
+
+ @Test
+ public void testGetPropertyEntryWithNonExistentProperty() {
+ TestBasePropertiesMetadata metadata = new TestBasePropertiesMetadata();
+
+ IllegalArgumentException exception =
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> metadata.getPropertyEntry("non-existent-property"));
+ assertTrue(exception.getMessage().contains("Property is not defined"));
+ }
+
+ @Test
+ public void testGetPropertyEntryWithInvalidInput() {
+ TestBasePropertiesMetadata metadata = new TestBasePropertiesMetadata();
+
+ IllegalArgumentException nullException =
+ assertThrows(IllegalArgumentException.class, () -> metadata.getPropertyEntry(null));
+ assertTrue(nullException.getMessage().contains("Property is not defined"));
+
+ IllegalArgumentException emptyException =
+ assertThrows(IllegalArgumentException.class, () -> metadata.getPropertyEntry(""));
+ assertTrue(emptyException.getMessage().contains("Property is not defined"));
+ }
}