]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9483 Add IT on "compare_to_profile" in rules search ws
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 30 Jun 2017 08:41:56 +0000 (10:41 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 4 Jul 2017 14:29:36 +0000 (16:29 +0200)
sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java
sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java
sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java
sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java
tests/src/test/java/org/sonarqube/tests/Category6Suite.java
tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesWsTest.java
tests/src/test/java/org/sonarqube/tests/rule/RulesWsTest.java [new file with mode: 0644]

index 668e6ed69dc888580a83c4d9f8e0a97b0c00b7d4..870fd29466089b689fceda30bb0ecd9482e37bb3 100644 (file)
@@ -29,6 +29,7 @@ import org.sonarqube.ws.client.WsConnector;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVATION;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVE_SEVERITIES;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_AVAILABLE_SINCE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_COMPARE_TO_PROFILE;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_INHERITANCE;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_IS_TEMPLATE;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_LANGUAGES;
@@ -63,6 +64,7 @@ public class RulesService extends BaseService {
         .setParam("ps", request.getPageSize())
         .setParam("q", request.getQuery())
         .setParam(PARAM_QPROFILE, request.getQProfile())
+        .setParam(PARAM_COMPARE_TO_PROFILE, request.getCompareToProfile())
         .setParam(PARAM_REPOSITORIES, inlineMultipleParamValue(request.getRepositories()))
         .setParam(PARAM_RULE_KEY, request.getRuleKey())
         .setParam("s", request.getSort())
index 0cbe4dc7e70e26c6124b481c962253ac507689b0..c820d8247dd15e0bc2ad622500482a79cb4f712e 100644 (file)
@@ -38,7 +38,7 @@ public class RulesWsParameters {
   public static final String PARAM_IS_TEMPLATE = "is_template";
   public static final String PARAM_TEMPLATE_KEY = "template_key";
   public static final String PARAM_ORGANIZATION = "organization";
-  public static final String PARAM_COMPARE_TO_PROFILE = "compare_to_profile";
+  public static final String PARAM_COMPARE_TO_PROFILE = "compareToProfile";
 
   public static final String FIELD_REPO = "repo";
   public static final String FIELD_NAME = "name";
index ac2c222a760c0b0aca24e8d8dbdd528bbd240489..5e0255ed732f4aff595d1c4ae7bcf77bf3a20790 100644 (file)
@@ -37,6 +37,7 @@ public class SearchWsRequest {
   private Integer pageSize;
   private String query;
   private String qProfile;
+  private String compareToProfile;
   private List<String> repositories;
   private String ruleKey;
   private String sort;
@@ -176,6 +177,16 @@ public class SearchWsRequest {
     return this;
   }
 
+  @CheckForNull
+  public String getCompareToProfile() {
+    return compareToProfile;
+  }
+
+  public SearchWsRequest setCompareToProfile(@Nullable String compareToProfile) {
+    this.compareToProfile = compareToProfile;
+    return this;
+  }
+
   @CheckForNull
   public List<String> getRepositories() {
     return repositories;
index 96d5eb7e5a374ef3b2d3de6e6b95366e4edcbe57..ae38a45e491dad6dff04066ff53b679bf54ec293 100644 (file)
@@ -35,6 +35,7 @@ import static org.mockito.Mockito.mock;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVATION;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVE_SEVERITIES;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_AVAILABLE_SINCE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_COMPARE_TO_PROFILE;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_INHERITANCE;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_IS_TEMPLATE;
 import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_LANGUAGES;
@@ -101,6 +102,7 @@ public class RulesServiceTest {
       .setPageSize(PAGE_SIZE_VALUE)
       .setQuery(QUERY_VALUE)
       .setQProfile(QPROFILE_VALUE)
+      .setCompareToProfile("CompareTo")
       .setRepositories(REPOSITORIES_VALUE)
       .setRuleKey(RULE_KEY_VALUE)
       .setSort(SORT_VALUE)
@@ -126,6 +128,7 @@ public class RulesServiceTest {
       .hasParam("ps", PAGE_SIZE_VALUE)
       .hasParam("q", QUERY_VALUE)
       .hasParam(PARAM_QPROFILE, QPROFILE_VALUE)
+      .hasParam(PARAM_COMPARE_TO_PROFILE, "CompareTo")
       .hasParam(PARAM_REPOSITORIES, REPOSITORIES_VALUE_INLINED)
       .hasParam(PARAM_RULE_KEY, RULE_KEY_VALUE)
       .hasParam(PARAM_LANGUAGES, LANGUAGES_VALUE_INLINED)
index 7ba1461f70223ad0e259e887db757ca6ca5706a0..eae3e358b8a80213c4eb04f04d017d5e83b4869a 100644 (file)
@@ -37,6 +37,7 @@ import org.sonarqube.tests.qualityProfile.BuiltInQualityProfilesTest;
 import org.sonarqube.tests.qualityProfile.CustomQualityProfilesTest;
 import org.sonarqube.tests.qualityProfile.OrganizationQualityProfilesUiTest;
 import org.sonarqube.tests.qualityProfile.QualityProfilesWsTest;
+import org.sonarqube.tests.rule.RulesWsTest;
 import org.sonarqube.tests.ui.OrganizationUiExtensionsTest;
 import org.sonarqube.tests.user.OrganizationIdentityProviderTest;
 
@@ -63,7 +64,8 @@ import static util.ItUtils.xooPlugin;
   BillingTest.class,
   IssueTagsTest.class,
   LeakProjectsPageTest.class,
-  SearchProjectsTest.class
+  SearchProjectsTest.class,
+  RulesWsTest.class
 })
 public class Category6Suite {
 
index 8cef4efa64cee6603eb42f3f618943f4d1f3e8af..ed468a4087427428a333f6d8c4ab5ece966ebf19 100644 (file)
@@ -32,6 +32,7 @@ import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
 import org.sonarqube.ws.QualityProfiles.ShowResponse;
 import org.sonarqube.ws.QualityProfiles.ShowResponse.CompareToSonarWay;
 import org.sonarqube.ws.QualityProfiles.ShowResponse.QualityProfile;
+import org.sonarqube.ws.client.PostRequest;
 import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
 import org.sonarqube.ws.client.qualityprofile.ShowRequest;
 
@@ -79,6 +80,29 @@ public class QualityProfilesWsTest {
       .containsExactly(sonarWay.getKey(), sonarWay.getName(), 2L);
   }
 
+  @Test
+  public void bulk_activate_missing_rules_from_sonar_way_profile() {
+    Organization org = tester.organizations().generate();
+    CreateWsResponse.QualityProfile xooProfile = tester.qProfiles().createXooProfile(org);
+    tester.qProfiles().activateRule(xooProfile, RULE_ONE_BUG_PER_LINE);
+    tester.qProfiles().activateRule(xooProfile, RULE_ONE_ISSUE_PER_LINE);
+    SearchWsResponse.QualityProfile sonarWay = getProfile(org, p -> "Sonar way".equals(p.getName()) && "xoo".equals(p.getLanguage()) && p.getIsBuiltIn());
+
+    // Bulk activate missing rules from the Sonar way profile
+    tester.wsClient().wsConnector().call(new PostRequest("api/qualityprofiles/activate_rules")
+      .setParam("targetProfile", xooProfile.getKey())
+      .setParam("qprofile", xooProfile.getKey())
+      .setParam("activation", "false")
+      .setParam("compareToProfile", sonarWay.getKey())).failIfNotSuccessful();
+
+    // Check that the profile has no missing rule from the Sonar way profile
+    assertThat(tester.qProfiles().service().show(new ShowRequest()
+      .setProfile(xooProfile.getKey())
+      .setCompareToSonarWay(true)).getCompareToSonarWay())
+        .extracting(CompareToSonarWay::getProfile, CompareToSonarWay::getProfileName, CompareToSonarWay::getMissingRuleCount)
+        .containsExactly(sonarWay.getKey(), sonarWay.getName(), 0L);
+  }
+
   private SearchWsResponse.QualityProfile getProfile(Organization organization, Predicate<SearchWsResponse.QualityProfile> filter) {
     return tester.qProfiles().service().search(new SearchWsRequest()
       .setOrganizationKey(organization.getKey())).getProfilesList()
diff --git a/tests/src/test/java/org/sonarqube/tests/rule/RulesWsTest.java b/tests/src/test/java/org/sonarqube/tests/rule/RulesWsTest.java
new file mode 100644 (file)
index 0000000..3b4d148
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * 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.rule;
+
+import com.sonar.orchestrator.Orchestrator;
+import java.util.List;
+import java.util.function.Predicate;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonarqube.tests.Category6Suite;
+import org.sonarqube.tests.Tester;
+import org.sonarqube.ws.Organizations.Organization;
+import org.sonarqube.ws.QualityProfiles.CreateWsResponse;
+import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
+import org.sonarqube.ws.Rules;
+import org.sonarqube.ws.client.rule.SearchWsRequest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RulesWsTest {
+
+  private static final String RULE_HAS_TAG = "xoo:HasTag";
+  private static final String RULE_ONE_ISSUE_PER_LINE = "xoo:OneIssuePerLine";
+  private static final String RULE_ONE_ISSUE_PER_FILE = "xoo:OneIssuePerFile";
+  private static final String RULE_ONE_BUG_PER_LINE = "xoo:OneBugIssuePerLine";
+  private static final String PROFILE_SONAR_WAY = "Sonar way";
+  private static final String LANGUAGE_XOO = "xoo";
+
+  @ClassRule
+  public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR;
+
+  @Rule
+  public Tester tester = new Tester(orchestrator);
+
+  @Test
+  public void search_activated_rules() {
+    Organization org = tester.organizations().generate();
+    SearchWsResponse.QualityProfile sonarWay = getProfile(org, p -> PROFILE_SONAR_WAY.equals(p.getName()) && LANGUAGE_XOO.equals(p.getLanguage()) && p.getIsBuiltIn());
+
+    List<Rules.Rule> result = tester.wsClient().rules().search(new SearchWsRequest().setQProfile(sonarWay.getKey()).setActivation(true))
+      .getRulesList();
+
+    assertThat(result)
+      .extracting(Rules.Rule::getKey)
+      .containsExactlyInAnyOrder(RULE_HAS_TAG, RULE_ONE_ISSUE_PER_LINE, RULE_ONE_ISSUE_PER_FILE);
+  }
+
+  @Test
+  public void search_sonar_way_rules_not_activated() {
+    Organization org = tester.organizations().generate();
+    CreateWsResponse.QualityProfile xooProfile = tester.qProfiles().createXooProfile(org);
+    tester.qProfiles().activateRule(xooProfile, RULE_ONE_BUG_PER_LINE);
+    tester.qProfiles().activateRule(xooProfile, RULE_ONE_ISSUE_PER_LINE);
+    SearchWsResponse.QualityProfile sonarWay = getProfile(org, p -> PROFILE_SONAR_WAY.equals(p.getName()) && LANGUAGE_XOO.equals(p.getLanguage()) && p.getIsBuiltIn());
+
+    List<Rules.Rule> result = tester.wsClient().rules().search(new SearchWsRequest()
+      .setQProfile(xooProfile.getKey())
+      .setActivation(false)
+      .setCompareToProfile(sonarWay.getKey()))
+      .getRulesList();
+
+    assertThat(result)
+      .extracting(Rules.Rule::getKey)
+      .containsExactlyInAnyOrder(RULE_HAS_TAG, RULE_ONE_ISSUE_PER_FILE);
+  }
+
+  private SearchWsResponse.QualityProfile getProfile(Organization organization, Predicate<SearchWsResponse.QualityProfile> filter) {
+    return tester.qProfiles().service().search(new org.sonarqube.ws.client.qualityprofile.SearchWsRequest()
+      .setOrganizationKey(organization.getKey())).getProfilesList()
+      .stream()
+      .filter(filter)
+      .findAny().orElseThrow(IllegalStateException::new);
+  }
+}