Search in sources :

Example 1 with PolicySerializer

use of org.apache.ranger.plugin.model.RangerPolicyResourceSignature.PolicySerializer 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());
}
Also used : RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) PolicySerializer(org.apache.ranger.plugin.model.RangerPolicyResourceSignature.PolicySerializer) Test(org.junit.Test)

Example 2 with PolicySerializer

use of org.apache.ranger.plugin.model.RangerPolicyResourceSignature.PolicySerializer 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());
}
Also used : HashMap(java.util.HashMap) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) PolicySerializer(org.apache.ranger.plugin.model.RangerPolicyResourceSignature.PolicySerializer) Test(org.junit.Test)

Aggregations

RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)2 PolicySerializer (org.apache.ranger.plugin.model.RangerPolicyResourceSignature.PolicySerializer)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1