/** * 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); }
@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); }
/** * Check creating new article with get user auth context * * @throws Exception */ @Test @PrepareForTest({SecurityContextHolder.class}) public void testCreateArticle() throws Exception { Article article = new Article(); PowerMockito.mockStatic(SecurityContextHolder.class); PowerMockito.when(SecurityContextHolder.getContext()).thenReturn(securityContext); Mockito.when(securityContext.getAuthentication()).thenReturn(authentication); Mockito.when(authentication.getPrincipal()).thenReturn(userDetail); Mockito.when(userDetail.getUsername()).thenReturn("userName"); Mockito.when(userService.getAuthorizedUser()).thenReturn(user); articleService.createArticle(article); ArgumentCaptor<Article> argument = ArgumentCaptor.forClass(Article.class); Mockito.verify(articleDao, Mockito.times(1)).createArticle(argument.capture()); Assert.assertEquals(argument.getValue().getUserOwner(), user); }