AnalysisInteractor { public CompletableFuture<Void> deleteMultiPageDocument( final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument) { return deleteDocument(multiPageDocument) .handle(new CompletableFuture.BiFun<NetworkRequestResult<GiniVisionDocument>, Throwable, Void>() { @Override public Void apply( final NetworkRequestResult<GiniVisionDocument> giniVisionDocumentNetworkRequestResult, final Throwable throwable) { return null; } }) .thenCompose( new CompletableFuture.Fun<Void, CompletableFuture<Void>>() { @Override public CompletableFuture<Void> apply( final Void result) { final NetworkRequestsManager networkRequestsManager = GiniVision.getInstance() .internal().getNetworkRequestsManager(); if (networkRequestsManager == null) { return CompletableFuture.completedFuture(null); } final List<CompletableFuture<NetworkRequestResult< GiniVisionDocument>>> futures = new ArrayList<>(); for (final GiniVisionDocument document : multiPageDocument.getDocuments()) { networkRequestsManager.cancel(document); futures.add(networkRequestsManager.delete(document)); } return CompletableFuture.allOf( futures.toArray(new CompletableFuture[0])); } }); } AnalysisInteractor(@NonNull final Application app); CompletableFuture<ResultHolder> analyzeMultiPageDocument(
            final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError>
                    multiPageDocument); CompletableFuture<Void> deleteMultiPageDocument(
            final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError>
                    multiPageDocument); CompletableFuture<NetworkRequestResult<GiniVisionDocument>> deleteDocument(
            final GiniVisionDocument document);  }
@Test public void should_notDeleteMultiPageDocument_whenNetworkRequestsManager_isNotAvailable() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); createGiniVision(null); final CompletableFuture<Void> future = mAnalysisInteractor.deleteMultiPageDocument(multiPageDocument); assertThat(future.get()).isNull(); }
@Test public void should_deleteMultiPageDocument() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final NetworkRequestResult<GiniVisionDocument> deletionRequestResult = mock( NetworkRequestResult.class); final NetworkRequestsManager networkRequestsManager = createtNetworkRequestsManager( deletionRequestResult); createGiniVision(networkRequestsManager); mAnalysisInteractor.deleteMultiPageDocument(multiPageDocument); verify(networkRequestsManager).cancel(multiPageDocument); verify(networkRequestsManager).delete(multiPageDocument); }
@Test public void should_deleteEveryPage_ofMultiPageDocument() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final List<GiniVisionDocument> documents = new ArrayList<>(); documents.add(mock(GiniVisionDocument.class)); documents.add(mock(GiniVisionDocument.class)); documents.add(mock(GiniVisionDocument.class)); when(multiPageDocument.getDocuments()).thenReturn(documents); final NetworkRequestResult<GiniVisionDocument> deletionRequestResult = mock( NetworkRequestResult.class); final NetworkRequestsManager networkRequestsManager = createtNetworkRequestsManager( deletionRequestResult); createGiniVision(networkRequestsManager); mAnalysisInteractor.deleteMultiPageDocument(multiPageDocument); for (final GiniVisionDocument document : documents) { verify(networkRequestsManager).cancel(document); verify(networkRequestsManager).delete(document); } }
@Test public void should_deleteEverypPage_ofMultiPageDocument_whenDeletion_ofMultiPageDocumentFailes() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final List<GiniVisionDocument> documents = new ArrayList<>(); documents.add(mock(GiniVisionDocument.class)); documents.add(mock(GiniVisionDocument.class)); documents.add(mock(GiniVisionDocument.class)); when(multiPageDocument.getDocuments()).thenReturn(documents); final NetworkRequestsManager networkRequestsManager = createtNetworkRequestsManagerWithDeletionException(new RuntimeException()); createGiniVision(networkRequestsManager); mAnalysisInteractor.deleteMultiPageDocument(multiPageDocument); verify(networkRequestsManager).cancel(multiPageDocument); verify(networkRequestsManager).delete(multiPageDocument); for (final GiniVisionDocument document : documents) { verify(networkRequestsManager).cancel(document); verify(networkRequestsManager).delete(document); } }