forked from SonarSource/sonarqube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Brandhof
committed
Jun 29, 2015
1 parent
81abdbf
commit 54159d5
Showing
7 changed files
with
248 additions
and
26 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
it/tests/src/test/java/qualitygate/QualityGateNotificationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* 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 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() | ||
.addPlugin(ItUtils.xooPlugin()) | ||
|
||
// 1 second. Required for notification test. | ||
.setServerProperty("sonar.notifications.delay", "1") | ||
|
||
.build(); | ||
|
||
@Before | ||
public void cleanUp() throws Exception { | ||
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", | ||
"/selenium/qualitygate/notifications/email_configuration.html", | ||
"/selenium/qualitygate/notifications/activate_notification_channels.html").build(); | ||
orchestrator.executeSelenese(selenese); | ||
|
||
// 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...src/test/resources/selenium/qualitygate/notifications/activate_notification_channels.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | ||
<head profile="http://selenium-ide.openqa.org/profiles/test-case"> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | ||
<title>create_user_with_email</title> | ||
</head> | ||
<body> | ||
<table cellpadding="1" cellspacing="1" border="1"> | ||
<thead> | ||
<tr> | ||
<td rowspan="1" colspan="3">create_user_with_email</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>open</td> | ||
<td>/sonar/sessions/new</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>type</td> | ||
<td>login</td> | ||
<td>tester</td> | ||
</tr> | ||
<tr> | ||
<td>type</td> | ||
<td>password</td> | ||
<td>tester</td> | ||
</tr> | ||
<tr> | ||
<td>clickAndWait</td> | ||
<td>commit</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>open</td> | ||
<td>/sonar/account/index</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>check</td> | ||
<td>global_notifs_NewAlerts.EmailNotificationChannel</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>clickAndWait</td> | ||
<td>//input[@value='Save changes']</td> | ||
<td></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</body> | ||
</html> |
64 changes: 64 additions & 0 deletions
64
it/tests/src/test/resources/selenium/qualitygate/notifications/email_configuration.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | ||
<head profile="http://selenium-ide.openqa.org/profiles/test-case"> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | ||
<title>email_configuration</title> | ||
</head> | ||
<body> | ||
<table cellpadding="1" cellspacing="1" border="1"> | ||
<tbody> | ||
<tr> | ||
<td>setTimeout</td> | ||
<td>300000</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>open</td> | ||
<td>/sonar/sessions/new</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>type</td> | ||
<td>login</td> | ||
<td>admin</td> | ||
</tr> | ||
<tr> | ||
<td>type</td> | ||
<td>password</td> | ||
<td>admin</td> | ||
</tr> | ||
<tr> | ||
<td>clickAndWait</td> | ||
<td>commit</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>open</td> | ||
<td>/sonar/email_configuration</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>verifyValue</td> | ||
<td>smtp_host</td> | ||
<td>localhost</td> | ||
</tr> | ||
<tr> | ||
<td>type</td> | ||
<td>to_address</td> | ||
<td>test@example.org</td> | ||
</tr> | ||
<tr> | ||
<td>clickAndWait</td> | ||
<td>submit_test</td> | ||
<td></td> | ||
</tr> | ||
<tr> | ||
<td>waitForVisible</td> | ||
<td>info</td> | ||
<td></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters