From 6367cfd321a0014410f405826064ab1c1f30a44d Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 9 Feb 2015 15:55:10 +0100 Subject: [PATCH] SONAR-6137 Protect against createdAfter > createdBefore --- .../java/org/sonar/server/issue/index/IssueIndex.java | 8 +++++++- .../server/issue/index/IssueIndexMediumTest.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java index 7b7b11590c2..5ed2fc39e52 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java @@ -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 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)) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java index acb942e098a..bd84b25b7cd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexMediumTest.java @@ -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(); -- 2.39.5