AnalysisInteractor { public CompletableFuture<ResultHolder> analyzeMultiPageDocument( final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument) { if (GiniVision.hasInstance()) { final NetworkRequestsManager networkRequestsManager = GiniVision.getInstance() .internal().getNetworkRequestsManager(); if (networkRequestsManager != null) { GiniVisionDebug.writeDocumentToFile(mApp, multiPageDocument, "_for_analysis"); for (final Object document : multiPageDocument.getDocuments()) { final GiniVisionDocument giniVisionDocument = (GiniVisionDocument) document; networkRequestsManager.upload(mApp, giniVisionDocument); } return networkRequestsManager.analyze(multiPageDocument) .handle(new CompletableFuture.BiFun<AnalysisNetworkRequestResult< GiniVisionMultiPageDocument>, Throwable, ResultHolder>() { @Override public ResultHolder apply( final AnalysisNetworkRequestResult<GiniVisionMultiPageDocument> requestResult, final Throwable throwable) { if (throwable != null && !isCancellation(throwable)) { throw new RuntimeException(throwable); } else if (requestResult != null) { final Map<String, GiniVisionSpecificExtraction> extractions = requestResult.getAnalysisResult().getExtractions(); if (extractions.isEmpty()) { return new ResultHolder(Result.SUCCESS_NO_EXTRACTIONS); } else { return new ResultHolder(Result.SUCCESS_WITH_EXTRACTIONS, extractions); } } return null; } }); } else { return CompletableFuture.completedFuture( new ResultHolder(Result.NO_NETWORK_SERVICE)); } } else { return CompletableFuture.completedFuture(new ResultHolder(Result.NO_NETWORK_SERVICE)); } } 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_completeWithNoNetworkService_whenGiniVision_isNotAvailable() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final CompletableFuture<AnalysisInteractor.ResultHolder> future = mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); assertThat(future.get().getResult()).isEqualTo( AnalysisInteractor.Result.NO_NETWORK_SERVICE); }
@Test public void should_completeWithNoNetworkService_whenNetworkRequestsManager_isNotAvailable() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); createGiniVision(null); final CompletableFuture<AnalysisInteractor.ResultHolder> future = mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); assertThat(future.get().getResult()).isEqualTo( AnalysisInteractor.Result.NO_NETWORK_SERVICE); }
@Test public void should_uploadEveryPage_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 NetworkRequestsManager networkRequestsManager = mock(NetworkRequestsManager.class); final CompletableFuture<AnalysisNetworkRequestResult<GiniVisionMultiPageDocument>> analysisResult = CompletableFuture.completedFuture(null); when(networkRequestsManager.analyze(any(GiniVisionMultiPageDocument.class))).thenReturn( analysisResult); createGiniVision(networkRequestsManager); mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); for (final GiniVisionDocument document : documents) { verify(networkRequestsManager).upload(mApp, document); } }
@Test public void should_completeWithException_whenAnalysisFailed_withoutCancellation() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final RuntimeException analysisException = new RuntimeException(); final NetworkRequestsManager networkRequestsManager = createtNetworkRequestsManagerWithAnalysisException(analysisException); createGiniVision(networkRequestsManager); final CompletableFuture<AnalysisInteractor.ResultHolder> future = mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); Throwable exception = null; try { future.get(); } catch (final ExecutionException e) { exception = e.getCause().getCause(); } assertThat(exception).isEqualTo(analysisException); }
@Test public void should_notCompleteWithException_whenAnalysisFailed_dueToCancellation() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final RuntimeException analysisException = new CancellationException(); final NetworkRequestsManager networkRequestsManager = createtNetworkRequestsManagerWithAnalysisException(analysisException); createGiniVision(networkRequestsManager); final CompletableFuture<AnalysisInteractor.ResultHolder> future = mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); Throwable exception = null; try { future.get(); } catch (final ExecutionException e) { exception = e; } assertThat(exception).isNull(); }
@Test public void should_completeWithNoExtractions_whenAnalysisSucceeded_withoutExtractions() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final AnalysisResult analysisResult = new AnalysisResult("apiDocumentId", Collections.<String, GiniVisionSpecificExtraction>emptyMap()); final NetworkRequestsManager networkRequestsManager = createtNetworkRequestsManager( analysisResult); createGiniVision(networkRequestsManager); final CompletableFuture<AnalysisInteractor.ResultHolder> future = mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); final AnalysisInteractor.ResultHolder resultHolder = future.get(); assertThat(resultHolder.getResult()).isEqualTo( AnalysisInteractor.Result.SUCCESS_NO_EXTRACTIONS); }
@Test public void should_completeWithExtractions_whenAnalysisSucceeded_withExtractions() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final Map<String, GiniVisionSpecificExtraction> extractions = new HashMap<>(); extractions.put("amountToPay", mock(GiniVisionSpecificExtraction.class)); extractions.put("paymentRecipient", mock(GiniVisionSpecificExtraction.class)); extractions.put("iban", mock(GiniVisionSpecificExtraction.class)); extractions.put("paymentReference", mock(GiniVisionSpecificExtraction.class)); final AnalysisResult analysisResult = new AnalysisResult("apiDocumentId", extractions); final NetworkRequestsManager networkRequestsManager = createtNetworkRequestsManager( analysisResult); createGiniVision(networkRequestsManager); final CompletableFuture<AnalysisInteractor.ResultHolder> future = mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); final AnalysisInteractor.ResultHolder resultHolder = future.get(); assertThat(resultHolder.getResult()).isEqualTo( AnalysisInteractor.Result.SUCCESS_WITH_EXTRACTIONS); assertThat(resultHolder.getExtractions()).isEqualTo(extractions); }
@Test public void should_completeWithoutResult_whenAnalysisSucceeded_withoutAnyResult() throws Exception { final GiniVisionMultiPageDocument<GiniVisionDocument, GiniVisionDocumentError> multiPageDocument = mock(GiniVisionMultiPageDocument.class); final CompletableFuture<AnalysisNetworkRequestResult<GiniVisionMultiPageDocument>> analysisResultFuture = new CompletableFuture<>(); analysisResultFuture.complete(null); final NetworkRequestsManager networkRequestsManager = mock(NetworkRequestsManager.class); when(networkRequestsManager.analyze(any(GiniVisionMultiPageDocument.class))).thenReturn( analysisResultFuture); createGiniVision(networkRequestsManager); final CompletableFuture<AnalysisInteractor.ResultHolder> future = mAnalysisInteractor.analyzeMultiPageDocument(multiPageDocument); final AnalysisInteractor.ResultHolder resultHolder = future.get(); assertThat(resultHolder).isNull(); }