From: Julien Lancelot Date: Fri, 19 May 2017 08:46:46 +0000 (+0200) Subject: SONAR-9245 Make SearchProjectsActionTest more stable X-Git-Tag: 6.5-M1~102 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b6ad5cfbba96fd03e03ec12a2fd45b96874368c3;p=sonarqube.git SONAR-9245 Make SearchProjectsActionTest more stable Do not directly insert documents into ES but insert data in db then calls indexer --- diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DbTester.java index 31ab11a427a..ff078a73d2d 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/DbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DbTester.java @@ -32,6 +32,7 @@ import org.sonar.db.component.ComponentDbTester; import org.sonar.db.event.EventDbTester; import org.sonar.db.favorite.FavoriteDbTester; import org.sonar.db.issue.IssueDbTester; +import org.sonar.db.measure.MeasureDbTester; import org.sonar.db.notification.NotificationDbTester; import org.sonar.db.organization.OrganizationDbTester; import org.sonar.db.organization.OrganizationDto; @@ -74,6 +75,7 @@ public class DbTester extends AbstractDbTester { private final NotificationDbTester notificationDbTester; private final RootFlagAssertions rootFlagAssertions; private final QualityProfileDbTester qualityProfileDbTester; + private final MeasureDbTester measureDbTester; public DbTester(System2 system2, @Nullable String schemaPath) { super(TestDb.create(schemaPath)); @@ -93,6 +95,7 @@ public class DbTester extends AbstractDbTester { this.notificationDbTester = new NotificationDbTester(this); this.rootFlagAssertions = new RootFlagAssertions(this); this.qualityProfileDbTester = new QualityProfileDbTester(this); + this.measureDbTester = new MeasureDbTester(this); } public static DbTester create() { @@ -219,6 +222,10 @@ public class DbTester extends AbstractDbTester { return qualityProfileDbTester; } + public MeasureDbTester measureDbTester() { + return measureDbTester; + } + @Override protected void after() { if (session != null) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDbTester.java new file mode 100644 index 00000000000..b1d45914ec3 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDbTester.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.measure; + +import java.util.Arrays; +import java.util.function.Consumer; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.SnapshotDto; +import org.sonar.db.metric.MetricDto; + +import static org.sonar.db.measure.MeasureTesting.newMeasureDto; +import static org.sonar.db.metric.MetricTesting.newMetricDto; + +public class MeasureDbTester { + private final DbTester db; + private final DbClient dbClient; + private final DbSession dbSession; + + public MeasureDbTester(DbTester db) { + this.db = db; + this.dbClient = db.getDbClient(); + this.dbSession = db.getSession(); + } + + @SafeVarargs + public final MeasureDto insertMeasure(ComponentDto component, SnapshotDto analysis, MetricDto metricDto, Consumer... consumers){ + MeasureDto measureDto = newMeasureDto(metricDto, component, analysis); + Arrays.stream(consumers).forEach(c -> c.accept(measureDto)); + dbClient.measureDao().insert(dbSession, measureDto); + dbSession.commit(); + return measureDto; + } + + @SafeVarargs + public final MetricDto insertMetric(Consumer... consumers){ + MetricDto metricDto = newMetricDto(); + Arrays.stream(consumers).forEach(c -> c.accept(metricDto)); + dbClient.metricDao().insert(dbSession, metricDto); + dbSession.commit(); + return metricDto; + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java index bdbd3b21a8d..5344cb879ec 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java @@ -20,14 +20,11 @@ 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; @@ -36,16 +33,16 @@ import org.sonar.api.server.ws.Change; 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; @@ -60,9 +57,7 @@ import org.sonarqube.ws.WsComponents.Component; 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; @@ -80,15 +75,9 @@ import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; 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; @@ -105,18 +94,18 @@ public class SearchProjectsActionTest { @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( @@ -164,36 +153,34 @@ public class SearchProjectsActionTest { @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); @@ -203,7 +190,9 @@ public class SearchProjectsActionTest { @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)); @@ -218,6 +207,8 @@ public class SearchProjectsActionTest { @Test public void empty_result() { + userSession.logIn(); + SearchProjectsWsResponse result = call(request); assertThat(result.getComponentsCount()).isEqualTo(0); @@ -227,104 +218,129 @@ public class SearchProjectsActionTest { 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("", "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("", "java", "xoo")); - insertMetrics(COVERAGE, NCLOC_LANGUAGE_DISTRIBUTION_KEY); - request.setFilter("languages IN (java, js, )"); + 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("=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("=1;java=5;xoo=13"))); - SearchProjectsWsResponse result = call(request); + SearchProjectsWsResponse result = call(request.setFilter("languages IN (java, js, )")); + + 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", @@ -340,17 +356,14 @@ public class SearchProjectsActionTest { OrganizationDto organization3 = db.organizations().insert(); OrganizationDto organization4 = db.organizations().insert(); OrganizationDto organization5 = db.organizations().insert(); - List> 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) @@ -384,77 +397,58 @@ public class SearchProjectsActionTest { @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( @@ -467,19 +461,19 @@ public class SearchProjectsActionTest { @Test public void return_languages_facet() { - OrganizationDto organization = db.getDefaultOrganization(); - insertProjectInDbAndEs(newPrivateProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("", "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("", "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("=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("=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( @@ -490,10 +484,11 @@ public class SearchProjectsActionTest { @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("", "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("=2;java=6"))); + insertProject(organizationDto, new Measure(languagesDistribution, c -> c.setData("java=5"))); SearchProjectsWsResponse result = call(request.setFilter("languages = xoo").setFacets(singletonList(FILTER_LANGUAGES))); @@ -510,17 +505,17 @@ public class SearchProjectsActionTest { @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( @@ -531,10 +526,11 @@ public class SearchProjectsActionTest { @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))); @@ -552,12 +548,13 @@ public class SearchProjectsActionTest { @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))); @@ -576,12 +573,13 @@ public class SearchProjectsActionTest { @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))); @@ -600,11 +598,12 @@ public class SearchProjectsActionTest { @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); @@ -613,11 +612,12 @@ public class SearchProjectsActionTest { @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"); @@ -626,46 +626,52 @@ public class SearchProjectsActionTest { } @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"))); @@ -675,20 +681,25 @@ public class SearchProjectsActionTest { @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]"); @@ -699,6 +710,8 @@ public class SearchProjectsActionTest { @Test public void fail_if_page_size_greater_than_500() { + userSession.logIn(); + expectedException.expect(IllegalArgumentException.class); call(request.setPageSize(501)); @@ -718,52 +731,32 @@ public class SearchProjectsActionTest { 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> 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 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> measures, @Nullable String qualityGateStatus, - List 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 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 consumer) { + this.metric = metric; + this.consumer = consumer; } - dbSession.commit(); - } - - private static Map 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())); } }