21
21
import static org .apache .gravitino .authorization .ranger .integration .test .RangerITEnv .currentFunName ;
22
22
import static org .apache .gravitino .authorization .ranger .integration .test .RangerITEnv .rangerClient ;
23
23
import static org .apache .gravitino .authorization .ranger .integration .test .RangerITEnv .verifyRoleInRanger ;
24
+ import static org .mockito .ArgumentMatchers .any ;
25
+ import static org .mockito .Mockito .when ;
24
26
25
27
import com .google .common .base .Joiner ;
26
28
import com .google .common .collect .ImmutableList ;
48
50
import org .apache .gravitino .authorization .SecurableObject ;
49
51
import org .apache .gravitino .authorization .SecurableObjects ;
50
52
import org .apache .gravitino .authorization .ranger .RangerAuthorizationPlugin ;
53
+ import org .apache .gravitino .authorization .ranger .RangerClientExtension ;
51
54
import org .apache .gravitino .authorization .ranger .RangerHadoopSQLMetadataObject ;
52
55
import org .apache .gravitino .authorization .ranger .RangerHadoopSQLSecurableObject ;
53
56
import org .apache .gravitino .authorization .ranger .RangerHelper ;
54
57
import org .apache .gravitino .authorization .ranger .RangerPrivileges ;
55
58
import org .apache .gravitino .authorization .ranger .reference .RangerDefines ;
59
+ import org .apache .gravitino .authorization .ranger .reference .VXUserList ;
60
+ import org .apache .gravitino .exceptions .AuthorizationPluginException ;
56
61
import org .apache .gravitino .integration .test .util .GravitinoITUtils ;
57
62
import org .apache .gravitino .meta .AuditInfo ;
58
63
import org .apache .gravitino .meta .GroupEntity ;
59
64
import org .apache .gravitino .meta .RoleEntity ;
60
65
import org .apache .gravitino .meta .UserEntity ;
61
66
import org .apache .ranger .RangerServiceException ;
62
67
import org .apache .ranger .plugin .model .RangerPolicy ;
68
+ import org .apache .ranger .plugin .model .RangerRole ;
69
+ import org .glassfish .jersey .internal .guava .Sets ;
63
70
import org .junit .jupiter .api .AfterAll ;
64
71
import org .junit .jupiter .api .AfterEach ;
65
72
import org .junit .jupiter .api .Assertions ;
66
73
import org .junit .jupiter .api .BeforeAll ;
67
74
import org .junit .jupiter .api .Tag ;
68
75
import org .junit .jupiter .api .Test ;
76
+ import org .mockito .Mockito ;
69
77
import org .slf4j .Logger ;
70
78
import org .slf4j .LoggerFactory ;
71
79
@@ -134,8 +142,24 @@ public RoleEntity mock3TableRole(String roleName) {
134
142
135
143
// Use the different db.table different privilege to test OnRoleCreated()
136
144
@ Test
137
- public void testOnRoleCreated () {
145
+ public void testOnRoleCreated () throws Exception {
138
146
RoleEntity role = mock3TableRole (currentFunName ());
147
+
148
+ // test to throw an exception
149
+ RangerClientExtension client = Mockito .mock (RangerClientExtension .class );
150
+ RangerClientExtension originClient = rangerAuthHivePlugin .getRangerClient ();
151
+ rangerAuthHivePlugin .setRangerClient (client );
152
+ RangerHelper originHelper = rangerAuthHivePlugin .getRangerHelper ();
153
+
154
+ RangerHelper helper =
155
+ new RangerHelper (client , "test" , "test" , Sets .newHashSet (), Lists .newArrayList ());
156
+ rangerAuthHivePlugin .setRangerHelper (helper );
157
+ when (client .createRole (any (), any ())).thenThrow (new RangerServiceException (new Exception ("" )));
158
+ Assertions .assertThrows (
159
+ AuthorizationPluginException .class , () -> rangerAuthHivePlugin .onRoleCreated (role ));
160
+ rangerAuthHivePlugin .setRangerClient (originClient );
161
+ rangerAuthHivePlugin .setRangerHelper (originHelper );
162
+
139
163
Assertions .assertTrue (rangerAuthHivePlugin .onRoleCreated (role ));
140
164
verifyRoleInRanger (rangerAuthHivePlugin , role );
141
165
@@ -259,7 +283,7 @@ public void testOnDenyRoleCreatedCatalog() {
259
283
}
260
284
261
285
@ Test
262
- public void testOnRoleDeleted () {
286
+ public void testOnRoleDeleted () throws Exception {
263
287
// prepare to create a role
264
288
RoleEntity role = mock3TableRole (currentFunName ());
265
289
Assertions .assertTrue (rangerAuthHivePlugin .onRoleCreated (role ));
@@ -272,6 +296,23 @@ public void testOnRoleDeleted() {
272
296
273
297
// Repeat to delete the same role to verify the idempotent operation
274
298
Assertions .assertTrue (rangerAuthHivePlugin .onRoleDeleted (role ));
299
+
300
+ // test to throw an exception
301
+ RangerClientExtension client = Mockito .mock (RangerClientExtension .class );
302
+ RangerClientExtension originClient = rangerAuthHivePlugin .getRangerClient ();
303
+ RangerHelper originHelper = rangerAuthHivePlugin .getRangerHelper ();
304
+ rangerAuthHivePlugin .setRangerClient (client );
305
+
306
+ RangerHelper helper = Mockito .mock (RangerHelper .class );
307
+ rangerAuthHivePlugin .setRangerHelper (helper );
308
+ Mockito .doThrow (new RangerServiceException (new Exception ("test" )))
309
+ .when (client )
310
+ .deleteRole (any (), any (), any ());
311
+ Mockito .when (helper .getRangerRole (any ())).thenReturn (Mockito .mock (RangerRole .class ));
312
+ Assertions .assertThrows (
313
+ AuthorizationPluginException .class , () -> rangerAuthHivePlugin .onRoleDeleted (role ));
314
+ rangerAuthHivePlugin .setRangerClient (originClient );
315
+ rangerAuthHivePlugin .setRangerHelper (originHelper );
275
316
}
276
317
277
318
@ Test
@@ -1089,7 +1130,7 @@ public void testRoleChangeCombinedOperation() {
1089
1130
}
1090
1131
1091
1132
@ Test
1092
- public void testOnGrantedRolesToUser () {
1133
+ public void testOnGrantedRolesToUser () throws Exception {
1093
1134
// prepare to create a role
1094
1135
RoleEntity role = mock3TableRole (currentFunName ());
1095
1136
Assertions .assertTrue (rangerAuthHivePlugin .onRoleCreated (role ));
@@ -1113,6 +1154,17 @@ public void testOnGrantedRolesToUser() {
1113
1154
rangerAuthHivePlugin .onGrantedRolesToUser (Lists .newArrayList (role ), userEntity1 ));
1114
1155
verifyRoleInRanger (rangerAuthHivePlugin , role , Lists .newArrayList (userName1 ));
1115
1156
1157
+ // test to throw an exception
1158
+ RangerClientExtension client = Mockito .mock (RangerClientExtension .class );
1159
+ RangerClientExtension originClient = rangerAuthHivePlugin .getRangerClient ();
1160
+ rangerAuthHivePlugin .setRangerClient (client );
1161
+ when (client .searchUser (any ())).thenReturn (Mockito .mock (VXUserList .class ));
1162
+ when (client .grantRole (any (), any ())).thenThrow (new AuthorizationPluginException ("test" ));
1163
+ Assertions .assertThrows (
1164
+ AuthorizationPluginException .class ,
1165
+ () -> rangerAuthHivePlugin .onGrantedRolesToUser (Lists .newArrayList (role ), userEntity1 ));
1166
+ rangerAuthHivePlugin .setRangerClient (originClient );
1167
+
1116
1168
// granted a role to the user2
1117
1169
String userName2 = "user2" ;
1118
1170
UserEntity userEntity2 =
@@ -1131,7 +1183,7 @@ public void testOnGrantedRolesToUser() {
1131
1183
}
1132
1184
1133
1185
@ Test
1134
- public void testOnRevokedRolesFromUser () {
1186
+ public void testOnRevokedRolesFromUser () throws Exception {
1135
1187
// prepare to create a role
1136
1188
RoleEntity role = mock3TableRole (currentFunName ());
1137
1189
Assertions .assertTrue (rangerAuthHivePlugin .onRoleCreated (role ));
@@ -1158,10 +1210,21 @@ public void testOnRevokedRolesFromUser() {
1158
1210
Assertions .assertTrue (
1159
1211
rangerAuthHivePlugin .onRevokedRolesFromUser (Lists .newArrayList (role ), userEntity1 ));
1160
1212
verifyRoleInRanger (rangerAuthHivePlugin , role , null , Lists .newArrayList (userName1 ));
1213
+
1214
+ // test to throw an exception
1215
+ RangerClientExtension client = Mockito .mock (RangerClientExtension .class );
1216
+ RangerClientExtension originClient = rangerAuthHivePlugin .getRangerClient ();
1217
+ rangerAuthHivePlugin .setRangerClient (client );
1218
+ when (client .searchUser (any ())).thenReturn (Mockito .mock (VXUserList .class ));
1219
+ when (client .revokeRole (any (), any ())).thenThrow (new AuthorizationPluginException ("test" ));
1220
+ Assertions .assertThrows (
1221
+ AuthorizationPluginException .class ,
1222
+ () -> rangerAuthHivePlugin .onRevokedRolesFromUser (Lists .newArrayList (role ), userEntity1 ));
1223
+ rangerAuthHivePlugin .setRangerClient (originClient );
1161
1224
}
1162
1225
1163
1226
@ Test
1164
- public void testOnGrantedRolesToGroup () {
1227
+ public void testOnGrantedRolesToGroup () throws Exception {
1165
1228
// prepare to create a role
1166
1229
RoleEntity role = mock3TableRole (currentFunName ());
1167
1230
Assertions .assertTrue (rangerAuthHivePlugin .onRoleCreated (role ));
@@ -1185,6 +1248,17 @@ public void testOnGrantedRolesToGroup() {
1185
1248
rangerAuthHivePlugin .onGrantedRolesToGroup (Lists .newArrayList (role ), groupEntity1 ));
1186
1249
verifyRoleInRanger (rangerAuthHivePlugin , role , null , null , Lists .newArrayList (groupName1 ));
1187
1250
1251
+ // test to throw an exception
1252
+ RangerClientExtension client = Mockito .mock (RangerClientExtension .class );
1253
+ RangerClientExtension originClient = rangerAuthHivePlugin .getRangerClient ();
1254
+ rangerAuthHivePlugin .setRangerClient (client );
1255
+ when (client .createGroup (any ())).thenReturn (true );
1256
+ when (client .grantRole (any (), any ())).thenThrow (new AuthorizationPluginException ("test" ));
1257
+ Assertions .assertThrows (
1258
+ AuthorizationPluginException .class ,
1259
+ () -> rangerAuthHivePlugin .onGrantedRolesToGroup (Lists .newArrayList (role ), groupEntity1 ));
1260
+ rangerAuthHivePlugin .setRangerClient (originClient );
1261
+
1188
1262
// granted a role to the group2
1189
1263
String groupName2 = "group2" ;
1190
1264
GroupEntity groupEntity2 =
@@ -1204,7 +1278,7 @@ public void testOnGrantedRolesToGroup() {
1204
1278
}
1205
1279
1206
1280
@ Test
1207
- public void testOnRevokedRolesFromGroup () {
1281
+ public void testOnRevokedRolesFromGroup () throws Exception {
1208
1282
// prepare to create a role
1209
1283
RoleEntity role = mock3TableRole (currentFunName ());
1210
1284
Assertions .assertTrue (rangerAuthHivePlugin .onRoleCreated (role ));
@@ -1233,6 +1307,17 @@ public void testOnRevokedRolesFromGroup() {
1233
1307
rangerAuthHivePlugin .onRevokedRolesFromGroup (Lists .newArrayList (role ), groupEntity1 ));
1234
1308
verifyRoleInRanger (
1235
1309
rangerAuthHivePlugin , role , null , null , null , Lists .newArrayList (groupName1 ));
1310
+
1311
+ // test to throw an exception
1312
+ RangerClientExtension client = Mockito .mock (RangerClientExtension .class );
1313
+ RangerClientExtension originClient = rangerAuthHivePlugin .getRangerClient ();
1314
+ rangerAuthHivePlugin .setRangerClient (client );
1315
+ when (client .createGroup (any ())).thenReturn (true );
1316
+ when (client .revokeRole (any (), any ())).thenThrow (new AuthorizationPluginException ("test" ));
1317
+ Assertions .assertThrows (
1318
+ AuthorizationPluginException .class ,
1319
+ () -> rangerAuthHivePlugin .onRevokedRolesFromGroup (Lists .newArrayList (role ), groupEntity1 ));
1320
+ rangerAuthHivePlugin .setRangerClient (originClient );
1236
1321
}
1237
1322
1238
1323
private void assertFindManagedPolicyItems (Role role , boolean gravitinoPolicyItemExist ) {
0 commit comments