@Test public void testGetUsersWithAuthority() throws SQLException { String expectedQuery = "select \"public\".\"users\".\"username\", \"public\".\"users\".\"locked\", \"public\".\"authorities\".\"authority\", count(\"public\".\"users\".\"username\") over () as \"count\" from \"public\".\"users\" join \"public\".\"authorities\" on \"public\".\"users\".\"username\" = \"public\".\"authorities\".\"username\" where \"public\".\"authorities\".\"authority\" = 'ROLE_ADMIN' order by \"public\".\"users\".\"username\" asc limit 5 offset 0"; Pageable pageable = Mockito.mock(Pageable.class); ResultSet resultSet = Mockito.mock(ResultSet.class); Mockito.when(pageable.getPageNumber()).thenReturn(0); Mockito.when(pageable.getPageSize()).thenReturn(5); Mockito.when(resultSet.getInt(PagingConstants.COUNT)).thenReturn(1); Mockito.when(resultSet.getString("username")).thenReturn("admin"); RESTPage<UserDTO> page = userDao.getUsersWithAuthority("ROLE_ADMIN", pageable); ArgumentCaptor<PagingRowCallbackHandler> pagingRowCallbackHandlerCaptor = ArgumentCaptor.forClass(PagingRowCallbackHandler.class); Mockito.verify(jdbcTemplate) .query(Matchers.eq(expectedQuery), pagingRowCallbackHandlerCaptor.capture()); PagingRowCallbackHandler pagingRowCallbackHandler = pagingRowCallbackHandlerCaptor.getValue(); pagingRowCallbackHandler.processRow(resultSet); Mockito.verify(resultSet).getInt(PagingConstants.COUNT); Mockito.verify(resultSet).getString("username"); Assert.assertEquals(1, page.getContentSize()); Assert.assertEquals("admin", page.getContent().get(0).getUsername()); }
@Test public void recruitersCanSeeJobseekersByJobAndDate() { setupRepositories(); setupActors(); JobPosting developerPosting = recruiter.post(developerJob).to(jobRepository); JobPosting architectPosting = recruiter.post(architectJob).to(jobRepository); TimeServer timeServerOne = Mockito.mock(TimeServer.class); TimeServer timeServerTwo = Mockito.mock(TimeServer.class); Date dayOne = null; Date dayTwo = null; try { dayOne = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2013-05-01 12:30:00"); dayTwo = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2013-07-04 12:30:00"); } catch (ParseException e) { fail(); } Mockito.when(timeServerOne.getCurrentTime()).thenReturn(dayOne); Mockito.when(timeServerTwo.getCurrentTime()).thenReturn(dayTwo); ApplicationProcessor appProcessorOne = new ApplicationProcessor(appRepo, timeServerOne); ApplicationProcessor appProcessorTwo = new ApplicationProcessor(appRepo, timeServerTwo); boolean applyStatus; applyStatus = jobseekerTom.applyFor(developerPosting).to(appProcessorOne); assertTrue(applyStatus); applyStatus = jobseekerHarry.applyFor(developerPosting).to(appProcessorOne); assertTrue(applyStatus); applyStatus = jobseekerTom.applyFor(architectPosting).to(appProcessorTwo); assertTrue(applyStatus); applyStatus = jobseekerDick.applyFor(architectPosting).to(appProcessorTwo); assertTrue(applyStatus); Applications appsOnDayOne = recruiter.getApplications().filterBy(dayOne).from(appRepo); ApplicationsDisplayer appsDisplayer = Mockito.mock(ApplicationsDisplayer.class); appsOnDayOne.displayTo(appsDisplayer); Mockito.verify(appsDisplayer) .displayApplications(Mockito.argThat(new SetOfTwoAppsWithDates(dayOne))); Applications appsOnDayTwoForArchitect = recruiter.getApplications().filterBy(architectPosting).filterBy(dayTwo).from(appRepo); appsDisplayer = Mockito.mock(ApplicationsDisplayer.class); appsOnDayTwoForArchitect.displayTo(appsDisplayer); Mockito.verify(appsDisplayer) .displayApplications( Mockito.argThat(new SetOfTwoAppsWithJobPostingsAndDates(dayTwo, architectPosting))); }
@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 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 recruitersAreDisplayedByName() { Recruiter recruiter = Recruiter.named("George"); RecruiterDisplayer recDisplayer = Mockito.mock(RecruiterDisplayer.class); recruiter.displayTo(recDisplayer); DisplayableName name = new Name("George"); Mockito.verify(recDisplayer).displayRecruiter(name); }
@Test public void recruiterCanPostJob() { Recruiter recruiter = Recruiter.named("George"); Job developerJob = ATSJob.titled("Developer"); JobRepository jobRepository = Mockito.mock(JobRepository.class); recruiter.post(developerJob).to(jobRepository); JobPosting posting = new JobPosting(recruiter, developerJob); Mockito.verify(jobRepository).add(posting); }
@Test public void testGetUsersWithAuthority_page2() { String expectedQuery = "select \"public\".\"users\".\"username\", \"public\".\"users\".\"locked\", \"public\".\"authorities\".\"authority\", count(\"public\".\"users\".\"username\") over () as \"count\" from \"public\".\"users\" join \"public\".\"authorities\" on \"public\".\"users\".\"username\" = \"public\".\"authorities\".\"username\" where \"public\".\"authorities\".\"authority\" = 'ROLE_ADMIN' order by \"public\".\"users\".\"username\" asc limit 5 offset 5"; Pageable pageable = Mockito.mock(Pageable.class); Mockito.when(pageable.getPageNumber()).thenReturn(1); Mockito.when(pageable.getPageSize()).thenReturn(5); userDao.getUsersWithAuthority("ROLE_ADMIN", pageable); Mockito.verify(jdbcTemplate) .query(Matchers.eq(expectedQuery), Matchers.any(PagingRowCallbackHandler.class)); }
@Test public void recruitersCanSeeJobseekersByJob() { setupActors(); setupRepositories(); JobPosting developerPosting = recruiter.post(developerJob).to(jobRepository); JobPosting architectPosting = recruiter.post(architectJob).to(jobRepository); boolean applyStatus; applyStatus = jobseekerTom.applyFor(developerPosting).to(appProcessor); assertTrue(applyStatus); applyStatus = jobseekerHarry.applyFor(developerPosting).to(appProcessor); assertTrue(applyStatus); applyStatus = jobseekerTom.applyFor(architectPosting).to(appProcessor); assertTrue(applyStatus); applyStatus = jobseekerDick.applyFor(architectPosting).to(appProcessor); assertTrue(applyStatus); Applications developerApps = recruiter.getApplications().filterBy(developerPosting).from(appRepo); ApplicationsDisplayer appsDisplayer = Mockito.mock(ApplicationsDisplayer.class); developerApps.displayTo(appsDisplayer); Mockito.verify(appsDisplayer) .displayApplications( Mockito.argThat(new SetOfTwoAppsWithJobSeekers(jobseekerTom, jobseekerHarry))); Applications architectApps = recruiter.getApplications().filterBy(architectPosting).from(appRepo); appsDisplayer = Mockito.mock(ApplicationsDisplayer.class); architectApps.displayTo(appsDisplayer); Mockito.verify(appsDisplayer) .displayApplications( Mockito.argThat(new SetOfTwoAppsWithJobSeekers(jobseekerTom, jobseekerDick))); }
@Test public void recruitersCanDisplayJobsTheyPosted() { setupActors(); JobRepository jobRepository = new JobRepository(); JobPosting developerPosting = recruiter.post(developerJob).to(jobRepository); JobPosting architectPosting = recruiter.post(architectJob).to(jobRepository); JobPosting programmerPosting = recruiter.post(programmerJob).to(jobRepository); JobPostings jobPostings = recruiter.getPostedJobs().from(jobRepository); JobPostingsDisplayer postingsDisplayer = Mockito.mock(JobPostingsDisplayer.class); jobPostings.displayTo(postingsDisplayer); Mockito.verify(postingsDisplayer) .displayJobPostings( Matchers.argThat( new SetOfThreeJobPostings(developerPosting, architectPosting, programmerPosting))); }
/** * 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); }