Search in sources :

Example 76 with RangerServiceDef

use of org.apache.ranger.plugin.model.RangerServiceDef in project ranger by apache.

the class TestRangerServiceValidator method testIsValid_failures.

@Test
public void testIsValid_failures() throws Exception {
    RangerService service = mock(RangerService.class);
    // passing in a null service to the check itself is an error
    Assert.assertFalse(_validator.isValid((RangerService) null, _action, _failures));
    _utils.checkFailureForMissingValue(_failures, "service");
    // id is required for update
    when(service.getId()).thenReturn(null);
    // let's verify the failure and the sort of error information that is returned (for one of these)
    // Assert.assert that among the failure reason is one about id being missing.
    checkFailure_isValid(_validator, service, Action.UPDATE, _failures, "missing", "id");
    when(service.getId()).thenReturn(7L);
    for (Action action : cu) {
        // null, empty of blank name renders a service invalid
        for (String name : new String[] { null, "", " 	" }) {
            // spaces and tabs
            when(service.getName()).thenReturn(name);
            checkFailure_isValid(_validator, service, action, _failures, "missing", "name");
        }
        // same is true for the type
        for (String type : new String[] { null, "", "    " }) {
            when(service.getType()).thenReturn(type);
            checkFailure_isValid(_validator, service, action, _failures, "missing", "type");
        }
    }
    when(service.getName()).thenReturn("aName");
    // if non-empty, then the type should exist!
    when(_store.getServiceDefByName("null-type")).thenReturn(null);
    when(_store.getServiceDefByName("throwing-type")).thenThrow(new Exception());
    for (Action action : cu) {
        for (String type : new String[] { "null-type", "throwing-type" }) {
            when(service.getType()).thenReturn(type);
            checkFailure_isValid(_validator, service, action, _failures, "semantic", "type");
        }
    }
    when(service.getType()).thenReturn("aType");
    RangerServiceDef serviceDef = mock(RangerServiceDef.class);
    when(_store.getServiceDefByName("aType")).thenReturn(serviceDef);
    // Create: No service should exist matching its id and/or name
    RangerService anExistingService = mock(RangerService.class);
    when(_store.getServiceByName("aName")).thenReturn(anExistingService);
    checkFailure_isValid(_validator, service, Action.CREATE, _failures, "semantic", "name");
    // Update: service should exist matching its id and name specified should not belong to a different service
    when(_store.getService(7L)).thenReturn(null);
    when(_store.getServiceByName("aName")).thenReturn(anExistingService);
    checkFailure_isValid(_validator, service, Action.UPDATE, _failures, "semantic", "id");
    when(_store.getService(7L)).thenReturn(anExistingService);
    RangerService anotherExistingService = mock(RangerService.class);
    when(anotherExistingService.getId()).thenReturn(49L);
    when(_store.getServiceByName("aName")).thenReturn(anotherExistingService);
    checkFailure_isValid(_validator, service, Action.UPDATE, _failures, "semantic", "id/name");
}
Also used : Action(org.apache.ranger.plugin.model.validation.RangerValidator.Action) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerService(org.apache.ranger.plugin.model.RangerService) Test(org.junit.Test)

Example 77 with RangerServiceDef

use of org.apache.ranger.plugin.model.RangerServiceDef in project ranger by apache.

the class TestRangerValidator method test_getServiceDef_byId.

@Test
public void test_getServiceDef_byId() throws Exception {
    // if service store returns null or throws an exception then service is deemed invalid
    when(_store.getServiceDef(1L)).thenReturn(null);
    when(_store.getServiceDef(2L)).thenThrow(new Exception());
    RangerServiceDef serviceDef = mock(RangerServiceDef.class);
    when(_store.getServiceDef(3L)).thenReturn(serviceDef);
    Assert.assertNull(_validator.getServiceDef(1L));
    Assert.assertNull(_validator.getServiceDef(2L));
    Assert.assertTrue(_validator.getServiceDef(3L) != null);
}
Also used : RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) Test(org.junit.Test)

Example 78 with RangerServiceDef

use of org.apache.ranger.plugin.model.RangerServiceDef in project ranger by apache.

the class TestRangerValidator method test_getServiceDef.

@Test
public void test_getServiceDef() {
    try {
        // if service store returns null or throws an exception then service is deemed invalid
        when(_store.getServiceDefByName("return null")).thenReturn(null);
        when(_store.getServiceDefByName("throw")).thenThrow(new Exception());
        RangerServiceDef serviceDef = mock(RangerServiceDef.class);
        when(_store.getServiceDefByName("good-service")).thenReturn(serviceDef);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Unexpected exception during mocking!");
    }
    Assert.assertNull(_validator.getServiceDef("return null"));
    Assert.assertNull(_validator.getServiceDef("throw"));
    Assert.assertFalse(_validator.getServiceDef("good-service") == null);
}
Also used : RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) Test(org.junit.Test)

Example 79 with RangerServiceDef

use of org.apache.ranger.plugin.model.RangerServiceDef in project ranger by apache.

the class TestRangerValidator method test_getAccessTypes.

@Test
public void test_getAccessTypes() {
    // passing in null service def
    Set<String> accessTypes = _validator.getAccessTypes((RangerServiceDef) null);
    Assert.assertTrue(accessTypes.isEmpty());
    // that has null or empty access type def
    RangerServiceDef serviceDef = mock(RangerServiceDef.class);
    when(serviceDef.getAccessTypes()).thenReturn(null);
    accessTypes = _validator.getAccessTypes(serviceDef);
    Assert.assertTrue(accessTypes.isEmpty());
    List<RangerAccessTypeDef> accessTypeDefs = new ArrayList<>();
    when(serviceDef.getAccessTypes()).thenReturn(accessTypeDefs);
    accessTypes = _validator.getAccessTypes(serviceDef);
    Assert.assertTrue(accessTypes.isEmpty());
    // having null accesstypedefs
    accessTypeDefs.add(null);
    accessTypes = _validator.getAccessTypes(serviceDef);
    Assert.assertTrue(accessTypes.isEmpty());
    // access type defs with null empty blank names are skipped, spaces within names are preserved
    String[] names = new String[] { null, "", "a", "  ", "b ", "		", " C", "	D	" };
    accessTypeDefs.addAll(_utils.createAccessTypeDefs(names));
    accessTypes = _validator.getAccessTypes(serviceDef);
    Assert.assertEquals(4, accessTypes.size());
    Assert.assertTrue(accessTypes.contains("a"));
    Assert.assertTrue(accessTypes.contains("b "));
    Assert.assertTrue(accessTypes.contains(" c"));
    Assert.assertTrue(accessTypes.contains("	d	"));
}
Also used : RangerAccessTypeDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 80 with RangerServiceDef

use of org.apache.ranger.plugin.model.RangerServiceDef in project ranger by apache.

the class ValidationTestUtils method createServiceDefWithAccessTypes.

RangerServiceDef createServiceDefWithAccessTypes(String[] accesses, String serviceName) {
    RangerServiceDef serviceDef = createServiceDefWithAccessTypes(accesses);
    when(serviceDef.getName()).thenReturn(serviceName);
    return serviceDef;
}
Also used : RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef)

Aggregations

RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)105 Test (org.junit.Test)52 ArrayList (java.util.ArrayList)38 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)19 XXServiceDef (org.apache.ranger.entity.XXServiceDef)18 Date (java.util.Date)15 HashMap (java.util.HashMap)13 ServicePolicies (org.apache.ranger.plugin.util.ServicePolicies)13 RangerService (org.apache.ranger.plugin.model.RangerService)12 RangerResourceDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerResourceDef)12 RangerServiceConfigDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef)12 RangerAccessTypeDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerAccessTypeDef)11 VXString (org.apache.ranger.view.VXString)10 RangerPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItem)9 RangerContextEnricherDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerContextEnricherDef)9 RangerPolicyConditionDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef)9 RangerPolicyItemAccess (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemAccess)8 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)8 RangerEnumDef (org.apache.ranger.plugin.model.RangerServiceDef.RangerEnumDef)8 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)7