VaultClientService { public Mono<byte[]> queryDocument(final DocumentEntity documentEntity) { final VaultClientLocationConfigurationProperties vaultClientLocationConfigurationProperties = vaultClientConfigurationProperties.getLocation(documentEntity.getVault()); return vaultWebClient.get() .uri(vaultClientLocationConfigurationProperties.getLocation() + "/document/" + documentEntity.getId()) .retrieve() .bodyToMono(byte[].class) .onErrorResume(WebClientResponseException.class, (error) -> { log.info("Missing document with id: {}!", documentEntity.getId()); if (error.getResponseBodyAsString().contains("Unable to get the content of a vault location!") || error.getResponseBodyAsString().contains("Error while decompressing document!")) { return documentManipulator.markIndexFailure(documentEntity.getId()) .then(Mono.empty()); } return Mono.empty(); }); }  Mono<byte[]> queryDocument(final DocumentEntity documentEntity); void recompressDocument(final DocumentEntity documentEntity, final DocumentCompression documentCompression); Mono<Long> getAvailableSpace(final String vaultName);  }
@Test public void testQueryDocumentWhenUnableToGetDocumentContents() { final WebClient webClient = WebClient.builder() .exchangeFunction(clientRequest -> { assertThat(clientRequest.url().toString(), is(equalTo("http: + TEST_DOCUMENT_ID.toString()))); return Mono.just( ClientResponse.create(HttpStatus.INTERNAL_SERVER_ERROR) .header("Content-Type", "application/json") .body("{\"error\": \"Unable to get the content of a vault location!\"}") .build() ); } ) .build(); final VaultClientService underTest = new VaultClientService( vaultClientConfigurationProperties, documentManipulator, webClient); final DocumentEntity documentEntity = DocumentEntity.builder() .id(TEST_DOCUMENT_ID) .vault("default") .build(); final VaultClientLocationConfigurationProperties vaultClientLocationConfigurationProperties = new VaultClientLocationConfigurationProperties(); vaultClientLocationConfigurationProperties.setHost("myhost"); vaultClientLocationConfigurationProperties.setPort(1234); when(vaultClientConfigurationProperties.getLocation("default")) .thenReturn(vaultClientLocationConfigurationProperties); final Mono<Void> indexingFailureMono = Mono.empty(); when(documentManipulator.markIndexFailure(TEST_DOCUMENT_ID)) .thenReturn(indexingFailureMono); final Mono<byte[]> result = underTest.queryDocument(documentEntity); StepVerifier.create(result) .verifyComplete(); StepVerifier.create(indexingFailureMono) .verifyComplete(); }
@Test public void testQueryDocumentWhenTheRequestWasSuccessful() { final DefaultDataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(); final WebClient webClient = WebClient.builder() .exchangeFunction(clientRequest -> { assertThat(clientRequest.url().toString(), is(equalTo("http: + TEST_DOCUMENT_ID.toString()))); return Mono.just( ClientResponse.create(HttpStatus.OK) .header("Content-Type", "application/octet-stream") .body(Flux.just(dataBufferFactory.wrap(new byte[]{0, 1, 2}))) .build() ); } ) .build(); final VaultClientService underTest = new VaultClientService( vaultClientConfigurationProperties, documentManipulator, webClient); final DocumentEntity documentEntity = DocumentEntity.builder() .id(TEST_DOCUMENT_ID) .vault("default") .build(); final VaultClientLocationConfigurationProperties vaultClientLocationConfigurationProperties = new VaultClientLocationConfigurationProperties(); vaultClientLocationConfigurationProperties.setHost("myhost"); vaultClientLocationConfigurationProperties.setPort(1234); when(vaultClientConfigurationProperties.getLocation("default")) .thenReturn(vaultClientLocationConfigurationProperties); final Mono<byte[]> result = underTest.queryDocument(documentEntity); StepVerifier.create(result) .consumeNextWith(documentContents -> assertThat(documentContents, is(equalTo(new byte[]{0, 1, 2})))) .verifyComplete(); verify(documentManipulator, never()).markIndexFailure(TEST_DOCUMENT_ID); }