@Test public void handleTaskCompleted_abbyyApiCallFails_exceptionThrown() throws Exception { final String errMsg = "This is the error message"; ProcessImageInput requestMock = mockAbbyyRequest(); when(requestMock.getExportFormats()) .thenReturn(Arrays.asList(ExportFormat.TXT, ExportFormat.XML, ExportFormat.PDF_SEARCHABLE)); Path destinationFileMock = mock(Path.class); when(destinationFileMock.toAbsolutePath()).thenReturn(Paths.get(StringUtils.EMPTY)); when(requestMock.getDestinationFile()).thenReturn(destinationFileMock); AbbyyResponse responseMock = mockAbbyyResponse(); when(responseMock.getResultUrls()).thenReturn(Arrays.asList("txt", "xml", "pdf")); Map<String, String> resultsDummy = new HashMap<>(); PowerMockito.whenNew(FileWriter.class).withAnyArguments().thenReturn(mock(FileWriter.class)); when(this.abbyyApiMock.getResult(eq(requestMock), anyString(), any(ExportFormat.class), anyString(), anyBoolean())) .thenThrow(new TimeoutException(errMsg)); try { this.sut.handleTaskCompleted(requestMock, responseMock, resultsDummy); fail(); } catch (AbbyySdkException ex) { assertTrue(ex.getMessage().contains(TimeoutException.class.getSimpleName())); assertTrue(ex.getMessage().contains(errMsg)); assertTrue(ex.getMessage().contains(ExportFormat.TXT.toString())); assertTrue(ex.getMessage().contains(ExportFormat.XML.toString())); assertTrue(ex.getMessage().contains(ExportFormat.PDF_SEARCHABLE.toString())); } }
@Override void handleTaskCompleted(@NotNull ProcessImageInput input, @NotNull AbbyyResponse response, @NotNull Map<String, String> results) throws Exception { super.handleTaskCompleted(input, response, results); Map<ExportFormat, String> failure = new HashMap<>(); String result; for (ExportFormat exportFormat : input.getExportFormats()) { try { switch (exportFormat) { case XML: result = getClearTextResult(input, response, ExportFormat.XML, xmlResultValidator); results.put(OutputNames.XML_RESULT, EncodingUtils.escapePotentialMaliciousCharsInXml(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case TXT: result = getClearTextResult(input, response, ExportFormat.TXT, txtResultValidator); results.put(OutputNames.TXT_RESULT, EncodingUtils.escapePotentialMaliciousCharsInTxt(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case PDF_SEARCHABLE: result = getBinaryResult(input, response, ExportFormat.PDF_SEARCHABLE, pdfResultValidator); results.put(OutputNames.PDF_URL, result); if (input.getDestinationFile() != null) { String targetPath = downloadOnDisk(input, result, exportFormat); ValidationException validationEx = pdfResultValidator.validateAfterDownload(input, targetPath); if (validationEx != null) { Files.delete(Paths.get(targetPath)); throw validationEx; } } break; } } catch (Exception ex) { failure.put(exportFormat, ex.toString()); } } if (!failure.isEmpty()) { StringBuilder errBuilder = new StringBuilder(ExceptionMsgs.ERROR_RETRIEVING_EXPORT_FORMATS); for (ExportFormat exportFormat : failure.keySet()) { errBuilder.append('\n') .append("For '" + exportFormat.toString() + "': " + failure.get(exportFormat)); } throw new AbbyySdkException(errBuilder.toString()); } }
ProcessImageService extends AbbyyService<ProcessImageInput> { @Override void handleTaskCompleted(@NotNull ProcessImageInput input, @NotNull AbbyyResponse response, @NotNull Map<String, String> results) throws Exception { super.handleTaskCompleted(input, response, results); Map<ExportFormat, String> failure = new HashMap<>(); String result; for (ExportFormat exportFormat : input.getExportFormats()) { try { switch (exportFormat) { case XML: result = getClearTextResult(input, response, ExportFormat.XML, xmlResultValidator); results.put(OutputNames.XML_RESULT, EncodingUtils.escapePotentialMaliciousCharsInXml(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case TXT: result = getClearTextResult(input, response, ExportFormat.TXT, txtResultValidator); results.put(OutputNames.TXT_RESULT, EncodingUtils.escapePotentialMaliciousCharsInTxt(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case PDF_SEARCHABLE: result = getBinaryResult(input, response, ExportFormat.PDF_SEARCHABLE, pdfResultValidator); results.put(OutputNames.PDF_URL, result); if (input.getDestinationFile() != null) { String targetPath = downloadOnDisk(input, result, exportFormat); ValidationException validationEx = pdfResultValidator.validateAfterDownload(input, targetPath); if (validationEx != null) { Files.delete(Paths.get(targetPath)); throw validationEx; } } break; } } catch (Exception ex) { failure.put(exportFormat, ex.toString()); } } if (!failure.isEmpty()) { StringBuilder errBuilder = new StringBuilder(ExceptionMsgs.ERROR_RETRIEVING_EXPORT_FORMATS); for (ExportFormat exportFormat : failure.keySet()) { errBuilder.append('\n') .append("For '" + exportFormat.toString() + "': " + failure.get(exportFormat)); } throw new AbbyySdkException(errBuilder.toString()); } } }
ProcessImageService extends AbbyyService<ProcessImageInput> { @Override void handleTaskCompleted(@NotNull ProcessImageInput input, @NotNull AbbyyResponse response, @NotNull Map<String, String> results) throws Exception { super.handleTaskCompleted(input, response, results); Map<ExportFormat, String> failure = new HashMap<>(); String result; for (ExportFormat exportFormat : input.getExportFormats()) { try { switch (exportFormat) { case XML: result = getClearTextResult(input, response, ExportFormat.XML, xmlResultValidator); results.put(OutputNames.XML_RESULT, EncodingUtils.escapePotentialMaliciousCharsInXml(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case TXT: result = getClearTextResult(input, response, ExportFormat.TXT, txtResultValidator); results.put(OutputNames.TXT_RESULT, EncodingUtils.escapePotentialMaliciousCharsInTxt(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case PDF_SEARCHABLE: result = getBinaryResult(input, response, ExportFormat.PDF_SEARCHABLE, pdfResultValidator); results.put(OutputNames.PDF_URL, result); if (input.getDestinationFile() != null) { String targetPath = downloadOnDisk(input, result, exportFormat); ValidationException validationEx = pdfResultValidator.validateAfterDownload(input, targetPath); if (validationEx != null) { Files.delete(Paths.get(targetPath)); throw validationEx; } } break; } } catch (Exception ex) { failure.put(exportFormat, ex.toString()); } } if (!failure.isEmpty()) { StringBuilder errBuilder = new StringBuilder(ExceptionMsgs.ERROR_RETRIEVING_EXPORT_FORMATS); for (ExportFormat exportFormat : failure.keySet()) { errBuilder.append('\n') .append("For '" + exportFormat.toString() + "': " + failure.get(exportFormat)); } throw new AbbyySdkException(errBuilder.toString()); } } ProcessImageService();   ProcessImageService(@NotNull AbbyyInputValidator<ProcessImageInput> requestValidator,
                        @Nullable AbbyyResultValidator xmlResultValidator,
                        @Nullable AbbyyResultValidator txtResultValidator,
                        @Nullable AbbyyResultValidator pdfResultValidator,
                        @NotNull AbbyyApi abbyyApi); }
ProcessImageService extends AbbyyService<ProcessImageInput> { @Override void handleTaskCompleted(@NotNull ProcessImageInput input, @NotNull AbbyyResponse response, @NotNull Map<String, String> results) throws Exception { super.handleTaskCompleted(input, response, results); Map<ExportFormat, String> failure = new HashMap<>(); String result; for (ExportFormat exportFormat : input.getExportFormats()) { try { switch (exportFormat) { case XML: result = getClearTextResult(input, response, ExportFormat.XML, xmlResultValidator); results.put(OutputNames.XML_RESULT, EncodingUtils.escapePotentialMaliciousCharsInXml(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case TXT: result = getClearTextResult(input, response, ExportFormat.TXT, txtResultValidator); results.put(OutputNames.TXT_RESULT, EncodingUtils.escapePotentialMaliciousCharsInTxt(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case PDF_SEARCHABLE: result = getBinaryResult(input, response, ExportFormat.PDF_SEARCHABLE, pdfResultValidator); results.put(OutputNames.PDF_URL, result); if (input.getDestinationFile() != null) { String targetPath = downloadOnDisk(input, result, exportFormat); ValidationException validationEx = pdfResultValidator.validateAfterDownload(input, targetPath); if (validationEx != null) { Files.delete(Paths.get(targetPath)); throw validationEx; } } break; } } catch (Exception ex) { failure.put(exportFormat, ex.toString()); } } if (!failure.isEmpty()) { StringBuilder errBuilder = new StringBuilder(ExceptionMsgs.ERROR_RETRIEVING_EXPORT_FORMATS); for (ExportFormat exportFormat : failure.keySet()) { errBuilder.append('\n') .append("For '" + exportFormat.toString() + "': " + failure.get(exportFormat)); } throw new AbbyySdkException(errBuilder.toString()); } } ProcessImageService();   ProcessImageService(@NotNull AbbyyInputValidator<ProcessImageInput> requestValidator,
                        @Nullable AbbyyResultValidator xmlResultValidator,
                        @Nullable AbbyyResultValidator txtResultValidator,
                        @Nullable AbbyyResultValidator pdfResultValidator,
                        @NotNull AbbyyApi abbyyApi);  }
ProcessImageService extends AbbyyService<ProcessImageInput> { @Override void handleTaskCompleted(@NotNull ProcessImageInput input, @NotNull AbbyyResponse response, @NotNull Map<String, String> results) throws Exception { super.handleTaskCompleted(input, response, results); Map<ExportFormat, String> failure = new HashMap<>(); String result; for (ExportFormat exportFormat : input.getExportFormats()) { try { switch (exportFormat) { case XML: result = getClearTextResult(input, response, ExportFormat.XML, xmlResultValidator); results.put(OutputNames.XML_RESULT, EncodingUtils.escapePotentialMaliciousCharsInXml(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case TXT: result = getClearTextResult(input, response, ExportFormat.TXT, txtResultValidator); results.put(OutputNames.TXT_RESULT, EncodingUtils.escapePotentialMaliciousCharsInTxt(result)); if (input.getDestinationFile() != null) { saveClearTextOnDisk(input, result, exportFormat); } break; case PDF_SEARCHABLE: result = getBinaryResult(input, response, ExportFormat.PDF_SEARCHABLE, pdfResultValidator); results.put(OutputNames.PDF_URL, result); if (input.getDestinationFile() != null) { String targetPath = downloadOnDisk(input, result, exportFormat); ValidationException validationEx = pdfResultValidator.validateAfterDownload(input, targetPath); if (validationEx != null) { Files.delete(Paths.get(targetPath)); throw validationEx; } } break; } } catch (Exception ex) { failure.put(exportFormat, ex.toString()); } } if (!failure.isEmpty()) { StringBuilder errBuilder = new StringBuilder(ExceptionMsgs.ERROR_RETRIEVING_EXPORT_FORMATS); for (ExportFormat exportFormat : failure.keySet()) { errBuilder.append('\n') .append("For '" + exportFormat.toString() + "': " + failure.get(exportFormat)); } throw new AbbyySdkException(errBuilder.toString()); } } ProcessImageService();   ProcessImageService(@NotNull AbbyyInputValidator<ProcessImageInput> requestValidator,
                        @Nullable AbbyyResultValidator xmlResultValidator,
                        @Nullable AbbyyResultValidator txtResultValidator,
                        @Nullable AbbyyResultValidator pdfResultValidator,
                        @NotNull AbbyyApi abbyyApi);   }