import java.io.IOException;
import org.apache.commons.io.IOUtils;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.sonar.db.measure.MeasureTesting.newLiveMeasure;
import static org.sonar.db.measure.MeasureTesting.newMeasureDto;
import static org.sonar.db.metric.MetricTesting.newMetricDto;
-import static org.sonar.test.JsonAssert.assertJson;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ANALYSIS_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PROJECT_KEY;
+import static org.sonar.test.JsonAssert.assertJson;
public class ProjectStatusActionTest {
private static final String ANALYSIS_ID = "task-uuid";
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private WsActionTester ws;
- private DbClient dbClient;
- private DbSession dbSession;
- private MetricDto gateDetailsMetric;
+ private DbClient dbClient = db.getDbClient();
+ private DbSession dbSession = db.getSession();
- @Before
- public void setUp() {
- dbClient = db.getDbClient();
- dbSession = db.getSession();
-
- gateDetailsMetric = dbClient.metricDao().insert(dbSession, newMetricDto()
- .setEnabled(true)
- .setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY));
-
- ws = new WsActionTester(new ProjectStatusAction(dbClient, TestComponentFinder.from(db), userSession));
- }
+ private WsActionTester ws = new WsActionTester(new ProjectStatusAction(dbClient, TestComponentFinder.from(db), userSession));
@Test
- public void test_definition() throws Exception {
+ public void test_definition() {
WebService.Action def = ws.getDef();
assertThat(def.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("analysisId", "projectKey", "projectId");
assertThat(def.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactly(
- tuple("6.4", "The field 'ignoredConditions' is added to the response")
- );
+ tuple("6.4", "The field 'ignoredConditions' is added to the response"));
}
@Test
public void test_json_example() throws IOException {
ComponentDto project = db.components().insertPrivateProject(db.organizations().insert());
userSession.addProjectPermission(UserRole.USER, project);
+ MetricDto gateDetailsMetric = insertGateDetailMetric();
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(project)
.setPeriodMode("last_version")
.setPeriodMode("last_version")
.setPeriodParam("2016-12-07")
.setPeriodDate(1_500L));
+ MetricDto gateDetailsMetric = insertGateDetailMetric();
dbClient.measureDao().insert(dbSession,
newMeasureDto(gateDetailsMetric, project, pastAnalysis)
.setData(IOUtils.toString(getClass().getResource("ProjectStatusActionTest/measure_data.json"))));
.setPeriodMode("last_version")
.setPeriodParam("2015-12-07")
.setPeriodDate(956789123987L));
+ MetricDto gateDetailsMetric = insertGateDetailMetric();
dbClient.liveMeasureDao().insert(dbSession,
newLiveMeasure(project, gateDetailsMetric)
.setData(IOUtils.toString(getClass().getResource("ProjectStatusActionTest/measure_data.json"))));
.setPeriodMode("last_version")
.setPeriodParam("2015-12-07")
.setPeriodDate(956789123987L));
+ MetricDto gateDetailsMetric = insertGateDetailMetric();
dbClient.liveMeasureDao().insert(dbSession,
newLiveMeasure(project, gateDetailsMetric)
.setData(IOUtils.toString(getClass().getResource("ProjectStatusActionTest/measure_data.json"))));
dbSession.commit();
userSession.addProjectPermission(UserRole.USER, project);
- ProjectStatusResponse result = callByAnalysisId(snapshot.getUuid());
+ ProjectStatusResponse result = ws.newRequest()
+ .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
+ .executeProtobuf(ProjectStatusResponse.class);
assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE);
assertThat(result.getProjectStatus().getConditionsCount()).isEqualTo(0);
ComponentDto project = db.components().insertPrivateProject(db.organizations().insert());
userSession.addProjectPermission(UserRole.USER, project);
- ProjectStatusResponse result = callByProjectId(project.uuid());
+ ProjectStatusResponse result = ws.newRequest()
+ .setParam(PARAM_PROJECT_ID, project.uuid())
+ .executeProtobuf(ProjectStatusResponse.class);
assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE);
assertThat(result.getProjectStatus().getConditionsCount()).isEqualTo(0);
dbSession.commit();
userSession.addProjectPermission(UserRole.ADMIN, project);
- callByAnalysisId(snapshot.getUuid());
+ ws.newRequest()
+ .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
+ .executeProtobuf(ProjectStatusResponse.class);
}
@Test
dbSession.commit();
userSession.addProjectPermission(UserRole.USER, project);
- callByAnalysisId(snapshot.getUuid());
+ ws.newRequest()
+ .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
+ .executeProtobuf(ProjectStatusResponse.class);
}
@Test
expectedException.expect(NotFoundException.class);
expectedException.expectMessage("Analysis with id 'task-uuid' is not found");
- callByAnalysisId(ANALYSIS_ID);
+ ws.newRequest()
+ .setParam(PARAM_ANALYSIS_ID, ANALYSIS_ID)
+ .executeProtobuf(ProjectStatusResponse.class);
}
@Test
expectedException.expect(ForbiddenException.class);
- callByAnalysisId(snapshot.getUuid());
+ ws.newRequest()
+ .setParam(PARAM_ANALYSIS_ID, snapshot.getUuid())
+ .executeProtobuf(ProjectStatusResponse.class);
}
@Test
}
@Test
- public void fail_when_using_branch_uuid() throws Exception {
+ public void fail_when_using_branch_uuid() {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertMainBranch(organization);
userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
.execute();
}
- private ProjectStatusResponse callByAnalysisId(String taskId) {
- return ws.newRequest()
- .setParam(PARAM_ANALYSIS_ID, taskId)
- .executeProtobuf(ProjectStatusResponse.class);
+ private void logInAsSystemAdministrator() {
+ userSession.logIn().setSystemAdministrator();
}
- private ProjectStatusResponse callByProjectId(String projectUuid) {
- return ws.newRequest()
- .setParam(PARAM_PROJECT_ID, projectUuid)
- .executeProtobuf(ProjectStatusResponse.class);
+ private MetricDto insertGateDetailMetric() {
+ return dbClient.metricDao().insert(dbSession, newMetricDto()
+ .setEnabled(true)
+ .setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY));
}
- private void logInAsSystemAdministrator() {
- userSession.logIn().setSystemAdministrator();
- }
}