RangerServiceValidator extends RangerValidator { boolean isValid(Long id, Action action, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug("==> RangerServiceValidator.isValid(" + id + ")"); } boolean valid = true; if (action != Action.DELETE) { ValidationErrorCode error = ValidationErrorCode.SERVICE_VALIDATION_ERR_UNSUPPORTED_ACTION; failures.add(new ValidationFailureDetailsBuilder() .isAnInternalError() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(action)) .build()); valid = false; } else if (id == null) { ValidationErrorCode error = ValidationErrorCode.SERVICE_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("id") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(id)) .build()); valid = false; } else if (getService(id) == null) { if (LOG.isDebugEnabled()) { LOG.debug("No service found for id[" + id + "]! ok!"); } } if(LOG.isDebugEnabled()) { LOG.debug("<== RangerServiceValidator.isValid(" + id + "): " + valid); } return valid; } RangerServiceValidator(ServiceStore store); void validate(RangerService service, Action action); boolean isValidString(final Pattern pattern, final String name);  }
@Test public void testIsValidServiceNameCreationWithOutSpecialCharacters() throws Exception{ String serviceName = "c1_yarn"; String serviceDisplayName = serviceName; RangerService rangerService = new RangerService(); rangerService.setName(serviceName); rangerService.setDisplayName(serviceDisplayName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, Action.CREATE, _failures); Assert.assertEquals(0, _failures.size()); Assert.assertTrue(valid); }
@Test public void testIsValidServiceNameUpdationWithOutSpecialCharacters() throws Exception{ String serviceName = "c1_yarn"; String serviceDisplayName = serviceName; RangerService rangerService = new RangerService(); rangerService.setId(1L); rangerService.setName(serviceName); rangerService.setDisplayName(serviceDisplayName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); when(_store.getService(1L)).thenReturn(rangerService); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, Action.UPDATE, _failures); Assert.assertEquals(0, _failures.size()); Assert.assertTrue(valid); }
@Test public void testIsValidServiceNameUpdationWithSpecialCharacters() throws Exception{ String serviceName = "<alert>c1_yarn</alert>"; ValidationErrorCode vErrCod = ValidationErrorCode.SERVICE_VALIDATION_ERR_SPECIAL_CHARACTERS_SERVICE_NAME; String errorMessage = vErrCod.getMessage(serviceName); int errorCode = vErrCod.getErrorCode(); RangerService rangerService = new RangerService(); rangerService.setId(1L); rangerService.setName(serviceName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); when(_store.getService(1L)).thenReturn(rangerService); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, Action.UPDATE, _failures); ValidationFailureDetails failureMessage = _failures.get(0); Assert.assertFalse(valid); Assert.assertEquals("name",failureMessage.getFieldName()); Assert.assertEquals(errorMessage, failureMessage._reason); Assert.assertEquals(errorCode, failureMessage._errorCode); }
@Test public void testIsValidServiceNameCreationWithSpecialCharacters() throws Exception{ String serviceName = "<script>c1_yarn</script>"; ValidationErrorCode vErrCod = ValidationErrorCode.SERVICE_VALIDATION_ERR_SPECIAL_CHARACTERS_SERVICE_NAME; String errorMessage = vErrCod.getMessage(serviceName); int errorCode = vErrCod.getErrorCode(); RangerService rangerService = new RangerService(); rangerService.setName(serviceName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, _action, _failures); ValidationFailureDetails failureMessage = _failures.get(0); Assert.assertFalse(valid); Assert.assertEquals("name",failureMessage.getFieldName()); Assert.assertEquals(errorMessage, failureMessage._reason); Assert.assertEquals(errorCode, failureMessage._errorCode); }
@Test public void testIsValidServiceNameCreationWithSpaceCharacter() throws Exception{ String serviceName = "Cluster 1_c1_yarn"; String serviceDisplayName = serviceName; ValidationErrorCode vErrCod = ValidationErrorCode.SERVICE_VALIDATION_ERR_SPECIAL_CHARACTERS_SERVICE_NAME; String errorMessage = vErrCod.getMessage(serviceName); int errorCode = vErrCod.getErrorCode(); RangerService rangerService = new RangerService(); rangerService.setName(serviceName); rangerService.setDisplayName(serviceDisplayName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, _action, _failures); ValidationFailureDetails failureMessage = _failures.get(0); Assert.assertFalse(valid); Assert.assertEquals("name",failureMessage.getFieldName()); Assert.assertEquals(errorMessage, failureMessage._reason); Assert.assertEquals(errorCode, failureMessage._errorCode); }
@Test public void testIsValidServiceNameUpdationWithSpaceCharacter() throws Exception{ String serviceName = "Cluster 1_c1_yarn"; String serviceDisplayName = serviceName; ValidationErrorCode vErrCod = ValidationErrorCode.SERVICE_VALIDATION_ERR_SPECIAL_CHARACTERS_SERVICE_NAME; String errorMessage = vErrCod.getMessage(serviceName); int errorCode = vErrCod.getErrorCode(); RangerService rangerService = new RangerService(); rangerService.setId(1L); rangerService.setName(serviceName); rangerService.setDisplayName(serviceDisplayName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); String serviceNameWithoutSpace = "Cluster_1_c1_yarn"; String serviceDisplayNameWithoutSpace = serviceNameWithoutSpace; RangerService rangerServiceWithoutSpace = new RangerService(); rangerServiceWithoutSpace.setId(1L); rangerServiceWithoutSpace.setName(serviceNameWithoutSpace); rangerServiceWithoutSpace.setDisplayName(serviceDisplayNameWithoutSpace); rangerServiceWithoutSpace.setType("yarn"); rangerServiceWithoutSpace.setTagService(""); when(_store.getService(1L)).thenReturn(rangerServiceWithoutSpace); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, Action.UPDATE, _failures); ValidationFailureDetails failureMessage = _failures.get(0); Assert.assertFalse(valid); Assert.assertEquals("name",failureMessage.getFieldName()); Assert.assertEquals(errorMessage, failureMessage._reason); Assert.assertEquals(errorCode, failureMessage._errorCode); when(_store.getService(1L)).thenReturn(rangerService); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean validWithSpace = _validator.isValid(rangerService, Action.UPDATE, _failures); Assert.assertTrue(validWithSpace); }
@Test public void testIsValidServiceNameCreationWithGreater255Characters() throws Exception{ String serviceName = "c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1"; ValidationErrorCode vErrCod = ValidationErrorCode.SERVICE_VALIDATION_ERR_SPECIAL_CHARACTERS_SERVICE_NAME; String errorMessage = vErrCod.getMessage(serviceName); int errorCode = vErrCod.getErrorCode(); RangerService rangerService = new RangerService(); rangerService.setName(serviceName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, _action, _failures); ValidationFailureDetails failureMessage = _failures.get(0); Assert.assertFalse(valid); Assert.assertEquals("name",failureMessage.getFieldName()); Assert.assertEquals(errorMessage, failureMessage._reason); Assert.assertEquals(errorCode, failureMessage._errorCode); }
@Test public void testIsValidServiceNameUpdationWithGreater255Characters() throws Exception{ String serviceName = "c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1_yarn_c1"; ValidationErrorCode vErrCod = ValidationErrorCode.SERVICE_VALIDATION_ERR_SPECIAL_CHARACTERS_SERVICE_NAME; String errorMessage = vErrCod.getMessage(serviceName); int errorCode = vErrCod.getErrorCode(); RangerService rangerService = new RangerService(); rangerService.setId(1L); rangerService.setName(serviceName); rangerService.setType("yarn"); rangerService.setTagService(""); RangerServiceConfigDef configDef = new RangerServiceConfigDef(); configDef.setMandatory(true); List<RangerServiceConfigDef> listRangerServiceConfigDef = new ArrayList<RangerServiceDef.RangerServiceConfigDef>(); listRangerServiceConfigDef.add(configDef); configDef.setName("myconfig1"); Map<String,String> testMap = new HashMap<String, String>(); testMap.put("myconfig1", "myconfig1"); rangerService.setConfigs(testMap); RangerServiceDef rangerServiceDef = new RangerServiceDef(); rangerServiceDef.setConfigs(listRangerServiceConfigDef); when(_store.getService(1L)).thenReturn(rangerService); when(_store.getServiceDefByName("yarn")).thenReturn(rangerServiceDef); boolean valid = _validator.isValid(rangerService, Action.UPDATE, _failures); ValidationFailureDetails failureMessage = _failures.get(0); Assert.assertFalse(valid); Assert.assertEquals("name",failureMessage.getFieldName()); Assert.assertEquals(errorMessage, failureMessage._reason); Assert.assertEquals(errorCode, failureMessage._errorCode); }
@Test public void testIsValid_failures() throws Exception { RangerService service = mock(RangerService.class); Assert.assertFalse(_validator.isValid((RangerService)null, _action, _failures)); _utils.checkFailureForMissingValue(_failures, "service"); when(service.getId()).thenReturn(null); checkFailure_isValid(_validator, service, Action.UPDATE, _failures, "missing", "id"); when(service.getId()).thenReturn(7L); for (Action action : cu) { for (String name : new String[] { null, "", " " }) { when(service.getName()).thenReturn(name); checkFailure_isValid(_validator, service, action, _failures, "missing", "name"); } for (String type : new String[] { null, "", " " }) { when(service.getType()).thenReturn(type); checkFailure_isValid(_validator, service, action, _failures, "missing", "type"); } } when(service.getName()).thenReturn("aName"); 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); RangerService anExistingService = mock(RangerService.class); when(_store.getServiceByName("aName")).thenReturn(anExistingService); checkFailure_isValid(_validator, service, Action.CREATE, _failures, "semantic", "name"); 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"); }
@Test public void test_isValid_happyPath() throws Exception { 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); RangerService service = mock(RangerService.class); when(service.getName()).thenReturn("aName"); when(service.getDisplayName()).thenReturn("aDisplayName"); when(service.getType()).thenReturn("aType"); String[] configs = new String[] { "param1", "param2", "param3", "param5", "param6" }; Map<String, String> configMap = _utils.createMap(configs); when(service.getConfigs()).thenReturn(configMap); when(_store.getServiceByName("aName")).thenReturn(null); when(_store.getServiceDefByName("aType")).thenReturn(serviceDef); Assert.assertTrue(_validator.isValid(service, Action.CREATE, _failures)); 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)); when(_store.getServiceByName("aName")).thenReturn(null); Assert.assertTrue(_validator.isValid(service, Action.UPDATE, _failures)); }
@Test public void test_isValid_withId_errorConditions() throws Exception { Assert.assertFalse(_validator.isValid(1L, Action.CREATE, _failures)); _utils.checkFailureForInternalError(_failures); _validator = new RangerServiceValidator(_store); _failures.clear(); Assert.assertFalse(_validator.isValid((Long)null, Action.DELETE, _failures)); _utils.checkFailureForMissingValue(_failures, "id"); when(_store.getService(1L)).thenReturn(null); when(_store.getService(2L)).thenThrow(new Exception()); _failures.clear(); Assert.assertTrue(_validator.isValid(1L, Action.DELETE, _failures)); Assert.assertTrue(_failures.isEmpty()); _failures.clear(); Assert.assertTrue(_validator.isValid(2L, Action.DELETE, _failures)); Assert.assertTrue(_failures.isEmpty()); }
@Test public void test_isValid_withId_happyPath() throws Exception { _validator = new RangerServiceValidator(_store); RangerService service = mock(RangerService.class); when(_store.getService(1L)).thenReturn(service); Assert.assertTrue(_validator.isValid(1L, Action.DELETE, _failures)); }