/**
   * Make sure the correct search is performed when theh search request does contain "update date"
   * critera and we have determined that they be included in the comment search.
   *
   * @throws Exception indicates some unexpected failure.
   */
  @Test
  public void testGetRecentCommentsIssuesUpdatedFromToDate() throws Exception {
    final String beforeDuration = "-3d";
    final Date beforeDate = dateSupport.convertToDate(beforeDuration);
    final Date afterDate = createDate(1981, Calendar.JANUARY, 12);
    final User user = new MockUser("me");

    final JqlQueryBuilder builder = JqlQueryBuilder.newBuilder();
    builder
        .where()
        .updated()
        .ltEq(beforeDuration)
        .and()
        .updatedAfter(afterDate)
        .and()
        .priority("major");
    final SearchRequest request = new SearchRequest(builder.buildQuery());

    final List<Long> issueIds = CollectionBuilder.newBuilder(6L, 3L, 13484L, 11111134L).asList();
    final BooleanQuery query = createIssueQuery(issueIds);

    final BooleanQuery dateQuery = new BooleanQuery();
    dateQuery.add(createBeforeQuery(beforeDate), BooleanClause.Occur.MUST);
    dateQuery.add(createAfterQuery(afterDate), BooleanClause.Occur.MUST);

    query.add(dateQuery, BooleanClause.Occur.MUST);

    _testGetRecentComments(issueIds, request, user, true, query);
  }
  /**
   * Make sure a pure issue key search is given when issues are found. Also, make sure there are no
   * "updated date" critera added to the query.
   *
   * @throws Exception indicates some unexpected failure.
   */
  @Test
  public void testGetRecentCommentsIssues() throws Exception {

    final JqlQueryBuilder builder = JqlQueryBuilder.newBuilder();
    builder.where().updatedBetween(createDate(1996, Calendar.SEPTEMBER, 11), null);
    final SearchRequest request = new SearchRequest(builder.buildQuery());

    final List<Long> issueIds = CollectionBuilder.newBuilder(1L, 2L, 4L, 5L).asList();
    _testGetRecentComments(issueIds, request, null, false, createIssueQuery(issueIds));
  }
  @Override
  public SearchRequest getSearchUrlSuffix(TimePeriod value, SearchRequest searchRequest) {
    TimePeriod timePeriod = value;
    Date startDate = timePeriod.getStart();
    Date endDate = new Date(timePeriod.getEnd().getTime());

    // copy the old searchrequest's query
    JqlQueryBuilder builder = JqlQueryBuilder.newBuilder(searchRequest.getQuery());
    builder.where().defaultAnd().addDateRangeCondition(documentConstant, startDate, endDate);

    return new SearchRequest(builder.buildQuery(), searchRequest.getOwnerUserName(), null, null);
  }
  /**
   * Make sure the correct search is performed when theh search request does contain "update date"
   * critera and we have determined that they be included in the comment search.
   *
   * @throws Exception indicates some unexpected failure.
   */
  @Test
  public void testGetRecentCommentsIssuesUpdatedToAbsoluteDate() throws Exception {
    final Date beforeDate = createDate(1994, Calendar.JANUARY, 26);

    final JqlQueryBuilder builder = JqlQueryBuilder.newBuilder();
    builder.where().updated().ltEq(beforeDate);
    final SearchRequest request = new SearchRequest(builder.buildQuery());

    final List<Long> issueIds = CollectionBuilder.newBuilder(5674L).asList();
    final BooleanQuery query = createIssueQuery(issueIds);

    final TermRangeQuery dateQuery = createBeforeQuery(beforeDate);
    query.add(dateQuery, BooleanClause.Occur.MUST);

    _testGetRecentComments(issueIds, request, null, true, query);
  }
  /**
   * Make sure the correct search is performed when theh search request does contain "update date"
   * critera and we have determined that they be included in the comment search.
   *
   * @throws Exception indicates some unexpected failure.
   */
  @Test
  public void testGetRecentCommentsIssuesUpdatedFromAbsoluteDate() throws Exception {
    final Date afterDate = createDate(2006, Calendar.JANUARY, 26);

    builder = JqlQueryBuilder.newBuilder();
    builder.where().updatedAfter(afterDate);
    final SearchRequest request = new SearchRequest(builder.buildQuery());

    final List<Long> issueIds = CollectionBuilder.newBuilder(5674L).asList();
    final BooleanQuery query = createIssueQuery(issueIds);

    final org.apache.lucene.search.Query dateQuery = createAfterQuery(afterDate);
    query.add(dateQuery, BooleanClause.Occur.MUST);

    _testGetRecentComments(issueIds, request, null, true, query);
  }
  private void _runSearch(
      final String query,
      final QueryType type,
      final User searcher,
      final int numberOfIssues,
      final String resolutionId)
      throws SearchException {
    final LuceneSearchProvider luceneSearchProvider = getSearchProvider();

    // Create a search request
    final JqlClauseBuilder jqlQuery =
        JqlQueryBuilder.newBuilder().where().defaultAnd().resolution(resolutionId);

    switch (type) {
      case COMMENT:
        jqlQuery.comment(query);
        break;
      case ISSUE:
        jqlQuery.summary(query);
        break;
    }
    final SearchResults issues =
        luceneSearchProvider.search(jqlQuery.buildQuery(), searcher, new PagerFilter(-1));
    assertNotNull(issues);
    assertEquals(query, numberOfIssues, issues.getTotal());
  }
  /**
   * Make sure the correct search is performed when theh search request does contain "update date"
   * critera and we have determined that they be included in the comment search.
   *
   * @throws Exception indicates some unexpected failure.
   */
  @Test
  public void testGetRecentCommentsIssuesUpdatedToRelativeDate() throws Exception {
    final String beforeDuration = "-3d";
    final Date beforeDate = dateSupport.convertToDate(beforeDuration);

    final JqlQueryBuilder builder = JqlQueryBuilder.newBuilder();
    builder.where().updated().ltEq(beforeDuration);
    final SearchRequest request = new SearchRequest(builder.buildQuery());

    final List<Long> issueIds = CollectionBuilder.newBuilder(5674L).asList();
    final BooleanQuery query = createIssueQuery(issueIds);

    final TermRangeQuery dateQuery = createBeforeQuery(beforeDate);
    query.add(dateQuery, BooleanClause.Occur.MUST);

    _testGetRecentComments(issueIds, request, null, true, query);
  }
  @Test
  public void testCreateSearchQuery() throws Exception {
    mockController.replay();

    BrowseProjectContext ctx = new BrowseProjectContext(null, new MockProject(100L, "STVO"));
    Query initialQuery = ctx.createQuery();
    assertEquals(JqlQueryBuilder.newBuilder().where().project("STVO").buildQuery(), initialQuery);
  }
  /**
   * Make sure the correct search is performed when theh search request does contain "update date",
   * but that criteria cannot be converted into a filter, that is, when all the "updated date"
   * clauses are not anded together.
   *
   * @throws Exception indicates some unexpected failure.
   */
  @Test
  public void testGetRecentCommentsIssuesBad() throws Exception {
    final String beforeDuration = "-3d";
    final Date afterDate = createDate(1981, Calendar.JANUARY, 12);
    final User user = new MockUser("me");

    final JqlQueryBuilder builder = JqlQueryBuilder.newBuilder();
    builder
        .where()
        .updatedAfter(afterDate)
        .and()
        .sub()
        .priority("minor")
        .or()
        .updated()
        .ltEq(beforeDuration)
        .endsub();
    final SearchRequest request = new SearchRequest(builder.buildQuery());

    final List<Long> issueIds = CollectionBuilder.newBuilder(6L, 3L, 13484L, 11111134L).asList();
    final BooleanQuery query = createIssueQuery(issueIds);

    _testGetRecentComments(issueIds, request, user, true, query);
  }
  public void testComplexQueryWithSummaryAndComments() throws SearchException {
    final LuceneSearchProvider luceneSearchProvider = getSearchProvider();

    // Create a search request
    JqlClauseBuilder jqlQuery =
        JqlQueryBuilder.newBuilder()
            .where()
            .resolution(resolutionId)
            .and()
            .sub()
            .comment(SUMMARY_BODY)
            .or()
            .summary(SUMMARY_BODY)
            .endsub();

    final SearchResults issues =
        luceneSearchProvider.search(jqlQuery.buildQuery(), bob, new PagerFilter(-1));
    assertNotNull(issues);
    assertEquals(2, issues.getTotal());
  }
  public void testSearchAndSortForPagerConstraints() throws SearchException, IOException {
    final LuceneSearchProvider luceneSearchProvider = getSearchProvider();

    // Create a search request
    Query jqlQuery =
        JqlQueryBuilder.newBuilder()
            .where()
            .resolution(resolutionId)
            .and()
            .summary(SUMMARY_BODY)
            .buildQuery();

    final MyDocumentHitCollector hitCollector = new MyDocumentHitCollector(null);

    // Test that we limit the results with a max on the filter
    luceneSearchProvider.searchAndSort(jqlQuery, bob, hitCollector, new PagerFilter(1));
    assertEquals(1, hitCollector.count);
    hitCollector.clearCount();

    // Test that we bring them all back with a max of 2
    luceneSearchProvider.searchAndSort(jqlQuery, bob, hitCollector, new PagerFilter(2));
    assertEquals(2, hitCollector.count);
    hitCollector.clearCount();

    // Test the start and end
    final PagerFilter filter = new PagerFilter(2);
    filter.setStart(1);
    luceneSearchProvider.searchAndSort(jqlQuery, bob, hitCollector, filter);
    assertEquals(1, hitCollector.count);
    hitCollector.clearCount();

    // Bring them all back
    luceneSearchProvider.searchAndSort(
        jqlQuery, bob, hitCollector, PagerFilter.getUnlimitedFilter());
    assertEquals(2, hitCollector.count);
  }