package org.sonar.server.issue.index;
import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
private void addDatesFilter(Map<String, FilterBuilder> filters, IssueQuery query) {
Date createdAfter = query.createdAfter();
+ Date createdBefore = query.createdBefore();
+ Preconditions.checkArgument(createdAfter == null || createdBefore == null || createdAfter.before(createdBefore),
+ "Start bound cannot be larger than end bound");
+
if (createdAfter != null) {
filters.put("__createdAfter", FilterBuilders
.rangeFilter(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT)
.gte(createdAfter)
.cache(false));
}
- Date createdBefore = query.createdBefore();
if (createdBefore != null) {
filters.put("__createdBefore", FilterBuilders
.rangeFilter(IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT)
long startTime = createdAfter == null ? getMinCreatedAt(filters, esQuery) : createdAfter.getTime();
Date createdBefore = query.createdBefore();
long endTime = createdBefore == null ? now.getTime() : createdBefore.getTime();
+
Duration timeSpan = new Duration(startTime, endTime);
+
if (timeSpan.isShorterThan(TWENTY_DAYS)) {
bucketSize = DateHistogram.Interval.DAY;
} else if (timeSpan.isShorterThan(TWENTY_WEEKS)) {
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
+import org.assertj.core.api.Fail;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
assertThat(index.search(IssueQuery.builder().createdBefore(DateUtils.parseDate("2014-09-25")).build(), new SearchOptions()).getDocs()).hasSize(2);
}
+ @Test
+ public void filter_by_created_before_must_be_lower_than_after() throws Exception {
+ try {
+ index.search(IssueQuery.builder().createdAfter(DateUtils.parseDate("2014-09-20")).createdBefore(DateUtils.parseDate("2014-09-19")).build(), new SearchOptions());
+ Fail.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
+ } catch (IllegalArgumentException exception) {
+ assertThat(exception.getMessage()).isEqualTo("Start bound cannot be larger than end bound");
+ }
+ }
+
@Test
public void filter_by_created_at() throws Exception {
ComponentDto project = ComponentTesting.newProjectDto();