23
23
import static org .apache .polaris .core .policy .PredefinedPolicyTypes .DATA_COMPACTION ;
24
24
import static org .assertj .core .api .Assertions .assertThat ;
25
25
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
26
+ import static org .mockito .Mockito .mock ;
27
+ import static org .mockito .Mockito .when ;
26
28
27
29
import org .apache .iceberg .catalog .Namespace ;
28
30
import org .apache .iceberg .catalog .TableIdentifier ;
31
+ import org .apache .polaris .core .config .FeatureConfiguration ;
32
+ import org .apache .polaris .core .config .RealmConfig ;
33
+ import org .apache .polaris .core .context .CallContext ;
29
34
import org .apache .polaris .core .entity .CatalogEntity ;
30
35
import org .apache .polaris .core .entity .NamespaceEntity ;
31
36
import org .apache .polaris .core .entity .PrincipalEntity ;
@@ -38,6 +43,7 @@ public class PolicyValidatorsTest {
38
43
Namespace ns = Namespace .of ("NS1" );
39
44
TableIdentifier tableIdentifier = TableIdentifier .of (ns , "table1" );
40
45
PolicyEntity policyEntity = new PolicyEntity .Builder (ns , "pn" , DATA_COMPACTION ).build ();
46
+ CallContext callContext = mock (CallContext .class );
41
47
42
48
@ Test
43
49
public void testInvalidPolicy () {
@@ -92,22 +98,22 @@ public void testValidPolicy() {
92
98
@ Test
93
99
public void testCanAttachReturnsTrueForCatalogType () {
94
100
var targetEntity = new CatalogEntity .Builder ().build ();
95
- var result = PolicyValidators .canAttach (policyEntity , targetEntity );
101
+ var result = PolicyValidators .canAttach (callContext , policyEntity , targetEntity );
96
102
assertThat (result ).isTrue ().as ("Expected canAttach() to return true for CATALOG type" );
97
103
}
98
104
99
105
@ Test
100
106
public void testCanAttachReturnsTrueForNamespaceType () {
101
107
var targetEntity = new NamespaceEntity .Builder (ns ).build ();
102
- var result = PolicyValidators .canAttach (policyEntity , targetEntity );
108
+ var result = PolicyValidators .canAttach (callContext , policyEntity , targetEntity );
103
109
assertThat (result ).isTrue ().as ("Expected canAttach() to return true for CATALOG type" );
104
110
}
105
111
106
112
@ Test
107
113
public void testCanAttachReturnsTrueForIcebergTableLikeWithTableSubtype () {
108
114
var targetEntity =
109
115
new IcebergTableLikeEntity .Builder (tableIdentifier , "" ).setSubType (ICEBERG_TABLE ).build ();
110
- var result = PolicyValidators .canAttach (policyEntity , targetEntity );
116
+ var result = PolicyValidators .canAttach (callContext , policyEntity , targetEntity );
111
117
assertThat (result )
112
118
.isTrue ()
113
119
.as ("Expected canAttach() to return true for ICEBERG_TABLE_LIKE with TABLE subtype" );
@@ -117,7 +123,7 @@ public void testCanAttachReturnsTrueForIcebergTableLikeWithTableSubtype() {
117
123
public void testCanAttachReturnsFalseForIcebergTableLikeWithNonTableSubtype () {
118
124
var targetEntity =
119
125
new IcebergTableLikeEntity .Builder (tableIdentifier , "" ).setSubType (ICEBERG_VIEW ).build ();
120
- var result = PolicyValidators .canAttach (policyEntity , targetEntity );
126
+ var result = PolicyValidators .canAttach (callContext , policyEntity , targetEntity );
121
127
assertThat (result )
122
128
.isFalse ()
123
129
.as ("Expected canAttach() to return false for ICEBERG_TABLE_LIKE with non-TABLE subtype" );
@@ -126,7 +132,36 @@ public void testCanAttachReturnsFalseForIcebergTableLikeWithNonTableSubtype() {
126
132
@ Test
127
133
public void testCanAttachReturnsFalseForUnattachableType () {
128
134
var targetEntity = new PrincipalEntity .Builder ().build ();
129
- var result = PolicyValidators .canAttach (policyEntity , targetEntity );
135
+ var result = PolicyValidators .canAttach (callContext , policyEntity , targetEntity );
130
136
assertThat (result ).isFalse ().as ("Expected canAttach() to return false for null" );
131
137
}
138
+
139
+ @ Test
140
+ public void testCanAttachAccessControlPolicy () {
141
+ // Arrange
142
+ Namespace ns = Namespace .of ("NS1" );
143
+ TableIdentifier tableId = TableIdentifier .of (ns , "table1" );
144
+ PolicyEntity policy =
145
+ new PolicyEntity .Builder (ns , "acp" , PredefinedPolicyTypes .ACCESS_CONTROL )
146
+ .setContent ("{\" columnProjections\" :[\" col1\" ],\" rowFilters\" :[]}" )
147
+ .build ();
148
+
149
+ // Mock CallContext and RealmConfiguration
150
+ var realmConfig = mock (RealmConfig .class );
151
+ when (callContext .getRealmConfig ()).thenReturn (realmConfig );
152
+ when (realmConfig .getConfig (
153
+ FeatureConfiguration .ALLOW_ATTACHING_FINE_GRAINED_POLICIES_TO_ENTITIES ))
154
+ .thenReturn (true );
155
+
156
+ // Use a valid table entity
157
+ var tableEntity =
158
+ new IcebergTableLikeEntity .Builder (tableId , "" )
159
+ .setSubType (org .apache .polaris .core .entity .PolarisEntitySubType .ICEBERG_TABLE )
160
+ .build ();
161
+ boolean canAttach =
162
+ org .apache .polaris .core .policy .validator .PolicyValidators .canAttach (
163
+ callContext , policy , tableEntity );
164
+
165
+ assertThat (canAttach ).isTrue ();
166
+ }
132
167
}
0 commit comments