aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/qualitygate/QualityGateNotificationTest.java
blob: 9547f29be5187f808953980ebbf94a28f6953b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2009-2014 SonarSource SA
 * All rights reserved
 * mailto:contact AT sonarsource DOT com
 */
package qualitygate;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarRunner;
import com.sonar.orchestrator.selenium.Selenese;
import com.sonar.orchestrator.util.NetworkUtils;
import java.util.Iterator;
import javax.mail.internet.MimeMessage;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.qualitygate.NewCondition;
import org.sonar.wsclient.qualitygate.QualityGate;
import org.sonar.wsclient.qualitygate.QualityGateClient;
import org.sonar.wsclient.services.Measure;
import org.sonar.wsclient.services.PropertyUpdateQuery;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
import selenium.SeleneseTest;
import util.ItUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.projectDir;

public class QualityGateNotificationTest {

  private static final String PROJECT_KEY = "sample";

  @ClassRule
  public static Orchestrator orchestrator = Orchestrator.builderEnv()
    .setSonarVersion("DEV")
    .addPlugin(ItUtils.xooPlugin())

    // 1 second. Required for notification test.
    .setServerProperty("sonar.notifications.delay", "1")

    .build();

  @Before
  public void cleanUp() {
    orchestrator.resetData();
  }

  @Test
  public void status_on_metric_variation_and_send_notifications() throws Exception {
    Wiser smtpServer = new Wiser(NetworkUtils.getNextAvailablePort());
    try {
      // Configure SMTP
      smtpServer.start();
      Sonar wsClient = orchestrator.getServer().getAdminWsClient();
      wsClient.update(new PropertyUpdateQuery("email.smtp_host.secured", "localhost"));
      wsClient.update(new PropertyUpdateQuery("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort())));

      // Create user, who will receive notifications for new violations
      orchestrator.getServer().adminWsClient().post("api/users/create", "login", "tester", "name", "Tester", "email", "tester@example.org", "password", "tester");
      Selenese selenese = Selenese
        .builder()
        .setHtmlTestsInClasspath("notifications",
          "/qualitygate/notifications/email_configuration.html",
          "/qualitygate/notifications/activate_notification_channels.html").build();
      new SeleneseTest(selenese).runOn(orchestrator);

      // 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"));

      SonarRunner analysis = SonarRunner.create(projectDir("qualitygate/xoo-sample"));
      orchestrator.executeBuild(analysis);
      assertThat(fetchGateStatus().getData()).isEqualTo("OK");

      orchestrator.executeBuild(analysis);
      assertThat(fetchGateStatus().getData()).isEqualTo("WARN");

      qgClient().unsetDefault();
      qgClient().destroy(simple.id());

      // Wait until all notifications are delivered
      Thread.sleep(10000);

      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 Sonar");

      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();

    } finally {
      smtpServer.stop();
    }
  }

  private Measure fetchGateStatus() {
    return fetchResourceWithGateStatus().getMeasure("alert_status");
  }

  private Resource fetchResourceWithGateStatus() {
    return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, "alert_status").setIncludeAlerts(true));
  }

  private static QualityGateClient qgClient() {
    return orchestrator.getServer().adminWsClient().qualityGateClient();
  }
}