]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10134 Use QGateWithOrgDto instead of id in ProjectQgateAssociationQuery
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 11:29:46 +0000 (12:29 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/ProjectQgateAssociationQuery.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java [deleted file]
server/sonar-db-dao/src/test/resources/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest/shared.xml [deleted file]
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/SearchAction.java

index 3962d8f6ff8851bf069a19eb3fed883a41ad877e..f57e20bd308c03e6b03a4e1f558557ccdb7cbd37 100644 (file)
@@ -54,7 +54,7 @@ public class ProjectQgateAssociationQuery {
   private final int pageIndex;
 
   private ProjectQgateAssociationQuery(Builder builder) {
-    this.gateId = Long.toString(builder.gateId);
+    this.gateId = Long.toString(builder.qualityGate.getId());
     this.membership = builder.membership;
     this.projectSearch = builder.projectSearch;
     if (this.projectSearch == null) {
@@ -97,7 +97,7 @@ public class ProjectQgateAssociationQuery {
   }
 
   public static class Builder {
-    private long gateId;
+    private QGateWithOrgDto qualityGate;
     private String membership;
     private String projectSearch;
 
@@ -107,8 +107,8 @@ public class ProjectQgateAssociationQuery {
     private Builder() {
     }
 
-    public Builder gateId(long gateId) {
-      this.gateId = gateId;
+    public Builder qualityGate(QGateWithOrgDto qualityGate) {
+      this.qualityGate = qualityGate;
       return this;
     }
 
index f79bd01d8470a778647af4af6cb88fff1d9b3e2e..3de2b4f21e6810f3be4a66eac720f5b013de232b 100644 (file)
@@ -23,67 +23,111 @@ import java.util.List;
 import java.util.Optional;
 import org.junit.Rule;
 import org.junit.Test;
-import org.sonar.api.utils.System2;
-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.property.PropertyDto;
+import org.sonar.db.organization.OrganizationDto;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
 
 public class ProjectQgateAssociationDaoTest {
 
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create();
 
-  private DbClient dbClient = db.getDbClient();
   private DbSession dbSession = db.getSession();
   private ProjectQgateAssociationDao underTest = db.getDbClient().projectQgateAssociationDao();
 
   @Test
   public void select_all_projects_by_query() {
-    db.prepareDbUnit(getClass(), "shared.xml");
-
-    ProjectQgateAssociationQuery query = ProjectQgateAssociationQuery.builder().gateId(42L).build();
-    List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, query);
-    assertThat(result).hasSize(5);
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
+    QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
+    ComponentDto project1 = db.components().insertPrivateProject(organization);
+    ComponentDto project2 = db.components().insertPrivateProject(organization);
+    ComponentDto project3 = db.components().insertPrivateProject(organization);
+    db.qualityGates().associateProjectToQualityGate(project1, qualityGate1);
+    db.qualityGates().associateProjectToQualityGate(project2, qualityGate1);
+    db.qualityGates().associateProjectToQualityGate(project3, qualityGate2);
+
+    List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
+      .qualityGate(qualityGate1)
+      .build());
+
+    assertThat(result)
+      .extracting(ProjectQgateAssociationDto::getId, ProjectQgateAssociationDto::getName, ProjectQgateAssociationDto::getGateId)
+      .containsExactlyInAnyOrder(
+        tuple(project1.getId(), project1.name(), qualityGate1.getId().toString()),
+        tuple(project2.getId(), project2.name(), qualityGate1.getId().toString()),
+        tuple(project3.getId(), project3.name(), null));
   }
 
   @Test
   public void select_projects_by_query() {
-    db.prepareDbUnit(getClass(), "shared.xml");
-
-    assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId(42L).membership(ProjectQgateAssociationQuery.IN).build())).hasSize(3);
-    assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId(42L).membership(ProjectQgateAssociationQuery.OUT).build())).hasSize(2);
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+    ComponentDto project1 = db.components().insertPrivateProject(organization);
+    ComponentDto project2 = db.components().insertPrivateProject(organization);
+    ComponentDto project3 = db.components().insertPrivateProject(organization);
+    db.qualityGates().associateProjectToQualityGate(project1, qualityGate);
+    db.qualityGates().associateProjectToQualityGate(project2, qualityGate);
+
+    assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
+      .qualityGate(qualityGate)
+      .membership(ProjectQgateAssociationQuery.IN)
+      .build()))
+        .extracting(ProjectQgateAssociationDto::getId, ProjectQgateAssociationDto::getName, ProjectQgateAssociationDto::getGateId)
+        .containsExactlyInAnyOrder(
+          tuple(project1.getId(), project1.name(), qualityGate.getId().toString()),
+          tuple(project2.getId(), project2.name(), qualityGate.getId().toString()));
+
+    assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
+      .qualityGate(qualityGate)
+      .membership(ProjectQgateAssociationQuery.OUT)
+      .build()))
+        .extracting(ProjectQgateAssociationDto::getId, ProjectQgateAssociationDto::getName, ProjectQgateAssociationDto::getGateId)
+        .containsExactlyInAnyOrder(tuple(project3.getId(), project3.name(), null));
   }
 
   @Test
   public void search_by_project_name() {
-    db.prepareDbUnit(getClass(), "shared.xml");
-
-    List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId(42L).projectSearch("one").build());
-    assertThat(result).hasSize(1);
-
-    assertThat(result.get(0).getName()).isEqualTo("Project One");
-
-    result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId(42L).projectSearch("one").build());
-    assertThat(result).hasSize(1);
-    result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId(42L).projectSearch("project").build());
-    assertThat(result).hasSize(5);
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+    ComponentDto project1 = db.components().insertPrivateProject(organization, p -> p.setName("Project One"));
+    ComponentDto project2 = db.components().insertPrivateProject(organization, p -> p.setName("Project Two"));
+    ComponentDto project3 = db.components().insertPrivateProject(organization, p -> p.setName("Project Three"));
+    db.qualityGates().associateProjectToQualityGate(project1, qualityGate);
+    db.qualityGates().associateProjectToQualityGate(project2, qualityGate);
+
+    assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
+      .qualityGate(qualityGate)
+      .projectSearch("one")
+      .build()))
+        .extracting(ProjectQgateAssociationDto::getId)
+        .containsExactlyInAnyOrder(project1.getId());
+
+    assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
+      .qualityGate(qualityGate)
+      .projectSearch("project")
+      .build()))
+        .extracting(ProjectQgateAssociationDto::getId)
+        .containsExactlyInAnyOrder(project1.getId(), project2.getId(), project3.getId());
   }
 
   @Test
-  public void should_be_sorted_by_project_name() {
-    db.prepareDbUnit(getClass(), "shared.xml");
-
-    List<ProjectQgateAssociationDto> result = underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder().gateId(42L).build());
-    assertThat(result).hasSize(5);
-    assertThat(result.get(0).getName()).isEqualTo("Project Five");
-    assertThat(result.get(1).getName()).isEqualTo("Project Four");
-    assertThat(result.get(2).getName()).isEqualTo("Project One");
-    assertThat(result.get(3).getName()).isEqualTo("Project Three");
-    assertThat(result.get(4).getName()).isEqualTo("Project Two");
+  public void sorted_by_project_name() {
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+    ComponentDto project1 = db.components().insertPrivateProject(organization, p -> p.setName("Project One"));
+    ComponentDto project2 = db.components().insertPrivateProject(organization, p -> p.setName("Project Two"));
+    ComponentDto project3 = db.components().insertPrivateProject(organization, p -> p.setName("Project Three"));
+
+    assertThat(underTest.selectProjects(dbSession, ProjectQgateAssociationQuery.builder()
+      .qualityGate(qualityGate)
+      .build()))
+        .extracting(ProjectQgateAssociationDto::getId)
+        .containsExactly(project1.getId(), project3.getId(), project2.getId());
   }
 
   @Test
@@ -97,19 +141,17 @@ public class ProjectQgateAssociationDaoTest {
 
   @Test
   public void select_qgate_id() {
-    associateProjectToQualityGate(10L, 1L);
-    associateProjectToQualityGate(11L, 2L);
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
+    QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
+    ComponentDto project1 = db.components().insertPrivateProject(organization);
+    ComponentDto project2 = db.components().insertPrivateProject(organization);
+    db.qualityGates().associateProjectToQualityGate(project1, qualityGate1);
+    db.qualityGates().associateProjectToQualityGate(project2, qualityGate2);
 
-    Optional<Long> result = underTest.selectQGateIdByComponentId(dbSession, 10L);
+    Optional<Long> result = underTest.selectQGateIdByComponentId(dbSession, project1.getId());
 
-    assertThat(result).contains(1L);
+    assertThat(result).contains(qualityGate1.getId());
   }
 
-  private void associateProjectToQualityGate(long componentId, long qualityGateId) {
-    dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
-      .setKey("sonar.qualitygate")
-      .setResourceId(componentId)
-      .setValue(String.valueOf(qualityGateId)));
-    db.commit();
-  }
 }
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/ProjectQgateAssociationQueryTest.java
deleted file mode 100644 (file)
index 2ff682b..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.qualitygate;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-public class ProjectQgateAssociationQueryTest {
-
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
-
-  @Test
-  public void fail_on_invalid_membership() {
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("Membership is not valid (got unknown). Available values are [all, selected, deselected]");
-
-    ProjectQgateAssociationQuery.Builder builder = ProjectQgateAssociationQuery.builder();
-    builder.gateId(42L);
-    builder.membership("unknown");
-
-    builder.build();
-  }
-}
diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest/shared.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/qualitygate/ProjectQgateAssociationDaoTest/shared.xml
deleted file mode 100644 (file)
index 081048e..0000000
+++ /dev/null
@@ -1,140 +0,0 @@
-<dataset>
-
-  <quality_gates id="42" uuid="1"
-                 name="Golden" is_built_in="[true]"/>
-  <quality_gates id="43" uuid="2"
-                 name="Ninth" is_built_in="[false]"/>
-
-  <projects organization_uuid="org1"
-            uuid="A"
-            uuid_path="NOT_USED"
-            root_uuid="A"
-            project_uuid="A"
-            kee="project-one"
-            main_branch_project_uuid="[null]"
-            name="Project One"
-            qualifier="TRK"
-            scope="PRJ"
-            enabled="[true]"
-            copy_component_uuid="[null]"
-            id="1"
-            private="[false]"/>
-  <projects organization_uuid="org1"
-            uuid="B"
-            uuid_path="NOT_USED"
-            root_uuid="B"
-            project_uuid="B"
-            kee="project-two"
-            main_branch_project_uuid="[null]"
-            name="Project Two"
-            qualifier="TRK"
-            scope="PRJ"
-            enabled="[true]"
-            copy_component_uuid="[null]"
-            id="2"
-            private="[false]"/>
-  <projects organization_uuid="org1"
-            uuid="C"
-            uuid_path="NOT_USED"
-            root_uuid="C"
-            project_uuid="C"
-            kee="project-three"
-            main_branch_project_uuid="[null]"
-            name="Project Three"
-            qualifier="TRK"
-            scope="PRJ"
-            enabled="[true]"
-            copy_component_uuid="[null]"
-            id="3"
-            private="[false]"/>
-  <projects organization_uuid="org1"
-            uuid="D"
-            uuid_path="NOT_USED"
-            root_uuid="D"
-            project_uuid="D"
-            kee="project-four"
-            main_branch_project_uuid="[null]"
-            name="Project Four"
-            qualifier="TRK"
-            scope="PRJ"
-            enabled="[true]"
-            copy_component_uuid="[null]"
-            id="4"
-            private="[false]"/>
-  <projects organization_uuid="org1"
-            uuid="E"
-            uuid_path="NOT_USED"
-            root_uuid="E"
-            project_uuid="E"
-            kee="project-five"
-            main_branch_project_uuid="[null]"
-            name="Project Five"
-            qualifier="TRK"
-            scope="PRJ"
-            enabled="[true]"
-            copy_component_uuid="[null]"
-            id="5"
-            private="[false]"/>
-  <projects organization_uuid="org1"
-            uuid="F"
-            uuid_path="NOT_USED"
-            root_uuid="F"
-            project_uuid="F"
-            kee="view-six"
-            main_branch_project_uuid="[null]"
-            name="View Six"
-            qualifier="VW"
-            scope="PRJ"
-            enabled="[true]"
-            copy_component_uuid="[null]"
-            id="6"
-            private="[false]"/>
-  <projects organization_uuid="org1"
-            uuid="G"
-            uuid_path="NOT_USED"
-            root_uuid="G"
-            project_uuid="G"
-            kee="file-one"
-            main_branch_project_uuid="[null]"
-            name="File One"
-            qualifier="TRK"
-            scope="FIL"
-            enabled="[true]"
-            copy_component_uuid="C"
-            id="7"
-            private="[false]"/>
-  <projects organization_uuid="org1"
-            uuid="H"
-            uuid_path="NOT_USED"
-            root_uuid="H"
-            project_uuid="H"
-            kee="branch"
-            main_branch_project_uuid="A"
-            name="branch"
-            qualifier="TRK"
-            scope="PRJ"
-            enabled="[true]"
-            copy_component_uuid="[null]"
-            id="8"
-            private="[false]"/>
-
-  <properties id="2"
-              prop_key="sonar.qualitygate"
-              resource_id="1"
-              is_empty="[false]"
-              text_value="42"
-              created_at="1555000"/>
-  <properties id="3"
-              prop_key="sonar.qualitygate"
-              resource_id="2"
-              is_empty="[false]"
-              text_value="42"
-              created_at="1555000"/>
-  <properties id="4"
-              prop_key="sonar.qualitygate"
-              resource_id="3"
-              is_empty="[false]"
-              text_value="42"
-              created_at="1555000"/>
-
-</dataset>
index bca22fd5672dabc2c7579e06885dfac933602052..43edb6c4960572e399756e440d3a74cf330f7418 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.qualitygate.ProjectQgateAssociation;
 import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
 import org.sonar.db.qualitygate.ProjectQgateAssociationQuery;
+import org.sonar.db.qualitygate.QGateWithOrgDto;
 import org.sonar.server.qualitygate.QualityGateFinder;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.Qualitygates;
@@ -98,9 +99,10 @@ public class SearchAction implements QualityGatesWsAction {
     try (DbSession dbSession = dbClient.openSession(false)) {
 
       OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
-      Association associations = find(dbSession, organization,
+      QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, request.mandatoryParamAsLong(PARAM_GATE_ID));
+      Association associations = find(dbSession,
         ProjectQgateAssociationQuery.builder()
-          .gateId(request.mandatoryParamAsLong(PARAM_GATE_ID))
+          .qualityGate(qualityGate)
           .membership(request.param(PARAM_QUERY) == null ? request.param(SELECTED) : ANY)
           .projectSearch(request.param(PARAM_QUERY))
           .pageIndex(request.paramAsInt(PARAM_PAGE))
@@ -121,8 +123,7 @@ public class SearchAction implements QualityGatesWsAction {
     }
   }
 
-  private SearchAction.Association find(DbSession dbSession, OrganizationDto organization, ProjectQgateAssociationQuery query) {
-    qualityGateFinder.getByOrganizationAndId(dbSession, organization, Long.parseLong(query.gateId()));
+  private SearchAction.Association find(DbSession dbSession, ProjectQgateAssociationQuery query) {
     List<ProjectQgateAssociationDto> projects = dbClient.projectQgateAssociationDao().selectProjects(dbSession, query);
     List<ProjectQgateAssociationDto> authorizedProjects = keepAuthorizedProjects(dbSession, projects);