SivaJSONValidationServiceClient implements ValidationService { @Override public String validateDocument(final String policy, final String report, final UploadedFile file) throws IOException { if (file == null) { throw new IOException("Invalid file object given"); } final String base64EncodedFile = file.getEncodedFile(); final ValidationRequest validationRequest = new ValidationRequest(); validationRequest.setDocument(base64EncodedFile); if (StringUtils.isNotBlank(policy)) validationRequest.setSignaturePolicy(policy); if (StringUtils.isNotBlank(report)) validationRequest.setReportType(report); final String filename = file.getFilename(); validationRequest.setFilename(filename); setValidationDocumentType(validationRequest, file); try { restTemplate.setErrorHandler(errorHandler); String fullUrl = properties.getServiceHost() + properties.getJsonServicePath(); return restTemplate.postForObject(fullUrl, validationRequest, String.class); } catch (ResourceAccessException ce) { String errorMessage = "Connection to web service failed. Make sure You have configured SiVa web service correctly"; return new ObjectMapper().writer().writeValueAsString(new ServiceError(GENERIC_ERROR_CODE, errorMessage)); } }  @Override String validateDocument(final String policy, final String report, final UploadedFile file); @Autowired void setProperties(SivaRESTWebServiceConfigurationProperties properties); @Autowired void setRestTemplate(final RestTemplate restTemplate); @Autowired void setErrorHandler(final SivaValidationServiceErrorHandler errorHandler);  }
@Test public void validRequestReturnsCorrectValidationResult() throws Exception { final String mockResponse = mockServiceResponse(); final String fileContents = "Hello Testing World"; final String filename = "testing.bdoc"; final UploadedFile inputFile = TestFileUtils.generateUploadFile(testingFolder, filename, fileContents); final String result = validationService.validateDocument("", "", inputFile); assertEquals(mockResponse, result); verify(restTemplate).postForObject(anyString(), validationRequestCaptor.capture(), any()); assertEquals(filename, validationRequestCaptor.getValue().getFilename()); assertEquals(Base64Utils.encodeToString(fileContents.getBytes()), validationRequestCaptor.getValue().getDocument()); }
@Test public void invalidFileTypeGivenRequestDocumentTypeIsNull() throws Exception { mockServiceResponse(); final UploadedFile file = TestFileUtils.generateUploadFile(testingFolder, "testing.exe", "error in file"); validationService.validateDocument("POLv3","Simple", file); verify(restTemplate).postForObject(anyString(), validationRequestCaptor.capture(), any()); assertEquals(null, validationRequestCaptor.getValue().getDocumentType()); }
@Test public void inputFileIsNullThrowsException() throws Exception { exception.expect(IOException.class); exception.expectMessage("Invalid file object given"); validationService.validateDocument(null, null, null); }
@Test public void givenRestServiceIsUnreachableReturnsGenericSystemError() throws Exception { final UploadedFile file = TestFileUtils.generateUploadFile(testingFolder, "testing.bdoc", "simple file"); BDDMockito.given(restTemplate.postForObject(anyString(), any(ValidationRequest.class), any())) .willThrow(new ResourceAccessException("Failed to connect to SiVa REST")); String result = validationService.validateDocument("", "", file); verify(restTemplate).postForObject(anyString(), validationRequestCaptor.capture(), any()); Assertions.assertThat(result).contains("errorCode"); Assertions.assertThat(result).contains("errorMessage"); }