package org.sonar.server.component.ws;
import com.google.common.base.Joiner;
-import com.google.common.base.Throwables;
-import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.util.function.Consumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;
-import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.System2;
-import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.SnapshotDto;
+import org.sonar.db.measure.MeasureDto;
+import org.sonar.db.metric.MetricDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.server.es.EsTester;
-import org.sonar.server.measure.index.ProjectMeasuresDoc;
import org.sonar.server.measure.index.ProjectMeasuresIndex;
import org.sonar.server.measure.index.ProjectMeasuresIndexDefinition;
import org.sonar.server.measure.index.ProjectMeasuresIndexer;
import org.sonarqube.ws.WsComponents.SearchProjectsWsResponse;
import org.sonarqube.ws.client.component.SearchProjectsRequest;
-import static com.google.common.collect.Lists.newArrayList;
import static java.util.Arrays.asList;
-import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Optional.ofNullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.server.ws.WebService.Param.SORT;
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.core.util.stream.MoreCollectors.toList;
-import static org.sonar.db.component.ComponentTesting.newDirectory;
-import static org.sonar.db.component.ComponentTesting.newFileDto;
-import static org.sonar.db.component.ComponentTesting.newModuleDto;
-import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
-import static org.sonar.db.component.ComponentTesting.newPublicProjectDto;
-import static org.sonar.db.component.ComponentTesting.newView;
-import static org.sonar.db.component.SnapshotTesting.newAnalysis;
-import static org.sonar.db.metric.MetricTesting.newMetricDto;
-import static org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.INDEX_TYPE_PROJECT_MEASURES;
+import static org.sonar.server.computation.task.projectanalysis.metric.Metric.MetricType.DATA;
+import static org.sonar.server.computation.task.projectanalysis.metric.Metric.MetricType.RATING;
+import static org.sonar.server.es.ProjectIndexer.Cause.PROJECT_CREATION;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_FILTER;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
- public UserSessionRule userSession = UserSessionRule.standalone().logIn().setUserId(23);
+ public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private ComponentDbTester componentDb = new ComponentDbTester(db);
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(es, new ProjectMeasuresIndexer(dbClient, es.client()));
private ProjectMeasuresIndex index = new ProjectMeasuresIndex(es.client(), new AuthorizationTypeSupport(userSession));
+ private ProjectMeasuresIndexer projectMeasuresIndexer = new ProjectMeasuresIndexer(db.getDbClient(), es.client());
private ProjectMeasuresQueryValidator queryValidator = new ProjectMeasuresQueryValidator(dbClient);
private WsActionTester ws = new WsActionTester(
@Test
public void json_example() {
+ userSession.logIn();
OrganizationDto organization1Dto = db.organizations().insertForKey("my-org-key-1");
OrganizationDto organization2Dto = db.organizations().insertForKey("my-org-key-2");
- ComponentDto project1 = insertProjectInDbAndEs(newPublicProjectDto(organization1Dto)
- .setUuid(Uuids.UUID_EXAMPLE_01)
+ ComponentDto project1 = insertProject(organization1Dto, c -> c
.setKey(KeyExamples.KEY_PROJECT_EXAMPLE_001)
.setName("My Project 1")
.setTagsString("finance, java"));
- insertProjectInDbAndEs(newPublicProjectDto(organization1Dto)
- .setUuid(Uuids.UUID_EXAMPLE_02)
+ insertProject(organization1Dto, c -> c
.setKey(KeyExamples.KEY_PROJECT_EXAMPLE_002)
.setName("My Project 2"));
- insertProjectInDbAndEs(newPublicProjectDto(organization2Dto)
- .setUuid(Uuids.UUID_EXAMPLE_03)
+ insertProject(organization2Dto, c -> c
.setKey(KeyExamples.KEY_PROJECT_EXAMPLE_003)
.setName("My Project 3")
.setTagsString("sales, offshore, java"));
- userSession.logIn().setUserId(23);
addFavourite(project1);
- dbSession.commit();
String result = ws.newRequest().execute().getInput();
- assertJson(result).withStrictArrayOrder().isSimilarTo(ws.getDef().responseExampleAsString());
+ assertJson(result).withStrictArrayOrder().ignoreFields("id").isSimilarTo(ws.getDef().responseExampleAsString());
}
@Test
public void order_by_name_case_insensitive() {
- insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Maven"));
- insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Apache"));
- insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("guava"));
+ userSession.logIn();
+ OrganizationDto organization = db.organizations().insert();
+ insertProject(organization, c -> c.setName("Maven"));
+ insertProject(organization, c -> c.setName("Apache"));
+ insertProject(organization, c -> c.setName("guava"));
SearchProjectsWsResponse result = call(request);
@Test
public void paginate_result() {
- IntStream.rangeClosed(1, 9).forEach(i -> insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("PROJECT-" + i)));
+ userSession.logIn();
+ OrganizationDto organization = db.organizations().insert();
+ IntStream.rangeClosed(1, 9).forEach(i -> insertProject(organization, c -> c.setName("PROJECT-" + i)));
SearchProjectsWsResponse result = call(request.setPage(2).setPageSize(3));
@Test
public void empty_result() {
+ userSession.logIn();
+
SearchProjectsWsResponse result = call(request);
assertThat(result.getComponentsCount()).isEqualTo(0);
assertThat(paging.getTotal()).isEqualTo(0);
}
- @Test
- public void return_only_projects() {
- OrganizationDto organizationDto = db.organizations().insert();
- ComponentDto project = newPrivateProjectDto(organizationDto).setName("SonarQube");
- ComponentDto directory = newDirectory(project, "path");
- insertProjectInDbAndEs(project);
- componentDb.insertComponents(newModuleDto(project), newView(organizationDto), directory, newFileDto(project, directory));
-
- SearchProjectsWsResponse result = call(request);
-
- assertThat(result.getComponentsCount()).isEqualTo(1);
- assertThat(result.getComponents(0).getName()).isEqualTo("SonarQube");
- }
-
@Test
public void filter_projects_with_query() {
- OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
- insertMetrics(COVERAGE, NCLOC);
- request.setFilter("coverage <= 80 and ncloc <= 10000");
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto coverage = db.measureDbTester().insertMetric(c -> c.setKey(COVERAGE).setValueType(INT.name()));
+ MetricDto ncloc = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name()));
+ ComponentDto project1 = insertProject(organizationDto,
+ new Measure(coverage, c -> c.setValue(81d)),
+ new Measure(ncloc, c -> c.setValue(10_000d)));
+ ComponentDto project2 = insertProject(organizationDto,
+ new Measure(coverage, c -> c.setValue(80d)),
+ new Measure(ncloc, c -> c.setValue(10_000d)));
+ ComponentDto project3 = insertProject(organizationDto,
+ new Measure(coverage, c -> c.setValue(80d)),
+ new Measure(ncloc, c -> c.setValue(10_001d)));
- SearchProjectsWsResponse result = call(request);
+ SearchProjectsWsResponse result = call(request.setFilter("coverage <= 80 and ncloc <= 10000"));
- assertThat(result.getComponentsCount()).isEqualTo(1);
- assertThat(result.getComponents(0).getName()).isEqualTo("Sonar Markdown");
+ assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactly(project2.key());
}
@Test
public void filter_projects_with_query_within_specified_organization() {
+ userSession.logIn();
OrganizationDto organization1 = db.organizations().insert();
OrganizationDto organization2 = db.organizations().insert();
- insertProjectInDbAndEs(newPrivateProjectDto(organization1).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization1).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization2).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
- insertMetrics(COVERAGE, NCLOC);
+ MetricDto coverage = db.measureDbTester().insertMetric(c -> c.setKey(COVERAGE).setValueType(INT.name()));
+ MetricDto ncloc = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name()));
+ ComponentDto project1 = insertProject(organization1, new Measure(coverage, c -> c.setValue(81d)), new Measure(ncloc, c -> c.setValue(10_000d)));
+ ComponentDto project2 = insertProject(organization1, new Measure(coverage, c -> c.setValue(80d)), new Measure(ncloc, c -> c.setValue(10_000d)));
+ ComponentDto project3 = insertProject(organization2, new Measure(coverage, c -> c.setValue(80d)), new Measure(ncloc, c -> c.setValue(10_000d)));
assertThat(call(request.setOrganization(null)).getComponentsList())
- .extracting(Component::getName)
- .containsOnly("Sonar Java", "Sonar Markdown", "Sonar Qube");
+ .extracting(Component::getKey)
+ .containsOnly(project1.getKey(), project2.getKey(), project3.getKey());
assertThat(call(request.setOrganization(organization1.getKey())).getComponentsList())
- .extracting(Component::getName)
- .containsOnly("Sonar Java", "Sonar Markdown");
+ .extracting(Component::getKey)
+ .containsOnly(project1.getKey(), project2.getKey());
assertThat(call(request.setOrganization(organization2.getKey())).getComponentsList())
- .extracting(Component::getName)
- .containsOnly("Sonar Qube");
+ .extracting(Component::getKey)
+ .containsOnly(project3.getKey());
}
@Test
public void filter_projects_by_quality_gate() {
- OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Java"), "OK");
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Markdown"), "OK");
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Qube"), "ERROR");
- insertMetrics(COVERAGE, NCLOC);
- request.setFilter("alert_status = OK");
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto qualityGateStatus = db.measureDbTester().insertMetric(c -> c.setKey(QUALITY_GATE_STATUS).setValueType(LEVEL.name()));
+ ComponentDto project1 = insertProject(organizationDto, new Measure(qualityGateStatus, c -> c.setData("OK")));
+ ComponentDto project2 = insertProject(organizationDto, new Measure(qualityGateStatus, c -> c.setData("OK")));
+ ComponentDto project3 = insertProject(organizationDto, new Measure(qualityGateStatus, c -> c.setData("ERROR")));
- SearchProjectsWsResponse result = call(request);
+ SearchProjectsWsResponse result = call(request.setFilter("alert_status = OK"));
- assertThat(result.getComponentsList()).extracting(Component::getName).containsOnly("Sonar Java", "Sonar Markdown");
+ assertThat(result.getComponentsList())
+ .extracting(Component::getKey)
+ .containsExactlyInAnyOrder(project1.getKey(), project2.getKey());
}
@Test
public void filter_projects_by_languages() {
- OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("<null>", "java", "xoo"));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81)), null, asList("java", "xoo"));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("xoo"));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("<null>", "java", "xoo"));
- insertMetrics(COVERAGE, NCLOC_LANGUAGE_DISTRIBUTION_KEY);
- request.setFilter("languages IN (java, js, <null>)");
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto languagesDistribution = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY).setValueType(DATA.name()));
+ ComponentDto project1 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("<null>=2;java=6;xoo=18")));
+ ComponentDto project2 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("java=3;xoo=9")));
+ ComponentDto project3 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("xoo=1")));
+ ComponentDto project4 = insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("<null>=1;java=5;xoo=13")));
- SearchProjectsWsResponse result = call(request);
+ SearchProjectsWsResponse result = call(request.setFilter("languages IN (java, js, <null>)"));
+
+ assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project2.getKey(), project4.getKey());
+ }
+
+ @Test
+ public void filter_projects_by_reliability_rating() {
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto reliabilityRating = db.measureDbTester().insertMetric(c -> c.setKey(RELIABILITY_RATING_KEY).setValueType(INT.name()));
+ ComponentDto project1 = insertProject(organizationDto, new Measure(reliabilityRating, c -> c.setValue(1d)));
+ ComponentDto project2 = insertProject(organizationDto, new Measure(reliabilityRating, c -> c.setValue(2d)));
+ ComponentDto project3 = insertProject(organizationDto, new Measure(reliabilityRating, c -> c.setValue(3d)));
+
+ SearchProjectsWsResponse result = call(request.setFilter("reliability_rating = 2"));
+
+ assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactly(project2.getKey());
+ }
+
+ @Test
+ public void filter_projects_by_new_reliability_rating() {
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto newReliabilityRating = db.measureDbTester().insertMetric(c -> c.setKey(NEW_RELIABILITY_RATING_KEY).setValueType(INT.name()));
+ ComponentDto project1 = insertProject(organizationDto, new Measure(newReliabilityRating, c -> c.setVariation(1d)));
+ ComponentDto project2 = insertProject(organizationDto, new Measure(newReliabilityRating, c -> c.setVariation(2d)));
+ ComponentDto project3 = insertProject(organizationDto, new Measure(newReliabilityRating, c -> c.setVariation(3d)));
- assertThat(result.getComponentsList()).extracting(Component::getName).containsOnly("Sonar Java", "Sonar Groovy", "Sonar Qube");
+ SearchProjectsWsResponse result = call(request.setFilter("new_reliability_rating = 2"));
+
+ assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactly(project2.getKey());
}
@Test
public void filter_projects_by_tags() {
- OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Java").setTags(newArrayList("finance", "platform")));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Markdown").setTags(singletonList("marketing")));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setName("Sonar Qube").setTags(newArrayList("offshore")));
- request.setFilter("tags in (finance, offshore)");
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ ComponentDto project1 = insertProject(organizationDto, c -> c.setTags(asList("finance", "platform")));
+ ComponentDto project2 = insertProject(organizationDto, c -> c.setTags(singletonList("marketing")));
+ ComponentDto project3 = insertProject(organizationDto, c -> c.setTags(singletonList("offshore")));
- SearchProjectsWsResponse result = call(request);
+ SearchProjectsWsResponse result = call(request.setFilter("tags in (finance, offshore)"));
- assertThat(result.getComponentsList()).extracting(Component::getName).containsOnly("Sonar Java", "Sonar Qube");
+ assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project3.getKey());
}
@Test
public void filter_projects_by_text_query() {
- OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setKey("sonar-java").setName("Sonar Java"));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setKey("sonar-groovy").setName("Sonar Groovy"));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setKey("sonar-markdown").setName("Sonar Markdown"));
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto).setKey("sonarqube").setName("Sonar Qube"));
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ insertProject(organizationDto, c -> c.setKey("sonar-java").setName("Sonar Java"));
+ insertProject(organizationDto, c -> c.setKey("sonar-groovy").setName("Sonar Groovy"));
+ insertProject(organizationDto, c -> c.setKey("sonar-markdown").setName("Sonar Markdown"));
+ insertProject(organizationDto, c -> c.setKey("sonarqube").setName("Sonar Qube"));
assertThat(call(request.setFilter("query = \"Groovy\"")).getComponentsList()).extracting(Component::getName).containsOnly("Sonar Groovy");
assertThat(call(request.setFilter("query = \"oNar\"")).getComponentsList()).extracting(Component::getName).containsOnly("Sonar Java", "Sonar Groovy", "Sonar Markdown",
OrganizationDto organization3 = db.organizations().insert();
OrganizationDto organization4 = db.organizations().insert();
OrganizationDto organization5 = db.organizations().insert();
- List<Map<String, Object>> someMeasure = singletonList(newMeasure(COVERAGE, 81));
- ComponentDto favourite1_1 = insertProjectInDbAndEs(newPrivateProjectDto(organization1), someMeasure);
- ComponentDto favourite1_2 = insertProjectInDbAndEs(newPrivateProjectDto(organization1), someMeasure);
- ComponentDto nonFavourite1 = insertProjectInDbAndEs(newPrivateProjectDto(organization1), someMeasure);
- ComponentDto favourite2 = insertProjectInDbAndEs(newPrivateProjectDto(organization2), someMeasure);
- ComponentDto nonFavourite2 = insertProjectInDbAndEs(newPrivateProjectDto(organization2), someMeasure);
- ComponentDto favourite3 = insertProjectInDbAndEs(newPrivateProjectDto(organization3), someMeasure);
- ComponentDto nonFavourite4 = insertProjectInDbAndEs(newPrivateProjectDto(organization4), someMeasure);
- Stream.of(favourite1_1, favourite1_2, favourite2, favourite3)
- .forEach(this::addFavourite);
- insertMetrics(COVERAGE, NCLOC);
+ ComponentDto favourite1_1 = insertProject(organization1);
+ ComponentDto favourite1_2 = insertProject(organization1);
+ ComponentDto nonFavourite1 = insertProject(organization1);
+ ComponentDto favourite2 = insertProject(organization2);
+ ComponentDto nonFavourite2 = insertProject(organization2);
+ ComponentDto favourite3 = insertProject(organization3);
+ ComponentDto nonFavourite4 = insertProject(organization4);
+ Stream.of(favourite1_1, favourite1_2, favourite2, favourite3).forEach(this::addFavourite);
assertThat(call(request.setFilter(null).setOrganization(null)).getComponentsList())
.extracting(Component::getName)
@Test
public void filter_projects_on_favorites() {
userSession.logIn();
- ComponentDto javaProject = insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization(), "java-id").setName("Sonar Java"),
- newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d)));
- ComponentDto markDownProject = insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization(), "markdown-id").setName("Sonar Markdown"),
- newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(db.organizations().insert()).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
- addFavourite(javaProject);
- addFavourite(markDownProject);
- dbSession.commit();
- request.setFilter("isFavorite");
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto javaProject = insertProject(organization);
+ ComponentDto markDownProject = insertProject(organization);
+ ComponentDto sonarQubeProject = insertProject(organization);
+ Stream.of(javaProject, markDownProject).forEach(this::addFavourite);
- SearchProjectsWsResponse result = call(request);
+ SearchProjectsWsResponse result = call(request.setFilter("isFavorite"));
assertThat(result.getComponentsCount()).isEqualTo(2);
- assertThat(result.getComponentsList()).extracting(Component::getId).containsExactly("java-id", "markdown-id");
+ assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactly(javaProject.getKey(), markDownProject.getKey());
}
@Test
public void filtering_on_favorites_returns_empty_results_if_not_logged_in() {
- ComponentDto javaProject = insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization(), "java-id").setName("Sonar Java"),
- newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(db.organizations().insert()).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
- addFavourite(javaProject);
- dbSession.commit();
- request.setFilter("isFavorite");
userSession.anonymous();
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto javaProject = insertProject(organization);
+ ComponentDto markDownProject = insertProject(organization);
+ ComponentDto sonarQubeProject = insertProject(organization);
+ Stream.of(javaProject, markDownProject).forEach(this::addFavourite);
- SearchProjectsWsResponse result = call(request);
+ SearchProjectsWsResponse result = call(request.setFilter("isFavorite"));
- assertThat(result.getComponentsCount()).isEqualTo(0);
+ assertThat(result.getComponentsCount()).isZero();
}
@Test
public void do_not_return_isFavorite_if_anonymous_user() {
- insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81)));
- insertMetrics(COVERAGE);
userSession.anonymous();
+ OrganizationDto organization = db.organizations().insert();
+ insertProject(organization);
SearchProjectsWsResponse result = call(request);
- assertThat(result.getComponentsCount()).isEqualTo(1);
- assertThat(result.getComponents(0).hasIsFavorite()).isFalse();
- }
-
- @Test
- public void empty_list_if_isFavorite_filter_and_anonymous_user() {
- insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81)));
- insertMetrics(COVERAGE);
- userSession.anonymous();
- request.setFilter("isFavorite");
-
- SearchProjectsWsResponse result = call(request);
-
- assertThat(result.getComponentsCount()).isEqualTo(0);
+ assertThat(result.getComponentsList()).extracting(Component::hasIsFavorite).containsExactlyInAnyOrder(false);
}
@Test
public void return_nloc_facet() {
- OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
- insertMetrics(COVERAGE, NCLOC);
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto ncloc = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name()));
+ insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(5d)));
+ insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(5d)));
+ insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(10_000d)));
+ insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(500_001d)));
SearchProjectsWsResponse result = call(request.setFacets(singletonList(NCLOC)));
Common.Facet facet = result.getFacets().getFacetsList().stream()
.filter(oneFacet -> NCLOC.equals(oneFacet.getProperty()))
.findFirst().orElseThrow(IllegalStateException::new);
- assertThat(facet.getProperty()).isEqualTo(NCLOC);
- assertThat(facet.getValuesCount()).isEqualTo(5);
assertThat(facet.getValuesList())
.extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
.containsExactly(
@Test
public void return_languages_facet() {
- OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("<null>", "java", "xoo"));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81)), null, asList("java", "xoo"));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("xoo"));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("<null>", "java", "xoo"));
- insertMetrics(COVERAGE, NCLOC_LANGUAGE_DISTRIBUTION_KEY);
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto languagesDistribution = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY).setValueType(DATA.name()));
+ insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("<null>=2;java=6;xoo=18")));
+ insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("java=5;xoo=19")));
+ insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("xoo=1")));
+ insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("<null>=1;java=3;xoo=8")));
SearchProjectsWsResponse result = call(request.setFacets(singletonList(FILTER_LANGUAGES)));
Common.Facet facet = result.getFacets().getFacetsList().stream()
.filter(oneFacet -> FILTER_LANGUAGES.equals(oneFacet.getProperty()))
.findFirst().orElseThrow(IllegalStateException::new);
- assertThat(facet.getProperty()).isEqualTo(FILTER_LANGUAGES);
assertThat(facet.getValuesList())
.extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
.containsExactly(
@Test
public void return_languages_facet_with_language_having_no_project_if_language_is_in_filter() {
- OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("<null>", "java"));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81)), null, asList("java"));
- insertMetrics(COVERAGE, NCLOC_LANGUAGE_DISTRIBUTION_KEY);
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto languagesDistribution = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY).setValueType(DATA.name()));
+ insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("<null>=2;java=6")));
+ insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("java=5")));
SearchProjectsWsResponse result = call(request.setFilter("languages = xoo").setFacets(singletonList(FILTER_LANGUAGES)));
@Test
public void return_tags_facet() {
+ userSession.logIn();
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java").setTags(newArrayList("finance", "platform")));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown").setTags(singletonList("offshore")));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube").setTags(newArrayList("offshore")));
+ insertProject(organization, c -> c.setTags(asList("finance", "platform")));
+ insertProject(organization, c -> c.setTags(singletonList("offshore")));
+ insertProject(organization, c -> c.setTags(singletonList("offshore")));
SearchProjectsWsResponse result = call(request.setFacets(singletonList(FILTER_TAGS)));
Common.Facet facet = result.getFacets().getFacetsList().stream()
.filter(oneFacet -> FILTER_TAGS.equals(oneFacet.getProperty()))
.findFirst().orElseThrow(IllegalStateException::new);
- assertThat(facet.getProperty()).isEqualTo(FILTER_TAGS);
assertThat(facet.getValuesList())
.extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
.containsExactly(
@Test
public void return_tags_facet_with_tags_having_no_project_if_tags_is_in_filter() {
+ userSession.logIn();
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java").setTags(newArrayList("finance", "platform")));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown").setTags(singletonList("offshore")));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube").setTags(newArrayList("offshore")));
+ insertProject(organization, c -> c.setTags(asList("finance", "platform")));
+ insertProject(organization, c -> c.setTags(singletonList("offshore")));
+ insertProject(organization, c -> c.setTags(singletonList("offshore")));
SearchProjectsWsResponse result = call(request.setFilter("tags = marketing").setFacets(singletonList(FILTER_TAGS)));
@Test
public void return_reliability_rating_facet() throws Exception {
- OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(RELIABILITY_RATING_KEY, 1d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(RELIABILITY_RATING_KEY, 1d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(RELIABILITY_RATING_KEY, 3d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(RELIABILITY_RATING_KEY, 5d)));
- insertMetrics(RELIABILITY_RATING_KEY);
+ userSession.logIn();
+ OrganizationDto organization = db.organizations().insert();
+ MetricDto reliabilityRating = db.measureDbTester().insertMetric(c -> c.setKey(RELIABILITY_RATING_KEY).setValueType(RATING.name()));
+ insertProject(organization, new Measure(reliabilityRating, c -> c.setValue(1d)));
+ insertProject(organization, new Measure(reliabilityRating, c -> c.setValue(1d)));
+ insertProject(organization, new Measure(reliabilityRating, c -> c.setValue(3d)));
+ insertProject(organization, new Measure(reliabilityRating, c -> c.setValue(5d)));
SearchProjectsWsResponse result = call(request.setFacets(singletonList(RELIABILITY_RATING_KEY)));
@Test
public void return_new_reliability_rating_facet() throws Exception {
- OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(NEW_RELIABILITY_RATING_KEY, 1d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(NEW_RELIABILITY_RATING_KEY, 1d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(NEW_RELIABILITY_RATING_KEY, 3d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(NEW_RELIABILITY_RATING_KEY, 5d)));
- insertMetrics(NEW_RELIABILITY_RATING_KEY);
+ userSession.logIn();
+ OrganizationDto organization = db.organizations().insert();
+ MetricDto newReliabilityRating = db.measureDbTester().insertMetric(c -> c.setKey(NEW_RELIABILITY_RATING_KEY).setValueType(RATING.name()));
+ insertProject(organization, new Measure(newReliabilityRating, c -> c.setVariation(1d)));
+ insertProject(organization, new Measure(newReliabilityRating, c -> c.setVariation(1d)));
+ insertProject(organization, new Measure(newReliabilityRating, c -> c.setVariation(3d)));
+ insertProject(organization, new Measure(newReliabilityRating, c -> c.setVariation(5d)));
SearchProjectsWsResponse result = call(request.setFacets(singletonList(NEW_RELIABILITY_RATING_KEY)));
@Test
public void default_sort_is_by_ascending_name() throws Exception {
+ userSession.logIn();
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
+ insertProject(organization, c -> c.setName("Sonar Java"));
+ insertProject(organization, c -> c.setName("Sonar Groovy"));
+ insertProject(organization, c -> c.setName("Sonar Markdown"));
+ insertProject(organization, c -> c.setName("Sonar Qube"));
SearchProjectsWsResponse result = call(request);
@Test
public void sort_by_name() throws Exception {
+ userSession.logIn();
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
+ insertProject(organization, c -> c.setName("Sonar Java"));
+ insertProject(organization, c -> c.setName("Sonar Groovy"));
+ insertProject(organization, c -> c.setName("Sonar Markdown"));
+ insertProject(organization, c -> c.setName("Sonar Qube"));
assertThat(call(request.setSort("name").setAsc(true)).getComponentsList()).extracting(Component::getName)
.containsExactly("Sonar Groovy", "Sonar Java", "Sonar Markdown", "Sonar Qube");
}
@Test
- public void sort_by_coverage() throws Exception {
- OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
- insertMetrics(COVERAGE);
-
- assertThat(call(request.setSort(COVERAGE).setAsc(true)).getComponentsList()).extracting(Component::getName)
- .containsExactly("Sonar Markdown", "Sonar Qube", "Sonar Groovy", "Sonar Java");
- assertThat(call(request.setSort(COVERAGE).setAsc(false)).getComponentsList()).extracting(Component::getName)
- .containsExactly("Sonar Groovy", "Sonar Java", "Sonar Markdown", "Sonar Qube");
+ public void sort_by_coverage_then_by_name() throws Exception {
+ userSession.logIn();
+ OrganizationDto organizationDto = db.organizations().insert();
+ MetricDto coverage = db.measureDbTester().insertMetric(c -> c.setKey(COVERAGE).setValueType(INT.name()));
+ ComponentDto project1 = insertProject(organizationDto, c -> c.setName("Sonar Java"), new Measure(coverage, c -> c.setValue(81d)));
+ ComponentDto project2 = insertProject(organizationDto, c -> c.setName("Sonar Groovy"), new Measure(coverage, c -> c.setValue(81d)));
+ ComponentDto project3 = insertProject(organizationDto, c -> c.setName("Sonar Markdown"), new Measure(coverage, c -> c.setValue(80d)));
+ ComponentDto project4 = insertProject(organizationDto, c -> c.setName("Sonar Qube"), new Measure(coverage, c -> c.setValue(80d)));
+
+ assertThat(call(request.setSort(COVERAGE).setAsc(true)).getComponentsList()).extracting(Component::getKey)
+ .containsExactly(project3.getKey(), project4.getKey(), project2.getKey(), project1.getKey());
+ assertThat(call(request.setSort(COVERAGE).setAsc(false)).getComponentsList()).extracting(Component::getKey)
+ .containsExactly(project2.getKey(), project1.getKey(), project3.getKey(), project4.getKey());
}
@Test
- public void sort_by_quality_gate() throws Exception {
- OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), "ERROR");
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Groovy"), "WARN");
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Markdown"), "OK");
- insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Qube"), "OK");
- dbClient.metricDao().insert(dbSession, newMetricDto().setKey(QUALITY_GATE_STATUS).setValueType(LEVEL.name()).setEnabled(true).setHidden(false));
- db.commit();
+ public void sort_by_quality_gate_then_by_name() throws Exception {
+ userSession.logIn();
+ OrganizationDto organization = db.organizations().insert();
+ MetricDto qualityGateStatus = db.measureDbTester().insertMetric(c -> c.setKey(QUALITY_GATE_STATUS).setValueType(LEVEL.name()));
+ ComponentDto project1 = insertProject(organization, c -> c.setName("Sonar Java"), new Measure(qualityGateStatus, c -> c.setData("ERROR")));
+ ComponentDto project2 = insertProject(organization, c -> c.setName("Sonar Groovy"), new Measure(qualityGateStatus, c -> c.setData("WARN")));
+ ComponentDto project3 = insertProject(organization, c -> c.setName("Sonar Markdown"), new Measure(qualityGateStatus, c -> c.setData("OK")));
+ ComponentDto project4 = insertProject(organization, c -> c.setName("Sonar Qube"), new Measure(qualityGateStatus, c -> c.setData("OK")));
- assertThat(call(request.setSort(QUALITY_GATE_STATUS).setAsc(true)).getComponentsList()).extracting(Component::getName)
- .containsExactly("Sonar Markdown", "Sonar Qube", "Sonar Groovy", "Sonar Java");
- assertThat(call(request.setSort(QUALITY_GATE_STATUS).setAsc(false)).getComponentsList()).extracting(Component::getName)
- .containsExactly("Sonar Java", "Sonar Groovy", "Sonar Markdown", "Sonar Qube");
+ assertThat(call(request.setSort(QUALITY_GATE_STATUS).setAsc(true)).getComponentsList()).extracting(Component::getKey)
+ .containsExactly(project3.getKey(), project4.getKey(), project2.getKey(), project1.getKey());
+ assertThat(call(request.setSort(QUALITY_GATE_STATUS).setAsc(false)).getComponentsList()).extracting(Component::getKey)
+ .containsExactly(project1.getKey(), project2.getKey(), project3.getKey(), project4.getKey());
}
@Test
public void return_last_analysis_date() {
- OrganizationDto organizationDto = db.organizations().insert();
- ComponentDto project1 = insertProjectInDbAndEs(newPrivateProjectDto(organizationDto));
- db.components().insertSnapshot(newAnalysis(project1).setCreatedAt(10_000_000_000L).setLast(false));
- db.components().insertSnapshot(newAnalysis(project1).setCreatedAt(20_000_000_000L).setLast(true));
- ComponentDto project2 = insertProjectInDbAndEs(newPrivateProjectDto(organizationDto));
- db.components().insertSnapshot(newAnalysis(project2).setCreatedAt(30_000_000_000L).setLast(true));
+ userSession.logIn();
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project1 = db.components().insertPublicProject(organization);
+ db.components().insertSnapshot(project1, snapshot -> snapshot.setCreatedAt(10_000_000_000L).setLast(false));
+ db.components().insertSnapshot(project1, snapshot -> snapshot.setCreatedAt(20_000_000_000L).setLast(true));
+ authorizationIndexerTester.allowOnlyAnyone(project1);
+ ComponentDto project2 = db.components().insertPublicProject(organization);
+ db.components().insertSnapshot(project2, snapshot -> snapshot.setCreatedAt(30_000_000_000L).setLast(true));
+ authorizationIndexerTester.allowOnlyAnyone(project2);
// No snapshot on project 3
- insertProjectInDbAndEs(newPrivateProjectDto(organizationDto));
+ ComponentDto project3 = db.components().insertPublicProject(organization);
+ authorizationIndexerTester.allowOnlyAnyone(project3);
+ projectMeasuresIndexer.indexOnStartup(null);
SearchProjectsWsResponse result = call(request.setAdditionalFields(singletonList("analysisDate")));
@Test
public void return_visibility_flag() {
+ userSession.logIn();
OrganizationDto organization = db.organizations().insert();
- ComponentDto privateProject = insertProjectInDbAndEs(newPrivateProjectDto(organization));
- ComponentDto publicProject = insertProjectInDbAndEs(newPublicProjectDto(organization));
+ ComponentDto privateProject = db.components().insertPublicProject(organization);
+ authorizationIndexerTester.allowOnlyAnyone(privateProject);
+ ComponentDto publicProject = db.components().insertPrivateProject(organization);
+ authorizationIndexerTester.allowOnlyAnyone(publicProject);
+ projectMeasuresIndexer.indexOnStartup(null);
SearchProjectsWsResponse result = call(request);
assertThat(result.getComponentsList()).extracting(Component::getKey, Component::getVisibility)
.containsExactly(
- tuple(privateProject.getKey(), privateProject.isPrivate() ? "private" : "public"),
- tuple(publicProject.getKey(), publicProject.isPrivate() ? "private" : "public"));
+ tuple(privateProject.getKey(), privateProject.isPrivate() ? "private" : "public"),
+ tuple(publicProject.getKey(), publicProject.isPrivate() ? "private" : "public"));
}
@Test
public void fail_when_metrics_are_unknown() {
+ userSession.logIn();
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Unknown metric(s) [coverage, debt]");
@Test
public void fail_if_page_size_greater_than_500() {
+ userSession.logIn();
+
expectedException.expect(IllegalArgumentException.class);
call(request.setPageSize(501));
return httpRequest.executeProtobuf(SearchProjectsWsResponse.class);
}
- private ComponentDto insertProjectInDbAndEs(ComponentDto project) {
- return insertProjectInDbAndEs(project, emptyList());
+ private void addFavourite(ComponentDto project) {
+ dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("favourite").setResourceId(project.getId()).setUserId(userSession.getUserId()));
+ dbSession.commit();
}
- private ComponentDto insertProjectInDbAndEs(ComponentDto project, List<Map<String, Object>> measures) {
- return insertProjectInDbAndEs(project, measures, null, emptyList());
+ private ComponentDto insertProject(OrganizationDto organizationDto, Measure... measures) {
+ return insertProject(organizationDto, c -> {
+ }, measures);
}
- private ComponentDto insertProjectInDbAndEs(ComponentDto project, String qualityGateStatus) {
- return insertProjectInDbAndEs(project, emptyList(), qualityGateStatus, emptyList());
+ private ComponentDto insertProject(OrganizationDto organizationDto, Consumer<ComponentDto> projectConsumer, Measure... measures) {
+ ComponentDto project = db.components().insertPublicProject(organizationDto, projectConsumer);
+ SnapshotDto analysis = db.components().insertSnapshot(project);
+ Arrays.stream(measures).forEach(m -> db.measureDbTester().insertMeasure(project, analysis, m.metric, m.consumer));
+ authorizationIndexerTester.allowOnlyAnyone(project);
+ projectMeasuresIndexer.indexProject(project.uuid(), PROJECT_CREATION);
+ return project;
}
- private ComponentDto insertProjectInDbAndEs(ComponentDto project, List<Map<String, Object>> measures, @Nullable String qualityGateStatus,
- List<String> languages) {
- ComponentDto res = componentDb.insertComponent(project);
- try {
- es.putDocuments(INDEX_TYPE_PROJECT_MEASURES,
- new ProjectMeasuresDoc()
- .setOrganizationUuid(project.getOrganizationUuid())
- .setId(project.uuid())
- .setKey(project.key())
- .setName(project.name())
- .setMeasures(measures)
- .setQualityGateStatus(qualityGateStatus)
- .setLanguages(languages)
- .setTags(project.getTags()));
- authorizationIndexerTester.allowOnlyAnyone(project);
- } catch (Exception e) {
- Throwables.propagate(e);
- }
+ private static class Measure {
+ private final MetricDto metric;
+ private final Consumer<MeasureDto> consumer;
- return res;
- }
-
- private void insertMetrics(String... metricKeys) {
- for (String metricKey : metricKeys) {
- dbClient.metricDao().insert(dbSession, newMetricDto().setKey(metricKey).setValueType(INT.name()).setEnabled(true).setHidden(false));
+ public Measure(MetricDto metric, Consumer<MeasureDto> consumer) {
+ this.metric = metric;
+ this.consumer = consumer;
}
- dbSession.commit();
- }
-
- private static Map<String, Object> newMeasure(String key, Object value) {
- return ImmutableMap.of("key", key, "value", value);
- }
-
- private void addFavourite(ComponentDto project) {
- dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("favourite").setResourceId(project.getId()).setUserId(userSession.getUserId()));
}
}