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;
.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())
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";
private Integer pageSize;
private String query;
private String qProfile;
+ private String compareToProfile;
private List<String> repositories;
private String ruleKey;
private String sort;
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;
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;
.setPageSize(PAGE_SIZE_VALUE)
.setQuery(QUERY_VALUE)
.setQProfile(QPROFILE_VALUE)
+ .setCompareToProfile("CompareTo")
.setRepositories(REPOSITORIES_VALUE)
.setRuleKey(RULE_KEY_VALUE)
.setSort(SORT_VALUE)
.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)
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;
BillingTest.class,
IssueTagsTest.class,
LeakProjectsPageTest.class,
- SearchProjectsTest.class
+ SearchProjectsTest.class,
+ RulesWsTest.class
})
public class Category6Suite {
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;
.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()
--- /dev/null
+/*
+ * 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);
+ }
+}