From cfc286fa9fe61a781578e3a78e6563aa5750dbad Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 12 Dec 2017 11:42:36 +0100 Subject: [PATCH] SONAR-10134 Fix unit tests not having default QG on organization --- .../db/organization/OrganizationDaoTest.java | 3 +- .../step/LoadPeriodsStepTest.java | 230 ++++++++++++------ .../DefaultOrganizationProviderImplTest.java | 3 +- .../organization/ws/CreateActionTest.java | 16 +- .../organization/ws/SearchActionTest.java | 59 ++--- .../step/LoadPeriodsStepTest/empty.xml | 12 - .../no_previous_version.xml | 161 ------------ .../previous_version_deleted.xml | 128 ---------- .../previous_version_is_last_one.xml | 49 ---- .../step/LoadPeriodsStepTest/shared.xml | 192 --------------- .../unprocessed_snapshots.xml | 48 ---- .../load_component_id_from_db.xml | 1 + .../load_project_id_from_db.xml | 1 + .../should_insert_new_issues.xml | 1 + .../should_update_issues.xml | 1 + 15 files changed, 185 insertions(+), 720 deletions(-) delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/empty.xml delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/no_previous_version.xml delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_deleted.xml delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_is_last_one.xml delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/shared.xml delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/unprocessed_snapshots.xml diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java index c6bfeb8e232..56becd292b1 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java @@ -55,6 +55,7 @@ import static org.mockito.Mockito.when; import static org.sonar.db.Pagination.forPage; import static org.sonar.db.organization.OrganizationQuery.newOrganizationQueryBuilder; import static org.sonar.db.organization.OrganizationQuery.returnAll; +import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto; public class OrganizationDaoTest { private static final long SOME_DATE = 1_200_999L; @@ -729,7 +730,7 @@ public class OrganizationDaoTest { insertOrganization(ORGANIZATION_DTO_1); when(system2.now()).thenReturn(DATE_3); - underTest.update(dbSession, new OrganizationDto() + underTest.update(dbSession, newOrganizationDto() .setUuid(ORGANIZATION_DTO_1.getUuid()) .setKey("new key") .setName("new name") diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java index 9a1a708bc6e..0d2026edc71 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java @@ -32,12 +32,14 @@ import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.db.DbClient; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.SnapshotDto; +import org.sonar.db.organization.OrganizationDto; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule; import org.sonar.server.computation.task.projectanalysis.component.Component; import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository; import org.sonar.server.computation.task.projectanalysis.component.ReportComponent; import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule; -import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent; import org.sonar.server.computation.task.projectanalysis.period.Period; import org.sonar.server.computation.task.projectanalysis.period.PeriodHolderImpl; import org.sonar.server.computation.task.step.ComputationStep; @@ -49,14 +51,14 @@ import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAT import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION; import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION; +import static org.sonar.db.component.SnapshotDto.STATUS_UNPROCESSED; +import static org.sonar.db.event.EventDto.CATEGORY_VERSION; +import static org.sonar.db.event.EventTesting.newEvent; @RunWith(DataProviderRunner.class) public class LoadPeriodsStepTest extends BaseStepTest { private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); - private static final String ROOT_KEY = "ROOT_KEY"; - private static final ReportComponent PROJECT_ROOT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(ROOT_KEY).setVersion("1.1").build(); - private static final ViewsComponent VIEW_ROOT = ViewsComponent.builder(Component.Type.VIEW, ROOT_KEY).setUuid("ABCD").build(); @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); @@ -86,14 +88,9 @@ public class LoadPeriodsStepTest extends BaseStepTest { analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime()); } - private void setupRoot(Component root) { - treeRootHolder.setRoot(root); - when(settingsRepository.getConfiguration()).thenReturn(settings.asConfig()); - } - @Test public void no_period_on_first_analysis() { - setupRoot(PROJECT_ROOT); + treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setKey("ROOT_KEY").setVersion("1.1").build()); // No project, no snapshot underTest.execute(); @@ -103,27 +100,31 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_one_period() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L)); // 2008-11-29 + setupRoot(project); String textDate = "2008-11-22"; - settings.setProperty("sonar.leak.period", textDate); + settings.setProperty("sonar.leak.period", textDate); underTest.execute(); Period period = periodsHolder.getPeriod(); assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE); assertThat(period.getModeParameter()).isEqualTo(textDate); - assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1003"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis.getUuid()); } @Test public void no_period_when_settings_match_no_analysis() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.leak.period", "UNKNWOWN VERSION"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L)); // 2008-11-29 + setupRoot(project); + settings.setProperty("sonar.leak.period", "UNKNWOWN VERSION"); underTest.execute(); assertThat(periodsHolder.getPeriod()).isNull(); @@ -131,10 +132,12 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void no_period_when_settings_is_empty() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.leak.period", ""); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L)); // 2008-11-29 + setupRoot(project); + settings.setProperty("sonar.leak.period", ""); underTest.execute(); assertThat(periodsHolder.getPeriod()).isNull(); @@ -142,10 +145,12 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void ignore_unprocessed_snapshots() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml"); - settings.setProperty("sonar.leak.period", "100"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + dbTester.components().insertSnapshot(project, snapshot -> snapshot.setStatus(STATUS_UNPROCESSED).setCreatedAt(1226379600000L)); // 2008-11-29 + setupRoot(project); + settings.setProperty("sonar.leak.period", "100"); underTest.execute(); assertThat(periodsHolder.getPeriod()).isNull(); @@ -153,11 +158,17 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_period_by_date() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setLast(false)); // 2008-11-11 + SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setLast(false)); // 2008-11-12 + SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setLast(false)); // 2008-11-20 + SnapshotDto analysis4 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setLast(false)); // 2008-11-22 + SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setLast(true)); // 2008-11-29 + setupRoot(project); + String textDate = "2008-11-22"; settings.setProperty("sonar.leak.period", textDate); - underTest.execute(); // Return analysis from given date 2008-11-22 @@ -165,8 +176,8 @@ public class LoadPeriodsStepTest extends BaseStepTest { assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE); assertThat(period.getModeParameter()).isEqualTo(textDate); - assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1003"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis4.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis4.getUuid()); assertThat(logTester.logs()).hasSize(1); assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of "); @@ -174,29 +185,37 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void search_by_date_return_nearest_later_analysis() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - String date = "2008-11-24"; + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setLast(false)); // 2008-11-11 + SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setLast(false)); // 2008-11-12 + SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setLast(false)); // 2008-11-20 + SnapshotDto analysis4 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setLast(false)); // 2008-11-22 + SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setLast(true)); // 2008-11-29 + setupRoot(project); + + String date = "2008-11-13"; settings.setProperty("sonar.leak.period", date); - underTest.execute(); - // Analysis form 2008-11-29 + // Analysis form 2008-11-20 Period period = periodsHolder.getPeriod(); assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE); assertThat(period.getModeParameter()).isEqualTo(date); - assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1004"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis3.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis3.getUuid()); } @Test public void no_period_by_date() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L)); // 2008-11-29 + setupRoot(project); + // No analysis at and after this date settings.setProperty("sonar.leak.period", "2008-11-30"); - underTest.execute(); assertThat(periodsHolder.getPeriod()).isNull(); @@ -204,10 +223,16 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_period_by_days() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.leak.period", "10"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setLast(false)); // 2008-11-11 + SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setLast(false)); // 2008-11-12 + SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setLast(false)); // 2008-11-20 + SnapshotDto analysis4 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setLast(false)); // 2008-11-22 + SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setLast(true)); // 2008-11-29 + setupRoot(project); + settings.setProperty("sonar.leak.period", "10"); underTest.execute(); // return analysis from 2008-11-20 @@ -215,8 +240,8 @@ public class LoadPeriodsStepTest extends BaseStepTest { assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DAYS); assertThat(period.getModeParameter()).isEqualTo("10"); - assertThat(period.getSnapshotDate()).isEqualTo(1227157200000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1002"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis3.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis3.getUuid()); assertThat(logTester.logs()).hasSize(1); assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of "); @@ -224,10 +249,11 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void no_period_by_days() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.leak.period", "0"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + setupRoot(project); + settings.setProperty("sonar.leak.period", "0"); underTest.execute(); assertThat(periodsHolder.getPeriod()).isNull(); @@ -235,10 +261,19 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_period_by_previous_version() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.leak.period", "previous_version"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(false)); // 2008-11-11 + SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setVersion("1.0").setLast(false)); // 2008-11-12 + SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setVersion("1.1").setLast(false)); // 2008-11-20 + SnapshotDto analysis4 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setVersion("1.1").setLast(false)); // 2008-11-22 + SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setVersion("1.1").setLast(true)); // 2008-11-29 + dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION).setDate(analysis1.getCreatedAt())); + dbTester.events().insertEvent(newEvent(analysis2).setName("1.0").setCategory(CATEGORY_VERSION).setDate(analysis2.getCreatedAt())); + dbTester.events().insertEvent(newEvent(analysis5).setName("1.1").setCategory(CATEGORY_VERSION).setDate(analysis4.getCreatedAt())); + setupRoot(project, "1.1"); + settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(); // Analysis form 2008-11-12 @@ -246,8 +281,8 @@ public class LoadPeriodsStepTest extends BaseStepTest { assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isEqualTo("1.0"); - assertThat(period.getSnapshotDate()).isEqualTo(1226494680000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1001"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis2.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis2.getUuid()); assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1); assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous version ("); @@ -255,10 +290,17 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_period_by_previous_version_with_previous_version_deleted() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml"); - settings.setProperty("sonar.leak.period", "previous_version"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(false)); // 2008-11-11 + SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setVersion("1.0").setLast(false)); // 2008-11-12 + SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setVersion("1.1").setLast(false)); // 2008-11-20 + dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION)); + // The "1.0" version was deleted from the history + dbTester.events().insertEvent(newEvent(analysis3).setName("1.1").setCategory(CATEGORY_VERSION)); + setupRoot(project, "1.1"); + settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(); // Analysis form 2008-11-11 @@ -266,16 +308,17 @@ public class LoadPeriodsStepTest extends BaseStepTest { assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isEqualTo("0.9"); - assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1000"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis1.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis1.getUuid()); } @Test public void no_period_by_previous_version() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.leak.period", "previous_version"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + setupRoot(project); + settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(); assertThat(periodsHolder.getPeriod()).isNull(); @@ -283,65 +326,90 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "no_previous_version.xml"); - settings.setProperty("sonar.leak.period", "previous_version"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(false)); // 2008-11-11 + SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setVersion("1.1").setLast(true)); // 2008-11-29 + dbTester.events().insertEvent(newEvent(analysis5).setName("1.1").setCategory(CATEGORY_VERSION)); + setupRoot(project, "1.1"); + settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(); Period period = periodsHolder.getPeriod(); assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isNull(); - assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1000"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis1.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis1.getUuid()); } @Test public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "previous_version_is_last_one.xml"); - settings.setProperty("sonar.leak.period", "previous_version"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(true)); // 2008-11-11 + setupRoot(project, "1.1"); + settings.setProperty("sonar.leak.period", "previous_version"); underTest.execute(); Period period = periodsHolder.getPeriod(); assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isNull(); - assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1000"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis.getUuid()); } @Test public void feed_period_by_version() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.leak.period", "0.9"); - + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + SnapshotDto analysis1 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226379600000L).setVersion("0.9").setLast(false)); // 2008-11-11 + SnapshotDto analysis2 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1226494680000L).setVersion("1.0").setLast(false)); // 2008-11-12 + SnapshotDto analysis3 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227157200000L).setVersion("1.1").setLast(false)); // 2008-11-20 + SnapshotDto analysis4 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227358680000L).setVersion("1.1").setLast(false)); // 2008-11-22 + SnapshotDto analysis5 = dbTester.components().insertSnapshot(project, snapshot -> snapshot.setCreatedAt(1227934800000L).setVersion("1.1").setLast(true)); // 2008-11-29 + dbTester.events().insertEvent(newEvent(analysis1).setName("0.9").setCategory(CATEGORY_VERSION)); + dbTester.events().insertEvent(newEvent(analysis2).setName("1.0").setCategory(CATEGORY_VERSION)); + dbTester.events().insertEvent(newEvent(analysis5).setName("1.1").setCategory(CATEGORY_VERSION)); + setupRoot(project, "1.1"); + + settings.setProperty("sonar.leak.period", "1.0"); underTest.execute(); // Analysis form 2008-11-11 Period period = periodsHolder.getPeriod(); assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_VERSION); - assertThat(period.getModeParameter()).isEqualTo("0.9"); - assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); - assertThat(period.getAnalysisUuid()).isEqualTo("u1000"); + assertThat(period.getModeParameter()).isEqualTo("1.0"); + assertThat(period.getSnapshotDate()).isEqualTo(analysis2.getCreatedAt()); + assertThat(period.getAnalysisUuid()).isEqualTo(analysis2.getUuid()); assertThat(logTester.logs()).hasSize(1); - assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) ("); + assertThat(logTester.logs().get(0)).startsWith("Compare to version (1.0) ("); } @Test public void no_period_by_version() { - setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.leak.period", "0.8"); + OrganizationDto organization = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertPrivateProject(organization); + setupRoot(project); + settings.setProperty("sonar.leak.period", "0.8"); underTest.execute(); assertThat(periodsHolder.getPeriod()).isNull(); } + private void setupRoot(ComponentDto project) { + setupRoot(project, "1.1"); + } + + private void setupRoot(ComponentDto project, String version) { + treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(project.uuid()).setKey(project.getKey()).setVersion(version).build()); + when(settingsRepository.getConfiguration()).thenReturn(settings.asConfig()); + } + } diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/DefaultOrganizationProviderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/DefaultOrganizationProviderImplTest.java index 09a22653625..ed00038c2aa 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/DefaultOrganizationProviderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/DefaultOrganizationProviderImplTest.java @@ -32,10 +32,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto; import static org.sonar.server.property.InternalProperties.DEFAULT_ORGANIZATION; public class DefaultOrganizationProviderImplTest { - private static final OrganizationDto ORGANIZATION_DTO_1 = new OrganizationDto() + private static final OrganizationDto ORGANIZATION_DTO_1 = newOrganizationDto() .setUuid("uuid1") .setName("the name of 1") .setKey("the key 1"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java index bc64b63b1e9..66b766cad91 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java @@ -352,7 +352,7 @@ public class CreateActionTest { @Test public void request_fails_if_key_is_specified_and_already_exists_in_DB() { logInAsSystemAdministrator(); - OrganizationDto org = insertOrganization("the-key"); + OrganizationDto org = dbTester.organizations().insert(o -> o.setKey("the-key")); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Key '" + org.getKey() + "' is already used. Specify another one."); @@ -364,7 +364,7 @@ public class CreateActionTest { public void request_fails_if_key_computed_from_name_already_exists_in_DB() { logInAsSystemAdministrator(); String key = STRING_65_CHARS_LONG.substring(0, 32); - insertOrganization(key); + dbTester.organizations().insert(o -> o.setKey(key)); String name = STRING_65_CHARS_LONG.substring(0, 64); @@ -662,18 +662,6 @@ public class CreateActionTest { assertThat(dto.getUpdatedAt()).isEqualTo(createdAt); } - private OrganizationDto insertOrganization(String key) { - OrganizationDto dto = new OrganizationDto() - .setUuid(key + "_uuid") - .setKey(key) - .setName(key + "_name") - .setCreatedAt((long) key.hashCode()) - .setUpdatedAt((long) key.hashCode()); - dbClient.organizationDao().insert(dbTester.getSession(), dto, false); - dbTester.commit(); - return dto; - } - private void logInAsSystemAdministrator() { userSession.logIn(user).setSystemAdministrator(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchActionTest.java index 8eedbae42ec..587b43afec6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchActionTest.java @@ -20,8 +20,6 @@ package org.sonar.server.organization.ws; import com.google.common.base.Joiner; -import java.io.IOException; -import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import java.util.Random; @@ -55,15 +53,7 @@ import static org.sonar.server.organization.ws.SearchAction.PARAM_MEMBER; import static org.sonar.test.JsonAssert.assertJson; public class SearchActionTest { - private static final OrganizationDto ORGANIZATION_DTO = new OrganizationDto() - .setUuid("a uuid") - .setKey("the_key") - .setName("the name") - .setDescription("the description") - .setUrl("the url") - .setAvatarUrl("the avatar url") - .setCreatedAt(1_999_000L) - .setUpdatedAt(1_888_000L); + private static final long SOME_DATE = 1_999_999L; private System2 system2 = mock(System2.class); @@ -116,9 +106,9 @@ public class SearchActionTest { } @Test - public void json_example() throws URISyntaxException, IOException { + public void json_example() { when(system2.now()).thenReturn(SOME_DATE, SOME_DATE + 1000); - OrganizationDto barOrganization = db.organizations().insert(new OrganizationDto() + OrganizationDto barOrganization = db.organizations().insert(organization -> organization .setUuid(Uuids.UUID_EXAMPLE_02) .setKey("bar-company") .setName("Bar Company") @@ -126,10 +116,13 @@ public class SearchActionTest { .setUrl("https://www.bar.com") .setAvatarUrl("https://www.bar.com/logo.png") .setGuarded(false)); - OrganizationDto fooOrganization = db.organizations().insert(new OrganizationDto() + OrganizationDto fooOrganization = db.organizations().insert(organization -> organization .setUuid(Uuids.UUID_EXAMPLE_01) .setKey("foo-company") .setName("Foo Company") + .setDescription(null) + .setUrl(null) + .setAvatarUrl(null) .setGuarded(true)); UserDto user = db.users().insertUser(); db.organizations().addMember(barOrganization, user); @@ -179,7 +172,7 @@ public class SearchActionTest { @Test public void request_returns_empty_on_table_with_single_row_when_not_requesting_the_first_page() { when(system2.now()).thenReturn(SOME_DATE); - db.organizations().insert(ORGANIZATION_DTO); + db.organizations().insert(); assertThat(executeRequestAndReturnList(2, null)).isEmpty(); assertThat(executeRequestAndReturnList(2, 1)).isEmpty(); @@ -191,11 +184,11 @@ public class SearchActionTest { @Test public void request_returns_rows_ordered_by_createdAt_descending_applying_requested_paging() { when(system2.now()).thenReturn(SOME_DATE, SOME_DATE + 1_000, SOME_DATE + 2_000, SOME_DATE + 3_000, SOME_DATE + 5_000); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid3").setKey("key-3")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid1").setKey("key-1")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid2").setKey("key-2")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid5").setKey("key-5")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid4").setKey("key-4")); + db.organizations().insert(organization -> organization.setKey("key-3")); + db.organizations().insert(organization -> organization.setKey("key-1")); + db.organizations().insert(organization -> organization.setKey("key-2")); + db.organizations().insert(organization -> organization.setKey("key-5")); + db.organizations().insert(organization -> organization.setKey("key-4")); assertThat(executeRequestAndReturnList(1, 1)) .extracting(Organization::getKey) @@ -231,11 +224,11 @@ public class SearchActionTest { @Test public void request_returns_only_specified_keys_ordered_by_createdAt_when_filtering_keys() { when(system2.now()).thenReturn(SOME_DATE, SOME_DATE + 1_000, SOME_DATE + 2_000, SOME_DATE + 3_000, SOME_DATE + 5_000); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid3").setKey("key-3")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid1").setKey("key-1")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid2").setKey("key-2")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid5").setKey("key-5")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid4").setKey("key-4")); + db.organizations().insert(organization -> organization.setKey("key-3")); + db.organizations().insert(organization -> organization.setKey("key-1")); + db.organizations().insert(organization -> organization.setKey("key-2")); + db.organizations().insert(organization -> organization.setKey("key-5")); + db.organizations().insert(organization -> organization.setKey("key-4")); assertThat(executeRequestAndReturnList(1, 10, "key-3", "key-1", "key-5")) .extracting(Organization::getKey) @@ -249,11 +242,11 @@ public class SearchActionTest { @Test public void result_is_paginated() { when(system2.now()).thenReturn(SOME_DATE, SOME_DATE + 1_000, SOME_DATE + 2_000, SOME_DATE + 3_000, SOME_DATE + 5_000); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid3").setKey("key-3")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid1").setKey("key-1")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid2").setKey("key-2")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid5").setKey("key-5")); - db.organizations().insert(ORGANIZATION_DTO.setUuid("uuid4").setKey("key-4")); + db.organizations().insert(organization -> organization.setKey("key-3")); + db.organizations().insert(organization -> organization.setKey("key-1")); + db.organizations().insert(organization -> organization.setKey("key-2")); + db.organizations().insert(organization -> organization.setKey("key-5")); + db.organizations().insert(organizationo -> organizationo.setKey("key-4")); SearchWsResponse response = call(1, 1, "key-1", "key-3", "key-5"); assertThat(response.getOrganizationsList()).extracting(Organization::getKey).containsOnly("key-5"); @@ -275,11 +268,11 @@ public class SearchActionTest { @Test public void request_returns_empty_when_filtering_on_non_existing_key() { when(system2.now()).thenReturn(SOME_DATE); - db.organizations().insert(ORGANIZATION_DTO); + OrganizationDto organization = db.organizations().insert(); - assertThat(executeRequestAndReturnList(1, 10, ORGANIZATION_DTO.getKey())) + assertThat(executeRequestAndReturnList(1, 10, organization.getKey())) .extracting(Organization::getKey) - .containsExactly(ORGANIZATION_DTO.getKey()); + .containsExactly(organization.getKey()); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/empty.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/empty.xml deleted file mode 100644 index 98fb91c83d0..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/empty.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/no_previous_version.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/no_previous_version.xml deleted file mode 100644 index eb9da8278d4..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/no_previous_version.xml +++ /dev/null @@ -1,161 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_deleted.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_deleted.xml deleted file mode 100644 index d235e0a4365..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_deleted.xml +++ /dev/null @@ -1,128 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_is_last_one.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_is_last_one.xml deleted file mode 100644 index 010f700391f..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/previous_version_is_last_one.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/shared.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/shared.xml deleted file mode 100644 index bbd500b4ade..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/shared.xml +++ /dev/null @@ -1,192 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/unprocessed_snapshots.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/unprocessed_snapshots.xml deleted file mode 100644 index fd783cd7c00..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest/unprocessed_snapshots.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_component_id_from_db.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_component_id_from_db.xml index c235e1fc744..222670b1485 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_component_id_from_db.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_component_id_from_db.xml @@ -5,6 +5,7 @@ name="org1_name" guarded="[false]" new_project_private="[false]" + default_quality_gate_uuid="DEFAULT_QG" created_at="1000" updated_at="2000"/> diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_project_id_from_db.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_project_id_from_db.xml index 72c6e708229..f3cfd334264 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_project_id_from_db.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/load_project_id_from_db.xml @@ -5,6 +5,7 @@ name="org1_name" guarded="[false]" new_project_private="[false]" + default_quality_gate_uuid="DEFAULT_QG" created_at="1000" updated_at="2000"/> diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_insert_new_issues.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_insert_new_issues.xml index c235e1fc744..222670b1485 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_insert_new_issues.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_insert_new_issues.xml @@ -5,6 +5,7 @@ name="org1_name" guarded="[false]" new_project_private="[false]" + default_quality_gate_uuid="DEFAULT_QG" created_at="1000" updated_at="2000"/> diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_update_issues.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_update_issues.xml index 0b8edb614d0..879f7973eb9 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_update_issues.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/ServerIssueStorageTest/should_update_issues.xml @@ -5,6 +5,7 @@ name="org1_name" guarded="[false]" new_project_private="[false]" + default_quality_gate_uuid="DEFAULT_QG" created_at="1000" updated_at="2000"/> -- 2.39.5