package org.sonar.server.qualitygate.ws;
import java.util.ArrayList;
-import java.util.List;
import javax.annotation.Nullable;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.WebService;
-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.metric.MetricDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
-import org.sonar.db.qualitygate.QualityGateDbTester;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonarqube.ws.Qualitygates.CreateConditionResponse;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.metric.MetricTesting.newMetricDto;
+import static org.assertj.core.api.Assertions.tuple;
+import static org.sonar.api.measures.Metric.ValueType.INT;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
-import static org.sonar.server.computation.task.projectanalysis.metric.Metric.MetricType.PERCENT;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ERROR;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC;
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create();
private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
- private QualityGateDbTester qualityGateDbTester = new QualityGateDbTester(db);
private CreateConditionAction underTest = new CreateConditionAction(userSession, dbClient, new QualityGateConditionsUpdater(dbClient), defaultOrganizationProvider);
- private QualityGateDto qualityGateDto;
- private MetricDto coverageMetricDto = newMetricDto()
- .setKey("coverage")
- .setShortName("Coverage")
- .setValueType(PERCENT.name())
- .setHidden(false);
private WsActionTester ws = new WsActionTester(underTest);
- @Before
- public void setUp() throws Exception {
- qualityGateDto = qualityGateDbTester.insertQualityGate();
- dbClient.metricDao().insert(dbSession, coverageMetricDto);
- dbSession.commit();
- }
-
@Test
public void create_warning_condition() throws Exception {
logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ MetricDto metric = insertMetric();
- CreateConditionResponse response = executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", "90", null, null);
+ executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", null, null);
- assertCondition(response, "LT", "90", null, null);
+ assertCondition(qualityGate, metric, "LT", "90", null, null);
}
@Test
public void create_error_condition() throws Exception {
logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ MetricDto metric = insertMetric();
- CreateConditionResponse response = executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", null, "90", null);
+ executeRequest(qualityGate.getId(), metric.getKey(), "LT", null, "90", null);
- assertCondition(response, "LT", null, "90", null);
+ assertCondition(qualityGate, metric, "LT", null, "90", null);
}
@Test
public void create_condition_over_leak_period() throws Exception {
logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ MetricDto metric = insertMetric();
- CreateConditionResponse response = executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", null, "90", 1);
+ executeRequest(qualityGate.getId(), metric.getKey(), "LT", null, "90", 1);
- assertCondition(response, "LT", null, "90", 1);
+ assertCondition(qualityGate, metric, "LT", null, "90", 1);
+ }
+
+ @Test
+ public void test_response() throws Exception {
+ logInAsQualityGateAdmin();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ MetricDto metric = insertMetric();
+
+ CreateConditionResponse response = executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", "45", 1);
+
+ QualityGateConditionDto condition = new ArrayList<>(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGate.getId())).get(0);
+ assertThat(response.getId()).isEqualTo(condition.getId());
+ assertThat(response.getMetric()).isEqualTo(metric.getKey());
+ assertThat(response.getOp()).isEqualTo("LT");
+ assertThat(response.getWarning()).isEqualTo("90");
+ assertThat(response.getError()).isEqualTo("45");
+ assertThat(response.getPeriod()).isEqualTo(1);
}
@Test
public void throw_ForbiddenException_if_not_gate_administrator() throws Exception {
userSession.logIn();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ MetricDto metric = insertMetric();
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
- executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", "90", null, null);
+ executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", null, null);
}
@Test
// is defined on the default organization
OrganizationDto org = db.organizations().insert();
userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, org);
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ MetricDto metric = insertMetric();
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
- executeRequest(qualityGateDto.getId(), coverageMetricDto.getKey(), "LT", "90", null, null);
+ executeRequest(qualityGate.getId(), metric.getKey(), "LT", "90", null, null);
}
@Test
assertThat(action.params()).hasSize(6);
}
- private void assertCondition(CreateConditionResponse response, String operator, @Nullable String warning, @Nullable String error, @Nullable Integer period) {
- List<QualityGateConditionDto> conditionDtoList = new ArrayList<>(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateDto.getId()));
- assertThat(conditionDtoList).hasSize(1);
- QualityGateConditionDto qualityGateConditionDto = conditionDtoList.get(0);
- assertThat(qualityGateConditionDto.getQualityGateId()).isEqualTo(qualityGateDto.getId());
- assertThat(qualityGateConditionDto.getMetricId()).isEqualTo(coverageMetricDto.getId().longValue());
- assertThat(qualityGateConditionDto.getOperator()).isEqualTo(operator);
- assertThat(qualityGateConditionDto.getWarningThreshold()).isEqualTo(warning);
- assertThat(qualityGateConditionDto.getErrorThreshold()).isEqualTo(error);
- assertThat(qualityGateConditionDto.getPeriod()).isEqualTo(period);
-
- assertThat(response.getId()).isEqualTo(qualityGateConditionDto.getId());
- assertThat(response.getMetric()).isEqualTo(coverageMetricDto.getKey());
- assertThat(response.getOp()).isEqualTo(operator);
- if (warning != null) {
- assertThat(response.getWarning()).isEqualTo(warning);
- } else {
- assertThat(response.hasWarning()).isFalse();
- }
- if (error != null) {
- assertThat(response.getError()).isEqualTo(error);
- } else {
- assertThat(response.hasError()).isFalse();
- }
- if (period != null) {
- assertThat(response.getPeriod()).isEqualTo(period);
- } else {
- assertThat(response.hasPeriod()).isFalse();
- }
+ private void assertCondition(QualityGateDto qualityGate, MetricDto metric, String operator, @Nullable String warning, @Nullable String error, @Nullable Integer period) {
+ assertThat(dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGate.getId()))
+ .extracting(QualityGateConditionDto::getQualityGateId, QualityGateConditionDto::getMetricId, QualityGateConditionDto::getOperator,
+ QualityGateConditionDto::getWarningThreshold, QualityGateConditionDto::getErrorThreshold, QualityGateConditionDto::getPeriod)
+ .containsExactlyInAnyOrder(tuple(qualityGate.getId(), metric.getId().longValue(), operator, warning, error, period));
}
private CreateConditionResponse executeRequest(long qualityProfileId, String metricKey, String operator, @Nullable String warning, @Nullable String error,
private void logInAsQualityGateAdmin() {
userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
}
+
+ private MetricDto insertMetric() {
+ return db.measures().insertMetric(m -> m.setValueType(INT.name()).setHidden(false));
+ }
}