diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-10-17 18:16:28 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk@users.noreply.github.com> | 2017-10-19 12:12:11 +0200 |
commit | 8cbb514261e2af99b4ee7f79a5c1210e1cee3714 (patch) | |
tree | 021da2b2a022f581649e37f71f281231a8e0a6f8 /tests | |
parent | d9564eed6baf772c31d3ee2888dffc7d8c51fca2 (diff) | |
download | sonarqube-8cbb514261e2af99b4ee7f79a5c1210e1cee3714.tar.gz sonarqube-8cbb514261e2af99b4ee7f79a5c1210e1cee3714.zip |
SONAR-7309 DB Cleaner strategy to keep only analyses with a version
Diffstat (limited to 'tests')
3 files changed, 41 insertions, 13 deletions
diff --git a/tests/src/test/java/org/sonarqube/tests/dbCleaner/PurgeTest.java b/tests/src/test/java/org/sonarqube/tests/dbCleaner/PurgeTest.java index dc4bb0fcc78..2b3115564e8 100644 --- a/tests/src/test/java/org/sonarqube/tests/dbCleaner/PurgeTest.java +++ b/tests/src/test/java/org/sonarqube/tests/dbCleaner/PurgeTest.java @@ -22,7 +22,6 @@ package org.sonarqube.tests.dbCleaner; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.BuildResult; import com.sonar.orchestrator.build.SonarScanner; -import org.sonarqube.tests.Category4Suite; import java.util.Calendar; import java.util.Date; import java.util.List; @@ -35,9 +34,13 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ErrorCollector; +import org.sonarqube.tests.Category4Suite; +import org.sonarqube.tests.Tester; +import org.sonarqube.ws.ProjectAnalyses; import org.sonarqube.ws.WsMeasures; import org.sonarqube.ws.WsMeasures.SearchHistoryResponse.HistoryValue; import org.sonarqube.ws.client.measure.SearchHistoryRequest; +import org.sonarqube.ws.client.projectanalysis.SearchRequest; import util.ItUtils; import static java.util.Collections.singletonList; @@ -46,8 +49,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.equalTo; import static util.ItUtils.formatDate; import static util.ItUtils.newAdminWsClient; +import static util.ItUtils.restoreProfile; import static util.ItUtils.runProjectAnalysis; import static util.ItUtils.setServerProperty; +import static util.ItUtils.toDatetime; @Ignore public class PurgeTest { @@ -62,7 +67,8 @@ public class PurgeTest { @ClassRule public static final Orchestrator orchestrator = Category4Suite.ORCHESTRATOR; - + @Rule + public Tester tester = new Tester(orchestrator); @Rule public ErrorCollector collector = new ErrorCollector(); @@ -72,11 +78,7 @@ public class PurgeTest { orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY); - ItUtils.restoreProfile(orchestrator, getClass().getResource("/dbCleaner/one-issue-per-line-profile.xml")); - - setServerProperty(orchestrator, "sonar.dbcleaner.cleanDirectory", null); - setServerProperty(orchestrator, "sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay", null); - setServerProperty(orchestrator, "sonar.dbcleaner.weeksBeforeKeepingOnlyOneSnapshotByWeek", null); + restoreProfile(orchestrator, getClass().getResource("/dbCleaner/one-issue-per-line-profile.xml")); } @Test @@ -216,6 +218,25 @@ public class PurgeTest { assertThat(response.getMeasuresList().get(0).getHistoryList()).extracting(HistoryValue::getDate).doesNotContain(lastWednesdayFormatted, lastThursdayFormatted); } + @Test + public void keep_only_analyses_with_a_version() { + tester.settings().setGlobalSettings("sonar.dbcleaner.weeksBeforeKeepingOnlyAnalysesWithVersion", "1"); + + Date now = new Date(); + String heightDaysAgo = formatDate(addDays(now, -8)); + String nineDaysAgo = formatDate(addDays(now, -9)); + + runProjectAnalysis(orchestrator, PROJECT_SAMPLE_PATH, "sonar.projectDate", nineDaysAgo, "sonar.projectVersion", "V1"); + runProjectAnalysis(orchestrator, PROJECT_SAMPLE_PATH, "sonar.projectDate", heightDaysAgo); + runProjectAnalysis(orchestrator, PROJECT_SAMPLE_PATH); + + ProjectAnalyses.SearchResponse result = tester.wsClient().projectAnalysis().search(SearchRequest.builder().setProject(PROJECT_KEY).build()); + + assertThat(result.getAnalysesList()).extracting(a -> formatDate(toDatetime(a.getDate()))) + .contains(nineDaysAgo, formatDate(now)) + .doesNotContain(heightDaysAgo); + } + /** * SONAR-3120 */ diff --git a/tests/src/test/java/org/sonarqube/tests/measure/TimeMachineTest.java b/tests/src/test/java/org/sonarqube/tests/measure/TimeMachineTest.java index 90dbd6f9009..f95081a24a7 100644 --- a/tests/src/test/java/org/sonarqube/tests/measure/TimeMachineTest.java +++ b/tests/src/test/java/org/sonarqube/tests/measure/TimeMachineTest.java @@ -22,7 +22,6 @@ package org.sonarqube.tests.measure; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.BuildResult; import com.sonar.orchestrator.build.SonarScanner; -import org.sonarqube.tests.Category1Suite; import java.util.Arrays; import java.util.Date; import java.util.Map; @@ -30,6 +29,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; +import org.sonarqube.tests.Category1Suite; import org.sonarqube.ws.WsMeasures.Measure; import org.sonarqube.ws.WsMeasures.SearchHistoryResponse; import org.sonarqube.ws.WsMeasures.SearchHistoryResponse.HistoryValue; @@ -39,6 +39,7 @@ import util.ItUtils; import util.ItUtils.ComponentNavigation; import static java.util.Collections.singletonList; +import static org.apache.commons.lang.time.DateUtils.addDays; import static org.assertj.core.api.Assertions.assertThat; import static util.ItUtils.formatDate; import static util.ItUtils.getComponentNavigation; @@ -51,8 +52,6 @@ import static util.ItUtils.setServerProperty; public class TimeMachineTest { private static final String PROJECT = "sample"; - private static final String FIRST_ANALYSIS_DATE = "2014-10-19"; - private static final String SECOND_ANALYSIS_DATE = "2014-11-13"; @ClassRule public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; @@ -65,8 +64,12 @@ public class TimeMachineTest { ItUtils.restoreProfile(orchestrator, TimeMachineTest.class.getResource("/measure/one-issue-per-line-profile.xml")); orchestrator.getServer().provisionProject("sample", "Sample"); orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line"); - analyzeProject("measure/xoo-history-v1", FIRST_ANALYSIS_DATE); - analyzeProject("measure/xoo-history-v2", SECOND_ANALYSIS_DATE); + + Date now = new Date(); + String yesterday = formatDate(addDays(now, -1)); + String aMonthAgo = formatDate(addDays(now, -30)); + analyzeProject("measure/xoo-history-v1", aMonthAgo); + analyzeProject("measure/xoo-history-v2", yesterday); wsMeasures = newAdminWsClient(orchestrator).measures(); } @@ -88,7 +91,6 @@ public class TimeMachineTest { public void projectIsAnalyzed() { ComponentNavigation component = getComponentNavigation(orchestrator, PROJECT); assertThat(component.getVersion()).isEqualTo("1.0-SNAPSHOT"); - assertThat(component.getDate().getMonth()).isEqualTo(10); // November } @Test diff --git a/tests/src/test/java/util/ItUtils.java b/tests/src/test/java/util/ItUtils.java index 05f231afc6c..e11e55d4d35 100644 --- a/tests/src/test/java/util/ItUtils.java +++ b/tests/src/test/java/util/ItUtils.java @@ -477,6 +477,11 @@ public class ItUtils { return sdf.format(d); } + public static String formatDateTime(Date d) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); + return sdf.format(d); + } + public static String extractCeTaskId(BuildResult buildResult) { List<String> taskIds = extractCeTaskIds(buildResult); checkState(taskIds.size() == 1, "More than one task id retrieved from logs"); |