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;
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);
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();
@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();
@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();
@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();
@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
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 ");
@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();
@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
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 ");
@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();
@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
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 (");
@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
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();
@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());
+ }
+
}
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;
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);
}
@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")
.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);
@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();
@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)
@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)
@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");
@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