@Nullable protected static List<RaveError> mustBeFalse(boolean input, ValidationContext validationContext) { if (input) { List<RaveError> list = new LinkedList<>(); list.add(new RaveError(validationContext, RaveErrorStrings.MUST_BE_FALSE_ERROR)); return list; } return null; }
private static String getFirstString(List<RaveError> errors) { return errors.isEmpty() ? "" : errors.get(0).toString(); } }
/** * @param error the error to match * @return Returns true if this error matches the criteria of this ignore rule. */ public boolean shouldIgnore(RaveError error) { if (!error.getClazz().equals(clazz)) { return false; } return ignoreClassAll || ignoreMethods.contains(error.getClassElement()); }
@Test public void checkNullableArray_whenCollectionIsNullButShouldntBe_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Integer.class); List<RaveError> errors = BaseValidator.checkNullable((Integer[]) null, false, context); assertThat(errors).isNotNull(); RaveError expectedError = new RaveError(context, RaveErrorStrings.NON_NULL_ERROR); assertThat(errors).hasSize(1); assertThat(errors.get(0).toString()).isEqualTo(expectedError.toString()); }
@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 checkNullableMap_whenMapIsNullButShouldntBe_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Map.class); List<RaveError> errors = BaseValidator.checkNullable((Map<Object, Object>) null, false, context); assertThat(errors).isNotNull(); RaveError expectedError = new RaveError(context, RaveErrorStrings.NON_NULL_ERROR); assertThat(errors).hasSize(1); assertThat(errors.get(0).toString()).isEqualTo(expectedError.toString()); }
@Test public void checkIntDef_whenValueDoesNotMatch_shouldProduceError() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); List<RaveError> errors = BaseValidator.checkIntDef(context, 9, false, 10, 11, 12, 13); assertThat(errors).isNotEmpty(); assertThat(errors).hasSize(1); RaveError error = errors.get(0); assertThat(error.getErrorMsg()).contains(RaveErrorStrings.INT_DEF_ERROR); }
@Test public void checkNullableCollection_whenCollectionIsNullButShouldntBe_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Collection.class); List<RaveError> errors = BaseValidator.checkNullable((Collection<Object>) null, false, context); assertThat(errors).isNotNull(); RaveError expectedError = new RaveError(context, RaveErrorStrings.NON_NULL_ERROR); assertThat(errors).hasSize(1); assertThat(errors.get(0).toString()).isEqualTo(expectedError.toString()); }
/** * Verifies the input to be always true. * * @param input the value to validate. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @return null if input is true and a {@link List} otherwise. */ protected static List<RaveError> mustBeTrue(boolean input, ValidationContext validationContext) { return input ? Collections.<RaveError>emptyList() : createNewList(new RaveError(validationContext, RaveErrorStrings.MUST_BE_TRUE_ERROR)); }
/** * @return Returns the list of error messages as a single string message. */ @Override public final String getMessage() { StringBuilder builder = new StringBuilder(); ListIterator<RaveError> iterator = errors.listIterator(errors.size()); String newline = "\n"; while (iterator.hasPrevious()) { builder.append(iterator.previous().toString()).append(newline); } return builder.toString(); }
@Test public void checkLongDef_whenValueDoesNotMatch_shouldProduceError() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); List<RaveError> errors = BaseValidator.checkLongDef(context, 9L, false, 10L, 11L, 12L, 13L); assertThat(errors).isNotEmpty(); assertThat(errors).hasSize(1); RaveError error = errors.get(0); assertThat(error.getErrorMsg()).contains(RaveErrorStrings.LONG_DEF_ERROR); }
@Test public void checkNullableMap_whenMapHasInvalidObjectAsValue_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(SingleMethodSampleModel.class); context.setValidatedItemName("getNotNullField()"); Map<Object, SingleMethodSampleModel> map = new HashMap<>(); map.put(new Object(), new SingleMethodSampleModel(null, SingleMethodSampleModel.MATCHED1)); List<RaveError> errors = BaseValidator.checkNullable(map, false, context); assertThat(errors).isNotNull(); assertThat(errors).hasSize(1); RaveError expectedError = new RaveError(context, RaveErrorStrings.NON_NULL_ERROR); assertThat(errors.get(0).toString()).isEqualTo(expectedError.toString()); }
/** * Merge a msg with a {@link List}s of {@link RaveError}s. * * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param msg the message to add to the list. * @param raveErrors the {@link List} of {@link RaveError} to add to. * @return a new {@link List} or {@link RaveError}s */ private static List<RaveError> appendError( ValidationContext validationContext, String msg, @Nullable List<RaveError> raveErrors) { // If the list is empty it is also immutable so we need a new one. if (raveErrors == null || raveErrors.isEmpty()) { return createNewList(new RaveError(validationContext, msg)); } raveErrors.add(new RaveError(validationContext, msg)); return raveErrors; }
@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 checkIntRange_whenValuesNotInRange_shouldReturnError() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkIntRange(context, -10, -1, 10)).isNotEmpty(); assertThat(BaseValidator.checkIntRange(context, 12, -1, 11)).isNotEmpty(); List<RaveError> errors = BaseValidator.checkIntRange(context, 12, -1, 11); assertThat(errors).hasSize(1); assertThat(errors.get(0).getErrorMsg()).contains(RaveErrorStrings.INT_RANGE_ERROR); }
@Test public void checkMustBeFalse_whenInvalidBoolean_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Object.class); context.setValidatedItemName("someMethodName"); List<RaveError> errors = BaseValidator.mustBeFalse(true, context); assertThat(errors).isNotNull(); RaveError expectedError = new RaveError(context, RaveErrorStrings.MUST_BE_FALSE_ERROR); assertThat(errors).hasSize(1); assertThat(errors.get(0).toString()).isEqualTo(expectedError.toString()); }
private static List<RaveError> createIntDefError( int value, int[] acceptableValues, ValidationContext validationContext) { StringBuilder stringBuilder = new StringBuilder(); boolean first = true; for (int string : acceptableValues) { if (!first) { stringBuilder.append(" "); } else { stringBuilder.append("{"); } stringBuilder.append(string); first = false; } stringBuilder.append("}"); return createNewList( new RaveError(validationContext, value + " " + RaveErrorStrings.INT_DEF_ERROR + stringBuilder.toString())); }
@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 checkIntRange_whenValuesNotInFloatRange_shouldReturnError() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkFloatRange(context, -10d, -1d, 10d)).isNotEmpty(); assertThat(BaseValidator.checkFloatRange(context, 12d, -1d, 11d)).isNotEmpty(); List<RaveError> errors = BaseValidator.checkFloatRange(context, 12d, -1d, 11d); assertThat(errors).hasSize(1); assertThat(errors.get(0).getErrorMsg()).contains(RaveErrorStrings.FLOAT_RANGE_ERROR); }
@Test public void checkNullableMap_whenMapHasInvalidObjectAsKey_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(SingleMethodSampleModel.class); context.setValidatedItemName("getNotNullField()"); Map<SingleMethodSampleModel, Object> map = new HashMap<>(); map.put(new SingleMethodSampleModel(null, SingleMethodSampleModel.MATCHED1), new Object()); List<RaveError> errors = BaseValidator.checkNullable(map, false, context); assertThat(errors).isNotNull(); assertThat(errors).hasSize(1); RaveError expectedError = new RaveError(context, RaveErrorStrings.NON_NULL_ERROR); assertThat(errors.get(0).toString()).isEqualTo(expectedError.toString()); }