RangerServiceDefValidator extends RangerValidator { public boolean isValidResources(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures, final Action action) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidResources(%s, %s)", serviceDef, failures)); } boolean valid = true; List<RangerResourceDef> resources = serviceDef.getResources(); if (CollectionUtils.isEmpty(resources)) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_MISSING_FIELD; failures.add(new ValidationFailureDetailsBuilder() .field("resources") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage("resources")) .build()); valid = false; } else { Map<Long, String> existingResourceIDNameMap = new HashMap<>(); if (action == Action.UPDATE) { List<RangerResourceDef> existingResources = this.getServiceDef(serviceDef.getId()).getResources(); for (RangerResourceDef existingResource : existingResources) { existingResourceIDNameMap.put(existingResource.getItemId(), existingResource.getName()); } } if(LOG.isDebugEnabled()) { LOG.debug("resource names from db = " + existingResourceIDNameMap.values()); } Set<String> names = new HashSet<String>(resources.size()); Set<Long> ids = new HashSet<Long>(resources.size()); for (RangerResourceDef resource : resources) { valid = isValidResourceName(resource.getName(), "resource type name", failures) && valid; String name = resource.getName(); Long itemId = resource.getItemId(); valid = isUnique(name, names, "resource name", "resources", failures) && valid; valid = isUnique(itemId, ids, "resource itemId", "resources", failures) && valid; if (action == Action.UPDATE) { if (existingResourceIDNameMap.get(itemId) != null && !existingResourceIDNameMap.get(itemId).equals(name)) { ValidationErrorCode error; error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_SERVICE_DEF_NAME_CONFICT; failures.add((new ValidationFailureDetailsBuilder()).field("resource name").isSemanticallyIncorrect().errorCode(error.getErrorCode()).becauseOf(String.format("changing %s[%s] in %s is not supported", "resource name", name, "resources")).build()); valid = false; } } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidResources(%s, %s): %s", serviceDef, failures, valid)); } return valid; } RangerServiceDefValidator(ServiceStore store); void validate(final RangerServiceDef serviceDef, final Action action); boolean isValidResources(RangerServiceDef serviceDef, List<ValidationFailureDetails> failures, final Action action);  }
@Test public final void test_isValidResources() { when(_serviceDef.getResources()).thenReturn(null); _failures.clear(); assertFalse(_validator.isValidResources(_serviceDef, _failures, Action.CREATE)); _utils.checkFailureForMissingValue(_failures, "resources"); List<RangerResourceDef> resources = new ArrayList<>(); when(_serviceDef.getResources()).thenReturn(resources); _failures.clear(); assertFalse(_validator.isValidResources(_serviceDef, _failures, Action.CREATE)); _utils.checkFailureForMissingValue(_failures, "resources"); resources.addAll(_utils.createResourceDefsWithIds(invalidResources)); _failures.clear(); assertFalse(_validator.isValidResources(_serviceDef, _failures, Action.CREATE)); _utils.checkFailureForMissingValue(_failures, "resource name"); _utils.checkFailureForMissingValue(_failures, "resource itemId"); _utils.checkFailureForSemanticError(_failures, "resource itemId", "1"); _utils.checkFailureForSemanticError(_failures, "resource name", "DataBase"); resources.clear(); resources.addAll(_utils.createResourceDefsWithIds(mixedCaseResources)); _failures.clear(); assertFalse(_validator.isValidResources(_serviceDef, _failures, Action.CREATE)); _utils.checkFailure(_failures, null, null, null, "DBase",null); _utils.checkFailure(_failures, null, null, null, "TABLE",null); _utils.checkFailure(_failures, null, null, null, "Column",null); }
@Test public final void test_isValidResources_happyPath() { Object[][] data = new Object[][] { { 1L, -10, "database" }, { 3L, 0, "table" }, { 5L, 10, "column" }, }; List<RangerResourceDef> resources = _utils.createResourceDefsWithIds(data); when(_serviceDef.getResources()).thenReturn(resources); assertTrue(_validator.isValidResources(_serviceDef, _failures, Action.CREATE)); assertTrue(_failures.isEmpty()); }