/** * Utility to check if the strings in a array of strings match acceptable values. If the input array is null then * nothing is done. * * @param isNullable if the value is allowed to be null or not. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param values a array of strings to validate. * @param acceptableValues list acceptable values for the input array. * @return returns an error if string is not present in list. Otherwise returns an empty list. */ protected static List<RaveError> checkStringDef( boolean isNullable, ValidationContext validationContext, @Nullable String[] values, String... acceptableValues) { List<RaveError> errors = checkNullable(values, isNullable, validationContext); if (values == null) { return errors; } for (String s : values) { errors = mergeErrors(errors, checkStringDef(false, validationContext, s, acceptableValues)); } return errors == null ? Collections.<RaveError>emptyList() : errors; }
/** * Utility to check if the strings in a {@link Collection} of strings match acceptable values. If the input * {@link Collection} is null then nothing is done. * * @param isNullable if the value is allowed to be null or not. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param values a {@link Collection} of strings to validate. * @param acceptableValues list acceptable values for the input list. * @return returns an error if string is not present in list. Otherwise returns an empty list. */ protected static List<RaveError> checkStringDef( boolean isNullable, ValidationContext validationContext, @Nullable Collection<String> values, String... acceptableValues) { List<RaveError> errors = checkNullable(values, isNullable, validationContext); if (values == null) { return errors; } for (String s : values) { errors = mergeErrors(errors, checkStringDef(false, validationContext, s, acceptableValues)); } return errors == null ? Collections.<RaveError>emptyList() : errors; }
@Test public void checkString_whenArrayIsNullOrEmpty_shouldNotProduceErrors() { String[] strings = new String[0]; BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); List<RaveError> errors = BaseValidator.checkStringDef(false, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (String[]) null, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (String[]) null); assertThat(errors).isEmpty(); }
@Test public void checkString_whenCollectionEmptyOrNull_shouldNotProduceErrors() { Collection<String> strings = new ArrayList<>(); BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Object.class); List<RaveError> errors = BaseValidator.checkStringDef(false, context, strings); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (Collection<String>) null); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (Collection<String>) null, "Valid"); assertThat(errors).isEmpty(); }
@Test public void checkString_whenCollectionIsValid_shouldNotProduceErrors() { Collection<String> strings = new ArrayList<>(); BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Object.class); strings.add("Valid1"); strings.add("Valid2"); strings.add("Valid3"); List<RaveError> errors = BaseValidator.checkStringDef(true, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(false, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); }
@Test public void checkString_whenArrayIsValid_shouldNotProduceErrors() { String[] strings = new String[] {"Valid1", "Valid2", "Valid3"}; BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Object.class); List<RaveError> errors = BaseValidator.checkStringDef(true, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(false, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); }
@Test public void checkString_whenCollectionIsInalid_shouldProduceErrors() { Collection<String> strings = new ArrayList<>(); strings.add("Valid1"); strings.add("InValid2"); strings.add("Valid3"); BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Object.class); List<RaveError> errors = BaseValidator.checkStringDef(false, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).hasSize(1); assertThat(errors.get(0).getErrorMsg()).contains(RaveErrorStrings.STRING_DEF_ERROR); errors = BaseValidator.checkStringDef(true, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).hasSize(1); assertThat(errors.get(0).getErrorMsg()).contains(RaveErrorStrings.STRING_DEF_ERROR); }
@Test public void checkString_whenArrayIsInValid_shouldProduceError() { String[] strings = new String[] {"Valid1", "Invalid2", "Valid3"}; BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); List<RaveError> errors = BaseValidator.checkStringDef(true, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).hasSize(1); assertThat(errors.get(0).toString()).contains(RaveErrorStrings.STRING_DEF_ERROR); errors = BaseValidator.checkStringDef(false, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).hasSize(1); assertThat(errors.get(0).toString()).contains(RaveErrorStrings.STRING_DEF_ERROR); }
@Test public void checkString_whenStringIsNull_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); List<RaveError> errors = BaseValidator.checkStringDef(false, context, (String) null, "Valid1", "Valid2", "Valid3"); assertThat(errors).hasSize(1); assertThat(errors.get(0).toString().contains(RaveErrorStrings.NON_NULL_ERROR)).isTrue(); }
@Test public void checkString_whenStringIsNotNull_shouldProduceNoErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); List<RaveError> errors = BaseValidator.checkStringDef(true, context, (String) null, "Valid1", "Valid2", "Valid3"); assertThat(errors).hasSize(0); }