]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10134 Add organization parameter in api/qualitygates/show
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 4 Dec 2017 10:46:07 +0000 (11:46 +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/QualityGateDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateFinderTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java

index f27486a2c0c5aacb36ba30fb03906e6d21e94f7d..11a5a704ca2d03f3ad71a2802060211cf2ec7503 100644 (file)
@@ -56,6 +56,11 @@ public class QualityGateDao implements Dao {
     return mapper(dbSession).selectByUuidAndOrganization(qualityGateUuid, organization.getUuid());
   }
 
+  @CheckForNull
+  public QGateWithOrgDto selectByOrganizationAndName(DbSession session, OrganizationDto organization, String name) {
+    return mapper(session).selectByNameAndOrganization(name, organization.getUuid());
+  }
+
   public void delete(QualityGateDto qGate, DbSession session) {
     mapper(session).delete(qGate.getId());
   }
index bdaa78e1d4ab78713115d17a2831572941336c52..e60464e237cbcae475f58545d6aa23d2fdcd97f3 100644 (file)
@@ -36,6 +36,8 @@ public interface QualityGateMapper {
 
   QGateWithOrgDto selectByUuidAndOrganization(@Param("qualityGateUuid") String qualityGateUuid, @Param("organizationUuid") String organizationUuid);
 
+  QGateWithOrgDto selectByNameAndOrganization(@Param("name") String name, @Param("organizationUuid") String organizationUuid);
+
   QualityGateDto selectBuiltIn();
 
   void delete(long id);
index 00070e4180c90e23dc092b3932ff47b21750c524..ad1cedba967680836c3b601c4df162d8a99c04da 100644 (file)
@@ -3,6 +3,20 @@
 
 <mapper namespace="org.sonar.db.qualitygate.QualityGateMapper">
 
+  <sql id="gateColumns">
+      id, name, uuid, is_built_in as isBuiltIn, created_at as createdAt, updated_at as updatedAt
+  </sql>
+
+  <sql id="qateWithOrgColumns">
+    qg.id as id,
+    qg.uuid as uuid,
+    qg.name as name,
+    qg.is_built_in as isBuiltIn,
+    oqg.organization_uuid as organizationUuid,
+    qg.created_at as createdAt,
+    qg.updated_at as updatedAd
+  </sql>
+
   <insert id="insertQualityGate" parameterType="QualityGate" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
     insert into quality_gates (uuid, name, is_built_in, created_at, updated_at)
     values (#{uuid, jdbcType=VARCHAR}, #{name, jdbcType=VARCHAR}, #{isBuiltIn, jdbcType=BOOLEAN}, #{createdAt, jdbcType=TIMESTAMP}, #{updatedAt, jdbcType=TIMESTAMP})
     VALUES (#{uuid, jdbcType=VARCHAR}, #{organizationUuid, jdbcType=VARCHAR}, #{qualityGateUuid, jdbcType=VARCHAR})
   </insert>
 
-  <sql id="gateColumns">
-    id, name, uuid, is_built_in as isBuiltIn, created_at as createdAt, updated_at as updatedAt
-  </sql>
-
   <select id="selectAll" resultType="QualityGate">
     select
     <include refid="gateColumns"/>
 
   <select id="selectByUuidAndOrganization" parameterType="Map" resultType="org.sonar.db.qualitygate.QGateWithOrgDto">
     SELECT
-      qg.id as id,
-      qg.uuid as uuid,
-      qg.name as name,
-      qg.is_built_in as isBuiltIn,
-      oqg.organization_uuid as organizationUuid,
-      qg.created_at as createdAt,
-      qg.updated_at as updatedAd
+      <include refid="qateWithOrgColumns"/>
     FROM
       quality_gates qg
     INNER JOIN
-      org_quality_gates oqg ON oqg.quality_gate_uuid = qg.uuid
+      org_quality_gates oqg ON oqg.quality_gate_uuid = qg.uuid AND oqg.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
     WHERE
-      qg.uuid = #{qualityGateUuid, jdbcType=VARCHAR} AND
-      oqg.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
+      qg.uuid = #{qualityGateUuid, jdbcType=VARCHAR}
   </select>
 
+  <select id="selectByNameAndOrganization" parameterType="Map" resultType="org.sonar.db.qualitygate.QGateWithOrgDto">
+    SELECT
+    <include refid="qateWithOrgColumns"/>
+    FROM
+      quality_gates qg
+    INNER JOIN
+      org_quality_gates oqg ON oqg.quality_gate_uuid = qg.uuid AND oqg.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
+    WHERE
+      qg.name = #{name, jdbcType=VARCHAR}
+  </select>
 
   <select id="selectById" parameterType="long" resultType="QualityGate">
     select
index aea886b441ff29dd6e185bb2479348b283653247..bf0be78d83da9c2476cdbb774eb4b900e3c20705 100644 (file)
@@ -101,6 +101,31 @@ public class QualityGateDaoTest {
     assertThat(underTest.selectById(dbSession, 42L)).isNull();
   }
 
+  @Test
+  public void select_by_organization_and_uuid() {
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+    OrganizationDto otherOrganization = db.organizations().insert();
+    QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
+
+    assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid()).getUuid()).isEqualTo(qualityGate.getUuid());
+    assertThat(underTest.selectByOrganizationAndUuid(dbSession, otherOrganization, qualityGate.getUuid())).isNull();
+    assertThat(underTest.selectByOrganizationAndUuid(dbSession, organization, otherQualityGate.getUuid())).isNull();
+  }
+
+  @Test
+  public void select_by_organization_and_name() {
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
+    QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
+    OrganizationDto otherOrganization = db.organizations().insert();
+    QGateWithOrgDto qualityGate3 = db.qualityGates().insertQualityGate(otherOrganization);
+
+    assertThat(underTest.selectByOrganizationAndName(dbSession, organization, qualityGate1.getName()).getUuid()).isEqualTo(qualityGate1.getUuid());
+    assertThat(underTest.selectByOrganizationAndName(dbSession, otherOrganization, qualityGate3.getName()).getUuid()).isEqualTo(qualityGate3.getUuid());
+    assertThat(underTest.selectByOrganizationAndName(dbSession, organization, "Unknown")).isNull();
+  }
+
   @Test
   public void testDelete() {
     insertQualityGates();
index b2d0eb3412c39e0cd50923b7d42fcd7d6261e0ea..32cedd3599d1340f4409e07848ad4ce38a5ab6ec 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.property.PropertyDto;
 
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
@@ -50,14 +51,20 @@ public class QualityGateDbTester {
 
   @SafeVarargs
   public final QualityGateDto insertQualityGate(Consumer<QualityGateDto>... dtoPopulators) {
+    return insertQualityGate(db.getDefaultOrganization(), dtoPopulators);
+  }
+
+  @SafeVarargs
+  public final QGateWithOrgDto insertQualityGate(OrganizationDto organization, Consumer<QualityGateDto>... dtoPopulators) {
     QualityGateDto qualityGate = new QualityGateDto()
       .setName(randomAlphanumeric(30))
       .setUuid(Uuids.createFast())
       .setBuiltIn(false);
     Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(qualityGate));
-    QualityGateDto updatedUser = dbClient.qualityGateDao().insert(dbSession, qualityGate);
+    dbClient.qualityGateDao().insert(dbSession, qualityGate);
+    dbClient.qualityGateDao().associate(dbSession, Uuids.createFast(), organization, qualityGate);
     db.commit();
-    return updatedUser;
+    return dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization, qualityGate.getUuid());
   }
 
   public void associateProjectToQualityGate(ComponentDto component, QualityGateDto qualityGate) {
index 8897b72e1c9f69d92a96b4de6a34fe600113ef28..77ebc6d4b5233b79c89a1b7718fbe15df6d1d1a6 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.qualitygate;
 
 import java.util.Optional;
-import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -62,16 +61,6 @@ public class QualityGateFinder {
     return checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId), "No quality gate has been found for id %s", qualityGateId);
   }
 
-  public QualityGateDto getByNameOrId(DbSession dbSession, @Nullable String name, @Nullable Long id) {
-    if (name != null) {
-      return checkFound(dbClient.qualityGateDao().selectByName(dbSession, name), "No quality gate has been found for name %s", name);
-    }
-    if (id != null) {
-      return getById(dbSession, id);
-    }
-    throw new IllegalArgumentException("No parameter has been set to identify a quality gate");
-  }
-
   public Optional<QualityGateDto> getDefault(DbSession dbSession) {
     Optional<Long> defaultQualityGateId = getDefaultId(dbSession);
 
index ff3de605c0bb9abea16fd652badfd990fbda0a67..3c190e4c089776abe2d56e61be1df4fff9f10678 100644 (file)
@@ -56,19 +56,32 @@ public class QualityGatesWsSupport {
     return checkFound(dbClient.gateConditionDao().selectById(id, dbSession), "No quality gate condition with id '%d'", id);
   }
 
+  /**
+   * @deprecated use {@link #isQualityGateAdmin(OrganizationDto)} instead
+   */
+  @Deprecated
   boolean isQualityGateAdmin() {
     return userSession.hasPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
   }
 
+  boolean isQualityGateAdmin(OrganizationDto organization) {
+    return userSession.hasPermission(ADMINISTER_QUALITY_GATES, organization);
+  }
+
   WebService.NewParam createOrganizationParam(NewAction action) {
     return action
       .createParam(PARAM_ORGANIZATION)
       .setDescription("Organization key. If no organization is provided, the default organization is used.")
+      .setSince("7.0")
       .setRequired(false)
       .setInternal(false)
       .setExampleValue("my-org");
   }
 
+  /**
+   * @deprecated use {@link #getActions(OrganizationDto, QualityGateDto, QualityGateDto)} instead
+   */
+  @Deprecated
   Qualitygates.Actions getActions(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate) {
     Long defaultId = defaultQualityGate == null ? null : defaultQualityGate.getId();
     boolean isDefault = qualityGate.getId().equals(defaultId);
@@ -84,11 +97,26 @@ public class QualityGatesWsSupport {
       .build();
   }
 
+  Qualitygates.Actions getActions(OrganizationDto organization, QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate) {
+    Long defaultId = defaultQualityGate == null ? null : defaultQualityGate.getId();
+    boolean isDefault = qualityGate.getId().equals(defaultId);
+    boolean isBuiltIn = qualityGate.isBuiltIn();
+    boolean isQualityGateAdmin = isQualityGateAdmin(organization);
+    return Qualitygates.Actions.newBuilder()
+      .setCopy(isQualityGateAdmin)
+      .setRename(!isBuiltIn && isQualityGateAdmin)
+      .setManageConditions(!isBuiltIn && isQualityGateAdmin)
+      .setDelete(!isDefault && !isBuiltIn && isQualityGateAdmin)
+      .setSetAsDefault(!isDefault && isQualityGateAdmin)
+      .setAssociateProjects(!isDefault && isQualityGateAdmin)
+      .build();
+  }
+
   OrganizationDto getOrganization(DbSession dbSession, Request request) {
-    String organizationUuid = Optional.ofNullable(request.param(PARAM_ORGANIZATION))
-      .orElseGet(() -> defaultOrganizationProvider.get().getUuid());
-    Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByUuid(dbSession, organizationUuid);
-    return checkFoundWithOptional(organizationDto, "No organization with key '%s'", organizationUuid);
+    String organizationKey = Optional.ofNullable(request.param(PARAM_ORGANIZATION))
+      .orElseGet(() -> defaultOrganizationProvider.get().getKey());
+    Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByKey(dbSession, organizationKey);
+    return checkFoundWithOptional(organizationDto, "No organization with key '%s'", organizationKey);
   }
 
   void checkCanEdit(QualityGateDto qualityGate) {
index 50823e89f474582e029226bd7d38c0542be78c46..ea9685620134d6f0b92567c8fe47c20c9b7d3d01 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.qualitygate.QualityGateConditionDto;
 import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.qualitygate.QualityGateFinder;
@@ -46,6 +47,7 @@ import static org.sonar.core.util.stream.MoreCollectors.toSet;
 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
+import static org.sonar.server.ws.WsUtils.checkFound;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 
 public class ShowAction implements QualityGatesWsAction {
@@ -78,6 +80,8 @@ public class ShowAction implements QualityGatesWsAction {
     action.createParam(PARAM_NAME)
       .setDescription("Name of the quality gate. Either id or name must be set")
       .setExampleValue("My Quality Gate");
+
+    wsSupport.createOrganizationParam(action);
   }
 
   @Override
@@ -87,12 +91,23 @@ public class ShowAction implements QualityGatesWsAction {
     checkOneOfIdOrNamePresent(id, name);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      QualityGateDto qualityGate = qualityGateFinder.getByNameOrId(dbSession, name, id);
+      OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
+      QualityGateDto qualityGate = getByNameOrId(dbSession, organization, name, id);
       Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
       Map<Integer, MetricDto> metricsById = getMetricsById(dbSession, conditions);
       Optional<QualityGateDto> defaultQualityGate = qualityGateFinder.getDefault(dbSession);
-      writeProtobuf(buildResponse(qualityGate, defaultQualityGate.orElse(null), conditions, metricsById), request, response);
+      writeProtobuf(buildResponse(organization, qualityGate, defaultQualityGate.orElse(null), conditions, metricsById), request, response);
+    }
+  }
+
+  private QualityGateDto getByNameOrId(DbSession dbSession, OrganizationDto organization, @Nullable String name, @Nullable Long id) {
+    if (name != null) {
+      return checkFound(dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, name), "No quality gate has been found for name %s", name);
+    }
+    if (id != null) {
+      return qualityGateFinder.getById(dbSession, id);
     }
+    throw new IllegalArgumentException("No parameter has been set to identify a quality gate");
   }
 
   public Collection<QualityGateConditionDto> getConditions(DbSession dbSession, QualityGateDto qualityGate) {
@@ -106,8 +121,8 @@ public class ShowAction implements QualityGatesWsAction {
       .collect(uniqueIndex(MetricDto::getId));
   }
 
-  private ShowWsResponse buildResponse(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate, Collection<QualityGateConditionDto> conditions,
-    Map<Integer, MetricDto> metricsById) {
+  private ShowWsResponse buildResponse(OrganizationDto organization, QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate,
+    Collection<QualityGateConditionDto> conditions, Map<Integer, MetricDto> metricsById) {
     return ShowWsResponse.newBuilder()
       .setId(qualityGate.getId())
       .setName(qualityGate.getName())
@@ -115,7 +130,7 @@ public class ShowAction implements QualityGatesWsAction {
       .addAllConditions(conditions.stream()
         .map(toWsCondition(metricsById))
         .collect(toList()))
-      .setActions(wsSupport.getActions(qualityGate, defaultQualityGate))
+      .setActions(wsSupport.getActions(organization, qualityGate, defaultQualityGate))
       .build();
   }
 
index d40b4672439b982f96379fb57abfb44232ef111c..fcac7f74ff6ac1c1ebd70101804cc4341a816e62 100644 (file)
@@ -99,35 +99,4 @@ public class QualityGateFinderTest {
     underTest.getQualityGate(dbSession, project.getId());
   }
 
-  @Test
-  public void get_by_name_or_id() {
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
-
-    assertThat(underTest.getByNameOrId(db.getSession(), qualityGate.getName(), null)).isNotNull();
-    assertThat(underTest.getByNameOrId(db.getSession(), null, qualityGate.getId())).isNotNull();
-  }
-
-  @Test
-  public void fail_get_by_name_or_id_when_name_matches_nothing() {
-    expectedException.expect(NotFoundException.class);
-    expectedException.expectMessage("No quality gate has been found for name UNKNOWN");
-
-    underTest.getByNameOrId(db.getSession(), "UNKNOWN", null);
-  }
-
-  @Test
-  public void fail_get_by_name_or_id_when_id_matches_nothing() {
-    expectedException.expect(NotFoundException.class);
-    expectedException.expectMessage("No quality gate has been found for id 123");
-
-    underTest.getByNameOrId(db.getSession(), null, 123L);
-  }
-
-  @Test
-  public void fail_get_by_name_or_id_when_parameters_are_null() {
-    expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("No parameter has been set to identify a quality gate");
-
-    underTest.getByNameOrId(db.getSession(), null, null);
-  }
 }
index 074b89347629f5a239e9d142f179373f95e00f25..beff812603e9ebeb8b3c88f1ce6b555cd4b7ea90 100644 (file)
@@ -26,11 +26,13 @@ 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.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualitygate.QGateWithOrgDto;
 import org.sonar.db.qualitygate.QualityGateConditionDto;
 import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.qualitygate.QualityGateFinder;
@@ -56,8 +58,8 @@ public class ShowActionTest {
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
 
-  private DbClient dbClient = db.getDbClient();
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+
   private WsActionTester ws = new WsActionTester(
     new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()),
       new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
@@ -72,13 +74,17 @@ public class ShowActionTest {
         tuple("7.0", "'actions' field is added in the response"));
     assertThat(action.params())
       .extracting(Param::key, Param::isRequired)
-      .containsExactlyInAnyOrder(tuple("id", false), tuple("name", false));
+      .containsExactlyInAnyOrder(
+        tuple("id", false),
+        tuple("name", false),
+        tuple("organization", false));
   }
 
   @Test
   public void json_example() {
-    userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate("My Quality Gate");
+    OrganizationDto organization = db.organizations().insert();
+    userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("My Quality Gate"));
     MetricDto blockerViolationsMetric = db.measures().insertMetric(m -> m.setKey("blocker_violations"));
     MetricDto criticalViolationsMetric = db.measures().insertMetric(m -> m.setKey("critical_violations"));
     db.qualityGates().addCondition(qualityGate, blockerViolationsMetric, c -> c.setOperator("GT").setPeriod(null).setErrorThreshold("0").setWarningThreshold(null));
@@ -86,6 +92,7 @@ public class ShowActionTest {
 
     String response = ws.newRequest()
       .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .execute()
       .getInput();
 
@@ -95,12 +102,15 @@ public class ShowActionTest {
 
   @Test
   public void show() {
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
     MetricDto metric = db.measures().insertMetric();
     QualityGateConditionDto condition1 = db.qualityGates().addCondition(qualityGate, metric, c -> c.setOperator("GT").setPeriod(null));
     QualityGateConditionDto condition2 = db.qualityGates().addCondition(qualityGate, metric, c -> c.setOperator("LT").setPeriod(1));
 
-    ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     assertThat(response.getId()).isEqualTo(qualityGate.getId());
@@ -114,11 +124,27 @@ public class ShowActionTest {
         tuple(condition2.getId(), metric.getKey(), true, 1, "LT", condition2.getErrorThreshold(), condition2.getWarningThreshold()));
   }
 
+  @Test
+  public void default_organization_is_used_when_no_organization_parameter() {
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+    OrganizationDto otherOrganization = db.organizations().insert();
+    QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
+
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .executeProtobuf(ShowWsResponse.class);
+
+    assertThat(response.getId()).isEqualTo(qualityGate.getId());
+  }
+
   @Test
   public void show_built_in() {
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
 
-    ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     assertThat(response.getIsBuiltIn()).isTrue();
@@ -126,9 +152,12 @@ public class ShowActionTest {
 
   @Test
   public void show_by_id() {
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
 
-    ShowWsResponse response = ws.newRequest().setParam("id", qualityGate.getId().toString())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("id", qualityGate.getId().toString())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     assertThat(response.getId()).isEqualTo(qualityGate.getId());
@@ -137,9 +166,12 @@ public class ShowActionTest {
 
   @Test
   public void no_condition() {
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
 
-    ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     assertThat(response.getId()).isEqualTo(qualityGate.getId());
@@ -149,10 +181,13 @@ public class ShowActionTest {
 
   @Test
   public void actions() {
-    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
 
-    ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     Actions actions = response.getActions();
@@ -166,11 +201,14 @@ public class ShowActionTest {
 
   @Test
   public void actions_on_default() {
-    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
     db.qualityGates().setDefaultQualityGate(qualityGate);
 
-    ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     Actions actions = response.getActions();
@@ -184,10 +222,13 @@ public class ShowActionTest {
 
   @Test
   public void actions_on_built_in() {
-    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+    OrganizationDto organization = db.organizations().insert();
+    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
 
-    ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     Actions actions = response.getActions();
@@ -201,10 +242,13 @@ public class ShowActionTest {
 
   @Test
   public void actions_when_not_quality_gate_administer() {
-    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganizationProvider.get().getUuid());
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+    OrganizationDto organization = db.organizations().insert();
+    userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization);
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
 
-    ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
       .executeProtobuf(ShowWsResponse.class);
 
     Actions actions = response.getActions();
@@ -218,15 +262,21 @@ public class ShowActionTest {
 
   @Test
   public void fail_when_no_name_or_id() {
+    OrganizationDto organization = db.organizations().insert();
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
+
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Either 'id' or 'name' must be provided");
 
-    ws.newRequest().execute();
+    ws.newRequest()
+      .setParam("organization", organization.getKey())
+      .execute();
   }
 
   @Test
   public void fail_when_both_name_or_id() {
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization);
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Either 'id' or 'name' must be provided");
@@ -234,12 +284,14 @@ public class ShowActionTest {
     ws.newRequest()
       .setParam("name", qualityGate.getName())
       .setParam("id", qualityGate.getId().toString())
+      .setParam("organization", organization.getKey())
       .execute();
   }
 
   @Test
   public void fail_when_condition_is_on_disabled_metric() {
-    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
     MetricDto metric = db.measures().insertMetric();
     db.qualityGates().addCondition(qualityGate, metric);
     db.getDbClient().metricDao().disableCustomByKey(db.getSession(), metric.getKey());
@@ -250,6 +302,49 @@ public class ShowActionTest {
 
     ws.newRequest()
       .setParam("name", qualityGate.getName())
+      .setParam("organization", organization.getKey())
+      .execute();
+  }
+
+  @Test
+  public void fail_when_quality_name_does_not_exist() {
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("No quality gate has been found for name UNKNOWN");
+
+    ws.newRequest()
+      .setParam("name", "UNKNOWN")
+      .setParam("organization", organization.getKey())
+      .execute();
+  }
+
+  @Test
+  public void fail_when_quality_id_does_not_exist() {
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("No quality gate has been found for id 123");
+
+    ws.newRequest()
+      .setParam("id", "123")
+      .setParam("organization", organization.getKey())
+      .execute();
+  }
+
+  @Test
+  public void fail_when_organization_does_not_exist() {
+    OrganizationDto organization = db.organizations().insert();
+    QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("No organization with key 'Unknown'");
+
+    ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .setParam("organization", "Unknown")
       .execute();
   }
 }