]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7973 SONAR-8055 Add IT 1200/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 1 Sep 2016 17:05:35 +0000 (19:05 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 2 Sep 2016 09:46:28 +0000 (11:46 +0200)
it/it-tests/src/test/java/it/Category1Suite.java
it/it-tests/src/test/java/it/settings/EmailsTest.java [new file with mode: 0644]
it/it-tests/src/test/java/util/ItUtils.java

index 8f1a4cbe3a01465f6af4c9aba4dc41edca7cf368..1cdca248bee8adbd533b5bd642fa0fb3f5f37ae1 100644 (file)
@@ -31,8 +31,8 @@ import it.authorisation.SystemAdminPermissionTest;
 import it.customMeasure.CustomMeasuresTest;
 import it.i18n.I18nTest;
 import it.measure.MeasuresWsTest;
-import it.measure.ProjectMeasuresPageTest;
 import it.measure.ProjectDashboardTest;
+import it.measure.ProjectMeasuresPageTest;
 import it.measure.ProjectWidgetsTest;
 import it.measureFilter.MeasureFiltersTest;
 import it.measureHistory.DifferentialPeriodsTest;
@@ -47,6 +47,7 @@ import it.projectAdministration.ProjectLinksPageTest;
 import it.qualityGate.QualityGateNotificationTest;
 import it.qualityGate.QualityGateTest;
 import it.qualityGate.QualityGateUiTest;
+import it.settings.EmailsTest;
 import it.settings.PropertySetsTest;
 import it.settings.SettingsTest;
 import it.sourceCode.EncodingTest;
@@ -69,6 +70,7 @@ import static util.ItUtils.xooPlugin;
   ProjectLinksPageTest.class,
   BackgroundTasksTest.class,
   // settings
+  EmailsTest.class,
   PropertySetsTest.class,
   SettingsTest.class,
   // i18n
diff --git a/it/it-tests/src/test/java/it/settings/EmailsTest.java b/it/it-tests/src/test/java/it/settings/EmailsTest.java
new file mode 100644 (file)
index 0000000..ecfe91b
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package it.settings;
+
+import com.sonar.orchestrator.Orchestrator;
+import it.Category1Suite;
+import java.util.Iterator;
+import javax.annotation.Nullable;
+import javax.mail.internet.MimeMessage;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonarqube.ws.Settings;
+import org.sonarqube.ws.client.PostRequest;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.setting.SettingsService;
+import org.sonarqube.ws.client.setting.ValuesRequest;
+import org.subethamail.wiser.Wiser;
+import org.subethamail.wiser.WiserMessage;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
+import static util.ItUtils.newAdminWsClient;
+import static util.ItUtils.resetSettings;
+
+public class EmailsTest {
+
+  @ClassRule
+  public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+
+  static Wiser SMTP_SERVER;
+  static WsClient ADMIN_WS_CLIENT;
+  static SettingsService SETTINGS;
+
+  @BeforeClass
+  public static void before() throws Exception {
+    ADMIN_WS_CLIENT = newAdminWsClient(orchestrator);
+    SETTINGS = ADMIN_WS_CLIENT.settingsService();
+
+    SMTP_SERVER = new Wiser(0);
+    SMTP_SERVER.start();
+    System.out.println("SMTP Server port: " + SMTP_SERVER.getServer().getPort());
+  }
+
+  @AfterClass
+  public static void stop() {
+    if (SMTP_SERVER != null) {
+      SMTP_SERVER.stop();
+    }
+    resetSettings(orchestrator, null, "email.smtp_host.secured", "email.smtp_port.secured", "email.smtp_secure_connection.secured", "email.smtp_username.secured",
+      "email.smtp_password.secured", "email.from", "email.prefix");
+  }
+
+  @Before
+  public void prepare() {
+    orchestrator.resetData();
+    SMTP_SERVER.getMessages().clear();
+  }
+
+  @Test
+  public void update_email_settings() throws Exception {
+    updateEmailSettings("localhost", "42", "noreply@email.com", "[EMAIL]", "ssl", "john", "123456");
+
+    Settings.ValuesWsResponse response = SETTINGS.values(ValuesRequest.builder()
+      .setKeys("email.smtp_host.secured", "email.smtp_port.secured", "email.smtp_secure_connection.secured", "email.smtp_username.secured", "email.smtp_password.secured",
+        "email.from", "email.prefix")
+      .build());
+
+    assertThat(response.getSettingsList()).extracting(Settings.Setting::getKey, Settings.Setting::getValue)
+      .containsOnly(
+        tuple("email.smtp_host.secured", "localhost"),
+        tuple("email.smtp_port.secured", "42"),
+        tuple("email.smtp_secure_connection.secured", "ssl"),
+        tuple("email.smtp_username.secured", "john"),
+        tuple("email.smtp_password.secured", "123456"),
+        tuple("email.from", "noreply@email.com"),
+        tuple("email.prefix", "[EMAIL]"));
+  }
+
+  @Test
+  public void send_test_email() throws Exception {
+    updateEmailSettings("localhost", Integer.toString(SMTP_SERVER.getServer().getPort()), null, null, null, null, null);
+
+    sendEmail("test@example.org", "Test Message from SonarQube", "This is a test message from SonarQube");
+
+    // We need to wait until all notifications will be delivered
+    waitUntilAllNotificationsAreDelivered();
+    Iterator<WiserMessage> emails = SMTP_SERVER.getMessages().iterator();
+    MimeMessage message = emails.next().getMimeMessage();
+    assertThat(message.getHeader("To", null)).isEqualTo("<test@example.org>");
+    assertThat(message.getSubject()).contains("Test Message from SonarQube");
+    assertThat((String) message.getContent()).contains("This is a test message from SonarQube");
+    assertThat(emails.hasNext()).isFalse();
+  }
+
+  private static void waitUntilAllNotificationsAreDelivered() throws InterruptedException {
+    Thread.sleep(10000);
+  }
+
+  private static void updateEmailSettings(@Nullable String host, @Nullable String port, @Nullable String from, @Nullable String prefix, @Nullable String secure,
+    @Nullable String username, @Nullable String password) {
+    ADMIN_WS_CLIENT.wsConnector().call(
+      new PostRequest("/api/emails/update_configuration")
+        .setParam("host", host)
+        .setParam("port", port)
+        .setParam("from", from)
+        .setParam("prefix", prefix)
+        .setParam("secure", secure)
+        .setParam("username", username)
+        .setParam("password", password))
+      .failIfNotSuccessful();
+  }
+
+  private static void sendEmail(String to, String subject, String message) {
+    ADMIN_WS_CLIENT.wsConnector().call(
+      new PostRequest("/api/emails/send")
+        .setParam("to", to)
+        .setParam("subject", subject)
+        .setParam("message", message))
+      .failIfNotSuccessful();
+  }
+
+}
index efa73ca63c69bf54c7636dd0250958d2e5a95369..6db0320f6b1e981694b697c2efd5b426b394ee29 100644 (file)
@@ -224,6 +224,12 @@ public class ItUtils {
     }
   }
 
+  public static void resetSettings(Orchestrator orchestrator, @Nullable String componentKey, String... keys) {
+    for (String key : keys) {
+      setServerProperty(orchestrator, componentKey, key, null);
+    }
+  }
+
   public static void resetPeriods(Orchestrator orchestrator) {
     setServerProperty(orchestrator, "sonar.timemachine.period1", null);
     setServerProperty(orchestrator, "sonar.timemachine.period2", null);