From 5477e21cb5f5329739e5e42c2cbcfbf79174e10f Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 30 Jun 2017 10:41:56 +0200 Subject: [PATCH] SONAR-9483 Add IT on "compare_to_profile" in rules search ws --- .../ws/client/rule/RulesService.java | 2 + .../ws/client/rule/RulesWsParameters.java | 2 +- .../ws/client/rule/SearchWsRequest.java | 11 +++ .../ws/client/rule/RulesServiceTest.java | 3 + .../org/sonarqube/tests/Category6Suite.java | 4 +- .../qualityProfile/QualityProfilesWsTest.java | 24 +++++ .../org/sonarqube/tests/rule/RulesWsTest.java | 92 +++++++++++++++++++ 7 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/src/test/java/org/sonarqube/tests/rule/RulesWsTest.java diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java index 668e6ed69dc..870fd294660 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java @@ -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()) diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java index 0cbe4dc7e70..c820d8247dd 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesWsParameters.java @@ -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"; diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java index ac2c222a760..5e0255ed732 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java @@ -37,6 +37,7 @@ public class SearchWsRequest { private Integer pageSize; private String query; private String qProfile; + private String compareToProfile; private List 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 getRepositories() { return repositories; diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java index 96d5eb7e5a3..ae38a45e491 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java @@ -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) diff --git a/tests/src/test/java/org/sonarqube/tests/Category6Suite.java b/tests/src/test/java/org/sonarqube/tests/Category6Suite.java index 7ba1461f702..eae3e358b8a 100644 --- a/tests/src/test/java/org/sonarqube/tests/Category6Suite.java +++ b/tests/src/test/java/org/sonarqube/tests/Category6Suite.java @@ -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 { diff --git a/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesWsTest.java b/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesWsTest.java index 8cef4efa64c..ed468a40874 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesWsTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityProfile/QualityProfilesWsTest.java @@ -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 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 index 00000000000..3b4d1482b94 --- /dev/null +++ b/tests/src/test/java/org/sonarqube/tests/rule/RulesWsTest.java @@ -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 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 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 filter) { + return tester.qProfiles().service().search(new org.sonarqube.ws.client.qualityprofile.SearchWsRequest() + .setOrganizationKey(organization.getKey())).getProfilesList() + .stream() + .filter(filter) + .findAny().orElseThrow(IllegalStateException::new); + } +} -- 2.39.5