From 66286b52fc378abc2015f7a5eba4d040d02d6d94 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Tue, 27 Dec 2016 17:10:01 +0100 Subject: [PATCH] remove hardcoded dates in ITs (#1485) --- .../src/test/java/it/dbCleaner/PurgeTest.java | 15 +++++++++------ .../ProjectAdministrationTest.java | 12 +++++++++--- .../java/it/qualityGate/QualityGateUiTest.java | 10 ++++++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java b/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java index 769d80febf7..73501238752 100644 --- a/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java +++ b/it/it-tests/src/test/java/it/dbCleaner/PurgeTest.java @@ -54,6 +54,9 @@ public class PurgeTest { private static final String PROJECT_KEY = "com.sonarsource.it.samples:multi-modules-sample"; private static final String PROJECT_SAMPLE_PATH = "dbCleaner/xoo-multi-modules-sample"; + private static final String ONE_DAY_AGO = DateFormatUtils.ISO_DATE_FORMAT.format(addDays(new Date(), -1)); + private static final String TWO_DAYS_AGO = DateFormatUtils.ISO_DATE_FORMAT.format(addDays(new Date(), -2)); + @ClassRule public static final Orchestrator orchestrator = Category4Suite.ORCHESTRATOR; @@ -249,12 +252,12 @@ public class PurgeTest { */ @Test public void should_delete_historical_data_of_directories_by_default() { - scan(PROJECT_SAMPLE_PATH, "2012-01-01"); + scan(PROJECT_SAMPLE_PATH, TWO_DAYS_AGO); int fileMeasures = count(COUNT_FILE_MEASURES); int dirMeasures = count(COUNT_DIR_MEASURES); - scan(PROJECT_SAMPLE_PATH, "2012-02-02"); + scan(PROJECT_SAMPLE_PATH, ONE_DAY_AGO); // second analysis with new_* metrics assertThat(count(COUNT_FILE_MEASURES)).isLessThan(2 * fileMeasures); @@ -266,14 +269,14 @@ public class PurgeTest { */ @Test public void should_not_delete_historical_data_of_directories() { - scan(PROJECT_SAMPLE_PATH, "2012-01-01"); + scan(PROJECT_SAMPLE_PATH, TWO_DAYS_AGO); int fileMeasures = count(COUNT_FILE_MEASURES); int dirMeasures = count(COUNT_DIR_MEASURES); setServerProperty(orchestrator, "sonar.dbcleaner.cleanDirectory", "false"); - scan(PROJECT_SAMPLE_PATH, "2012-02-02"); + scan(PROJECT_SAMPLE_PATH, ONE_DAY_AGO); // second analysis as NEW_* metrics assertThat(count(COUNT_FILE_MEASURES)).isLessThan( 2 * fileMeasures); @@ -285,7 +288,7 @@ public class PurgeTest { */ @Test public void should_delete_historical_data_of_flagged_metrics() { - scan(PROJECT_SAMPLE_PATH, "2012-01-01"); + scan(PROJECT_SAMPLE_PATH, TWO_DAYS_AGO); // historical data of complexity_in_classes is supposed to be deleted (see CoreMetrics) String selectNcloc = "project_measures where metric_id in (select id from metrics where name='ncloc')"; @@ -293,7 +296,7 @@ public class PurgeTest { int nclocCount = count(selectNcloc); int complexitInClassesCount = count(selectComplexityInClasses); - scan(PROJECT_SAMPLE_PATH, "2012-02-02"); + scan(PROJECT_SAMPLE_PATH, ONE_DAY_AGO); assertThat(count(selectNcloc)).isGreaterThan(nclocCount); assertThat(count(selectComplexityInClasses)).isEqualTo(complexitInClassesCount); } diff --git a/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java b/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java index 5026725cbc8..732af4fadbc 100644 --- a/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java +++ b/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java @@ -26,8 +26,10 @@ import it.Category1Suite; import java.io.UnsupportedEncodingException; import java.sql.SQLException; import java.util.Calendar; +import java.util.Date; import java.util.GregorianCalendar; import javax.annotation.Nullable; +import org.apache.commons.lang.time.DateFormatUtils; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; @@ -41,12 +43,16 @@ import pageobjects.Navigation; import pageobjects.settings.SettingsPage; import util.selenium.SeleneseTest; +import static org.apache.commons.lang.time.DateUtils.addDays; import static org.assertj.core.api.Assertions.assertThat; import static util.ItUtils.projectDir; public class ProjectAdministrationTest { private static final String DELETE_WS_ENDPOINT = "api/projects/bulk_delete"; + // take some day in the past + private static final String ANALYSIS_DATE = DateFormatUtils.ISO_DATE_FORMAT.format(addDays(new Date(), -1)); + @ClassRule public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; @@ -66,7 +72,7 @@ public class ProjectAdministrationTest { @Test public void delete_project_by_web_service() { - scanSampleWithDate("2012-01-01"); + scanSampleWithDate(ANALYSIS_DATE); assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(PROJECT_KEY))).isNotNull(); assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(FILE_KEY))).isNotNull(); @@ -80,7 +86,7 @@ public class ProjectAdministrationTest { @Test public void fail_when_trying_to_delete_a_file() { expectedException.expect(HttpException.class); - scanSampleWithDate("2012-01-01"); + scanSampleWithDate(ANALYSIS_DATE); assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(PROJECT_KEY))).isNotNull(); assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(FILE_KEY))).isNotNull(); @@ -92,7 +98,7 @@ public class ProjectAdministrationTest { @Test public void fail_when_insufficient_privilege() { expectedException.expect(HttpException.class); - scanSampleWithDate("2012-01-01"); + scanSampleWithDate(ANALYSIS_DATE); assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(PROJECT_KEY))).isNotNull(); diff --git a/it/it-tests/src/test/java/it/qualityGate/QualityGateUiTest.java b/it/it-tests/src/test/java/it/qualityGate/QualityGateUiTest.java index e5c086800d4..5c2d5021258 100644 --- a/it/it-tests/src/test/java/it/qualityGate/QualityGateUiTest.java +++ b/it/it-tests/src/test/java/it/qualityGate/QualityGateUiTest.java @@ -23,7 +23,9 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.selenium.Selenese; import it.Category1Suite; +import java.util.Date; import javax.annotation.Nullable; +import org.apache.commons.lang3.time.DateFormatUtils; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -37,6 +39,7 @@ import org.sonar.wsclient.qualitygate.UpdateCondition; import util.ItUtils; import util.selenium.SeleneseTest; +import static org.apache.commons.lang.time.DateUtils.addDays; import static util.ItUtils.projectDir; import static util.ItUtils.setServerProperty; @@ -75,12 +78,15 @@ public class QualityGateUiTest { QualityGate qGate = qgClient.create("AlertsForHistory"); qgClient.setDefault(qGate.id()); + String firstAnalysisDate = DateFormatUtils.ISO_DATE_FORMAT.format(addDays(new Date(), -2)); + String secondAnalysisDate = DateFormatUtils.ISO_DATE_FORMAT.format(addDays(new Date(), -1)); + // with this configuration, project should have an Orange alert QualityGateCondition lowThresholds = qgClient.createCondition(NewCondition.create(qGate.id()).metricKey("lines").operator("GT").warningThreshold("5").errorThreshold("50")); - scanSampleWithDate("2012-01-01"); + scanSampleWithDate(firstAnalysisDate); // with this configuration, project should have a Green alert qgClient.updateCondition(UpdateCondition.create(lowThresholds.id()).metricKey("lines").operator("GT").warningThreshold("5000").errorThreshold("5000")); - scanSampleWithDate("2012-01-02"); + scanSampleWithDate(secondAnalysisDate); new SeleneseTest(Selenese.builder() .setHtmlTestsInClasspath("display-alerts-history-page", -- 2.39.5