@@ -35,4 +35,6 @@ public interface Session { | |||
UserTester users(); | |||
SettingTester settings(); | |||
} |
@@ -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()); | |||
} | |||
} | |||
} |
@@ -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); | |||
} | |||
} | |||
} |
@@ -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) |
@@ -240,10 +240,18 @@ public class ItUtils { | |||
return scan; | |||
} | |||
/** | |||
* @deprecated replaced by {@link org.sonarqube.tests.SettingTester#setGlobalSetting(String, String)} | |||
*/ | |||
@Deprecated | |||
public static void setServerProperty(Orchestrator orchestrator, String key, @Nullable String value) { | |||
setServerProperty(orchestrator, null, key, value); | |||
} | |||
/** | |||
* @deprecated replaced by {@link org.sonarqube.tests.SettingTester#setProjectSetting(String, String, String)} | |||
*/ | |||
@Deprecated | |||
public static void setServerProperty(Orchestrator orchestrator, @Nullable String componentKey, String key, @Nullable String value) { | |||
if (value == null) { | |||
newAdminWsClient(orchestrator).settings().reset(ResetRequest.builder().setKeys(key).setComponent(componentKey).build()); | |||
@@ -252,23 +260,39 @@ public class ItUtils { | |||
} | |||
} | |||
/** | |||
* @deprecated replaced by {@link org.sonarqube.tests.SettingTester#setGlobalSetting(String, String)} or {@link org.sonarqube.tests.SettingTester#setProjectSettings(String, String...)} | |||
*/ | |||
@Deprecated | |||
public static void setServerProperties(Orchestrator orchestrator, @Nullable String componentKey, String... properties) { | |||
for (int i = 0; i < properties.length; i += 2) { | |||
setServerProperty(orchestrator, componentKey, properties[i], properties[i + 1]); | |||
} | |||
} | |||
/** | |||
* @deprecated replaced by {@link org.sonarqube.tests.SettingTester#resetSettings(String...)} | |||
*/ | |||
@Deprecated | |||
public static void resetSettings(Orchestrator orchestrator, @Nullable String componentKey, String... keys) { | |||
if (keys.length > 0) { | |||
newAdminWsClient(orchestrator).settings().reset(ResetRequest.builder().setKeys(keys).setComponent(componentKey).build()); | |||
} | |||
} | |||
/** | |||
* @deprecated no more needed as already done by n by {@link Tester#after()} | |||
*/ | |||
@Deprecated | |||
public static void resetEmailSettings(Orchestrator orchestrator) { | |||
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"); | |||
} | |||
/** | |||
* @deprecated no more needed as already done by n by {@link Tester#after()} | |||
*/ | |||
@Deprecated | |||
public static void resetPeriod(Orchestrator orchestrator) { | |||
resetSettings(orchestrator, null, "sonar.leak.period"); | |||
} |