package org.sonarqube.tests;
import com.sonar.orchestrator.Orchestrator;
-import org.sonarqube.tests.projectAdministration.ProjectVisibilityPageTest;
-import org.sonarqube.tests.user.UsersPageTest;
+import org.junit.ClassRule;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
import org.sonarqube.tests.authorisation.ExecuteAnalysisPermissionTest;
import org.sonarqube.tests.authorisation.IssuePermissionTest;
import org.sonarqube.tests.authorisation.PermissionSearchTest;
import org.sonarqube.tests.complexity.ComplexityMeasuresTest;
import org.sonarqube.tests.customMeasure.CustomMeasuresTest;
import org.sonarqube.tests.i18n.I18nTest;
+import org.sonarqube.tests.measure.DifferentialPeriodsTest;
import org.sonarqube.tests.measure.MeasuresWsTest;
import org.sonarqube.tests.measure.ProjectDashboardTest;
import org.sonarqube.tests.measure.ProjectMeasuresPageTest;
-import org.sonarqube.tests.measure.DifferentialPeriodsTest;
import org.sonarqube.tests.measure.SincePreviousVersionHistoryTest;
import org.sonarqube.tests.measure.SinceXDaysHistoryTest;
import org.sonarqube.tests.measure.TimeMachineTest;
import org.sonarqube.tests.projectAdministration.BackgroundTasksTest;
-import org.sonarqube.tests.projectAdministration.ProjectBulkDeletionPageTest;
import org.sonarqube.tests.projectAdministration.ProjectAdministrationTest;
+import org.sonarqube.tests.projectAdministration.ProjectBulkDeletionPageTest;
import org.sonarqube.tests.projectAdministration.ProjectLinksPageTest;
+import org.sonarqube.tests.projectAdministration.ProjectVisibilityPageTest;
import org.sonarqube.tests.projectSearch.ProjectsPageTest;
import org.sonarqube.tests.qualityGate.QualityGateNotificationTest;
+import org.sonarqube.tests.qualityGate.QualityGateOnRatingMeasuresTest;
import org.sonarqube.tests.qualityGate.QualityGateTest;
import org.sonarqube.tests.qualityGate.QualityGateUiTest;
import org.sonarqube.tests.settings.DeprecatedPropertiesWsTest;
import org.sonarqube.tests.sourceCode.EncodingTest;
import org.sonarqube.tests.sourceCode.HighlightingTest;
import org.sonarqube.tests.sourceCode.ProjectCodeTest;
-import org.junit.ClassRule;
-import org.junit.runner.RunWith;
-import org.junit.runners.Suite;
+import org.sonarqube.tests.user.UsersPageTest;
import static util.ItUtils.pluginArtifact;
import static util.ItUtils.xooPlugin;
QualityGateTest.class,
QualityGateUiTest.class,
QualityGateNotificationTest.class,
+ QualityGateOnRatingMeasuresTest.class,
// authorisation
ExecuteAnalysisPermissionTest.class,
IssuePermissionTest.class,
package org.sonarqube.tests.qualityGate;
import com.sonar.orchestrator.Orchestrator;
-import org.sonarqube.tests.Category1Suite;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.Test;
-import org.sonar.wsclient.qualitygate.QualityGateClient;
+import org.sonarqube.tests.Category1Suite;
+import org.sonarqube.tests.Tester;
import org.sonarqube.ws.WsMeasures;
-import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.WsProjects.CreateWsResponse.Project;
+import org.sonarqube.ws.WsQualityGates;
import org.sonarqube.ws.client.qualitygate.CreateConditionRequest;
-import org.sonarqube.ws.client.qualitygate.QualityGatesService;
-import org.sonarqube.ws.client.qualitygate.SelectWsRequest;
import util.ItUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.getMeasure;
-import static util.ItUtils.newAdminWsClient;
-import static util.ItUtils.resetSettings;
import static util.ItUtils.runProjectAnalysis;
-import static util.ItUtils.setServerProperty;
public class QualityGateOnRatingMeasuresTest {
@ClassRule
public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
- private static final String PROJECT_KEY = "sample";
-
- static WsClient wsClient;
-
- static QualityGatesService QUALITY_GATES;
-
- Long qualityGateId;
-
- @BeforeClass
- public static void init() {
- wsClient = newAdminWsClient(orchestrator);
- QUALITY_GATES = wsClient.qualityGates();
- }
-
- @Before
- public void prepareData() {
- orchestrator.resetData();
- qualityGateId = QUALITY_GATES.create("QualityGate").getId();
- orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);
- QUALITY_GATES.associateProject(new SelectWsRequest().setGateId(qualityGateId).setProjectKey(PROJECT_KEY));
- }
-
- @After
- public void resetData() throws Exception {
- qgClient().destroy(qualityGateId);
- resetSettings(orchestrator, null, "sonar.leak.period");
- }
+ @Rule
+ public Tester tester = new Tester(orchestrator).disableOrganizations();
@Test
public void generate_warning_qgate_on_rating_metric() throws Exception {
- QUALITY_GATES.createCondition(CreateConditionRequest.builder()
- .setQualityGateId(qualityGateId.intValue())
+ Project project = tester.projects().generate(null);
+ WsQualityGates.CreateWsResponse qualityGate = tester.qGates().generate();
+ tester.qGates().associateProject(qualityGate, project);
+ tester.qGates().service().createCondition(CreateConditionRequest.builder()
+ .setQualityGateId(qualityGate.getId())
.setMetricKey("security_rating")
.setOperator("GT")
.setWarning("3")
.build());
ItUtils.restoreProfile(orchestrator, getClass().getResource("/qualityGate/QualityGateOnRatingMeasuresTest/with-many-rules.xml"));
- orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "with-many-rules");
+ orchestrator.getServer().associateProjectToQualityProfile(project.getKey(), "xoo", "with-many-rules");
- runProjectAnalysis(orchestrator, "qualitygate/xoo-sample");
+ runProjectAnalysis(orchestrator, "qualitygate/xoo-sample", "sonar.projectKey", project.getKey());
- assertThat(getGateStatusMeasure().getValue()).isEqualTo("WARN");
+ assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("WARN");
}
@Test
public void generate_error_qgate_on_rating_metric_on_leak_period() throws Exception {
- setServerProperty(orchestrator, "sonar.leak.period", "previous_analysis");
- QUALITY_GATES.createCondition(CreateConditionRequest.builder()
- .setQualityGateId(qualityGateId.intValue())
+ Project project = tester.projects().generate(null);
+ WsQualityGates.CreateWsResponse qualityGate = tester.qGates().generate();
+ tester.qGates().associateProject(qualityGate, project);
+ tester.settings().setGlobalSetting("sonar.leak.period", "previous_analysis");
+ tester.wsClient().qualityGates().createCondition(CreateConditionRequest.builder()
+ .setQualityGateId(qualityGate.getId())
.setMetricKey("new_security_rating")
.setOperator("GT")
.setError("3")
.build());
// Run first analysis with empty quality gate -> quality gate is green
- orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "empty");
- runProjectAnalysis(orchestrator, "qualitygate/xoo-sample");
- assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK");
+ orchestrator.getServer().associateProjectToQualityProfile(project.getKey(), "xoo", "empty");
+ runProjectAnalysis(orchestrator, "qualitygate/xoo-sample", "sonar.projectKey", project.getKey());
+ assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("OK");
// Run second analysis with some rules that makes Security Rating to E -> quality gate is red
ItUtils.restoreProfile(orchestrator, getClass().getResource("/qualityGate/QualityGateOnRatingMeasuresTest/with-many-rules.xml"));
- orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "with-many-rules");
- runProjectAnalysis(orchestrator, "qualitygate/xoo-sample");
- assertThat(getGateStatusMeasure().getValue()).isEqualTo("ERROR");
- }
-
- private WsMeasures.Measure getGateStatusMeasure() {
- return getMeasure(orchestrator, PROJECT_KEY, "alert_status");
+ orchestrator.getServer().associateProjectToQualityProfile(project.getKey(), "xoo", "with-many-rules");
+ runProjectAnalysis(orchestrator, "qualitygate/xoo-sample", "sonar.projectKey", project.getKey());
+ assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("ERROR");
}
- private static QualityGateClient qgClient() {
- return orchestrator.getServer().adminWsClient().qualityGateClient();
+ private WsMeasures.Measure getGateStatusMeasure(Project project) {
+ return getMeasure(orchestrator, project.getKey(), "alert_status");
}
}