--- /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.sonar.server.qualityprofile.ws;
+
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.KeyValueFormat;
+import org.sonar.core.util.Uuids;
+import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.server.qualityprofile.RuleActivation;
+
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ACTIVATE_RULE;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_PARAMS;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_PROFILE_KEY;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_RESET;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_RULE_KEY;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_SEVERITY;
+
+@ServerSide
+public class ActivateRuleAction implements QProfileWsAction {
+
+ private final QProfileService service;
+
+ public ActivateRuleAction(QProfileService service) {
+ this.service = service;
+ }
+
+ public void define(WebService.NewController controller) {
+ WebService.NewAction activate = controller
+ .createAction(ACTION_ACTIVATE_RULE)
+ .setDescription("Activate a rule on a Quality profile")
+ .setHandler(this)
+ .setPost(true)
+ .setSince("4.4");
+
+ activate.createParam(PARAM_PROFILE_KEY)
+ .setDescription("Key of Quality profile, can be obtained through <code>api/profiles/list</code>")
+ .setRequired(true)
+ .setExampleValue(Uuids.UUID_EXAMPLE_01);
+
+ activate.createParam(PARAM_RULE_KEY)
+ .setDescription("Key of the rule")
+ .setRequired(true)
+ .setExampleValue("squid:AvoidCycles");
+
+ activate.createParam(PARAM_SEVERITY)
+ .setDescription(String.format("Severity. Ignored if parameter %s is true.", PARAM_RESET))
+ .setPossibleValues(Severity.ALL);
+
+ activate.createParam(PARAM_PARAMS)
+ .setDescription(String.format("Parameters as semi-colon list of <key>=<value>, for example " +
+ "'<code>params=key1=v1;key2=v2</code>'. Ignored if parameter %s is true.", PARAM_RESET));
+
+ activate.createParam(PARAM_RESET)
+ .setDescription("Reset severity and parameters of activated rule. Set the values defined on parent profile " +
+ "or from rule default values.")
+ .setBooleanPossibleValues();
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ RuleKey ruleKey = readRuleKey(request);
+ RuleActivation activation = new RuleActivation(ruleKey);
+ activation.setSeverity(request.param(PARAM_SEVERITY));
+ String params = request.param(PARAM_PARAMS);
+ if (params != null) {
+ activation.setParameters(KeyValueFormat.parse(params));
+ }
+ activation.setReset(Boolean.TRUE.equals(request.paramAsBoolean(PARAM_RESET)));
+ service.activate(request.mandatoryParam(PARAM_PROFILE_KEY), activation);
+ }
+
+ private static RuleKey readRuleKey(Request request) {
+ return RuleKey.parse(request.mandatoryParam(PARAM_RULE_KEY));
+ }
+}
--- /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.sonar.server.qualityprofile.ws;
+
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.core.util.Uuids;
+import org.sonar.db.qualityprofile.ActiveRuleKey;
+import org.sonar.server.qualityprofile.QProfileService;
+
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULE;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_PROFILE_KEY;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_RULE_KEY;
+
+@ServerSide
+public class DeactivateRuleAction implements QProfileWsAction {
+
+ private final QProfileService service;
+
+ public DeactivateRuleAction(QProfileService service) {
+ this.service = service;
+ }
+
+ public void define(WebService.NewController controller) {
+ WebService.NewAction deactivate = controller
+ .createAction(ACTION_DEACTIVATE_RULE)
+ .setDescription("Deactivate a rule on a Quality profile")
+ .setHandler(this)
+ .setPost(true)
+ .setSince("4.4");
+
+ deactivate.createParam(PARAM_PROFILE_KEY)
+ .setDescription("Key of Quality profile, can be obtained through <code>api/profiles/list</code>")
+ .setRequired(true)
+ .setExampleValue(Uuids.UUID_EXAMPLE_01);
+
+ deactivate.createParam(PARAM_RULE_KEY)
+ .setDescription("Key of the rule")
+ .setRequired(true)
+ .setExampleValue("squid:AvoidCycles");
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ RuleKey ruleKey = readRuleKey(request);
+ service.deactivate(ActiveRuleKey.of(request.mandatoryParam(PARAM_PROFILE_KEY), ruleKey));
+ }
+
+ private static RuleKey readRuleKey(Request request) {
+ return RuleKey.parse(request.mandatoryParam(PARAM_RULE_KEY));
+ }
+}
public static final String API_ENDPOINT = CONTROLLER_QUALITY_PROFILES;
- private final RuleActivationActions ruleActivationActions;
private final BulkRuleActivationActions bulkRuleActivationActions;
private final QProfileWsAction[] actions;
- public QProfilesWs(RuleActivationActions ruleActivationActions,
- BulkRuleActivationActions bulkRuleActivationActions,
+ public QProfilesWs(BulkRuleActivationActions bulkRuleActivationActions,
QProfileWsAction... actions) {
- this.ruleActivationActions = ruleActivationActions;
this.bulkRuleActivationActions = bulkRuleActivationActions;
this.actions = actions;
}
.setDescription("Manage quality profiles.")
.setSince("4.4");
- ruleActivationActions.define(controller);
bulkRuleActivationActions.define(controller);
for (QProfileWsAction action : actions) {
action.define(controller);
RemoveProjectAction.class,
RestoreAction.class,
RestoreBuiltInAction.class,
- RuleActivationActions.class,
+ ActivateRuleAction.class,
+ DeactivateRuleAction.class,
SearchAction.class,
SetDefaultAction.class
);
+++ /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.sonar.server.qualityprofile.ws;
-
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.Severity;
-import org.sonar.api.server.ServerSide;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.KeyValueFormat;
-import org.sonar.core.util.Uuids;
-import org.sonar.db.qualityprofile.ActiveRuleKey;
-import org.sonar.server.qualityprofile.QProfileService;
-import org.sonar.server.qualityprofile.RuleActivation;
-
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ACTIVATE_RULE;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULE;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_PARAMS;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_PROFILE_KEY;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_RESET;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_RULE_KEY;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_SEVERITY;
-
-@ServerSide
-public class RuleActivationActions {
-
- private final QProfileService service;
-
- public RuleActivationActions(QProfileService service) {
- this.service = service;
- }
-
- void define(WebService.NewController controller) {
- defineActivateAction(controller);
- defineDeactivateAction(controller);
- }
-
- private void defineActivateAction(WebService.NewController controller) {
- WebService.NewAction activate = controller
- .createAction(ACTION_ACTIVATE_RULE)
- .setDescription("Activate a rule on a Quality profile")
- .setHandler(this::activate)
- .setPost(true)
- .setSince("4.4");
-
- defineActiveRuleKeyParameters(activate);
-
- activate.createParam(PARAM_SEVERITY)
- .setDescription(String.format("Severity. Ignored if parameter %s is true.", PARAM_RESET))
- .setPossibleValues(Severity.ALL);
-
- activate.createParam(PARAM_PARAMS)
- .setDescription(String.format("Parameters as semi-colon list of <key>=<value>, for example " +
- "'<code>params=key1=v1;key2=v2</code>'. Ignored if parameter %s is true.", PARAM_RESET));
-
- activate.createParam(PARAM_RESET)
- .setDescription("Reset severity and parameters of activated rule. Set the values defined on parent profile " +
- "or from rule default values.")
- .setBooleanPossibleValues();
- }
-
- private void defineDeactivateAction(WebService.NewController controller) {
- WebService.NewAction deactivate = controller
- .createAction(ACTION_DEACTIVATE_RULE)
- .setDescription("Deactivate a rule on a Quality profile")
- .setHandler(this::deactivate)
- .setPost(true)
- .setSince("4.4");
- defineActiveRuleKeyParameters(deactivate);
- }
-
- private static void defineActiveRuleKeyParameters(WebService.NewAction action) {
- action.createParam(PARAM_PROFILE_KEY)
- .setDescription("Key of Quality profile, can be obtained through <code>api/profiles/list</code>")
- .setRequired(true)
- .setExampleValue(Uuids.UUID_EXAMPLE_01);
-
- action.createParam(PARAM_RULE_KEY)
- .setDescription("Key of the rule")
- .setRequired(true)
- .setExampleValue("squid:AvoidCycles");
- }
-
- private void activate(Request request, Response response) {
- RuleKey ruleKey = readRuleKey(request);
- RuleActivation activation = new RuleActivation(ruleKey);
- activation.setSeverity(request.param(PARAM_SEVERITY));
- String params = request.param(PARAM_PARAMS);
- if (params != null) {
- activation.setParameters(KeyValueFormat.parse(params));
- }
- activation.setReset(Boolean.TRUE.equals(request.paramAsBoolean(PARAM_RESET)));
- service.activate(request.mandatoryParam(PARAM_PROFILE_KEY), activation);
- }
-
- private void deactivate(Request request, Response response) {
- RuleKey ruleKey = readRuleKey(request);
- service.deactivate(ActiveRuleKey.of(request.mandatoryParam(PARAM_PROFILE_KEY), ruleKey));
- }
-
- private static RuleKey readRuleKey(Request request) {
- return RuleKey.parse(request.mandatoryParam(PARAM_RULE_KEY));
- }
-
-}
--- /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.sonar.server.qualityprofile.ws;
+
+import org.junit.Test;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class ActivateRuleActionTest {
+
+ @Test
+ public void define_activate_rule_action() {
+ ActivateRuleAction action = new ActivateRuleAction(mock(QProfileService.class));
+ WebService.Action definition = new WsActionTester(action).getDef();
+ assertThat(definition).isNotNull();
+ assertThat(definition.isPost()).isTrue();
+ assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "profile_key", "reset", "rule_key", "params");
+ }
+}
@Test
public void define_change_parent_action() {
- WebService.Action changeParent = new WsTester(new QProfilesWs(mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), underTest))
+ WebService.Action changeParent = new WsTester(new QProfilesWs(mock(BulkRuleActivationActions.class), underTest))
.action(QualityProfileWsParameters.CONTROLLER_QUALITY_PROFILES, "change_parent");
assertThat(changeParent).isNotNull();
assertThat(changeParent.isPost()).isTrue();
@Before
public void before() {
- wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class),
+ wsTester = new WsTester(new QProfilesWs(mock(BulkRuleActivationActions.class),
new ChangelogAction(changelogLoader, wsSupport, new Languages(), dbTester.getDbClient())));
organization = dbTester.organizations().insert();
}
--- /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.sonar.server.qualityprofile.ws;
+
+import org.junit.Test;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class DeactivateRuleActionTest {
+
+ @Test
+ public void define_deactivate_rule_action() {
+ DeactivateRuleAction action = new DeactivateRuleAction(mock(QProfileService.class));
+ WebService.Action definition = new WsActionTester(action).getDef();
+ assertThat(definition).isNotNull();
+ assertThat(definition.isPost()).isTrue();
+ assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("profile_key", "rule_key");
+ }
+}
@Test
public void importers_nominal() throws Exception {
WsTester wsTester = new WsTester(new QProfilesWs(
- mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class),
+ mock(BulkRuleActivationActions.class),
new ExportersAction(createExporters())));
wsTester.newGetRequest("api/qualityprofiles", "exporters").execute().assertJson(getClass(), "exporters.json");
@Test
public void importers_nominal() throws Exception {
WsTester wsTester = new WsTester(new QProfilesWs(
- mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class),
+ mock(BulkRuleActivationActions.class),
new ImportersAction(createImporters())));
wsTester.newGetRequest("api/qualityprofiles", "importers").execute().assertJson(getClass(), "importers.json");
private ComponentDto project4;
private WsTester wsTester = new WsTester(new QProfilesWs(
- mock(RuleActivationActions.class),
mock(BulkRuleActivationActions.class),
new ProjectsAction(dbClient, userSessionRule)));
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new QProfilesWsModule().configure(container);
- assertThat(container.size()).isEqualTo(24 + 2);
+ assertThat(container.size()).isEqualTo(25 + 2);
}
}
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ACTIVATE_RULE;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DEACTIVATE_RULE;
public class QProfilesWsTest {
@Rule
ProfileImporter[] importers = createImporters(languages);
controller = new WsTester(new QProfilesWs(
- new RuleActivationActions(profileService),
new BulkRuleActivationActions(profileService, null),
new CreateAction(null, null, null, languages, wsSupport, userSessionRule, null, importers),
new ImportersAction(importers),
assertThat(search.param("organization").since()).isEqualTo("6.4");
}
- @Test
- public void define_activate_rule_action() {
- WebService.Action restoreProfiles = controller.action(ACTION_ACTIVATE_RULE);
- assertThat(restoreProfiles).isNotNull();
- assertThat(restoreProfiles.isPost()).isTrue();
- assertThat(restoreProfiles.params()).hasSize(5);
- }
-
- @Test
- public void define_deactivate_rule_action() {
- WebService.Action restoreProfiles = controller.action(ACTION_DEACTIVATE_RULE);
- assertThat(restoreProfiles).isNotNull();
- assertThat(restoreProfiles.isPost()).isTrue();
- assertThat(restoreProfiles.params()).hasSize(2);
- }
-
@Test
public void define_set_default_action() {
WebService.Action setDefault = controller.action("set_default");
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
-import org.sonar.core.util.UuidFactoryFast;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
dbClient,
userSessionRule);
tester = new WsTester(new QProfilesWs(
- mock(RuleActivationActions.class),
mock(BulkRuleActivationActions.class),
underTest));
organization = db.organizations().insert();