]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7300 Fix ITs using removed POST/PUT api/properties WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 6 Jan 2017 12:57:00 +0000 (13:57 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 10 Jan 2017 08:56:55 +0000 (09:56 +0100)
it/it-tests/src/test/java/it/analysis/ScannerTest.java
it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java
it/it-tests/src/test/java/util/ItUtils.java
it/it-tests/src/test/java/util/ProjectAnalysisRule.java

index b5bad6419e60c4588e1d9ec20a26e7bc7a36b1b7..da7ff0ad2e5c9aecc6cfca62efb2234ed2085598 100644 (file)
@@ -37,13 +37,13 @@ import org.junit.Test;
 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 {
 
@@ -92,14 +92,12 @@ 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);
 
@@ -107,7 +105,7 @@ public class ScannerTest {
     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);
 
@@ -115,7 +113,7 @@ public class ScannerTest {
     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);
 
@@ -126,16 +124,14 @@ public class ScannerTest {
   // 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);
 
@@ -190,21 +186,21 @@ public class ScannerTest {
 
   @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");
@@ -212,21 +208,21 @@ public class ScannerTest {
       "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));
@@ -308,11 +304,11 @@ public class ScannerTest {
     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");
     }
   }
 
index 79883def3b4c43bf2d915c83c81a4e901b8f8a80..bd02216a3185bd8e675c79794880a273b5d4090c 100644 (file)
@@ -29,19 +29,17 @@ import org.junit.Before;
 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;
@@ -49,6 +47,7 @@ import static util.ItUtils.newAdminWsClient;
 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 {
@@ -59,23 +58,39 @@ 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
@@ -85,62 +100,53 @@ public class QualityGateNotificationTest {
 
   @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() {
index e0dcfc5fd3e3f6b8653a42e0210094114c7aa0f9..48ab757068f02a91f128b1593b1c8c0fa53e5d35 100644 (file)
@@ -230,7 +230,9 @@ public class ItUtils {
   }
 
   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) {
index 10c954d4891293af409892b468126f15c5bc21c0..2bd1b29ad4068bbc3b12125157a35b22ce8be720 100644 (file)
@@ -30,12 +30,11 @@ import javax.annotation.CheckForNull;
 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:
@@ -111,17 +110,11 @@ public class ProjectAnalysisRule extends ExternalResource {
   }
 
   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) {