]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6137 Protect against createdAfter > createdBefore
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 9 Feb 2015 14:55:10 +0000 (15:55 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 9 Feb 2015 15:30:53 +0000 (16:30 +0100)
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java
server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java

index 7b7b11590c261fc5618cf24414525d6b1291c9a5..5ed2fc39e5258b07906cd4f7a39d16cd0308d560 100644 (file)
@@ -20,6 +20,7 @@
 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;
@@ -375,13 +376,16 @@ public class IssueIndex extends BaseIndex {
 
   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)
@@ -457,7 +461,9 @@ public class IssueIndex extends BaseIndex {
     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)) {
index acb942e098a27d7d8d8afd6c8580475f2695b731..bd84b25b7cdab7b6f18fa3dc0ed7ebd4acc773e4 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.issue.index;
 
 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;
@@ -631,6 +632,16 @@ public class IssueIndexMediumTest {
     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();