@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); }
@After public void tearDown() throws Exception { // Удаляем тестовый фаил Files.delete(Paths.get(pathTempFile)); // Удаляем тестовые папки и файлы Files.walkFileTree( Paths.get(storagePath), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); }
/** * 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); }