Пример #1
0
  @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 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);
  }
  @Test
  public void testRemoveArticle() throws Exception {

    MessageResponseServiceImpl mrs = new MessageResponseServiceImpl();

    // Successful case
    mrs.setMessageResponse(new MessageResponse());
    articleService.setMessageResponseService(mrs);

    Article articleSuccess = new Article();
    articleSuccess.setTitle("test success");
    articleSuccess.setDeleted(true);

    Mockito.when(articleDao.removeArticle(articleSuccess)).thenReturn(articleSuccess);

    MessageResponse messageSuccess = articleService.removeArticle(articleSuccess);

    // Fail case
    mrs.setMessageResponse(new MessageResponse());
    articleService.setMessageResponseService(mrs);

    Article articleFail = new Article();
    articleFail.setTitle("test fail");
    articleFail.setDeleted(false);

    Mockito.when(articleDao.removeArticle(articleFail)).thenReturn(articleFail);

    MessageResponse messageFail = articleService.removeArticle(articleFail);

    Assert.assertTrue(messageSuccess.getCode() == 1 && messageFail.getCode() == 0);
  }
Пример #4
0
  @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)));
  }
Пример #5
0
  @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));
  }
  /**
   * 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
  @PrepareForTest({UtilsHibernate.class})
  public void testGetArticles() throws Exception {

    PowerMockito.mockStatic(UtilsHibernate.class);

    List<Article> articleList = new ArrayList<>();

    Article article1 = new Article();
    article1.setTitle("article1");
    articleList.add(article1);

    Article article2 = new Article();
    article2.setTitle("article2");
    articleList.add(article2);

    Mockito.when(
            articleDao.getAllArticles(
                2, 1, UtilsHibernate.getOrderByString("asc", "title"), "title", 1))
        .thenReturn(articleList);

    List<Article> articles = articleService.getArticles(2, 1, "asc", "title", 1);

    Assert.assertTrue(
        articles.get(0).getTitle().equals("article1")
            && articles.get(1).getTitle().equals("article2"));
  }
Пример #8
0
  @Test
  public void enrollStudentTest() throws StudentException, ServiceException, PaymentException {

    Mockito.when(
            studentController.newStudent(
                "Ana Julia Costa",
                cpf,
                rg,
                date,
                email,
                address,
                phone1,
                phone2,
                "Maria Julia",
                "Julio Costa"))
        .thenReturn(studentMock);
    Mockito.when(serviceController.newService(studentMock, courses, packages, value))
        .thenReturn(serviceMock);
    Mockito.when(paymentController.newPayment(serviceMock, 1, 1, 1)).thenReturn(paymentMock);

    try {
      enrollController.enrollStudent(
          "Ana Julia Costa",
          cpf,
          rg,
          date,
          email,
          address,
          phone1,
          phone2,
          "Maria Julia",
          "Julio Costa",
          courses,
          packages,
          1,
          1,
          2,
          value);
    } catch (StudentException | ServiceException | PaymentException e) {
      fail("Should not throw this exception: " + e.getMessage());
    }
  }
  @Test
  public void testUpdateArticle() throws Exception {

    Article article = new Article();
    article.setTitle("test");

    Mockito.when(articleDao.updateArticle(article)).thenReturn(article);

    Article articleResult = articleService.updateArticle(article);

    Assert.assertEquals(articleResult, article);
  }
  /**
   * 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);
  }
  @Test
  public void testGetArticle() throws Exception {

    Article article = new Article();
    article.setArticleId(1);
    article.setTitle("test title");

    Mockito.when(articleDao.getArticle(article)).thenReturn(article);

    Article responseArticle = articleService.getArticle(article);

    Assert.assertEquals(article.getArticleId(), responseArticle.getArticleId());
  }
  @Test
  public void testGetArticlesByUser() throws Exception {

    Article article = new Article();
    article.setArticleId(1);
    article.setTitle("test title");

    List<Article> articles = new ArrayList<>();
    articles.add(article);

    Mockito.when(articleDao.getArticlesByUserOwner(user)).thenReturn(articles);

    List<Article> responseArticles = articleService.getArticlesByUser(user);

    Assert.assertEquals(responseArticles.get(0).getArticleId(), 1);
  }
Пример #13
0
 @Before
 public void init() {
   Mockito.when(databaseProperties.getJooqDialect()).thenReturn("POSTGRES_9_3");
 }