From 68eefa86f059bd37b25566e2a3f8ef7b9d72d0f2 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 13 Sep 2016 19:06:15 +0200 Subject: [PATCH] SONAR-8028 Search QP changes with inclusive date and date+time --- .../qualityprofile/ws/ChangelogAction.java | 10 ++++---- .../ws/ChangelogActionTest.java | 24 ++++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java index 0c883f468ee..eaa028dc45d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ChangelogAction.java @@ -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()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangelogActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangelogActionTest.java index 9397b731343..3c7c590417d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangelogActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangelogActionTest.java @@ -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 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")); -- 2.39.5