Skip to content

Commit 875caf2

Browse files
committed
Throw error instead of -1
1 parent 45dd562 commit 875caf2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

polaris-core/src/main/java/org/apache/polaris/core/policy/PolicyEntity.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ public PolicyType getPolicyType() {
5353

5454
@JsonIgnore
5555
public int getPolicyTypeCode() {
56+
Preconditions.checkArgument(
57+
getPropertiesAsMap().containsKey(POLICY_TYPE_CODE_KEY),
58+
"Invalid policy entity: policy type must exist");
5659
String policyTypeCode = getPropertiesAsMap().get(POLICY_TYPE_CODE_KEY);
57-
if (policyTypeCode != null) {
58-
return Integer.parseInt(policyTypeCode);
59-
}
60-
61-
return -1;
60+
return Integer.parseInt(policyTypeCode);
6261
}
6362

6463
@JsonIgnore
@@ -93,7 +92,7 @@ public Builder(PolicyEntity original) {
9392
@Override
9493
public PolicyEntity build() {
9594
Preconditions.checkArgument(
96-
properties.get(POLICY_TYPE_CODE_KEY) != null, "Policy type must be specified");
95+
properties.containsKey(POLICY_TYPE_CODE_KEY), "Policy type must be specified");
9796

9897
return new PolicyEntity(buildBase());
9998
}
@@ -107,6 +106,7 @@ public Builder setParentNamespace(Namespace namespace) {
107106
}
108107

109108
public Builder setPolicyType(PolicyType policyType) {
109+
Preconditions.checkArgument(policyType != null, "Policy type must be specified");
110110
properties.put(POLICY_TYPE_CODE_KEY, Integer.toString(policyType.getCode()));
111111
return this;
112112
}

polaris-core/src/test/java/org/apache/polaris/core/policy/PolicyEntityTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.iceberg.catalog.Namespace;
2323
import org.apache.polaris.core.entity.PolarisEntityType;
2424
import org.assertj.core.api.Assertions;
25+
import org.junit.jupiter.api.Test;
2526
import org.junit.jupiter.params.ParameterizedTest;
2627
import org.junit.jupiter.params.provider.Arguments;
2728
import org.junit.jupiter.params.provider.MethodSource;
@@ -49,4 +50,16 @@ public void testPolicyEntity(PolicyType policyType) {
4950
Assertions.assertThat(entity.getPolicyTypeCode()).isEqualTo(policyType.getCode());
5051
Assertions.assertThat(entity.getContent()).isEqualTo("test_content");
5152
}
53+
54+
@Test
55+
public void testBuildPolicyEntityWithoutPolicyTye() {
56+
Assertions.assertThatThrownBy(
57+
() ->
58+
new PolicyEntity.Builder(Namespace.of("NS1"), "testPolicy", null)
59+
.setContent("test_content")
60+
.setPolicyVersion(0)
61+
.build())
62+
.isInstanceOf(IllegalArgumentException.class)
63+
.hasMessage("Policy type must be specified");
64+
}
5265
}

0 commit comments

Comments
 (0)