Search in sources :

Example 1 with QualityGate

use of org.sonar.wsclient.qualitygate.QualityGate in project sonarqube by SonarSource.

the class ProjectQualityGatePageTest method should_set_custom.

@Test
public void should_set_custom() {
    QualityGate customQualityGate = createCustomQualityGate("should_set_custom");
    ProjectQualityGatePage page = openPage();
    page.setQualityGate(customQualityGate.name());
    SelenideElement selectedQualityGate = page.getSelectedQualityGate();
    selectedQualityGate.should(Condition.hasText(customQualityGate.name()));
}
Also used : SelenideElement(com.codeborne.selenide.SelenideElement) ProjectQualityGatePage(pageobjects.ProjectQualityGatePage) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) Test(org.junit.Test)

Example 2 with QualityGate

use of org.sonar.wsclient.qualitygate.QualityGate in project sonarqube by SonarSource.

the class QualityGateUiTest method display_alerts_correctly_in_history_page.

/**
   * SONAR-3326
   */
@Test
public void display_alerts_correctly_in_history_page() {
    QualityGateClient qgClient = qgClient();
    QualityGate qGate = qgClient.create("AlertsForHistory");
    qgClient.setDefault(qGate.id());
    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);
    // 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);
    ProjectActivityPage page = Navigation.get(orchestrator).openProjectActivity("sample");
    page.assertFirstAnalysisOfTheDayHasText(secondAnalysisDate, "Green (was Orange)").assertFirstAnalysisOfTheDayHasText(firstAnalysisDate, "Orange");
    qgClient.unsetDefault();
    qgClient.destroy(qGate.id());
}
Also used : QualityGateClient(org.sonar.wsclient.qualitygate.QualityGateClient) ProjectActivityPage(pageobjects.ProjectActivityPage) QualityGateCondition(org.sonar.wsclient.qualitygate.QualityGateCondition) Date(java.util.Date) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) Test(org.junit.Test)

Example 3 with QualityGate

use of org.sonar.wsclient.qualitygate.QualityGate in project sonarqube by SonarSource.

the class QualityGateNotificationTest method status_on_metric_variation_and_send_notifications.

@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()));
    // Create user, who will receive notifications for new violations
    userRule.createUser("tester", "Tester", "tester@example.org", "tester");
    // Send test email to the test user
    newAdminWsClient(orchestrator).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").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"));
    SonarScanner analysis = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
    orchestrator.executeBuild(analysis);
    assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK");
    orchestrator.executeBuild(analysis);
    assertThat(getGateStatusMeasure().getValue()).isEqualTo("WARN");
    qgClient().unsetDefault();
    qgClient().destroy(simple.id());
    waitUntilAllNotificationsAreDelivered(smtpServer);
    Iterator<WiserMessage> emails = smtpServer.getMessages().iterator();
    MimeMessage message = emails.next().getMimeMessage();
    assertThat(message.getHeader("To", null)).isEqualTo("<test@example.org>");
    assertThat((String) message.getContent()).contains("This is a test message from SonarQube");
    assertThat(emails.hasNext()).isTrue();
    message = emails.next().getMimeMessage();
    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/index/sample");
    assertThat(emails.hasNext()).isFalse();
}
Also used : PostRequest(org.sonarqube.ws.client.PostRequest) MimeMessage(javax.mail.internet.MimeMessage) WiserMessage(org.subethamail.wiser.WiserMessage) WsClient(org.sonarqube.ws.client.WsClient) ItUtils.newUserWsClient(util.ItUtils.newUserWsClient) ItUtils.newAdminWsClient(util.ItUtils.newAdminWsClient) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 4 with QualityGate

use of org.sonar.wsclient.qualitygate.QualityGate in project sonarqube by SonarSource.

the class QualityGateTest method test_status_error.

@Test
public void test_status_error() throws IOException {
    QualityGate simple = qgClient().create("SimpleWithLowThreshold");
    qgClient().setDefault(simple.id());
    qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("10"));
    try {
        SonarScanner build = SonarScanner.create(projectDir("qualitygate/xoo-sample"));
        BuildResult buildResult = orchestrator.executeBuild(build);
        verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR);
        assertThat(getGateStatusMeasure().getValue()).isEqualTo("ERROR");
    } finally {
        qgClient().unsetDefault();
        qgClient().destroy(simple.id());
    }
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Example 5 with QualityGate

use of org.sonar.wsclient.qualitygate.QualityGate in project sonarqube by SonarSource.

the class QualityGateTest method ad_hoc_build_break_strategy.

@Test
public void ad_hoc_build_break_strategy() throws IOException {
    QualityGate simple = qgClient().create("SimpleWithLowThresholdForBuildBreakStrategy");
    qgClient().setDefault(simple.id());
    qgClient().createCondition(NewCondition.create(simple.id()).metricKey("ncloc").operator("GT").errorThreshold("7"));
    try {
        File projectDir = projectDir("qualitygate/xoo-sample");
        SonarScanner build = SonarScanner.create(projectDir);
        BuildResult buildResult = orchestrator.executeBuild(build);
        verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR);
        String taskId = getTaskIdInLocalReport(projectDir);
        String analysisId = getAnalysisId(taskId);
        ProjectStatusWsResponse projectStatusWsResponse = wsClient.qualityGates().projectStatus(new ProjectStatusWsRequest().setAnalysisId(analysisId));
        ProjectStatusWsResponse.ProjectStatus projectStatus = projectStatusWsResponse.getProjectStatus();
        assertThat(projectStatus.getStatus()).isEqualTo(ProjectStatusWsResponse.Status.ERROR);
        assertThat(projectStatus.getConditionsCount()).isEqualTo(1);
        ProjectStatusWsResponse.Condition condition = projectStatus.getConditionsList().get(0);
        assertThat(condition.getMetricKey()).isEqualTo("ncloc");
        assertThat(condition.getErrorThreshold()).isEqualTo("7");
    } finally {
        qgClient().unsetDefault();
        qgClient().destroy(simple.id());
    }
}
Also used : BuildResult(com.sonar.orchestrator.build.BuildResult) ProjectStatusWsResponse(org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse) ProjectStatusWsRequest(org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest) File(java.io.File) QualityGate(org.sonar.wsclient.qualitygate.QualityGate) SonarScanner(com.sonar.orchestrator.build.SonarScanner) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 QualityGate (org.sonar.wsclient.qualitygate.QualityGate)15 BuildResult (com.sonar.orchestrator.build.BuildResult)8 SonarScanner (com.sonar.orchestrator.build.SonarScanner)8 ProjectQualityGatePage (pageobjects.ProjectQualityGatePage)5 SelenideElement (com.codeborne.selenide.SelenideElement)4 File (java.io.File)1 Date (java.util.Date)1 MimeMessage (javax.mail.internet.MimeMessage)1 Ignore (org.junit.Ignore)1 QualityGateClient (org.sonar.wsclient.qualitygate.QualityGateClient)1 QualityGateCondition (org.sonar.wsclient.qualitygate.QualityGateCondition)1 Measure (org.sonarqube.ws.WsMeasures.Measure)1 ProjectStatusWsResponse (org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse)1 PostRequest (org.sonarqube.ws.client.PostRequest)1 WsClient (org.sonarqube.ws.client.WsClient)1 ProjectStatusWsRequest (org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest)1 WiserMessage (org.subethamail.wiser.WiserMessage)1 ProjectActivityPage (pageobjects.ProjectActivityPage)1 ItUtils.getMeasure (util.ItUtils.getMeasure)1