FileValidatorImpl implements FileValidator { @Override public void validate(MultipartFile file) throws FileValidationException { Assert.notNull(file); if (isMissingFile(file)) { throw new FileIsMissingValidationException(); } if (isEmptyFile(file)) { throw new FileIsEmptyValidationException(); } if (isGreaterFileSizeThanTheMaximumFileSize(file)) { throw new FileIsTooLargeValidationException(); } if (isFileInWrongExtension(file)) { throw new FileIsInWrongExtensionValidationException(); } if (isFileContainsNotOnlyValidCharacters(file)) { throw new FileContainsInvalidCharacterValidationException(); } }  @Override void validate(MultipartFile file);  }
@Test(expected = IllegalArgumentException.class) public void validateShouldThrowIllegalArgumentExceptionWhenFileIsNull() throws FileValidationException { fileValidator.validate(null); }
@Test(expected = FileIsMissingValidationException.class) public void isMissingFileShouldThrowFileIsMissingValidationExceptionWhenOriginalFilenameIsEmpty() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_EMPTY, FILE_SIZE_ZERO_MB); fileValidator.validate(multipartFile); }
@Test(expected = FileIsEmptyValidationException.class) public void isEmptyFileShouldThrowFileIsEmptyValidationExceptionWhenFileIsEmpty() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_PDF, FILE_SIZE_ZERO_MB); fileValidator.validate(multipartFile); }
@Test public void isGreaterFileSizeThanTheMaximumFileSizeShouldNotThrowFileIsTooLargeValidationExceptionWhenFileSizeIsLessThanMaximumFileSize() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_PDF, FILE_SIZE_TEN_MB_MINUS_ONE_BYTE); fileValidator.validate(multipartFile); }
@Test public void isGreaterFileSizeThanTheMaximumFileSizeShouldNotThrowFileIsTooLargeValidationExceptionWhenFileSizeEqualsMaximumFileSize() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_PDF, FILE_SIZE_TEN_MB); fileValidator.validate(multipartFile); }
@Test(expected = FileIsTooLargeValidationException.class) public void isGreaterFileSizeThanTheMaximumFileSizeShouldThrowFileIsTooLargeValidationExceptionWhenFileSizeIsGreaterThanMaximumFileSize() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_PDF, FILE_SIZE_TEN_MB_PLUS_ONE_BYTE); fileValidator.validate(multipartFile); }
@Test(expected = FileIsInWrongExtensionValidationException.class) public void isFileInWrongExtensionShouldThrowFileIsInWrongExtensionValidationExceptionWhenFileIsInWrongExtension() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IN_WRONG_EXTENSION, FILE_SIZE_FIVE_MB); fileValidator.validate(multipartFile); }
@Test(expected = FileContainsInvalidCharacterValidationException.class) public void isFileContainsNotOnlyValidCharactersShouldThrowFileContainsInvalidCharacterValidationExceptionWhenFileContainsInvalidCharacter() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_CONTAINS_INVALID_CHARACTER, FILE_SIZE_FIVE_MB); fileValidator.validate(multipartFile); }
@Test public void validateShouldNotThrowFileValidationExceptionWhenOriginalFileNameIsValidDoc() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_DOC, FILE_SIZE_FIVE_MB); fileValidator.validate(multipartFile); }
@Test public void validateShouldNotThrowFileValidationExceptionWhenOriginalFileNameIsValidDocx() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_DOCX, FILE_SIZE_FIVE_MB); fileValidator.validate(multipartFile); }
@Test public void validateShouldNotThrowFileValidationExceptionWhenOriginalFileNameIsValidOdt() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_ODT, FILE_SIZE_FIVE_MB); fileValidator.validate(multipartFile); }
@Test public void validateShouldNotThrowFileValidationExceptionWhenOriginalFileNameIsValidPdf() throws FileValidationException { MultipartFile multipartFile = MultipartFileCreatorTestHelper .createMultipartFile(ORIGINAL_FILENAME_IS_VALID_PDF, FILE_SIZE_FIVE_MB); fileValidator.validate(multipartFile); }