/** * Checks to see if the object is null. * * @param collection the collection to validate. * @param isNullable is the object is allowed to be null. * @param validationContext the context of the item in the class being validated. This is used in case of an error. * @return a list of of {@link RaveError}s if the object is not allowed to be null. Returns null otherwise. */ protected static List<RaveError> checkNullable( @Nullable Collection<?> collection, boolean isNullable, ValidationContext validationContext) { List<RaveError> errors = checkNullable((Object) collection, isNullable, validationContext); return collection == null ? errors : checkIterable(collection, errors); }
@Test public void checkMapCollection_whenMapIsNotNull_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(HashMap.class); List<RaveError> errors = BaseValidator.checkNullable(new HashMap<>(), false, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableCollection_whenCollectionHas2InvalidObjects_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(SingleMethodSampleModel.class); List<SingleMethodSampleModel> list = new LinkedList<>(); list.add(new SingleMethodSampleModel(null, SingleMethodSampleModel.MATCHED1)); list.add(new SingleMethodSampleModel("lengthiseven", "Not matching the specified string defs")); List<RaveError> errors = BaseValidator.checkNullable(list, false, context); assertThat(errors).isNotNull(); assertThat(errors).hasSize(2); }
@Test public void checkNullableMap_whenMapIsNotNullAndIsNullable_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(HashMap.class); List<RaveError> errors = BaseValidator.checkNullable(new HashMap<>(), true, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableCollection_whenCollectionIsNotNull_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Collection.class); List<RaveError> errors = BaseValidator.checkNullable(new LinkedList<>(), false, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableCollection_whenCollectionIsNotNullAndIsNullable_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Collection.class); List<RaveError> errors = BaseValidator.checkNullable(new LinkedList<>(), true, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableArray_whenCollectionIsNull_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Integer[].class); List<RaveError> errors = BaseValidator.checkNullable((Integer[]) null, true, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableArray_whenCollectionIsNotNullAndIsNullable_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Integer.class); List<RaveError> errors = BaseValidator.checkNullable(new Integer[0], true, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableArray_whenCollectionIsNotNull_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Integer[].class); List<RaveError> errors = BaseValidator.checkNullable(new Integer[0], false, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableMap_whenMapIsNull_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(HashMap.class); List<RaveError> errors = BaseValidator.checkNullable((Map<Object, Object>) null, true, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableCollection_whenCollectionIsNull_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Collection.class); List<RaveError> errors = BaseValidator.checkNullable((Collection<Object>) null, true, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkNullableMap_whenMapKeyandValueAreInvalidObjects_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(SingleMethodSampleModel.class); context.setValidatedItemName("getNotNullField()"); SingleMethodSampleModel validSingle = new SingleMethodSampleModel("ab", SingleMethodSampleModel.MATCHED1); Map<SingleMethodSampleModel, SingleMethodSampleModel> map = new HashMap<>(); map.put(validSingle, new SingleMethodSampleModel(null, SingleMethodSampleModel.MATCHED1)); map.put(new SingleMethodSampleModel("lengthiseven", "Not matching the specified string defs"), validSingle); List<RaveError> errors = BaseValidator.checkNullable(map, false, context); assertThat(errors).isNotNull(); assertThat(errors).hasSize(2); }
@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()); }
@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()); }
@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()); }
@Test public void checkNullableArray_whenCollectionHas2InvalidObjects_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(SingleMethodSampleModel.class); SingleMethodSampleModel[] array = new SingleMethodSampleModel[2]; array[0] = new SingleMethodSampleModel(null, SingleMethodSampleModel.MATCHED1); array[1] = new SingleMethodSampleModel("lengthiseven", "Not matching the specified string defs"); List<RaveError> errors = BaseValidator.checkNullable(array, false, context); assertThat(errors).isNotNull(); assertThat(errors).hasSize(2); }
@Test public void checkNullableCollection_whenCollectionHasInvalidObject_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(SingleMethodSampleModel.class); context.setValidatedItemName("getNotNullField()"); List<SingleMethodSampleModel> list = new LinkedList<>(); list.add(new SingleMethodSampleModel(null, SingleMethodSampleModel.MATCHED1)); List<RaveError> errors = BaseValidator.checkNullable(list, 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()); }
@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 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 checkNullableArray_whenCollectionHasInvalidObject_shouldProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(SingleMethodSampleModel.class); context.setValidatedItemName("getNotNullField()"); SingleMethodSampleModel[] array = new SingleMethodSampleModel[1]; array[0] = new SingleMethodSampleModel(null, SingleMethodSampleModel.MATCHED1); List<RaveError> errors = BaseValidator.checkNullable(array, 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()); }