import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.wsclient.Sonar;
-import org.sonar.wsclient.services.PropertyDeleteQuery;
-import org.sonar.wsclient.services.PropertyUpdateQuery;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;
import util.ItUtils;
import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.resetSettings;
+import static util.ItUtils.setServerProperty;
public class ScannerTest {
// SONAR-4680
@Test
public void module_should_load_own_settings_from_database() {
- orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "Sonar :: Integration Tests :: Multi-modules Sample");
+ String rootModuleKey = "com.sonarsource.it.samples:multi-modules-sample";
+ orchestrator.getServer().provisionProject(rootModuleKey, "Sonar :: Integration Tests :: Multi-modules Sample");
- Sonar sonar = orchestrator.getServer().getAdminWsClient();
String propKey = "myFakeProperty";
- String rootModuleKey = "com.sonarsource.it.samples:multi-modules-sample";
String moduleBKey = rootModuleKey + ":module_b";
- sonar.delete(new PropertyDeleteQuery(propKey, rootModuleKey));
- sonar.delete(new PropertyDeleteQuery(propKey, moduleBKey));
+ resetSettings(orchestrator, rootModuleKey, propKey);
BuildResult result = scan("shared/xoo-multi-modules-sample", "sonar.showSettings", propKey);
assertThat(result.getLogs()).doesNotContain(moduleBKey + ":" + propKey);
// Set property only on root project
- sonar.update(new PropertyUpdateQuery(propKey, "project", rootModuleKey));
+ setServerProperty(orchestrator, rootModuleKey, propKey, "project");
result = scan("shared/xoo-multi-modules-sample", "sonar.showSettings", propKey);
assertThat(result.getLogs()).contains(moduleBKey + ":" + propKey + " = project");
// Override property on moduleB
- sonar.update(new PropertyUpdateQuery(propKey, "moduleB", moduleBKey));
+ setServerProperty(orchestrator, moduleBKey, propKey, "moduleB");
result = scan("shared/xoo-multi-modules-sample", "sonar.showSettings", propKey);
// SONAR-4680
@Test
public void module_should_load_settings_from_parent() {
- orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "Sonar :: Integration Tests :: Multi-modules Sample");
+ String rootModuleKey = "com.sonarsource.it.samples:multi-modules-sample";
+ orchestrator.getServer().provisionProject(rootModuleKey, "Sonar :: Integration Tests :: Multi-modules Sample");
- Sonar sonar = orchestrator.getServer().getAdminWsClient();
String propKey = "myFakeProperty";
- String rootModuleKey = "com.sonarsource.it.samples:multi-modules-sample";
String moduleBKey = rootModuleKey + ":module_b";
- // Set property on provisionned project
- sonar.update(new PropertyUpdateQuery(propKey, "project", rootModuleKey));
- sonar.delete(new PropertyDeleteQuery(propKey, moduleBKey));
+ // Set property on provisioned project
+ setServerProperty(orchestrator, rootModuleKey, propKey, "project");
BuildResult result = scan("shared/xoo-multi-modules-sample", "sonar.showSettings", propKey);
@Test
public void should_create_project_without_name_version() {
- //some of the sub-modules have a name defined, others don't
+ // some of the sub-modules have a name defined, others don't
BuildResult buildResult = scan("shared/xoo-multi-module-sample-without-project-name-version");
assertThat(buildResult.isSuccess()).isTrue();
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample", "com.sonarsource.it.samples:multi-modules-sample", "not provided");
-
+
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b", "module_b", "not provided");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1", "module_b1", "not provided");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b2", "Sub-module B2", "not provided");
-
+
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a", "Module A", "not provided");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1", "Sub-module A1", "not provided");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a2", "Sub-module A2", "not provided");
}
-
+
@Test
public void should_analyze_project_without_name_version() {
orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "My project name");
"sonar.projectName", "My project name",
"sonar.projectVersion", "1.0");
assertThat(buildResult.isSuccess()).isTrue();
-
+
buildResult = scan("shared/xoo-multi-module-sample-without-project-name-version");
assertThat(buildResult.isSuccess()).isTrue();
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample", "My project name", "1.0");
-
+
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b", "module_b", "1.0");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b1", "module_b1", "1.0");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_b:module_b2", "Sub-module B2", "1.0");
-
+
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a", "Module A", "1.0");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1", "Sub-module A1", "1.0");
assertNameAndVersion("com.sonarsource.it.samples:multi-modules-sample:module_a:module_a2", "Sub-module A2", "1.0");
}
-
+
private void assertNameAndVersion(String projectKey, String expectedProjectName, String expectedProjectVersion) {
// new WS Client with api/components doesn't return the project version, so use the old one
Resource resource = orchestrator.getServer().getAdminWsClient().find(new ResourceQuery(projectKey));
assertThat(result.getLogs()).contains("/dashboard/index/com.sonarsource.it.samples:multi-modules-sample:mybranch");
try {
- orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery("sonar.core.serverBaseURL", "http://foo:123/sonar"));
+ setServerProperty(orchestrator, null, "sonar.core.serverBaseURL", "http://foo:123/sonar");
result = scan("shared/xoo-multi-modules-sample");
assertThat(result.getLogs()).contains("http://foo:123/sonar/dashboard/index/com.sonarsource.it.samples:multi-modules-sample");
} finally {
- orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery("sonar.core.serverBaseURL", null));
+ resetSettings(orchestrator, null, "sonar.core.serverBaseURL");
}
}
import org.junit.BeforeClass;
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.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.setting.SettingsService;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
-import util.ItUtils;
import util.user.UserRule;
import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.newUserWsClient;
import static util.ItUtils.projectDir;
import static util.ItUtils.resetEmailSettings;
+import static util.ItUtils.resetPeriods;
import static util.ItUtils.setServerProperty;
public class QualityGateNotificationTest {
@ClassRule
public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+
@ClassRule
public static UserRule userRule = UserRule.from(orchestrator);
+ private static Wiser smtpServer;
+
+ private static SettingsService adminSettingsService;
+
@BeforeClass
- public static void initPeriods() throws Exception {
+ public static void init() throws Exception {
+ adminSettingsService = newAdminWsClient(orchestrator).settingsService();
+
+ DEFAULT_QUALITY_GATE = qgClient().list().defaultGate().id();
+
setServerProperty(orchestrator, "sonar.timemachine.period1", "previous_analysis");
setServerProperty(orchestrator, "sonar.timemachine.period2", "30");
setServerProperty(orchestrator, "sonar.timemachine.period3", "previous_version");
- DEFAULT_QUALITY_GATE = qgClient().list().defaultGate().id();
resetEmailSettings(orchestrator);
+
+ smtpServer = new Wiser(0);
+ smtpServer.start();
}
@AfterClass
public static void resetData() throws Exception {
- ItUtils.resetPeriods(orchestrator);
qgClient().setDefault(DEFAULT_QUALITY_GATE);
+
+ resetPeriods(orchestrator);
resetEmailSettings(orchestrator);
+
+ if (smtpServer != null) {
+ smtpServer.stop();
+ }
}
@Before
@Test
public void status_on_metric_variation_and_send_notifications() throws Exception {
- Wiser smtpServer = new Wiser(0);
- try {
- // Configure SMTP
- smtpServer.start();
- Sonar oldWsClient = orchestrator.getServer().getAdminWsClient();
- oldWsClient.update(new PropertyUpdateQuery("email.smtp_host.secured", "localhost"));
- oldWsClient.update(new PropertyUpdateQuery("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort())));
-
- // Create user, who will receive notifications for new violations
- userRule.createUser("tester", "Tester", "tester@example.org", "tester");
- // Send test email to the test user
- newAdminWsClient(orchestrator).wsConnector().call(new PostRequest("api/emails/send")
- .setParam("to", "test@example.org")
- .setParam("message", "This is a test message from SonarQube"))
- .failIfNotSuccessful();
- // Add notifications to the test user
- WsClient wsClient = newUserWsClient(orchestrator, "tester", "tester");
- wsClient.wsConnector().call(new PostRequest("api/notifications/add")
- .setParam("type", "NewAlerts")
- .setParam("channel", "EmailNotificationChannel"))
- .failIfNotSuccessful();
-
- // 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"));
-
- SonarScanner analysis = SonarScanner.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());
-
- waitUntilAllNotificationsAreDelivered(smtpServer);
-
- 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 SonarQube");
-
- 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();
- }
+ setServerProperty(orchestrator, "email.smtp_host.secured", "localhost");
+ setServerProperty(orchestrator, "email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort()));
+
+ // Create user, who will receive notifications for new violations
+ userRule.createUser("tester", "Tester", "tester@example.org", "tester");
+ // Send test email to the test user
+ newAdminWsClient(orchestrator).wsConnector().call(new PostRequest("api/emails/send")
+ .setParam("to", "test@example.org")
+ .setParam("message", "This is a test message from SonarQube"))
+ .failIfNotSuccessful();
+ // Add notifications to the test user
+ WsClient wsClient = newUserWsClient(orchestrator, "tester", "tester");
+ wsClient.wsConnector().call(new PostRequest("api/notifications/add")
+ .setParam("type", "NewAlerts")
+ .setParam("channel", "EmailNotificationChannel"))
+ .failIfNotSuccessful();
+
+ // 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"));
+
+ SonarScanner analysis = SonarScanner.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());
+
+ waitUntilAllNotificationsAreDelivered(smtpServer);
+
+ 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 SonarQube");
+
+ 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();
}
private Measure fetchGateStatus() {
}
public static void resetSettings(Orchestrator orchestrator, @Nullable String componentKey, String... keys) {
- newAdminWsClient(orchestrator).settingsService().reset(ResetRequest.builder().setKeys(keys).setComponent(componentKey).build());
+ if (keys.length > 0) {
+ newAdminWsClient(orchestrator).settingsService().reset(ResetRequest.builder().setKeys(keys).setComponent(componentKey).build());
+ }
}
public static void resetEmailSettings(Orchestrator orchestrator) {
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.junit.rules.ExternalResource;
-import org.sonar.wsclient.services.PropertyDeleteQuery;
-import org.sonar.wsclient.services.PropertyUpdateQuery;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;
+import static util.ItUtils.resetSettings;
/**
* Rule wrapping around an {@link Orchestrator} instance which handle:
}
private void resetServerProperties() {
- for (String serverProperty : serverProperties) {
- setServerPropertyImpl(serverProperty, null);
- }
+ resetSettings(orchestrator, null, serverProperties.toArray(new String[] {}));
}
public void setServerPropertyImpl(String key, @Nullable String value) {
- if (value == null) {
- orchestrator.getServer().getAdminWsClient().delete(new PropertyDeleteQuery(key));
- } else {
- orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery().setKey(key).setValue(value));
- }
+ ItUtils.setServerProperty(orchestrator, key, value);
}
public ProjectAnalysisRule setServerProperty(String key, String value) {