use of org.apache.ranger.biz.TestPolicyDb.PolicyDbTestCase.TestData in project ranger by apache.
the class TestPolicyDb method runTests.
private void runTests(InputStreamReader reader, String testName, RangerServiceDef serviceDef) {
PolicyDbTestCase testCase = gsonBuilder.fromJson(reader, PolicyDbTestCase.class);
if (serviceDef != null) {
// Override serviceDef in the json test-file with a global service-def
testCase.servicePolicies.setServiceDef(serviceDef);
}
assertTrue("invalid input: " + testName, testCase != null && testCase.servicePolicies != null && testCase.tests != null && testCase.servicePolicies.getPolicies() != null);
RangerPolicyEngineOptions policyEngineOptions = new RangerPolicyEngineOptions();
policyEngineOptions.evaluatorType = RangerPolicyEvaluator.EVALUATOR_TYPE_OPTIMIZED;
policyEngineOptions.cacheAuditResults = false;
policyEngineOptions.disableContextEnrichers = true;
policyEngineOptions.disableCustomConditions = true;
RangerPluginContext pluginContext = new RangerPluginContext(new RangerPluginConfig("hive", null, "test-policydb", "cl1", "on-prem", policyEngineOptions));
RangerPolicyAdmin policyAdmin = new RangerPolicyAdminImpl(testCase.servicePolicies, pluginContext, null);
for (TestData test : testCase.tests) {
boolean expected = test.result;
if (test.allowedPolicies != null) {
List<RangerPolicy> allowedPolicies = policyAdmin.getAllowedUnzonedPolicies(test.user, test.userGroups, test.accessType);
assertEquals("allowed-policy count mismatch!", test.allowedPolicies.size(), allowedPolicies.size());
Set<Long> allowedPolicyIds = new HashSet<>();
for (RangerPolicy allowedPolicy : allowedPolicies) {
allowedPolicyIds.add(allowedPolicy.getId());
}
assertEquals("allowed-policy list mismatch!", test.allowedPolicies, allowedPolicyIds);
} else {
boolean result = policyAdmin.isAccessAllowedByUnzonedPolicies(test.resources, test.user, test.userGroups, test.accessType);
assertEquals("isAccessAllowed mismatched! - " + test.name, expected, result);
}
}
}
Aggregations