File tree Expand file tree Collapse file tree 2 files changed +19
-6
lines changed
main/java/org/apache/polaris/core/policy
test/java/org/apache/polaris/core/policy Expand file tree Collapse file tree 2 files changed +19
-6
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 2222import org .apache .iceberg .catalog .Namespace ;
2323import org .apache .polaris .core .entity .PolarisEntityType ;
2424import org .assertj .core .api .Assertions ;
25+ import org .junit .jupiter .api .Test ;
2526import org .junit .jupiter .params .ParameterizedTest ;
2627import org .junit .jupiter .params .provider .Arguments ;
2728import 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}
You can’t perform that action at this time.
0 commit comments