diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-09-05 08:23:05 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-09-05 09:59:34 +0200 |
commit | 01aadce427df99146be5cb53717ce0ff425eec44 (patch) | |
tree | ebaba6b04116b26b2a8dbc4fa67ec09f54efbb04 /tests/src/test/java/org | |
parent | 92e63318a312ad5ac0c24056b2b9e74c564f0029 (diff) | |
download | sonarqube-01aadce427df99146be5cb53717ce0ff425eec44.tar.gz sonarqube-01aadce427df99146be5cb53717ce0ff425eec44.zip |
Add SettingTester to clean settings between each IT
Diffstat (limited to 'tests/src/test/java/org')
4 files changed, 136 insertions, 29 deletions
diff --git a/tests/src/test/java/org/sonarqube/tests/Session.java b/tests/src/test/java/org/sonarqube/tests/Session.java index 6889f873b7e..ce225da4129 100644 --- a/tests/src/test/java/org/sonarqube/tests/Session.java +++ b/tests/src/test/java/org/sonarqube/tests/Session.java @@ -35,4 +35,6 @@ public interface Session { UserTester users(); + SettingTester settings(); + } diff --git a/tests/src/test/java/org/sonarqube/tests/SettingTester.java b/tests/src/test/java/org/sonarqube/tests/SettingTester.java new file mode 100644 index 00000000000..b05dc16dac4 --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/SettingTester.java @@ -0,0 +1,94 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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 org.sonarqube.tests; + +import com.google.common.collect.ImmutableSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.annotation.Nullable; +import org.sonarqube.ws.Settings; +import org.sonarqube.ws.client.setting.ListDefinitionsRequest; +import org.sonarqube.ws.client.setting.ResetRequest; +import org.sonarqube.ws.client.setting.SetRequest; +import org.sonarqube.ws.client.setting.SettingsService; + +import static org.sonarqube.ws.Settings.Type.LICENSE; + +public class SettingTester { + + private static final Set<String> EMAIL_SETTINGS = ImmutableSet.of("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"); + + private final Session session; + + SettingTester(Session session) { + this.session = session; + } + + public SettingsService service() { + return session.wsClient().settings(); + } + + void deleteAll() { + List<String> settingKeys = Stream.concat( + session.wsClient().settings().listDefinitions(ListDefinitionsRequest.builder().build()).getDefinitionsList() + .stream() + .filter(def -> def.getType() != LICENSE) + .map(Settings.Definition::getKey), + EMAIL_SETTINGS.stream()) + .collect(Collectors.toList()); + session.wsClient().settings().reset(ResetRequest.builder().setKeys(settingKeys).build()); + } + + public void resetSettings(String... keys){ + session.wsClient().settings().reset(ResetRequest.builder().setKeys(keys).build()); + } + + public void setGlobalSetting(String key, @Nullable String value) { + setSetting(null, key, value); + } + + public void setGlobalSettings(String... properties) { + for (int i = 0; i < properties.length; i += 2) { + setSetting(null, properties[i], properties[i + 1]); + } + } + + public void setProjectSetting(String componentKey, String key, @Nullable String value) { + setSetting(componentKey, key, value); + } + + public void setProjectSettings(String componentKey, String... properties) { + for (int i = 0; i < properties.length; i += 2) { + setSetting(componentKey, properties[i], properties[i + 1]); + } + } + + private void setSetting(@Nullable String componentKey, String key, @Nullable String value) { + if (value == null) { + session.wsClient().settings().reset(ResetRequest.builder().setKeys(key).setComponent(componentKey).build()); + } else { + session.wsClient().settings().set(SetRequest.builder().setKey(key).setValue(value).setComponent(componentKey).build()); + } + } + +} diff --git a/tests/src/test/java/org/sonarqube/tests/Tester.java b/tests/src/test/java/org/sonarqube/tests/Tester.java index 7ab41b836d5..8041625cd45 100644 --- a/tests/src/test/java/org/sonarqube/tests/Tester.java +++ b/tests/src/test/java/org/sonarqube/tests/Tester.java @@ -97,6 +97,7 @@ public class Tester extends ExternalResource implements Session { } users().deleteAll(); projects().deleteAll(); + settings().deleteAll(); } public Session asAnonymous() { @@ -179,6 +180,11 @@ public class Tester extends ExternalResource implements Session { return rootSession.users(); } + @Override + public SettingTester settings() { + return rootSession.settings(); + } + private static class SessionImpl implements Session { private final WsClient client; @@ -215,5 +221,10 @@ public class Tester extends ExternalResource implements Session { public UserTester users() { return new UserTester(this); } + + @Override + public SettingTester settings() { + return new SettingTester(this); + } } } diff --git a/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java b/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java index 86967abb6c2..50b895fbae2 100644 --- a/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java +++ b/tests/src/test/java/org/sonarqube/tests/settings/EmailsTest.java @@ -20,7 +20,6 @@ package org.sonarqube.tests.settings; import com.sonar.orchestrator.Orchestrator; -import org.sonarqube.tests.Category1Suite; import java.util.Iterator; import javax.annotation.Nullable; import javax.mail.internet.MimeMessage; @@ -28,35 +27,32 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; +import org.sonarqube.tests.Category1Suite; +import org.sonarqube.tests.Tester; 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 junit.framework.TestCase.fail; 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.resetEmailSettings; -import static util.ItUtils.setServerProperty; public class EmailsTest { @ClassRule public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR; - static Wiser SMTP_SERVER; - static WsClient ADMIN_WS_CLIENT; - static SettingsService SETTINGS; + @Rule + public Tester tester = new Tester(orchestrator).disableOrganizations(); + + private static Wiser SMTP_SERVER; @BeforeClass public static void before() throws Exception { - ADMIN_WS_CLIENT = newAdminWsClient(orchestrator); - SETTINGS = ADMIN_WS_CLIENT.settings(); - SMTP_SERVER = new Wiser(0); SMTP_SERVER.start(); System.out.println("SMTP Server port: " + SMTP_SERVER.getServer().getPort()); @@ -67,21 +63,18 @@ public class EmailsTest { if (SMTP_SERVER != null) { SMTP_SERVER.stop(); } - resetEmailSettings(orchestrator); } @Before public void prepare() { - orchestrator.resetData(); SMTP_SERVER.getMessages().clear(); - resetEmailSettings(orchestrator); } @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() + Settings.ValuesWsResponse response = tester.settings().service().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()); @@ -104,7 +97,7 @@ public class EmailsTest { 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(); + waitUntilAllNotificationsAreDelivered(1); Iterator<WiserMessage> emails = SMTP_SERVER.getMessages().iterator(); MimeMessage message = emails.next().getMimeMessage(); assertThat(message.getHeader("To", null)).isEqualTo("<test@example.org>"); @@ -113,23 +106,30 @@ public class EmailsTest { assertThat(emails.hasNext()).isFalse(); } - private static void waitUntilAllNotificationsAreDelivered() throws InterruptedException { - Thread.sleep(10000); + private static void waitUntilAllNotificationsAreDelivered(int expectedNumberOfEmails) throws InterruptedException { + for (int i = 0; i < 10; i++) { + if (SMTP_SERVER.getMessages().size() == expectedNumberOfEmails) { + return; + } + Thread.sleep(1_000); + } + fail(String.format("Received %d emails, expected %d", SMTP_SERVER.getMessages().size(), expectedNumberOfEmails)); } - private static void updateEmailSettings(@Nullable String host, @Nullable String port, @Nullable String from, @Nullable String prefix, @Nullable String secure, + private void updateEmailSettings(@Nullable String host, @Nullable String port, @Nullable String from, @Nullable String prefix, @Nullable String secure, @Nullable String username, @Nullable String password) { - setServerProperty(orchestrator, "email.smtp_host.secured", host); - setServerProperty(orchestrator, "email.smtp_port.secured", port); - setServerProperty(orchestrator, "email.smtp_secure_connection.secured", secure); - setServerProperty(orchestrator, "email.smtp_username.secured", username); - setServerProperty(orchestrator, "email.smtp_password.secured", password); - setServerProperty(orchestrator, "email.from", from); - setServerProperty(orchestrator, "email.prefix", prefix); + tester.settings().setGlobalSettings( + "email.smtp_host.secured", host, + "email.smtp_port.secured", port, + "email.smtp_secure_connection.secured", secure, + "email.smtp_username.secured", username, + "email.smtp_password.secured", password, + "email.from", from, + "email.prefix", prefix); } - private static void sendEmail(String to, String subject, String message) { - ADMIN_WS_CLIENT.wsConnector().call( + private void sendEmail(String to, String subject, String message) { + tester.wsClient().wsConnector().call( new PostRequest("/api/emails/send") .setParam("to", to) .setParam("subject", subject) |