RangerServiceDefValidator extends RangerValidator { boolean isValidPolicyConditions(Long serviceDefId, List<RangerPolicyConditionDef> policyConditions, List<ValidationFailureDetails> failures, final Action action) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidPolicyConditions(%s, %s)", policyConditions, failures)); } boolean valid = true; if (CollectionUtils.isEmpty(policyConditions)) { LOG.debug("Configs collection was null/empty! ok"); } else { Map<Long, String> existingPolicyCondIDNameMap = new HashMap<>(); if (action == Action.UPDATE) { List<RangerPolicyConditionDef> existingPolicyConditions = this.getServiceDef(serviceDefId).getPolicyConditions(); for (RangerPolicyConditionDef existingPolicyCondition : existingPolicyConditions) { existingPolicyCondIDNameMap.put(existingPolicyCondition.getItemId(), existingPolicyCondition.getName()); } } if(LOG.isDebugEnabled()) { LOG.debug("policy condition names from db = " + existingPolicyCondIDNameMap.values()); } Set<Long> ids = new HashSet<>(); Set<String> names = new HashSet<>(); for (RangerPolicyConditionDef conditionDef : policyConditions) { Long itemId = conditionDef.getItemId(); valid = isUnique(itemId, ids, "policy condition def itemId", "policy condition defs", failures) && valid; String name = conditionDef.getName(); valid = isUnique(name, names, "policy condition def name", "policy condition defs", failures) && valid; if (action == Action.UPDATE) { if (existingPolicyCondIDNameMap.get(itemId) != null && !existingPolicyCondIDNameMap.get(itemId).equals(name)) { ValidationErrorCode error; error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_SERVICE_DEF_NAME_CONFICT; failures.add((new ValidationFailureDetailsBuilder()).field("policy condition def name").isSemanticallyIncorrect().errorCode(error.getErrorCode()).becauseOf(String.format("changing %s[%s] in %s is not supported", "policy condition def name", name, "policy condition defs")).build()); valid = false; } } if (StringUtils.isBlank(conditionDef.getEvaluator())) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_POLICY_CONDITION_NULL_EVALUATOR; failures.add(new ValidationFailureDetailsBuilder() .field("policy condition def evaluator") .subField(name) .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(name)) .build()); valid = false; } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidPolicyConditions(%s, %s): %s", policyConditions, 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_isValidPolicyConditions() { long id = 7; when(_serviceDef.getId()).thenReturn(id); assertTrue(_validator.isValidPolicyConditions(id,null, _failures, Action.CREATE)); assertTrue(_failures.isEmpty()); List<RangerPolicyConditionDef> conditionDefs = new ArrayList<>(); assertTrue(_validator.isValidPolicyConditions(id, conditionDefs, _failures, Action.CREATE)); assertTrue(_failures.isEmpty()); Object[][] policyCondition_data = { { null, null, null}, { 1L, "condition-1", null }, { 1L, "condition-2", "" }, { 2L, "condition-1", "com.evaluator" }, }; conditionDefs.addAll(_utils.createPolicyConditionDefs(policyCondition_data)); _failures.clear(); assertFalse(_validator.isValidPolicyConditions(id, conditionDefs, _failures, Action.CREATE)); _utils.checkFailureForMissingValue(_failures, "policy condition def itemId"); _utils.checkFailureForMissingValue(_failures, "policy condition def name"); _utils.checkFailureForMissingValue(_failures, "policy condition def evaluator"); _utils.checkFailureForSemanticError(_failures, "policy condition def itemId", "1"); _utils.checkFailureForSemanticError(_failures, "policy condition def name", "condition-1"); _utils.checkFailureForMissingValue(_failures, "policy condition def evaluator", "condition-2"); _utils.checkFailureForMissingValue(_failures, "policy condition def evaluator", "condition-1"); }