ClinicalDocumentController implements InitializingBean { String scanMultipartFile(MultipartFile file){ Boolean isItClean=null; try (InputStream inputStream = file.getInputStream()){ isItClean = clamAVUtil.fileScanner(inputStream); } catch (ClamAVClientNotAvailableException e){ logger.error(e.getMessage()); return "error"; } catch (Exception e) { logger.error(e.getMessage()); return "error"; } return isItClean.toString(); } ClinicalDocumentController(
			ClinicalDocumentService clinicalDocumentService,
			PatientService patientService,
			ClinicalDocumentTypeCodeService clinicalDocumentTypeCodeService,
			UserContext userContext,
			AccessReferenceMapper accessReferenceMapper,
			ClamAVService clamAVUtil, RecaptchaService recaptchaUtil,
			EventService eventService, XmlValidation xmlValidator); @SuppressWarnings("unused") private  ClinicalDocumentController(); @RequestMapping("/medicalinfo.html") String documentHome(Model model); @RequestMapping("/clinicaldocuments.html") String showSecureClinicalDocuments(
			Model model,
			@RequestParam(value = "notify", required = false) String notification); @RequestMapping(value = "/clinicaldocuments.html", method = RequestMethod.POST) String uploadClinicalDocumentsSecurely(
			HttpServletRequest request,
			@ModelAttribute("document") ClinicalDocumentDto clinicalDocumentDto,
			@RequestParam("file") MultipartFile file,
			@RequestParam("name") String documentName,
			@RequestParam("description") String description,
			@RequestParam("documentType") String documentTypeCode); @RequestMapping(value = "/downloaddoc.html", method = RequestMethod.POST) String download(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("download_id") String documentId); @RequestMapping(value = "/deletedoc.html", method = RequestMethod.POST) String remove(@RequestParam("delete_id") String documentId); @Override void afterPropertiesSet(); static final String C32_CDA_XSD_PATH; static final String C32_CDA_XSD_NAME; }
@Test public void testSecureClinicalDocumentsUploadWhenAllChecksSucced() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); doReturn(true).when(xmlValidator).validate(any(InputStream.class)); doReturn("true").when(sut).scanMultipartFile(file); doReturn(false).when(clinicalDocumentService).isDocumentOversized(file); doReturn(true).when(clinicalDocumentService).isDocumentExtensionPermitted(file); doReturn(true).when(recaptchaUtil).checkAnswer(anyString(), anyString(), anyString()); mockMvc.perform(fileUpload("/patients/clinicaldocuments.html").file(file) .param("name", "mocked_name") .param("description", "mocked_description") .param("documentType", "mocked_type") .param("recaptcha_challenge_field", "recaptcha_challenge_field") .param("recaptcha_response_field", "recaptcha_response_field")) .andExpect(view().name("redirect:/patients/clinicaldocuments.html")); }
@Test public void testSecureClinicalDocumentsUploadWhenFileIsOversized() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); doReturn("true").when(sut).scanMultipartFile(file); doReturn(true).when(clinicalDocumentService).isDocumentOversized(file); doReturn(true).when(clinicalDocumentService).isDocumentExtensionPermitted(file); doReturn(true).when(recaptchaUtil).checkAnswer(anyString(), anyString(), anyString()); mockMvc.perform(fileUpload("/patients/clinicaldocuments.html").file(file) .param("name", "mocked_name") .param("description", "mocked_description") .param("documentType", "mocked_type")) .andExpect(view().name("redirect:/patients/clinicaldocuments.html?notify=size_over_limits")); }
@Test public void testSecureClinicalDocumentsUploadWhenFileExtensionIsNotPermitted() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); doReturn("true").when(sut).scanMultipartFile(file); doReturn(false).when(clinicalDocumentService).isDocumentOversized(file); doReturn(false).when(clinicalDocumentService).isDocumentExtensionPermitted(file); doReturn(true).when(recaptchaUtil).checkAnswer(anyString(), anyString(), anyString()); mockMvc.perform(fileUpload("/patients/clinicaldocuments.html").file(file) .param("name", "mocked_name") .param("description", "mocked_description") .param("documentType", "mocked_type")) .andExpect(view().name("redirect:/patients/clinicaldocuments.html?notify=extension_not_permitted")); }
@Test public void testSecureClinicalDocumentsUploadWhenWrongCaptchaIsEnterred() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); doReturn("true").when(sut).scanMultipartFile(file); doReturn(false).when(clinicalDocumentService).isDocumentOversized(file); doReturn(true).when(clinicalDocumentService).isDocumentExtensionPermitted(file); doReturn(false).when(recaptchaUtil).checkAnswer(anyString(), anyString(), anyString()); mockMvc.perform(fileUpload("/patients/clinicaldocuments.html").file(file) .param("name", "mocked_name") .param("description", "mocked_description") .param("documentType", "mocked_type")) .andExpect(view().name("redirect:/patients/clinicaldocuments.html?notify=wrong_captcha")); }
@Test public void testScanMultipartFileWhenFileIsClean() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); doReturn(true).when(clamAVUtil).fileScanner(any(InputStream.class)); assertTrue(clinicalDocumentController.scanMultipartFile(file).toString().equals("true")); }
@Test public void testScanMultipartFileWhenFileIsNotClean() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "orig", null, "bar".getBytes()); doReturn(false).when(clamAVUtil).fileScanner(any(InputStream.class)); assertFalse(clinicalDocumentController.scanMultipartFile(file).toString().equals("true")); }