RangerServiceDefValidator extends RangerValidator { boolean isValidEnums(List<RangerEnumDef> enumDefs, List<ValidationFailureDetails> failures) { if(LOG.isDebugEnabled()) { LOG.debug(String.format("==> RangerServiceDefValidator.isValidEnums(%s, %s)", enumDefs, failures)); } boolean valid = true; if (CollectionUtils.isEmpty(enumDefs)) { LOG.debug("enum def collection passed in was null/empty. Ok."); } else { Set<String> names = new HashSet<>(); Set<Long> ids = new HashSet<>(); for (RangerEnumDef enumDef : enumDefs) { if (enumDef == null) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_NULL_OBJECT; failures.add(new ValidationFailureDetailsBuilder() .field("enum def") .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage()) .build()); valid = false; } else { String enumName = enumDef.getName(); valid = isUnique(enumName, names, "enum def name", "enum defs", failures) && valid; valid = isUnique(enumDef.getItemId(), ids, "enum def itemId", "enum defs", failures) && valid; if (CollectionUtils.isEmpty(enumDef.getElements())) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_NO_VALUES; failures.add(new ValidationFailureDetailsBuilder() .field("enum values") .subField(enumName) .isMissing() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(enumName)) .build()); valid = false; } else { valid = isValidEnumElements(enumDef.getElements(), failures, enumName) && valid; int defaultIndex = getEnumDefaultIndex(enumDef); if (defaultIndex < 0 || defaultIndex >= enumDef.getElements().size()) { ValidationErrorCode error = ValidationErrorCode.SERVICE_DEF_VALIDATION_ERR_ENUM_DEF_INVALID_DEFAULT_INDEX; failures.add(new ValidationFailureDetailsBuilder() .field("enum default index") .subField(enumName) .isSemanticallyIncorrect() .errorCode(error.getErrorCode()) .becauseOf(error.getMessage(defaultIndex, enumName)) .build()); valid = false; } } } } } if(LOG.isDebugEnabled()) { LOG.debug(String.format("<== RangerServiceDefValidator.isValidEnums(%s, %s): %s", enumDefs, 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_isValidEnums_happyPath() { List<RangerEnumDef> input = _utils.createEnumDefs(enums_good); assertTrue(_validator.isValidEnums(input, _failures)); assertTrue(_failures.isEmpty()); }
@Test public final void test_isValidEnums_failures() { List<RangerEnumDef> input = _utils.createEnumDefs(enums_good); input.add(null); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForMissingValue(_failures, "enum def"); input = _utils.createEnumDefs(enums_bad_enumName_null); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForMissingValue(_failures, "enum def name"); input = _utils.createEnumDefs(enums_bad_enumName_blank); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForMissingValue(_failures, "enum def name"); _utils.checkFailureForSemanticError(_failures, "enum def itemId", "1"); input = _utils.createEnumDefs(enums_good); RangerEnumDef anEnumDef = mock(RangerEnumDef.class); when(anEnumDef.getName()).thenReturn("anEnum"); when(anEnumDef.getElements()).thenReturn(null); input.add(anEnumDef); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForMissingValue(_failures, "enum values", "anEnum"); input = _utils.createEnumDefs(enums_bad_Elements_empty); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForMissingValue(_failures, "enum values", "anEnum"); _utils.checkFailureForMissingValue(_failures, "enum def itemId"); input = _utils.createEnumDefs(enums_good); String name = input.iterator().next().getName(); when(anEnumDef.getName()).thenReturn(name); List<RangerEnumElementDef> elementDefs = _utils.createEnumElementDefs(new String[] {"val1", "val2"}); when(anEnumDef.getElements()).thenReturn(elementDefs); input.add(anEnumDef); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForSemanticError(_failures, "enum def name", name); input = _utils.createEnumDefs(enums_bad_enumName_duplicate_differentCase); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForSemanticError(_failures, "enum def name", "Authentication-Type"); input = _utils.createEnumDefs(enums_good); when(input.iterator().next().getDefaultIndex()).thenReturn(-1); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForSemanticError(_failures, "enum default index", "authentication-type"); when(input.iterator().next().getDefaultIndex()).thenReturn(2); _failures.clear(); assertFalse(_validator.isValidEnums(input, _failures)); _utils.checkFailureForSemanticError(_failures, "enum default index", "authentication-type"); }