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");
}
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);
}
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);
}
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 "));
}
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;
}
Aggregations