From f6d670f1aad505c149d9f2e3459f2c74edc77c9e Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 1 Sep 2016 19:05:35 +0200 Subject: [PATCH] SONAR-7973 SONAR-8055 Add IT --- .../src/test/java/it/Category1Suite.java | 4 +- .../src/test/java/it/settings/EmailsTest.java | 142 ++++++++++++++++++ it/it-tests/src/test/java/util/ItUtils.java | 6 + 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 it/it-tests/src/test/java/it/settings/EmailsTest.java diff --git a/it/it-tests/src/test/java/it/Category1Suite.java b/it/it-tests/src/test/java/it/Category1Suite.java index 8f1a4cbe3a0..1cdca248bee 100644 --- a/it/it-tests/src/test/java/it/Category1Suite.java +++ b/it/it-tests/src/test/java/it/Category1Suite.java @@ -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 index 00000000000..ecfe91b0e32 --- /dev/null +++ b/it/it-tests/src/test/java/it/settings/EmailsTest.java @@ -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 emails = SMTP_SERVER.getMessages().iterator(); + MimeMessage message = emails.next().getMimeMessage(); + assertThat(message.getHeader("To", null)).isEqualTo(""); + 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(); + } + +} diff --git a/it/it-tests/src/test/java/util/ItUtils.java b/it/it-tests/src/test/java/util/ItUtils.java index efa73ca63c6..6db0320f6b1 100644 --- a/it/it-tests/src/test/java/util/ItUtils.java +++ b/it/it-tests/src/test/java/util/ItUtils.java @@ -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); -- 2.39.5