@Test public void testRemoveAttachment() throws Exception { // CREATING ATTACHMENT FOR REMOVE List<MultipartFile> files = new ArrayList<>(); files.add(multipartFile); Mockito.when(attachmentFactory.newAttachment()).thenReturn(null); attachmentService.setAttachment(new Attachment()); attachmentService.setMessageResponse(new MessageResponse()); MessageResponse messageResponse1 = attachmentService.createAttachment(files); Mockito.verify(attachmentDao, Mockito.times(1)).createAttachments(argumentCaptor.capture()); Attachment attachment = argumentCaptor.getValue().get(0); // REMOVE ATTACHMENT Mockito.when(attachmentDao.removeAttachment(attachment.getAttachmentId())) .thenReturn(attachment); MessageResponse messageResponse = attachmentService.removeAttachment(attachment.getAttachmentId()); boolean isExistPreview = Files.exists(Paths.get(storagePath + attachment.getPreviewPath())); boolean isExistImage = Files.exists(Paths.get(storagePath + attachment.getFilePathInStorage())); Assert.assertTrue(!isExistPreview && !isExistImage && messageResponse.getCode() == 1); }
@Before public void setUp() throws Exception { // Download file for test URL website = new URL("http://archive.org/web/images/logo_wayback_210x77.png"); ReadableByteChannel resFile = Channels.newChannel(website.openStream()); // Create file File file = new File(pathTempFile); FileOutputStream fos = new FileOutputStream(pathTempFile); fos.getChannel().transferFrom(resFile, 0, Long.MAX_VALUE); // Save to MultipartFile FileInputStream input = new FileInputStream(file); multipartFile = new MockMultipartFile("file", file.getName(), "image/png", IOUtils.toByteArray(input)); fos.close(); input.close(); // Set paths attachmentService.setStoragePath(storagePath); attachmentService.setPathDefaultPreview(pathDefaultPreview); }
/** * Check uploading files and create attachments * * @throws Exception */ @Test public void testCreateAttachment() throws Exception { List<MultipartFile> files = new ArrayList<>(); files.add(multipartFile); Mockito.when(attachmentFactory.newAttachment()).thenReturn(null); attachmentService.setAttachment(new Attachment()); attachmentService.setMessageResponse(new MessageResponse()); MessageResponse messageResponse = attachmentService.createAttachment(files); Mockito.verify(attachmentDao, Mockito.times(1)).createAttachments(argumentCaptor.capture()); Attachment attachment = argumentCaptor.getValue().get(0); boolean isExistPreview = Files.exists(Paths.get(storagePath + attachment.getPreviewPath())); boolean isExistImage = Files.exists(Paths.get(storagePath + attachment.getFilePathInStorage())); Files.delete(Paths.get(storagePath + attachment.getPreviewPath())); Files.delete(Paths.get(storagePath + attachment.getFilePathInStorage())); Assert.assertTrue( attachment.getMimeType().equals("image/png") && messageResponse.getCode() == 0 && isExistPreview && isExistImage); }