Search in sources :

Example 21 with RangerServiceDef

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

the class TestRangerServiceDefValidator method test_isValidServiceDefId_happyPath.

@Test
public final void test_isValidServiceDefId_happyPath() throws Exception {
    // create: null id is ok
    assertTrue(_validator.isValidServiceDefId(null, Action.CREATE, _failures));
    assertTrue(_failures.isEmpty());
    // update: a service with same id exist
    long id = 7;
    when(_serviceDef.getId()).thenReturn(id);
    RangerServiceDef serviceDefFromDb = mock(RangerServiceDef.class);
    when(serviceDefFromDb.getId()).thenReturn(id);
    when(_store.getServiceDef(id)).thenReturn(serviceDefFromDb);
    assertTrue(_validator.isValidServiceDefId(id, Action.UPDATE, _failures));
    assertTrue(_failures.isEmpty());
}
Also used : RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) Test(org.junit.Test)

Example 22 with RangerServiceDef

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

the class TestRangerServiceDefValidator method testIsValid_failures.

@Test
public final void testIsValid_failures() throws Exception {
    // null service def and bad service def name
    for (Action action : cu) {
        // passing in null service def is an error
        assertFalse(_validator.isValid((RangerServiceDef) null, action, _failures));
        _utils.checkFailureForMissingValue(_failures, "service def");
    }
}
Also used : Action(org.apache.ranger.plugin.model.validation.RangerValidator.Action) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) Test(org.junit.Test)

Example 23 with RangerServiceDef

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

the class TestRangerServiceDefValidator method test_isValidName_create.

@Test
public final void test_isValidName_create() throws Exception {
    // id should be irrelevant for name check for create.
    Long id = null;
    // for create a service shouldn't exist with the name
    String name = "existing-service";
    when(_serviceDef.getName()).thenReturn(name);
    when(_store.getServiceDefByName(name)).thenReturn(null);
    assertTrue(_validator.isValidServiceDefName(name, id, Action.CREATE, _failures));
    assertTrue(_failures.isEmpty());
    RangerServiceDef existingServiceDef = mock(RangerServiceDef.class);
    when(_store.getServiceDefByName(name)).thenReturn(existingServiceDef);
    _failures.clear();
    assertFalse(_validator.isValidServiceDefName(name, id, Action.CREATE, _failures));
    _utils.checkFailureForSemanticError(_failures, "name");
}
Also used : RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) Test(org.junit.Test)

Example 24 with RangerServiceDef

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

the class TestRangerServiceDefValidator method test_isValidName_update.

@Test
public final void test_isValidName_update() throws Exception {
    // update: if service exists with the same name then it can't point to a different service
    long id = 7;
    when(_serviceDef.getId()).thenReturn(id);
    String name = "aServiceDef";
    when(_serviceDef.getName()).thenReturn(name);
    // no service with the new name (we are updating the name to a unique value)
    when(_store.getServiceDefByName(name)).thenReturn(null);
    assertTrue(_validator.isValidServiceDefName(name, id, Action.UPDATE, _failures));
    assertTrue(_failures.isEmpty());
    RangerServiceDef existingServiceDef = mock(RangerServiceDef.class);
    when(existingServiceDef.getId()).thenReturn(id);
    when(existingServiceDef.getName()).thenReturn(name);
    when(_store.getServiceDefByName(name)).thenReturn(existingServiceDef);
    assertTrue(_validator.isValidServiceDefName(name, id, Action.UPDATE, _failures));
    assertTrue(_failures.isEmpty());
    long anotherId = 49;
    when(existingServiceDef.getId()).thenReturn(anotherId);
    assertFalse(_validator.isValidServiceDefName(name, id, Action.UPDATE, _failures));
    _utils.checkFailureForSemanticError(_failures, "id/name");
}
Also used : RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) Test(org.junit.Test)

Example 25 with RangerServiceDef

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

the class TestRangerServiceValidator method test_isValid_happyPath.

@Test
public void test_isValid_happyPath() throws Exception {
    // create a service def with some required parameters
    Object[][] serviceDefInput = new Object[][] { { "param1", true }, { "param2", true }, { "param3", false }, { "param4", false }, { "param5", true } };
    List<RangerServiceConfigDef> configDefs = _utils.createServiceConditionDefs(serviceDefInput);
    RangerServiceDef serviceDef = mock(RangerServiceDef.class);
    when(serviceDef.getConfigs()).thenReturn(configDefs);
    // create a service with some parameters on it
    RangerService service = mock(RangerService.class);
    when(service.getName()).thenReturn("aName");
    when(service.getType()).thenReturn("aType");
    // contains an extra parameter (param6) and one optional is missing(param4)
    String[] configs = new String[] { "param1", "param2", "param3", "param5", "param6" };
    Map<String, String> configMap = _utils.createMap(configs);
    when(service.getConfigs()).thenReturn(configMap);
    // wire then into the store
    // service does not exists
    when(_store.getServiceByName("aName")).thenReturn(null);
    // service def exists
    when(_store.getServiceDefByName("aType")).thenReturn(serviceDef);
    Assert.assertTrue(_validator.isValid(service, Action.CREATE, _failures));
    // for update to work the only additional requirement is that id is required and service should exist
    // if name is not null and it points to a service then it should match the id
    when(service.getId()).thenReturn(7L);
    RangerService existingService = mock(RangerService.class);
    when(existingService.getId()).thenReturn(Long.valueOf(7L));
    when(_store.getService(7L)).thenReturn(existingService);
    when(_store.getServiceByName("aName")).thenReturn(existingService);
    Assert.assertTrue(_validator.isValid(service, Action.UPDATE, _failures));
    // name need not point to a service for update to work, of course.
    when(_store.getServiceByName("aName")).thenReturn(null);
    Assert.assertTrue(_validator.isValid(service, Action.UPDATE, _failures));
}
Also used : RangerServiceConfigDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerService(org.apache.ranger.plugin.model.RangerService) Test(org.junit.Test)

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