@Test
  public void testSearchPosts() {
    String phrase = "phrase";

    topicFetchService.searchByTitleAndContent(phrase, 50);

    Mockito.verify(searchDao)
        .searchByTitleAndContent(Matchers.anyString(), Matchers.<JCommunePageRequest>any());
  }
  @Test
  public void testGetTopic() throws NotFoundException {
    Topic expectedTopic = new Topic(user, "title");
    when(topicDao.isExist(999L)).thenReturn(true);
    when(topicDao.get(999L)).thenReturn(expectedTopic);

    int viewsCount = expectedTopic.getViews();

    Topic actualTopic = topicFetchService.get(999L);

    assertEquals(actualTopic.getViews(), viewsCount + 1);
    assertEquals(actualTopic, expectedTopic, "Topics aren't equal");
    verify(topicDao).isExist(999L);
    verify(topicDao).get(999L);
  }
  @Test
  public void testGetUnansweredTopics() {
    int pageNumber = 1;
    int pageSize = 20;
    List<Topic> expectedList = Collections.nCopies(2, new Topic(user, "title"));
    Page<Topic> expectedPage = new PageImpl<Topic>(expectedList);
    when(topicDao.getUnansweredTopics(Matchers.<JCommunePageRequest>any(), eq(user)))
        .thenReturn(expectedPage);
    user.setPageSize(pageSize);
    when(userService.getCurrentUser()).thenReturn(user);

    Page<Topic> actualPage = topicFetchService.getUnansweredTopics(pageNumber);
    assertNotNull(actualPage);
    assertEquals(actualPage, expectedPage);
  }
 /**
  * Deactivates branch updates subscription for the current user
  *
  * @param id identifies topic to unsubscribe from
  * @throws NotFoundException if no object is found for id given
  */
 @RequestMapping("topics/{id}/unsubscribe")
 @ResponseBody
 public ModelAndView unsubscribeFromTopic(
     @PathVariable Long id,
     @RequestHeader(value = "X-Requested-With", defaultValue = "NotAjax") String header)
     throws NotFoundException {
   Topic topic = topicFetchService.get(id);
   subscriptionService.toggleTopicSubscription(topic);
   if (header.equals("NotAjax")) {
     // redirect to new page
     return new ModelAndView("redirect:/topics/" + id);
   } else {
     // no redirecting, perform AJAX request
     return null;
   }
 }
  @Test
  public void testGetTopics() {
    int pageSize = 50;
    Branch branch = createBranch();
    Page<Topic> expectedPage = new PageImpl<Topic>(Collections.<Topic>emptyList());

    JCUser currentUser = new JCUser("current", null, null);
    currentUser.setPageSize(pageSize);
    when(userService.getCurrentUser()).thenReturn(currentUser);
    when(topicDao.getTopics(Matchers.any(Branch.class), Matchers.any(JCommunePageRequest.class)))
        .thenReturn(expectedPage);

    Page<Topic> actualPage = topicFetchService.getTopics(branch, pageSize, true);

    assertEquals(
        actualPage, expectedPage, "Service returned incorrect data for one page of topics");
    verify(topicDao).getTopics(Matchers.any(Branch.class), Matchers.any(JCommunePageRequest.class));
  }
  @Test
  public void testGetAllTopicsPastLastDay() throws NotFoundException {
    int pageNumber = 1;
    int pageSize = 20;
    List<Topic> expectedList = Collections.nCopies(2, new Topic(user, "title"));
    Page<Topic> expectedPage = new PageImpl<Topic>(expectedList);
    when(topicDao.getTopicsUpdatedSince(
            Matchers.<DateTime>any(), Matchers.<JCommunePageRequest>any(), eq(user)))
        .thenReturn(expectedPage);
    user.setPageSize(pageSize);
    when(userService.getCurrentUser()).thenReturn(user);

    Page<Topic> actualPage = topicFetchService.getRecentTopics(pageNumber);

    assertNotNull(actualPage);
    assertEquals(expectedPage, actualPage);
    verify(topicDao)
        .getTopicsUpdatedSince(
            Matchers.<DateTime>any(), Matchers.<JCommunePageRequest>any(), eq(user));
  }
  @Test(expectedExceptions = {NotFoundException.class})
  public void testGetTopicWithIncorrectId() throws NotFoundException {
    when(topicDao.isExist(333L)).thenReturn(false);

    topicFetchService.get(333L);
  }
  @Test
  public void testRebuildIndex() {
    topicFetchService.rebuildSearchIndex();

    Mockito.verify(searchDao).rebuildIndex();
  }
  @Test(dataProvider = "parameterSearchPostsWithEmptySearchPhrase")
  public void testSearchPostsWithEmptySearchPhrase(String phrase) {
    Page<Topic> searchResultPage = topicFetchService.searchByTitleAndContent(phrase, 50);

    Assert.assertTrue(!searchResultPage.hasContent(), "The search result must be empty.");
  }