]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8028 Search QP changes with inclusive date and date+time 1246/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 13 Sep 2016 17:06:15 +0000 (19:06 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 14 Sep 2016 09:25:24 +0000 (11:25 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangelogActionTest.java

index 0c883f468ee85a82f531ae513a9b8d80cdde5636..eaa028dc45d0680aa25b8baa5c6ab29521104b4b 100644 (file)
@@ -36,12 +36,14 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.qualityprofile.QProfileFactory;
 import org.sonar.server.qualityprofile.QProfileRef;
 
+import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime;
+import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime;
 import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
 
 public class ChangelogAction implements QProfileWsAction {
 
-  private static final String PARAM_SINCE = "since";
-  private static final String PARAM_TO = "to";
+  static final String PARAM_SINCE = "since";
+  static final String PARAM_TO = "to";
 
   private final ChangelogLoader changelogLoader;
   private final QProfileFactory profileFactory;
@@ -83,11 +85,11 @@ public class ChangelogAction implements QProfileWsAction {
       QualityProfileDto profile = profileFactory.find(dbSession, QProfileRef.from(request));
 
       QProfileChangeQuery query = new QProfileChangeQuery(profile.getKey());
-      Date since = request.paramAsDateTime(PARAM_SINCE);
+      Date since = parseStartingDateOrDateTime(request.param(PARAM_SINCE));
       if (since != null) {
         query.setFromIncluded(since.getTime());
       }
-      Date to = request.paramAsDateTime(PARAM_TO);
+      Date to = parseEndingDateOrDateTime(request.param(PARAM_TO));
       if (to != null) {
         query.setToExcluded(to.getTime());
       }
index 9397b7313430f2beafda14b507dfc748d9e88801..3c7c590417d1473e461cdac8a0abf2360b0e15c9 100644 (file)
@@ -24,6 +24,7 @@ import java.util.List;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
@@ -37,12 +38,17 @@ import org.sonar.server.qualityprofile.QProfileTesting;
 import org.sonar.server.ws.WsTester;
 
 import static java.util.Arrays.asList;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.sonar.api.utils.DateUtils.parseDate;
 import static org.sonar.server.qualityprofile.QProfileRef.PARAM_PROFILE_KEY;
 import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY;
+import static org.sonar.server.qualityprofile.ws.ChangelogAction.PARAM_SINCE;
+import static org.sonar.server.qualityprofile.ws.ChangelogAction.PARAM_TO;
 
 public class ChangelogActionTest {
 
@@ -50,7 +56,6 @@ public class ChangelogActionTest {
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
-  private DbSession dbSession = dbTester.getSession();
 
   private WsTester wsTester;
   private ChangelogLoader changelogLoader = mock(ChangelogLoader.class);
@@ -96,6 +101,23 @@ public class ChangelogActionTest {
       .execute().assertJson(getClass(), "changelog_full.json");
   }
 
+  @Test
+  public void changelog_inclusive_for_dates() throws Exception {
+    when(profileFactory.find(any(DbSession.class), eq(QProfileRef.fromKey(XOO_P1_KEY)))).thenReturn(QProfileTesting.newXooP1());
+    when(changelogLoader.load(any(DbSession.class), any(QProfileChangeQuery.class))).thenReturn(new ChangelogLoader.Changelog(0, Collections.emptyList()));
+
+    wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog")
+      .setParam(PARAM_PROFILE_KEY, XOO_P1_KEY)
+      .setParam(PARAM_SINCE, "2016-09-01")
+      .setParam(PARAM_TO, "2016-09-01")
+      .execute();
+
+    ArgumentCaptor<QProfileChangeQuery> argumentCaptor = ArgumentCaptor.forClass(QProfileChangeQuery.class);
+    verify(changelogLoader).load(any(DbSession.class), argumentCaptor.capture());
+    assertThat(argumentCaptor.getValue().getFromIncluded()).isEqualTo(parseDate("2016-09-01").getTime());
+    assertThat(argumentCaptor.getValue().getToExcluded()).isEqualTo(parseDate("2016-09-02").getTime());
+  }
+
   @Test(expected = NotFoundException.class)
   public void fail_on_unknown_profile() throws Exception {
     when(profileFactory.find(any(DbSession.class), eq(QProfileRef.fromKey(XOO_P1_KEY)))).thenThrow(new NotFoundException("Profile not found"));