use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.
the class TestServiceREST method test30getPolicyFromEventTime.
@Test
public void test30getPolicyFromEventTime() throws Exception {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
String strdt = new Date().toString();
String userName = "Admin";
Set<String> userGroupsList = new HashSet<String>();
userGroupsList.add("group1");
userGroupsList.add("group2");
Mockito.when(request.getParameter("eventTime")).thenReturn(strdt);
Mockito.when(request.getParameter("policyId")).thenReturn("1");
RangerPolicy policy = new RangerPolicy();
Map<String, RangerPolicyResource> resources = new HashMap<String, RangerPolicy.RangerPolicyResource>();
policy.setService("services");
policy.setResources(resources);
Mockito.when(svcStore.getPolicyFromEventTime(strdt, 1l)).thenReturn(policy);
Mockito.when(bizUtil.isAdmin()).thenReturn(false);
Mockito.when(bizUtil.getCurrentUserLoginId()).thenReturn(userName);
Mockito.when(restErrorUtil.createRESTException(Matchers.anyInt(), Matchers.anyString(), Matchers.anyBoolean())).thenThrow(new WebApplicationException());
thrown.expect(WebApplicationException.class);
RangerPolicy dbRangerPolicy = serviceREST.getPolicyFromEventTime(request);
Assert.assertNull(dbRangerPolicy);
Mockito.verify(request).getParameter("eventTime");
Mockito.verify(request).getParameter("policyId");
}
use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.
the class TestRangerPolicy method test_03_PolicyResource_SetListMethods.
@Test
public void test_03_PolicyResource_SetListMethods() {
RangerPolicyResource policyResource = new RangerPolicyResource();
List<String> values = getList("value");
Assert.assertEquals("RangerPolicyResource.getValues()", 0, policyResource.getValues().size());
policyResource.getValues().add(new String());
Assert.assertEquals("RangerPolicyResource.getValues().add()", 1, policyResource.getValues().size());
policyResource.setValues(values);
Assert.assertEquals("RangerPolicyResource.setValues()", values.size(), policyResource.getValues().size());
}
use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.
the class TestRangerPolicyResourceSignature method test_getResourceSignature_happyPath.
@Test
public void test_getResourceSignature_happyPath() {
// null policy returns signature of empty resource
RangerPolicy policy = null;
PolicySerializer serializer = new PolicySerializer(policy);
Assert.assertTrue("Null policy", serializer.toString() == "");
policy = mock(RangerPolicy.class);
when(policy.getPolicyType()).thenReturn(null);
Map<String, RangerPolicyResource> policyResources = _utils.createPolicyResourceMap(first);
when(policy.getResources()).thenReturn(policyResources);
serializer = new PolicySerializer(policy);
String expectedVersion = "version=1";
String expectedType = "type=0";
String expectedResource = "{" + "col={values=[col1, col2, col3],excludes=false,recursive=true}, " + "db={values=[db1, db2],excludes=false,recursive=false}, " + "table={values=[tbl1, tbl2, tbl3],excludes=true,recursive=false}" + "}";
String serializationFormat = "{%s,%s,resource=%s}";
String expectedFull = String.format(serializationFormat, expectedVersion, expectedType, expectedResource);
Assert.assertEquals(expectedFull, serializer.toString());
// order of values should not matter
policyResources = _utils.createPolicyResourceMap(data_second);
when(policy.getResources()).thenReturn(policyResources);
Assert.assertEquals(expectedFull, serializer.toString());
// changing the policy type has expected changes
when(policy.getPolicyType()).thenReturn(1);
expectedType = "type=1";
expectedFull = String.format(serializationFormat, expectedVersion, expectedType, expectedResource);
Assert.assertEquals(expectedFull, serializer.toString());
}
use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.
the class TestRangerPolicyResourceSignature method createPolicyResource.
RangerPolicyResource createPolicyResource(String[] values, Boolean recursive, Boolean excludes) {
RangerPolicyResource resource = mock(RangerPolicyResource.class);
if (values == null) {
when(resource.getValues()).thenReturn(null);
} else {
when(resource.getValues()).thenReturn(Arrays.asList(values));
}
when(resource.getIsRecursive()).thenReturn(recursive);
when(resource.getIsExcludes()).thenReturn(excludes);
return resource;
}
use of org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource in project ranger by apache.
the class TestRangerPolicyResourceSignature method test_isPolicyValidForResourceSignatureComputation.
@Test
public void test_isPolicyValidForResourceSignatureComputation() {
// null policy is invalid
RangerPolicy rangerPolicy = null;
PolicySerializer policySerializer = new PolicySerializer(rangerPolicy);
Assert.assertFalse("policy==null", policySerializer.isPolicyValidForResourceSignatureComputation());
// null resource map is invalid
rangerPolicy = mock(RangerPolicy.class);
when(rangerPolicy.getResources()).thenReturn(null);
policySerializer = new PolicySerializer(rangerPolicy);
Assert.assertFalse("policy.getResources()==null", policySerializer.isPolicyValidForResourceSignatureComputation());
// empty resources map is ok!
Map<String, RangerPolicyResource> policyResources = new HashMap<>();
when(rangerPolicy.getResources()).thenReturn(policyResources);
policySerializer = new PolicySerializer(rangerPolicy);
Assert.assertTrue("policy.getResources().isEmpty()", policySerializer.isPolicyValidForResourceSignatureComputation());
// but having a resource map with null key is not ok!
RangerPolicyResource aPolicyResource = mock(RangerPolicyResource.class);
policyResources.put(null, aPolicyResource);
policySerializer = new PolicySerializer(rangerPolicy);
Assert.assertFalse("policy.getResources().contains(null)", policySerializer.isPolicyValidForResourceSignatureComputation());
}
Aggregations