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());
}
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);
<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
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();
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;
@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) {
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;
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);
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);
.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) {
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;
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 {
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
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) {
.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())
.addAllConditions(conditions.stream()
.map(toWsCondition(metricsById))
.collect(toList()))
- .setActions(wsSupport.getActions(qualityGate, defaultQualityGate))
+ .setActions(wsSupport.getActions(organization, qualityGate, defaultQualityGate))
.build();
}
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);
- }
}
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;
@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)));
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));
String response = ws.newRequest()
.setParam("name", qualityGate.getName())
+ .setParam("organization", organization.getKey())
.execute()
.getInput();
@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());
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();
@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());
@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());
@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();
@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();
@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();
@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();
@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");
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());
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();
}
}