DocumentFileValidator { public Mono<Boolean> isValidDocument(final String documentId, final DocumentType documentType) { return stageLocationFactory.getLocation(documentId, documentType) .flatMap(stageFileLocation -> stageFileLocation.size() .map(size -> isValidFileSize(size) && isParsable(documentId, documentType, stageFileLocation)) ); }  Mono<Boolean> isValidDocument(final String documentId, final DocumentType documentType);  }
@Test void testIsValidDocumentWhenDocumentIsTooSmall() { final StageLocation stageLocation = mock(StageLocation.class); when(stageLocationFactory.getLocation(DOCUMENT_ID.toString(), DOCUMENT_TYPE)) .thenReturn(Mono.just(stageLocation)); when(stageLocation.size()).thenReturn(Mono.just(0L)); final Mono<Boolean> result = underTest.isValidDocument(DOCUMENT_ID.toString(), DOCUMENT_TYPE); StepVerifier.create(result) .consumeNextWith(Assertions::assertFalse) .verifyComplete(); }
@Test void testIsValidDocumentWhenDocumentIsTooBig() { final StageLocation stageLocation = mock(StageLocation.class); when(stageLocationFactory.getLocation(DOCUMENT_ID.toString(), DOCUMENT_TYPE)) .thenReturn(Mono.just(stageLocation)); when(stageLocation.size()).thenReturn(Mono.just(MAX_FILE_SIZE_IN_BYTES + 1)); when(downloaderConfigurationProperties.getMaximumArchiveSize()).thenReturn(MAX_FILE_SIZE_IN_BYTES); final Mono<Boolean> result = underTest.isValidDocument(DOCUMENT_ID.toString(), DOCUMENT_TYPE); StepVerifier.create(result) .consumeNextWith(Assertions::assertFalse) .verifyComplete(); }
@Test void testIsValidDocumentWhenDocumentIsntParsable() { final StageLocation stageLocation = mock(StageLocation.class); when(stageLocationFactory.getLocation(DOCUMENT_ID.toString(), DOCUMENT_TYPE)) .thenReturn(Mono.just(stageLocation)); when(stageLocation.size()).thenReturn(Mono.just(MAX_FILE_SIZE_IN_BYTES - 1)); when(downloaderConfigurationProperties.getMaximumArchiveSize()).thenReturn(MAX_FILE_SIZE_IN_BYTES); final InputStream documentInputStream = mock(InputStream.class); when(stageLocation.openStream()).thenReturn(documentInputStream); when(documentDataParser.parseDocumentMetadata(DOCUMENT_ID, DOCUMENT_TYPE, documentInputStream)) .thenThrow(new RuntimeException("Some error!")); final Mono<Boolean> result = underTest.isValidDocument(DOCUMENT_ID.toString(), DOCUMENT_TYPE); StepVerifier.create(result) .consumeNextWith(Assertions::assertFalse) .verifyComplete(); verify(documentDataParser).parseDocumentMetadata(DOCUMENT_ID, DOCUMENT_TYPE, documentInputStream); }
@Test void testIsValidDocumentWhenDocumentIsValid() { final StageLocation stageLocation = mock(StageLocation.class); when(stageLocationFactory.getLocation(DOCUMENT_ID.toString(), DOCUMENT_TYPE)) .thenReturn(Mono.just(stageLocation)); when(stageLocation.size()).thenReturn(Mono.just(MAX_FILE_SIZE_IN_BYTES - 1)); when(downloaderConfigurationProperties.getMaximumArchiveSize()).thenReturn(MAX_FILE_SIZE_IN_BYTES); final InputStream documentInputStream = mock(InputStream.class); when(stageLocation.openStream()).thenReturn(documentInputStream); when(documentDataParser.parseDocumentMetadata(DOCUMENT_ID, DOCUMENT_TYPE, documentInputStream)) .thenReturn( DocumentMetadata.builder() .build() ); final Mono<Boolean> result = underTest.isValidDocument(DOCUMENT_ID.toString(), DOCUMENT_TYPE); StepVerifier.create(result) .consumeNextWith(Assertions::assertTrue) .verifyComplete(); }