]> source.dussan.org Git - sonarqube.git/commitdiff
reduce code duplication in package issue.suite
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 20 Oct 2015 11:56:54 +0000 (13:56 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 23 Oct 2015 08:44:15 +0000 (10:44 +0200)
it/it-tests/src/test/java/analysis/suite/measure/NewDebtRatioMeasureTest.java
it/it-tests/src/test/java/analysis/suite/measure/TechnicalDebtMeasureVariationTest.java
it/it-tests/src/test/java/issue/suite/CommonRulesTest.java
it/it-tests/src/test/java/issue/suite/CustomRulesTest.java
it/it-tests/src/test/java/issue/suite/IssueActionTest.java
it/it-tests/src/test/java/issue/suite/IssueBulkChangeTest.java
it/it-tests/src/test/java/issue/suite/IssueChangelogTest.java
it/it-tests/src/test/java/issue/suite/IssueTestSuite.java
it/it-tests/src/test/java/util/ItUtils.java

index 40d6c62e2fc776cbd7a7ab2fa321812ab26a8a9a..1c92aaac6a28e9bab5a50fc56fd4c0abbb086e61 100644 (file)
@@ -80,13 +80,13 @@ public class NewDebtRatioMeasureTest {
   }
 
   private void runSampleProjectAnalysis(String projectVersion, String... properties) {
-    ItUtils.runProjectAnalysis(
-      NewDebtRatioMeasureTest.orchestrator,
-      "measure/xoo-new-debt-ratio-" + projectVersion,
-      ItUtils.concat(properties,
-        // disable standard scm support so that it does not interfere with Xoo Scm sensor
-        "sonar.scm.disabled", "false")
-      );
+    ItUtils.runVerboseProjectAnalysis(
+        NewDebtRatioMeasureTest.orchestrator,
+        "measure/xoo-new-debt-ratio-" + projectVersion,
+        ItUtils.concat(properties,
+            // disable standard scm support so that it does not interfere with Xoo Scm sensor
+            "sonar.scm.disabled", "false")
+    );
   }
 
   private Resource getFileResourceWithVariations(String metricKey) {
index 24b78ebebb4a1e0f6ba042d259bca393cd022a57..a28f40fe645856fec1f876492da7fc3e63799362 100644 (file)
@@ -146,7 +146,7 @@ public class TechnicalDebtMeasureVariationTest {
   }
 
   private void runSampleProjectAnalysis(String... properties) {
-    ItUtils.runProjectAnalysis(TechnicalDebtMeasureVariationTest.orchestrator, "shared/xoo-sample", properties);
+    ItUtils.runVerboseProjectAnalysis(TechnicalDebtMeasureVariationTest.orchestrator, "shared/xoo-sample", properties);
   }
 
   private Resource getSampleProjectResourceWithVariations(String metricKey) {
index d844b0ae6ed056f062348242199323babeb0a612..8805d2bfdba34213591572094b2271b76894fd3d 100644 (file)
@@ -6,7 +6,6 @@
 package issue.suite;
 
 import com.sonar.orchestrator.Orchestrator;
-import com.sonar.orchestrator.build.SonarRunner;
 import com.sonar.orchestrator.locator.FileLocation;
 import java.util.List;
 import org.junit.BeforeClass;
@@ -15,8 +14,9 @@ import org.junit.Test;
 import org.sonar.wsclient.issue.Issue;
 import org.sonar.wsclient.issue.IssueQuery;
 
+import static issue.suite.IssueTestSuite.searchIssues;
 import static org.assertj.core.api.Assertions.assertThat;
-import static util.ItUtils.projectDir;
+import static util.ItUtils.runProjectAnalysis;
 
 public class CommonRulesTest {
 
@@ -32,10 +32,9 @@ public class CommonRulesTest {
     orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/CommonRulesTest/xoo-common-rules-profile.xml"));
     orchestrator.getServer().provisionProject("common-rules-project", "Sample");
     orchestrator.getServer().associateProjectToQualityProfile("common-rules-project", "xoo", "xoo-common-rules");
-    SonarRunner analysis = SonarRunner.create(projectDir("issue/common-rules"),
+    runProjectAnalysis(orchestrator, "issue/common-rules",
       "sonar.cpd.xoo.minimumTokens", "2",
       "sonar.cpd.xoo.minimumLines", "2");
-    orchestrator.executeBuild(analysis);
   }
 
   @Test
@@ -72,6 +71,6 @@ public class CommonRulesTest {
   }
 
   private List<Issue> findIssues(String componentKey, String ruleKey) {
-    return orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().components(componentKey).rules(ruleKey)).list();
+    return searchIssues(IssueQuery.create().components(componentKey).rules(ruleKey));
   }
 }
index b0d294447345d5f844efdabea40eeb1dae6c6f2a..2271dc3f951f01869f3b5b72bdd36a6c33e1f604 100644 (file)
@@ -6,17 +6,16 @@
 package issue.suite;
 
 import com.sonar.orchestrator.Orchestrator;
-import com.sonar.orchestrator.build.SonarRunner;
 import com.sonar.orchestrator.locator.FileLocation;
 import java.util.List;
 import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
 import org.sonar.wsclient.issue.Issue;
-import org.sonar.wsclient.issue.IssueQuery;
-import util.ItUtils;
 
+import static issue.suite.IssueTestSuite.searchIssues;
 import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.runProjectAnalysis;
 
 public class CustomRulesTest {
 
@@ -44,9 +43,9 @@ public class CustomRulesTest {
     orchestrator.getServer().provisionProject("sample", "Sample");
     orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "Custom");
 
-    orchestrator.executeBuild(SonarRunner.create().setProjectDir(ItUtils.projectDir("shared/xoo-sample")));
+    runProjectAnalysis(orchestrator, "shared/xoo-sample");
 
-    List<Issue> issues = orchestrator.getServer().adminWsClient().issueClient().find(IssueQuery.create()).list();
+    List<Issue> issues = searchIssues();
     assertThat(issues).hasSize(1);
 
     Issue issue = issues.get(0);
index c6d1b75e982e914a02e39dddebd0e4ce35bfda74..8ea2c10b601b31fc577bede76561208714d79e34 100644 (file)
@@ -25,7 +25,6 @@ import com.sonar.orchestrator.locator.FileLocation;
 import java.util.List;
 import org.junit.Before;
 import org.junit.ClassRule;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.wsclient.base.HttpException;
 import org.sonar.wsclient.issue.ActionPlan;
@@ -38,14 +37,13 @@ import org.sonar.wsclient.issue.NewActionPlan;
 
 import static issue.suite.IssueTestSuite.ORCHESTRATOR;
 import static issue.suite.IssueTestSuite.adminIssueClient;
-import static issue.suite.IssueTestSuite.issueClient;
 import static issue.suite.IssueTestSuite.search;
 import static issue.suite.IssueTestSuite.searchIssueByKey;
 import static issue.suite.IssueTestSuite.searchIssues;
 import static issue.suite.IssueTestSuite.searchRandomIssue;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.fail;
-import static util.ItUtils.projectDir;
+import static util.ItUtils.runProjectAnalysis;
 import static util.ItUtils.toDate;
 import static util.ItUtils.verifyHttpException;
 
@@ -72,8 +70,7 @@ public class IssueActionTest {
     orchestrator.getServer().provisionProject("sample", "Sample");
     orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "xoo-one-issue-per-line-profile");
 
-    scan = SonarRunner.create(projectDir("shared/xoo-sample"));
-    orchestrator.executeBuild(scan);
+    scan = runProjectAnalysis(orchestrator, "shared/xoo-sample");
     issue = searchRandomIssue();
   }
 
@@ -91,7 +88,7 @@ public class IssueActionTest {
     assertThat(comment.createdAt()).isNotNull();
 
     // reload issue
-    Issue reloaded = searchIssueByKey(issue.key());
+    Issue reloaded = searchIssues(issue.key(), true).iterator().next();
 
     assertThat(reloaded.comments()).hasSize(1);
     assertThat(reloaded.comments().get(0).key()).isEqualTo(comment.key());
@@ -149,7 +146,7 @@ public class IssueActionTest {
     assertThat(issues.users()).isEmpty();
 
     adminIssueClient().assign(issue.key(), "admin");
-    assertThat(search(IssueQuery.create().assignees("admin")).list()).hasSize(1);
+    assertThat(searchIssues(IssueQuery.create().assignees("admin"))).hasSize(1);
 
     orchestrator.executeBuild(scan);
     Issue reloaded = searchIssueByKey(issue.key());
@@ -164,7 +161,7 @@ public class IssueActionTest {
     adminIssueClient().assign(issue.key(), null);
     reloaded = searchIssueByKey(issue.key());
     assertThat(reloaded.assignee()).isNull();
-    assertThat(issueClient().find(IssueQuery.create().assignees("admin")).list()).isEmpty();
+    assertThat(searchIssues(IssueQuery.create().assignees("admin"))).isEmpty();
   }
 
   /**
@@ -248,7 +245,7 @@ public class IssueActionTest {
     adminIssueClient().doAction(issue.key(), "fake");
 
     // reload issue
-    Issue reloaded = searchIssueByKey(issue.key());
+    Issue reloaded = searchIssues(issue.key(), true).iterator().next();
 
     assertThat(reloaded.comments()).hasSize(1);
     assertThat(reloaded.comments().get(0).htmlText()).isEqualTo("New Comment from fake action");
index 38c63b06e87c30b987425c837c36ae51d347a9bf..b1d4ca7177b4c1464ee7e62e1340259aa541a26d 100644 (file)
@@ -22,7 +22,6 @@ package issue.suite;
 import com.google.common.base.Function;
 import com.google.common.collect.FluentIterable;
 import com.sonar.orchestrator.Orchestrator;
-import com.sonar.orchestrator.build.SonarRunner;
 import com.sonar.orchestrator.locator.FileLocation;
 import java.util.List;
 import org.junit.Before;
@@ -34,14 +33,14 @@ import org.sonar.wsclient.issue.ActionPlanClient;
 import org.sonar.wsclient.issue.BulkChange;
 import org.sonar.wsclient.issue.BulkChangeQuery;
 import org.sonar.wsclient.issue.Issue;
-import org.sonar.wsclient.issue.IssueClient;
-import org.sonar.wsclient.issue.IssueQuery;
 import org.sonar.wsclient.issue.NewActionPlan;
 import util.ItUtils;
 
 import static issue.suite.IssueTestSuite.ORCHESTRATOR;
+import static issue.suite.IssueTestSuite.adminIssueClient;
+import static issue.suite.IssueTestSuite.issueClient;
 import static org.assertj.core.api.Assertions.assertThat;
-import static util.ItUtils.projectDir;
+import static util.ItUtils.runProjectAnalysis;
 
 /**
  * SONAR-4421
@@ -90,7 +89,7 @@ public class IssueBulkChangeTest {
     BulkChange bulkChange = buldChangeAssigneeOfIssues(issueKeys, "admin");
 
     assertThat(bulkChange.totalIssuesChanged()).isEqualTo(BULK_EDITED_ISSUE_COUNT);
-    for (Issue issue : searchIssues(issueKeys)) {
+    for (Issue issue : IssueTestSuite.searchIssues(issueKeys)) {
       assertThat(issue.assignee()).isEqualTo("admin");
     }
   }
@@ -112,7 +111,7 @@ public class IssueBulkChangeTest {
       );
 
     assertThat(bulkChange.totalIssuesChanged()).isEqualTo(BULK_EDITED_ISSUE_COUNT);
-    for (Issue issue : searchIssues(issueKeys)) {
+    for (Issue issue : IssueTestSuite.searchIssues(issueKeys)) {
       assertThat(issue.actionPlan()).isEqualTo(newActionPlan.key());
     }
   }
@@ -133,7 +132,7 @@ public class IssueBulkChangeTest {
       );
 
     assertThat(bulkChange.totalIssuesChanged()).isEqualTo(BULK_EDITED_ISSUE_COUNT);
-    for (Issue issue : searchIssues(issueKeys, true)) {
+    for (Issue issue : IssueTestSuite.searchIssues(issueKeys, true)) {
       assertThat(issue.comments()).hasSize(1);
       assertThat(issue.comments().get(0).htmlText()).isEqualTo(COMMENT_AS_HTML);
     }
@@ -157,7 +156,7 @@ public class IssueBulkChangeTest {
       );
 
     assertThat(bulkChange.totalIssuesChanged()).isEqualTo(BULK_EDITED_ISSUE_COUNT);
-    for (Issue issue : searchIssues(issueKeys, true)) {
+    for (Issue issue : IssueTestSuite.searchIssues(issueKeys, true)) {
       assertThat(issue.status()).isEqualTo("CONFIRMED");
       assertThat(issue.assignee()).isEqualTo("admin");
       assertThat(issue.severity()).isEqualTo(newSeverity);
@@ -228,7 +227,7 @@ public class IssueBulkChangeTest {
     String[] issueKeys = searchIssueKeys(nbIssues);
 
     // Confirm an issue
-    adminIssueClient().doTransition(searchIssues().iterator().next().key(), "confirm");
+    adminIssueClient().doTransition(IssueTestSuite.searchIssues().iterator().next().key(), "confirm");
 
     // Apply a bulk change on unconfirm transition
     BulkChangeQuery query = (BulkChangeQuery.create()
@@ -241,7 +240,7 @@ public class IssueBulkChangeTest {
     assertThat(bulkChange.totalIssuesChanged()).isEqualTo(1);
 
     int nbIssuesWithComment = 0;
-    for (Issue issue : searchIssues(issueKeys, true)) {
+    for (Issue issue : IssueTestSuite.searchIssues(issueKeys, true)) {
       if (!issue.comments().isEmpty()) {
         nbIssuesWithComment++;
       }
@@ -254,17 +253,17 @@ public class IssueBulkChangeTest {
     orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/IssueBulkChangeTest/one-issue-per-line-profile.xml"));
     orchestrator.getServer().provisionProject("sample", "Sample");
     orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
-    orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample")));
+    runProjectAnalysis(orchestrator, "shared/xoo-sample");
   }
 
   private static void assertIssueSeverity(String[] issueKeys, String expectedSeverity) {
-    for (Issue issue : searchIssues(issueKeys)) {
+    for (Issue issue : IssueTestSuite.searchIssues(issueKeys)) {
       assertThat(issue.severity()).isEqualTo(expectedSeverity);
     }
   }
 
   private static void assertIssueStatus(String[] issueKeys, String expectedStatus) {
-    for (Issue issue : searchIssues(issueKeys)) {
+    for (Issue issue : IssueTestSuite.searchIssues(issueKeys)) {
       assertThat(issue.status()).isEqualTo(expectedStatus);
     }
   }
@@ -315,36 +314,8 @@ public class IssueBulkChangeTest {
       .toArray(String.class);
   }
 
-  private static List<Issue> searchIssues(String... issueKeys) {
-    return searchIssues(issueKeys, false);
-  }
-
-  private static List<Issue> searchIssues(String[] issueKeys, boolean withComments) {
-    IssueQuery query = IssueQuery.create().issues(issueKeys);
-    if (withComments) {
-      query.urlParams().put("additionalFields", "comments");
-    }
-    return searchIssues(query);
-  }
-
-  private static List<Issue> searchIssues() {
-    return searchIssues(IssueQuery.create());
-  }
-
   private static String[] searchIssueKeys(int limit) {
-    return getIssueKeys(searchIssues(), limit);
-  }
-
-  private static List<Issue> searchIssues(IssueQuery issueQuery) {
-    return issueClient().find(issueQuery).list();
-  }
-
-  private static IssueClient issueClient() {
-    return orchestrator.getServer().wsClient().issueClient();
-  }
-
-  private static IssueClient adminIssueClient() {
-    return orchestrator.getServer().adminWsClient().issueClient();
+    return getIssueKeys(IssueTestSuite.searchIssues(), limit);
   }
 
   private static ActionPlanClient adminActionPlanClient() {
index 199b6f6a2a1668a1ece56905c164ee27549d3aa4..a8b20a5f90f46537251df7a1c35a81834fe2c586 100644 (file)
@@ -29,12 +29,13 @@ import org.junit.Test;
 import org.sonar.wsclient.issue.Issue;
 import org.sonar.wsclient.issue.IssueChange;
 import org.sonar.wsclient.issue.IssueChangeDiff;
-import org.sonar.wsclient.issue.IssueClient;
 import org.sonar.wsclient.issue.IssueQuery;
 
 import static issue.suite.IssueTestSuite.ORCHESTRATOR;
+import static issue.suite.IssueTestSuite.adminIssueClient;
+import static issue.suite.IssueTestSuite.issueClient;
 import static org.assertj.core.api.Assertions.assertThat;
-import static util.ItUtils.projectDir;
+import static util.ItUtils.runProjectAnalysis;
 
 public class IssueChangelogTest {
 
@@ -50,8 +51,7 @@ public class IssueChangelogTest {
     orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/issue/suite/IssueChangelogTest/one-issue-per-line-profile.xml"));
     orchestrator.getServer().provisionProject("sample", "Sample");
     orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
-    scan = SonarRunner.create(projectDir("shared/xoo-sample"));
-    orchestrator.executeBuild(scan);
+    scan = runProjectAnalysis(orchestrator, "shared/xoo-sample");
     issue = searchRandomIssue();
   }
 
@@ -131,11 +131,4 @@ public class IssueChangelogTest {
     return issueClient().changes(issueKey);
   }
 
-  private static IssueClient issueClient() {
-    return orchestrator.getServer().wsClient().issueClient();
-  }
-
-  private static IssueClient adminIssueClient() {
-    return orchestrator.getServer().adminWsClient().issueClient();
-  }
 }
index ca2a494b6ae2cc3e77eba7c9fcdb3bf10419f811..5f73041862981109273b8cfdbd5b3e4a3f7b7808 100644 (file)
@@ -56,14 +56,33 @@ public class IssueTestSuite {
     return issueClient().find(issueQuery);
   }
 
-  static List<Issue> searchIssues(IssueQuery issueQuery) {
-    return search(issueQuery).list();
-  }
-
   static Issue searchIssueByKey(String issueKey) {
     List<Issue> issues = searchIssues(IssueQuery.create().issues(issueKey));
     assertThat(issues).hasSize(1);
     return issues.get(0);
   }
 
+  static List<Issue> searchIssues(String... issueKeys) {
+    return searchIssues(issueKeys, false);
+  }
+
+  static List<Issue> searchIssues(String issueKey, boolean withComments) {
+    return searchIssues(new String[] { issueKey }, withComments);
+  }
+
+  static List<Issue> searchIssues(String[] issueKeys, boolean withComments) {
+    IssueQuery query = IssueQuery.create().issues(issueKeys);
+    if (withComments) {
+      query.urlParams().put("additionalFields", "comments");
+    }
+    return searchIssues(query);
+  }
+
+  static List<Issue> searchIssues() {
+    return searchIssues(IssueQuery.create());
+  }
+
+  static List<Issue> searchIssues(IssueQuery issueQuery) {
+    return issueClient().find(issueQuery).list();
+  }
 }
index 4cb4df2c0455cd979e5b3288937901f92b1f7e9c..b3cee3b356150355c18319c9e0b8bd81b24e9ea0 100644 (file)
@@ -4,33 +4,34 @@ package util;/*
  * mailto:contact AT sonarsource DOT com
  */
 
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
 import com.sonar.orchestrator.Orchestrator;
 import com.sonar.orchestrator.build.BuildResult;
 import com.sonar.orchestrator.build.SonarRunner;
+import com.sonar.orchestrator.locator.FileLocation;
+import java.io.File;
+import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.annotation.Nullable;
+import org.apache.commons.io.FileUtils;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.JSONValue;
-import com.google.common.base.Supplier;
-import com.google.common.base.Suppliers;
-import com.sonar.orchestrator.locator.FileLocation;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import org.sonar.wsclient.base.HttpException;
+import org.sonar.wsclient.services.PropertyDeleteQuery;
+import org.sonar.wsclient.services.PropertyUpdateQuery;
 
 import static com.google.common.collect.FluentIterable.from;
 import static java.util.Arrays.asList;
-import static org.assertj.core.api.Assertions.fail;
-
 import static org.assertj.core.api.Assertions.assertThat;
-import org.apache.commons.io.FileUtils;
-import org.sonar.wsclient.base.HttpException;
+import static org.assertj.core.api.Assertions.fail;
 
 public class ItUtils {
 
@@ -92,7 +93,7 @@ public class ItUtils {
   /**
    * Locate the pom file of a sample project
    *
-   * @param relativePath project path related to the directory it/it-projects, for example "qualitygate/xoo-sample"
+   * @param projectName project path related to the directory it/it-projects, for example "qualitygate/xoo-sample"
    */
   public static File projectPom(String projectName) {
     File pom = new File(projectDir(projectName), "pom.xml");
@@ -136,19 +137,37 @@ public class ItUtils {
     return count;
   }
 
-  public static void runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) {
+  public static SonarRunner runVerboseProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) {
+    return runProjectAnalysis(orchestrator, projectRelativePath, true, properties);
+  }
+
+  public static SonarRunner runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, String... properties) {
+    return runProjectAnalysis(orchestrator, projectRelativePath, false, properties);
+  }
+
+  private static SonarRunner runProjectAnalysis(Orchestrator orchestrator, String projectRelativePath, boolean enableDebugLogs, String... properties) {
     SonarRunner sonarRunner = SonarRunner.create(projectDir(projectRelativePath));
     ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
-    for (int i = 0; i < properties.length; i+=2) {
-      builder.put(properties[i], properties[i+1]);
+    for (int i = 0; i < properties.length; i += 2) {
+      builder.put(properties[i], properties[i + 1]);
+    }
+    SonarRunner scan = sonarRunner.setDebugLogs(enableDebugLogs).setProperties(builder.build());
+    orchestrator.executeBuild(scan);
+    return scan;
+  }
+
+  public static void setServerProperty(Orchestrator orchestrator, 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));
     }
-    orchestrator.executeBuild(sonarRunner.setDebugLogs(true).setProperties(builder.build()));
   }
 
   /**
    * Concatenates a vararg to a String array.
    *
-   * Useful when using {@link #runProjectAnalysis(Orchestrator, String, String...)}, eg.:
+   * Useful when using {@link #runVerboseProjectAnalysis(Orchestrator, String, String...)}, eg.:
    * <pre>
    * ItUtils.runProjectAnalysis(orchestrator, "project_name",
    *    ItUtils.concat(properties, "sonar.scm.disabled", "false")