import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarScanner;
-import org.sonarqube.tests.Category1Suite;
import java.util.Iterator;
import javax.mail.internet.MimeMessage;
import org.junit.AfterClass;
-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.NewCondition;
-import org.sonar.wsclient.qualitygate.QualityGate;
-import org.sonar.wsclient.qualitygate.QualityGateClient;
+import org.sonarqube.tests.Category1Suite;
+import org.sonarqube.tests.Tester;
+import org.sonarqube.ws.WsProjects.CreateWsResponse.Project;
+import org.sonarqube.ws.WsQualityGates;
import org.sonarqube.ws.client.PostRequest;
-import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.qualitygate.CreateConditionRequest;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
-import util.user.UserRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonarqube.ws.WsMeasures.Measure;
import static util.ItUtils.getMeasure;
-import static util.ItUtils.newAdminWsClient;
-import static util.ItUtils.newUserWsClient;
import static util.ItUtils.projectDir;
-import static util.ItUtils.resetEmailSettings;
-import static util.ItUtils.resetPeriod;
-import static util.ItUtils.setServerProperty;
public class QualityGateNotificationTest {
- private static long DEFAULT_QUALITY_GATE;
-
- private static final String PROJECT_KEY = "sample";
-
@ClassRule
public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
- @ClassRule
- public static UserRule userRule = UserRule.from(orchestrator);
+ @Rule
+ public Tester tester = new Tester(orchestrator).disableOrganizations();
- private static Wiser smtpServer;
+ private static Wiser SMTP_SERVER;
@BeforeClass
- public static void init() throws Exception {
- DEFAULT_QUALITY_GATE = qgClient().list().defaultGate().id();
-
- setServerProperty(orchestrator, "sonar.leak.period", "previous_analysis");
- resetEmailSettings(orchestrator);
-
- smtpServer = new Wiser(0);
- smtpServer.start();
+ public static void startSmtpServer() throws Exception {
+ SMTP_SERVER = new Wiser(0);
+ SMTP_SERVER.start();
}
@AfterClass
- public static void resetData() throws Exception {
- qgClient().setDefault(DEFAULT_QUALITY_GATE);
-
- resetPeriod(orchestrator);
- resetEmailSettings(orchestrator);
-
- if (smtpServer != null) {
- smtpServer.stop();
+ public static void stopSmtpServer() throws Exception {
+ if (SMTP_SERVER != null) {
+ SMTP_SERVER.stop();
}
}
- @Before
- public void cleanUp() {
- orchestrator.resetData();
- }
-
@Test
public void status_on_metric_variation_and_send_notifications() throws Exception {
- setServerProperty(orchestrator, "email.smtp_host.secured", "localhost");
- setServerProperty(orchestrator, "email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort()));
+ tester.settings().setGlobalSettings("sonar.leak.period", "previous_analysis");
+ tester.settings().setGlobalSettings("email.smtp_host.secured", "localhost");
+ tester.settings().setGlobalSettings("email.smtp_port.secured", Integer.toString(SMTP_SERVER.getServer().getPort()));
// Create user, who will receive notifications for new violations
- userRule.createUser("tester", "Tester", "tester@example.org", "tester");
+ tester.users().generate(u -> u.setLogin("tester").setPassword("tester").setEmail("tester@example.org"));
// Send test email to the test user
- newAdminWsClient(orchestrator).wsConnector().call(new PostRequest("api/emails/send")
+ tester.wsClient().wsConnector().call(new PostRequest("api/emails/send")
.setParam("to", "test@example.org")
.setParam("message", "This is a test message from SonarQube"))
.failIfNotSuccessful();
// Add notifications to the test user
- WsClient wsClient = newUserWsClient(orchestrator, "tester", "tester");
- wsClient.wsConnector().call(new PostRequest("api/notifications/add")
+ tester.as("tester").wsClient().wsConnector().call(new PostRequest("api/notifications/add")
.setParam("type", "NewAlerts")
.setParam("channel", "EmailNotificationChannel"))
.failIfNotSuccessful();
// Create quality gate with conditions on variations
- QualityGate simple = qgClient().create("SimpleWithDifferential");
- qgClient().setDefault(simple.id());
- qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").period(1).operator("EQ").warningThreshold("0"));
+ WsQualityGates.CreateWsResponse simple = tester.qGates().generate();
+ tester.qGates().service().createCondition(CreateConditionRequest.builder().setQualityGateId(simple.getId()).setMetricKey("ncloc").setPeriod(1).setOperator("EQ").setWarning("0").build());
+ Project project = tester.projects().generate(null);
+ tester.qGates().associateProject(simple, project);
- SonarScanner analysis = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
+ SonarScanner analysis = SonarScanner.create(projectDir("qualitygate/xoo-sample")).setProperty("sonar.projectKey", project.getKey());
orchestrator.executeBuild(analysis);
- assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK");
+ assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("OK");
orchestrator.executeBuild(analysis);
- assertThat(getGateStatusMeasure().getValue()).isEqualTo("WARN");
+ assertThat(getGateStatusMeasure(project).getValue()).isEqualTo("WARN");
- qgClient().unsetDefault();
- qgClient().destroy(simple.id());
+ waitUntilAllNotificationsAreDelivered(SMTP_SERVER);
- waitUntilAllNotificationsAreDelivered(smtpServer);
-
- Iterator<WiserMessage> emails = smtpServer.getMessages().iterator();
+ Iterator<WiserMessage> emails = SMTP_SERVER.getMessages().iterator();
MimeMessage message = emails.next().getMimeMessage();
assertThat(message.getHeader("To", null)).isEqualTo("<test@example.org>");
assertThat(message.getHeader("To", null)).isEqualTo("<tester@example.org>");
assertThat((String) message.getContent()).contains("Quality gate status: Orange (was Green)");
assertThat((String) message.getContent()).contains("Quality gate threshold: Lines of Code variation = 0 since previous analysis");
- assertThat((String) message.getContent()).contains("/dashboard?id=sample");
+ assertThat((String) message.getContent()).contains("/dashboard?id=" + project.getKey());
assertThat(emails.hasNext()).isFalse();
}
- private Measure getGateStatusMeasure() {
- return getMeasure(orchestrator, PROJECT_KEY, "alert_status");
- }
-
- private static QualityGateClient qgClient() {
- return orchestrator.getServer().adminWsClient().qualityGateClient();
+ private Measure getGateStatusMeasure(Project project) {
+ return getMeasure(orchestrator, project.getKey(), "alert_status");
}
private static void waitUntilAllNotificationsAreDelivered(Wiser smtpServer) throws InterruptedException {
assertThat(getGateStatusMeasure(projectKey).getValue()).isEqualTo("OK");
} finally {
qgClient().unsetDefault();
- qgClient().destroy(empty.id());
}
}
assertThat(getGateStatusMeasure(projectKey).getValue()).isEqualTo("OK");
} finally {
qgClient().unsetDefault();
- qgClient().destroy(simple.id());
}
}
assertThat(getGateStatusMeasure(projectKey).getValue()).isEqualTo("WARN");
} finally {
qgClient().unsetDefault();
- qgClient().destroy(simple.id());
}
}
assertThat(getGateStatusMeasure(projectKey).getValue()).isEqualTo("ERROR");
} finally {
qgClient().unsetDefault();
- qgClient().destroy(simple.id());
}
}
assertThat(getGateStatusMeasure(projectKey).getValue()).isEqualTo("ERROR");
} finally {
qgClient().unsetDefault();
- qgClient().destroy(alert.id());
- qgClient().destroy(error.id());
}
}
.contains(tuple("ncloc", "GT", "10"), tuple("duplicated_lines_density", "GT", "20"));
} finally {
qgClient().unsetDefault();
- qgClient().destroy(allTypes.id());
}
}
assertThat(condition.getErrorThreshold()).isEqualTo("7");
} finally {
qgClient().unsetDefault();
- qgClient().destroy(simple.id());
}
}
@Test
public void does_not_fail_when_condition_is_on_removed_metric() throws Exception {
-
// create project
Project project = tester.projects().generate(null);
String projectKey = project.getKey();
String customMetricKey = randomAlphabetic(10);
createCustomIntMetric(customMetricKey);
try {
-
// create quality gate
WsQualityGates.CreateWsResponse simple = tester.wsClient().qualityGates().create("OnCustomMetric");
Long qualityGateId = simple.getId();
- try {
- qgClient().createCondition(NewCondition.create(qualityGateId).metricKey(customMetricKey).operator("GT").warningThreshold("40"));
+ qgClient().createCondition(NewCondition.create(qualityGateId).metricKey(customMetricKey).operator("GT").warningThreshold("40"));
- // delete custom metric
- deleteCustomMetric(customMetricKey);
+ // delete custom metric
+ deleteCustomMetric(customMetricKey);
- // run analysis
- tester.wsClient().qualityGates().associateProject(new SelectWsRequest().setProjectKey(projectKey).setGateId(qualityGateId));
- BuildResult buildResult = executeAnalysis(projectKey);
+ // run analysis
+ tester.wsClient().qualityGates().associateProject(new SelectWsRequest().setProjectKey(projectKey).setGateId(qualityGateId));
+ BuildResult buildResult = executeAnalysis(projectKey);
- // verify quality gate
- verifyQGStatusInPostTask(buildResult, projectKey, TASK_STATUS_SUCCESS, QG_STATUS_OK);
- assertThat(getGateStatusMeasure(projectKey).getValue()).isEqualTo("OK");
- } finally {
- qgClient().destroy(qualityGateId);
- }
+ // verify quality gate
+ verifyQGStatusInPostTask(buildResult, projectKey, TASK_STATUS_SUCCESS, QG_STATUS_OK);
+ assertThat(getGateStatusMeasure(projectKey).getValue()).isEqualTo("OK");
} finally {
deleteCustomMetric(customMetricKey);
}
import java.util.Date;
import javax.annotation.Nullable;
import org.apache.commons.lang.time.DateFormatUtils;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
-import org.sonar.wsclient.qualitygate.NewCondition;
-import org.sonar.wsclient.qualitygate.QualityGate;
-import org.sonar.wsclient.qualitygate.QualityGateClient;
-import org.sonar.wsclient.qualitygate.QualityGateCondition;
-import org.sonar.wsclient.qualitygate.UpdateCondition;
import org.sonarqube.pageobjects.Navigation;
import org.sonarqube.pageobjects.ProjectActivityPage;
import org.sonarqube.tests.Category1Suite;
import org.sonarqube.tests.Tester;
+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.UpdateConditionRequest;
import static com.codeborne.selenide.Selenide.$;
import static org.apache.commons.lang.time.DateUtils.addDays;
import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.projectDir;
-import static util.ItUtils.resetPeriod;
-import static util.ItUtils.setServerProperty;
import static util.selenium.Selenese.runSelenese;
public class QualityGateUiTest {
@Rule
public Tester tester = new Tester(orchestrator).disableOrganizations();
- private static long DEFAULT_QUALITY_GATE;
-
- @BeforeClass
- public static void initPeriod() throws Exception {
- setServerProperty(orchestrator, "sonar.leak.period", "previous_analysis");
- DEFAULT_QUALITY_GATE = qgClient().list().defaultGate().id();
- }
-
- @AfterClass
- public static void resetData() throws Exception {
- resetPeriod(orchestrator);
- qgClient().setDefault(DEFAULT_QUALITY_GATE);
- }
-
@Before
- public void cleanUp() {
- orchestrator.resetData();
+ public void initPeriod() throws Exception {
+ tester.settings().setGlobalSettings("sonar.leak.period", "previous_analysis");
}
/**
*/
@Test
public void display_alerts_correctly_in_history_page() {
- QualityGateClient qgClient = qgClient();
- QualityGate qGate = qgClient.create("AlertsForHistory");
- qgClient.setDefault(qGate.id());
+ Project project = tester.projects().generate(null);
+ WsQualityGates.CreateWsResponse qGate = tester.qGates().generate();
+ tester.qGates().associateProject(qGate, project);
String firstAnalysisDate = DateFormatUtils.ISO_DATE_FORMAT.format(addDays(new Date(), -2));
String secondAnalysisDate = DateFormatUtils.ISO_DATE_FORMAT.format(addDays(new Date(), -1));
// with this configuration, project should have an Orange alert
- QualityGateCondition lowThresholds = qgClient.createCondition(NewCondition.create(qGate.id()).metricKey("lines").operator("GT").warningThreshold("5").errorThreshold("50"));
- scanSampleWithDate(firstAnalysisDate);
+ WsQualityGates.CreateConditionWsResponse lowThresholds = tester.qGates().service()
+ .createCondition(CreateConditionRequest.builder().setQualityGateId(qGate.getId()).setMetricKey("lines").setOperator("GT").setWarning("5").setError("50").build());
+ scanSampleWithDate(project, firstAnalysisDate);
// with this configuration, project should have a Green alert
- qgClient.updateCondition(UpdateCondition.create(lowThresholds.id()).metricKey("lines").operator("GT").warningThreshold("5000").errorThreshold("5000"));
- scanSampleWithDate(secondAnalysisDate);
+ tester.qGates().service().updateCondition(UpdateConditionRequest.builder().setConditionId(lowThresholds.getId()).setMetricKey("lines").setOperator("GT").setWarning("5000").setError("5000").build());
+ scanSampleWithDate(project, secondAnalysisDate);
Navigation nav = Navigation.create(orchestrator);
- ProjectActivityPage page = nav.openProjectActivity("sample");
+ ProjectActivityPage page = nav.openProjectActivity(project.getKey());
page
.assertFirstAnalysisOfTheDayHasText(secondAnalysisDate, "Green (was Orange)")
.assertFirstAnalysisOfTheDayHasText(firstAnalysisDate, "Orange");
-
- qgClient.unsetDefault();
- qgClient.destroy(qGate.id());
}
@Test
.displayIntro();
}
- private void scanSampleWithDate(String date) {
- scanSample(date, null);
+ private void scanSampleWithDate(Project project, String date) {
+ scanSample(project, date, null);
}
- private void scanSample(@Nullable String date, @Nullable String profile) {
- SonarScanner scan = SonarScanner.create(projectDir("shared/xoo-sample"))
+ private void scanSample(Project project, @Nullable String date, @Nullable String profile) {
+ SonarScanner scan = SonarScanner.create(projectDir("shared/xoo-sample")).setProperty("sonar.projectKey", project.getKey())
.setProperty("sonar.cpd.exclusions", "**/*");
if (date != null) {
scan.setProperty("sonar.projectDate", date);
orchestrator.executeBuild(scan);
}
- private static QualityGateClient qgClient() {
- return orchestrator.getServer().adminWsClient().qualityGateClient();
- }
-
}