package it.projectSearch;
import com.sonar.orchestrator.Orchestrator;
-import it.Category4Suite;
+import com.sonar.orchestrator.build.SonarScanner;
+import it.Category6Suite;
import java.io.IOException;
+import org.assertj.core.groups.Tuple;
+import org.junit.After;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
-import org.sonarqube.ws.Common.FacetValue;
+import org.sonarqube.ws.Common;
import org.sonarqube.ws.WsComponents.Component;
import org.sonarqube.ws.WsComponents.SearchProjectsWsResponse;
import org.sonarqube.ws.client.component.SearchProjectsRequest;
+import org.sonarqube.ws.client.organization.CreateWsRequest;
+import org.sonarqube.ws.client.project.CreateRequest;
+import util.ItUtils;
import static com.sonar.orchestrator.build.SonarScanner.create;
+import static it.Category6Suite.enableOrganizationsSupport;
+import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
+import static util.ItUtils.deleteOrganizationsIfExists;
import static util.ItUtils.newAdminWsClient;
+import static util.ItUtils.newProjectKey;
import static util.ItUtils.projectDir;
+import static util.ItUtils.restoreProfile;
+import static util.ItUtils.setServerProperty;
/**
* Tests WS api/components/search_projects
*/
public class SearchProjectsTest {
- private static final String PROJECT_KEY = "sample";
- private static final String PROJECT_NAME = "Sample";
-
@ClassRule
- public static Orchestrator orchestrator = Category4Suite.ORCHESTRATOR;
+ public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR;
+
+ private String organizationKey;
+
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ enableOrganizationsSupport();
+ }
@Before
public void setUp() throws Exception {
- orchestrator.resetData();
+ organizationKey = ItUtils.newOrganizationKey();
+ newAdminWsClient(orchestrator).organizations().create(CreateWsRequest.builder()
+ .setKey(organizationKey)
+ .setName(organizationKey)
+ .build());
+ restoreProfile(orchestrator, SearchProjectsTest.class.getResource("/projectSearch/SearchProjectsTest/with-many-rules.xml"), organizationKey);
}
- @Test
- public void filter_projects_by_measure_values() throws Exception {
- orchestrator.executeBuild(create(projectDir("shared/xoo-sample")));
-
- verifyFilterMatches("ncloc > 1");
- verifyFilterMatches("ncloc > 1 and comment_lines < 10000");
- verifyFilterDoesNotMatch("ncloc <= 1");
+ @After
+ public void tearDown() throws Exception {
+ deleteOrganizationsIfExists(orchestrator, organizationKey);
}
@Test
- public void provisioned_projects_should_be_included_to_results() throws Exception {
- orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_NAME);
-
- SearchProjectsWsResponse response = searchProjects(SearchProjectsRequest.builder().build());
+ public void filter_projects_by_measure_values() throws Exception {
+ String projectKey = newProjectKey();
+ analyzeProject(projectKey, "shared/xoo-sample");
- assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly(PROJECT_KEY);
+ verifyFilterMatches(projectKey, "ncloc > 1");
+ verifyFilterMatches(projectKey, "ncloc > 1 and comment_lines < 10000");
+ verifyFilterDoesNotMatch("ncloc <= 1");
}
@Test
.extracting(Component::getKey).containsExactly("project3");
assertThat(searchProjects(SearchProjectsRequest.builder().setFilter("query = \"nd\" AND ncloc > 50").build()).getComponentsList())
.extracting(Component::getKey).containsExactly("project3", "project4");
- assertThat(searchProjects(SearchProjectsRequest.builder().setFilter("query = \"unknown\" AND ncloc > 50").build()).getComponentsList()).isEmpty();;
+ assertThat(searchProjects(SearchProjectsRequest.builder().setFilter("query = \"unknown\" AND ncloc > 50").build()).getComponentsList()).isEmpty();
+ ;
// Check facets
assertThat(searchProjects(SearchProjectsRequest.builder().setFilter("query = \"apache\"").setFacets(singletonList("ncloc")).build()).getFacets().getFacets(0).getValuesList())
- .extracting(FacetValue::getVal, FacetValue::getCount)
+ .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
.containsOnly(tuple("*-1000.0", 3L), tuple("1000.0-10000.0", 0L), tuple("10000.0-100000.0", 0L), tuple("100000.0-500000.0", 0L), tuple("500000.0-*", 0L));
assertThat(searchProjects(SearchProjectsRequest.builder().setFilter("query = \"unknown\"").setFacets(singletonList("ncloc")).build()).getFacets().getFacets(0)
- .getValuesList()).extracting(FacetValue::getVal, FacetValue::getCount)
- .containsOnly(tuple("*-1000.0", 0L), tuple("1000.0-10000.0", 0L), tuple("10000.0-100000.0", 0L), tuple("100000.0-500000.0", 0L), tuple("500000.0-*", 0L));
+ .getValuesList()).extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
+ .containsOnly(tuple("*-1000.0", 0L), tuple("1000.0-10000.0", 0L), tuple("10000.0-100000.0", 0L), tuple("100000.0-500000.0", 0L), tuple("500000.0-*", 0L));
+ }
+
+ @Test
+ public void provisioned_projects_should_be_included_to_results() throws Exception {
+ String projectKey = newProjectKey();
+ newAdminWsClient(orchestrator).projects().create(CreateRequest.builder().setKey(projectKey).setName(projectKey).setOrganization(organizationKey).build());
+
+ SearchProjectsWsResponse response = searchProjects(SearchProjectsRequest.builder().setOrganization(organizationKey).build());
+
+ assertThat(response.getComponentsList()).extracting(Component::getKey).contains(projectKey);
+ }
+
+ @Test
+ public void should_return_facets() throws Exception {
+ analyzeProject(newProjectKey(), "shared/xoo-sample");
+ analyzeProject(newProjectKey(), "shared/xoo-multi-modules-sample");
+
+ SearchProjectsWsResponse response = searchProjects(SearchProjectsRequest.builder().setOrganization(organizationKey).setFacets(asList(
+ "alert_status",
+ "coverage",
+ "duplicated_lines_density",
+ "languages",
+ "ncloc",
+ "reliability_rating",
+ "security_rating",
+ "sqale_rating",
+ "tags")).build());
+
+ checkFacet(response, "alert_status",
+ tuple("OK", 2L),
+ tuple("WARN", 0L),
+ tuple("ERROR", 0L));
+ checkFacet(response, "coverage",
+ tuple("*-30.0", 0L),
+ tuple("30.0-50.0", 0L),
+ tuple("50.0-70.0", 0L),
+ tuple("70.0-80.0", 0L),
+ tuple("80.0-*", 0L));
+ checkFacet(response, "duplicated_lines_density",
+ tuple("*-3.0", 2L),
+ tuple("3.0-5.0", 0L),
+ tuple("5.0-10.0", 0L),
+ tuple("10.0-20.0", 0L),
+ tuple("20.0-*", 0L));
+ checkFacet(response, "languages",
+ tuple("xoo", 2L));
+ checkFacet(response, "ncloc",
+ tuple("*-1000.0", 2L),
+ tuple("1000.0-10000.0", 0L),
+ tuple("10000.0-100000.0", 0L),
+ tuple("100000.0-500000.0", 0L),
+ tuple("500000.0-*", 0L));
+ checkFacet(response, "reliability_rating",
+ tuple("1", 2L),
+ tuple("2", 0L),
+ tuple("3", 0L),
+ tuple("4", 0L),
+ tuple("5", 0L));
+ checkFacet(response, "security_rating",
+ tuple("1", 2L),
+ tuple("2", 0L),
+ tuple("3", 0L),
+ tuple("4", 0L),
+ tuple("5", 0L));
+ checkFacet(response, "sqale_rating",
+ tuple("1", 0L),
+ tuple("2", 0L),
+ tuple("3", 0L),
+ tuple("4", 2L),
+ tuple("5", 0L));
+ checkFacet(response, "tags");
+ }
+
+ @Test
+ public void should_return_facets_on_leak() throws Exception {
+ setServerProperty(orchestrator, "sonar.leak.period", "previous_analysis");
+ String projectKey = newProjectKey();
+ analyzeProject(projectKey, "shared/xoo-history-v1");
+ analyzeProject(projectKey, "shared/xoo-history-v2");
+
+ SearchProjectsWsResponse response = searchProjects(SearchProjectsRequest.builder().setOrganization(organizationKey).setFacets(asList(
+ "new_reliability_rating")).build());
+
+ checkFacet(response, "new_reliability_rating",
+ tuple("1", 1L),
+ tuple("2", 0L),
+ tuple("3", 0L),
+ tuple("4", 0L),
+ tuple("5", 0L));
+ }
+
+ private void checkFacet(SearchProjectsWsResponse response, String facetKey, Tuple... values) {
+ Common.Facet facet = response.getFacets().getFacetsList().stream().filter(f -> f.getProperty().equals(facetKey)).findAny().get();
+ assertThat(facet.getValuesList()).extracting(Common.FacetValue::getVal, Common.FacetValue::getCount).containsExactly(values);
+ }
+
+ private void analyzeProject(String projectKey, String relativePath) {
+ orchestrator.executeBuild(SonarScanner.create(projectDir(relativePath),
+ "sonar.projectKey", projectKey,
+ "sonar.organization", organizationKey,
+ "sonar.profile", "with-many-rules",
+ "sonar.login", "admin", "sonar.password", "admin"));
}
private SearchProjectsWsResponse searchProjects(String filter) throws IOException {
- return searchProjects(SearchProjectsRequest.builder().setFilter(filter).build());
+ return searchProjects(SearchProjectsRequest.builder().setOrganization(organizationKey).setFilter(filter).build());
}
private SearchProjectsWsResponse searchProjects(SearchProjectsRequest request) throws IOException {
return newAdminWsClient(orchestrator).components().searchProjects(request);
}
- private void verifyFilterMatches(String filter) throws IOException {
- assertThat(searchProjects(filter).getComponentsList()).extracting(Component::getKey).containsOnly(PROJECT_KEY);
+ private void verifyFilterMatches(String projectKey, String filter) throws IOException {
+ assertThat(searchProjects(filter).getComponentsList()).extracting(Component::getKey).containsOnly(projectKey);
}
private void verifyFilterDoesNotMatch(String filter) throws IOException {
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.server.es.EsTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
+import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING_KEY;
+import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY;
import static org.sonar.api.measures.Metric.ValueType.INT;
import static org.sonar.api.measures.Metric.ValueType.LEVEL;
import static org.sonar.api.server.ws.WebService.Param.ASCENDING;
Param facets = def.param("facets");
assertThat(facets.defaultValue()).isNull();
assertThat(facets.possibleValues()).containsOnly("ncloc", "duplicated_lines_density", "coverage", "sqale_rating", "reliability_rating", "security_rating", "alert_status",
- "languages", "tags");
+ "languages", "tags", "new_reliability_rating");
}
@Test
public void json_example() {
OrganizationDto organization1Dto = db.organizations().insertForKey("my-org-key-1");
OrganizationDto organization2Dto = db.organizations().insertForKey("my-org-key-2");
- ComponentDto project1 = insertProjectInDbAndEs(ComponentTesting.newPublicProjectDto(organization1Dto)
+ ComponentDto project1 = insertProjectInDbAndEs(newPublicProjectDto(organization1Dto)
.setUuid(Uuids.UUID_EXAMPLE_01)
.setKey(KeyExamples.KEY_PROJECT_EXAMPLE_001)
.setName("My Project 1")
.setTagsString("finance, java"));
- insertProjectInDbAndEs(ComponentTesting.newPublicProjectDto(organization1Dto)
+ insertProjectInDbAndEs(newPublicProjectDto(organization1Dto)
.setUuid(Uuids.UUID_EXAMPLE_02)
.setKey(KeyExamples.KEY_PROJECT_EXAMPLE_002)
.setName("My Project 2"));
- insertProjectInDbAndEs(ComponentTesting.newPublicProjectDto(organization2Dto)
+ insertProjectInDbAndEs(newPublicProjectDto(organization2Dto)
.setUuid(Uuids.UUID_EXAMPLE_03)
.setKey(KeyExamples.KEY_PROJECT_EXAMPLE_003)
.setName("My Project 3")
@Test
public void order_by_name_case_insensitive() {
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("Maven"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("Apache"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("guava"));
+ insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Maven"));
+ insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Apache"));
+ insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("guava"));
SearchProjectsWsResponse result = call(request);
@Test
public void paginate_result() {
- IntStream.rangeClosed(1, 9).forEach(i -> insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("PROJECT-" + i)));
+ IntStream.rangeClosed(1, 9).forEach(i -> insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("PROJECT-" + i)));
SearchProjectsWsResponse result = call(request.setPage(2).setPageSize(3));
@Test
public void return_only_projects() {
OrganizationDto organizationDto = db.organizations().insert();
- ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto).setName("SonarQube");
+ ComponentDto project = newPrivateProjectDto(organizationDto).setName("SonarQube");
ComponentDto directory = newDirectory(project, "path");
insertProjectInDbAndEs(project);
componentDb.insertComponents(newModuleDto(project), newView(organizationDto), directory, newFileDto(project, directory));
@Test
public void filter_projects_with_query() {
OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
+ 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");
public void filter_projects_with_query_within_specified_organization() {
OrganizationDto organization1 = db.organizations().insert();
OrganizationDto organization2 = db.organizations().insert();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization1).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization1).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization2).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
+ 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);
assertThat(call(request.setOrganization(null)).getComponentsList())
@Test
public void filter_projects_by_quality_gate() {
OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Java"), "OK");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Markdown"), "OK");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Qube"), "ERROR");
+ 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");
@Test
public void filter_projects_by_languages() {
OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("<null>", "java", "xoo"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81)), null, asList("java", "xoo"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("xoo"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("<null>", "java", "xoo"));
+ 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>)");
@Test
public void filter_projects_by_tags() {
OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Java").setTags(newArrayList("finance", "platform")));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Markdown").setTags(singletonList("marketing")));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setName("Sonar Qube").setTags(newArrayList("offshore")));
+ 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)");
SearchProjectsWsResponse result = call(request);
@Test
public void filter_projects_by_text_query() {
OrganizationDto organizationDto = db.organizations().insertForKey("my-org-key-1");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setKey("sonar-java").setName("Sonar Java"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setKey("sonar-groovy").setName("Sonar Groovy"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setKey("sonar-markdown").setName("Sonar Markdown"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto).setKey("sonarqube").setName("Sonar Qube"));
+ 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"));
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 organization4 = db.organizations().insert();
OrganizationDto organization5 = db.organizations().insert();
List<Map<String, Object>> someMeasure = singletonList(newMeasure(COVERAGE, 81));
- ComponentDto favourite1_1 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization1), someMeasure);
- ComponentDto favourite1_2 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization1), someMeasure);
- ComponentDto nonFavourite1 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization1), someMeasure);
- ComponentDto favourite2 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization2), someMeasure);
- ComponentDto nonFavourite2 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization2), someMeasure);
- ComponentDto favourite3 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization3), someMeasure);
- ComponentDto nonFavourite4 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization4), someMeasure);
+ 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);
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(ComponentTesting.newPrivateProjectDto(db.organizations().insert()).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
+ insertProjectInDbAndEs(newPrivateProjectDto(db.organizations().insert()).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
addFavourite(javaProject);
addFavourite(markDownProject);
dbSession.commit();
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(ComponentTesting.newPrivateProjectDto(db.organizations().insert()).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
+ insertProjectInDbAndEs(newPrivateProjectDto(db.organizations().insert()).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_001d)));
addFavourite(javaProject);
dbSession.commit();
request.setFilter("isFavorite");
@Test
public void do_not_return_isFavorite_if_anonymous_user() {
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81)));
+ insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81)));
insertMetrics(COVERAGE);
userSession.anonymous();
@Test
public void empty_list_if_isFavorite_filter_and_anonymous_user() {
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81)));
+ insertProjectInDbAndEs(newPrivateProjectDto(db.getDefaultOrganization()).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81)));
insertMetrics(COVERAGE);
userSession.anonymous();
request.setFilter("isFavorite");
@Test
public void return_nloc_facet() {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
+ 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);
SearchProjectsWsResponse result = call(request.setFacets(singletonList(NCLOC)));
@Test
public void return_languages_facet() {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("<null>", "java", "xoo"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81)), null, asList("java", "xoo"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("xoo"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d)), null, asList("<null>", "java", "xoo"));
+ 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);
SearchProjectsWsResponse result = call(request.setFacets(singletonList(FILTER_LANGUAGES)));
@Test
public void return_languages_facet_with_language_having_no_project_if_language_is_in_filter() {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("<null>", "java"));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81)), null, asList("java"));
+ 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);
SearchProjectsWsResponse result = call(request.setFilter("languages = xoo").setFacets(singletonList(FILTER_LANGUAGES)));
@Test
public void return_tags_facet() {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java").setTags(newArrayList("finance", "platform")));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown").setTags(singletonList("offshore")));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube").setTags(newArrayList("offshore")));
+ 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")));
SearchProjectsWsResponse result = call(request.setFacets(singletonList(FILTER_TAGS)));
@Test
public void return_tags_facet_with_tags_having_no_project_if_tags_is_in_filter() {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java").setTags(newArrayList("finance", "platform")));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown").setTags(singletonList("offshore")));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube").setTags(newArrayList("offshore")));
+ 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")));
SearchProjectsWsResponse result = call(request.setFilter("tags = marketing").setFacets(singletonList(FILTER_TAGS)));
tuple("marketing", 0L));
}
+ @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);
+
+ SearchProjectsWsResponse result = call(request.setFacets(singletonList(RELIABILITY_RATING_KEY)));
+
+ Common.Facet facet = result.getFacets().getFacetsList().stream()
+ .filter(oneFacet -> RELIABILITY_RATING_KEY.equals(oneFacet.getProperty()))
+ .findFirst().orElseThrow(IllegalStateException::new);
+ assertThat(facet.getValuesList())
+ .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
+ .containsExactly(
+ tuple("1", 2L),
+ tuple("2", 0L),
+ tuple("3", 1L),
+ tuple("4", 0L),
+ tuple("5", 1L));
+ }
+
+ @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);
+
+ SearchProjectsWsResponse result = call(request.setFacets(singletonList(NEW_RELIABILITY_RATING_KEY)));
+
+ Common.Facet facet = result.getFacets().getFacetsList().stream()
+ .filter(oneFacet -> NEW_RELIABILITY_RATING_KEY.equals(oneFacet.getProperty()))
+ .findFirst().orElseThrow(IllegalStateException::new);
+ assertThat(facet.getValuesList())
+ .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
+ .containsExactly(
+ tuple("1", 2L),
+ tuple("2", 0L),
+ tuple("3", 1L),
+ tuple("4", 0L),
+ tuple("5", 1L));
+ }
+
@Test
public void default_sort_is_by_ascending_name() throws Exception {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
+ 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)));
SearchProjectsWsResponse result = call(request);
@Test
public void sort_by_name() throws Exception {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
+ 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)));
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(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d)));
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube"), newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 500_001d)));
+ 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)
@Test
public void sort_by_quality_gate() throws Exception {
OrganizationDto organization = db.getDefaultOrganization();
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Java"), "ERROR");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Groovy"), "WARN");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Markdown"), "OK");
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organization).setName("Sonar Qube"), "OK");
+ 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();
@Test
public void return_last_analysis_date() {
OrganizationDto organizationDto = db.organizations().insert();
- ComponentDto project1 = insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto));
+ 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(ComponentTesting.newPrivateProjectDto(organizationDto));
+ ComponentDto project2 = insertProjectInDbAndEs(newPrivateProjectDto(organizationDto));
db.components().insertSnapshot(newAnalysis(project2).setCreatedAt(30_000_000_000L).setLast(true));
// No snapshot on project 3
- insertProjectInDbAndEs(ComponentTesting.newPrivateProjectDto(organizationDto));
+ insertProjectInDbAndEs(newPrivateProjectDto(organizationDto));
SearchProjectsWsResponse result = call(request.setAdditionalFields(singletonList("analysisDate")));