From 7ac92581bbed6357a781bb77a5147d0a141e620e Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Wed, 21 Jun 2017 12:51:48 +0200 Subject: [PATCH] SONAR-9448 Sanitize api/qualityprofiles/activate_rule --- .../qualityprofile/ws/ActivateRuleAction.java | 39 ++--- .../ws/ActivateRuleActionTest.java | 136 ++++++++++-------- .../ws/QProfilesWsMediumTest.java | 61 ++++---- .../QualityProfileWsParameters.java | 36 ++--- .../QualityProfilesService.java | 16 ++- .../QualityProfilesServiceTest.java | 27 ++++ 6 files changed, 185 insertions(+), 130 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java index 4c18c12695e..9badee93750 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ActivateRuleAction.java @@ -23,12 +23,10 @@ import java.util.List; import java.util.Map; 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.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.QProfileDto; @@ -39,14 +37,14 @@ import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.user.UserSession; import static java.lang.String.format; +import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; 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; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARAMS; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RESET; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SEVERITY; -@ServerSide public class ActivateRuleAction implements QProfileWsAction { private final DbClient dbClient; @@ -66,18 +64,21 @@ public class ActivateRuleAction implements QProfileWsAction { public void define(WebService.NewController controller) { WebService.NewAction activate = controller .createAction(ACTION_ACTIVATE_RULE) - .setDescription("Activate a rule on a Quality profile") + .setDescription("Activate a rule on a Quality Profile.
" + + "Requires to be logged in and the 'Administer Quality Profiles' permission.") .setHandler(this) .setPost(true) .setSince("4.4"); - activate.createParam(PARAM_PROFILE_KEY) - .setDescription("Key of Quality profile, can be obtained through api/qualityprofiles/search") + activate.createParam(PARAM_PROFILE) + .setDescription("Quality Profile key. Can be obtained through api/qualityprofiles/search") + .setDeprecatedKey("profile_key", "6.5") .setRequired(true) - .setExampleValue(Uuids.UUID_EXAMPLE_01); + .setExampleValue(UUID_EXAMPLE_01); - activate.createParam(PARAM_RULE_KEY) - .setDescription("Key of the rule") + activate.createParam(PARAM_RULE) + .setDescription("Rule key") + .setDeprecatedKey("rule_key", "6.5") .setRequired(true) .setExampleValue("squid:AvoidCycles"); @@ -86,12 +87,11 @@ public class ActivateRuleAction implements QProfileWsAction { .setPossibleValues(Severity.ALL); activate.createParam(PARAM_PARAMS) - .setDescription(format("Parameters as semi-colon list of =. Ignored if parameter %s is true.", PARAM_RESET)) + .setDescription(format("Parameters as semi-colon list of key=value. Ignored if parameter %s is true.", PARAM_RESET)) .setExampleValue("params=key1=v1;key2=v2"); activate.createParam(PARAM_RESET) - .setDescription("Reset severity and parameters of activated rule. Set the values defined on parent profile " + - "or from rule default values.") + .setDescription("Reset severity and parameters of activated rule. Set the values defined on parent profile or from rule default values.") .setBooleanPossibleValues(); } @@ -99,7 +99,7 @@ public class ActivateRuleAction implements QProfileWsAction { public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); try (DbSession dbSession = dbClient.openSession(false)) { - String profileKey = request.mandatoryParam(PARAM_PROFILE_KEY); + String profileKey = request.mandatoryParam(PARAM_PROFILE); QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(profileKey)); wsSupport.checkPermission(dbSession, profile); wsSupport.checkNotBuiltInt(profile); @@ -108,11 +108,12 @@ public class ActivateRuleAction implements QProfileWsAction { dbSession.commit(); activeRuleIndexer.indexChanges(dbSession, changes); } + response.noContent(); } private static RuleActivation readActivation(Request request) { - RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE_KEY)); + RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE)); boolean reset = Boolean.TRUE.equals(request.paramAsBoolean(PARAM_RESET)); if (reset) { return RuleActivation.createReset(ruleKey); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java index c00824b2cf1..c2598bee161 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ActivateRuleActionTest.java @@ -19,12 +19,17 @@ */ package org.sonar.server.qualityprofile.ws; +import java.net.HttpURLConnection; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.Severity; import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; +import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.permission.OrganizationPermission; @@ -34,16 +39,22 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.organization.TestDefaultOrganizationProvider; +import org.sonar.server.qualityprofile.RuleActivation; import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; +import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE; public class ActivateRuleActionTest { @@ -58,8 +69,9 @@ public class ActivateRuleActionTest { private RuleActivator ruleActivator = mock(RuleActivator.class); private QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(dbTester)); private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class); - private ActivateRuleAction underTest = new ActivateRuleAction(dbClient, ruleActivator, userSession, wsSupport, activeRuleIndexer); - private WsActionTester wsActionTester = new WsActionTester(underTest); + + private WsActionTester ws = new WsActionTester(new ActivateRuleAction(dbClient, ruleActivator, userSession, wsSupport, activeRuleIndexer)); + private OrganizationDto defaultOrganization; private OrganizationDto organization; @@ -71,20 +83,25 @@ public class ActivateRuleActionTest { @Test public void define_activate_rule_action() { - WebService.Action definition = wsActionTester.getDef(); + WebService.Action definition = ws.getDef(); assertThat(definition).isNotNull(); assertThat(definition.isPost()).isTrue(); - assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "profile_key", "reset", "rule_key", "params"); + assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "profile", "reset", "rule", "params"); + WebService.Param profileKey = definition.param("profile"); + assertThat(profileKey.deprecatedKey()).isEqualTo("profile_key"); + WebService.Param ruleKey = definition.param("rule"); + assertThat(ruleKey.deprecatedKey()).isEqualTo("rule_key"); } @Test public void should_fail_if_not_logged_in() { - TestRequest request = wsActionTester.newRequest() + TestRequest request = ws.newRequest() .setMethod("POST") - .setParam("rule_key", RuleTesting.newRuleDto().getKey().toString()) - .setParam("profile_key", randomAlphanumeric(UUID_SIZE)); + .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString()) + .setParam(PARAM_PROFILE, randomAlphanumeric(UUID_SIZE)); expectedException.expect(UnauthorizedException.class); + request.execute(); } @@ -92,10 +109,10 @@ public class ActivateRuleActionTest { public void should_fail_if_not_organization_quality_profile_administrator() { userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization); QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization); - TestRequest request = wsActionTester.newRequest() + TestRequest request = ws.newRequest() .setMethod("POST") - .setParam("rule_key", RuleTesting.newRuleDto().getKey().toString()) - .setParam("profile_key", qualityProfile.getKee()); + .setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString()) + .setParam(PARAM_PROFILE, qualityProfile.getKee()); expectedException.expect(ForbiddenException.class); @@ -107,10 +124,10 @@ public class ActivateRuleActionTest { userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization); QProfileDto qualityProfile = dbTester.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo")); - TestRequest request = wsActionTester.newRequest() + TestRequest request = ws.newRequest() .setMethod("POST") - .setParam("rule_key", RuleTesting.newRuleDto().getKey().toString()) - .setParam("profile_key", qualityProfile.getKee()); + .setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString()) + .setParam(PARAM_PROFILE, qualityProfile.getKee()); expectedException.expect(BadRequestException.class); expectedException.expectMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'"); @@ -118,51 +135,50 @@ public class ActivateRuleActionTest { request.execute(); } -// @Test -// public void activate_rule_in_default_organization() { -// userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization); -// QProfileDto qualityProfile = dbTester.qualityProfiles().insert(defaultOrganization); -// RuleKey ruleKey = RuleTesting.randomRuleKey(); -// TestRequest request = wsActionTester.newRequest() -// .setMethod("POST") -// .setParam("rule_key", ruleKey.toString()) -// .setParam("profile_key", qualityProfile.getKee()) -// .setParam("severity", "BLOCKER") -// .setParam("params", "key1=v1;key2=v2") -// .setParam("reset", "false"); -// -// TestResponse response = request.execute(); -// -// assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT); -// ArgumentCaptor captor = ArgumentCaptor.forClass(RuleActivation.class); -// Mockito.verify(ruleActivator).activate(any(DbSession.class), captor.capture(), eq(qualityProfile.getKee())); -// assertThat(captor.getValue().getRuleKey()).isEqualTo(ruleKey); -// assertThat(captor.getValue().getSeverity()).isEqualTo(Severity.BLOCKER); -// assertThat(captor.getValue().getParameters()).containsExactly(entry("key1", "v1"), entry("key2", "v2")); -// assertThat(captor.getValue().isReset()).isFalse(); -// } -// -// @Test -// public void activate_rule_in_specific_organization() { -// userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization); -// QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization); -// RuleKey ruleKey = RuleTesting.randomRuleKey(); -// TestRequest request = wsActionTester.newRequest() -// .setMethod("POST") -// .setParam("rule_key", ruleKey.toString()) -// .setParam("profile_key", qualityProfile.getKee()) -// .setParam("severity", "BLOCKER") -// .setParam("params", "key1=v1;key2=v2") -// .setParam("reset", "false"); -// -// TestResponse response = request.execute(); -// -// assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT); -// ArgumentCaptor captor = ArgumentCaptor.forClass(RuleActivation.class); -// Mockito.verify(ruleActivator).activate(any(DbSession.class), captor.capture(), eq(qualityProfile.getKee())); -// assertThat(captor.getValue().getRuleKey()).isEqualTo(ruleKey); -// assertThat(captor.getValue().getSeverity()).isEqualTo(Severity.BLOCKER); -// assertThat(captor.getValue().getParameters()).containsExactly(entry("key1", "v1"), entry("key2", "v2")); -// assertThat(captor.getValue().isReset()).isFalse(); -// } + @Test + public void activate_rule_in_default_organization() { + userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization); + QProfileDto qualityProfile = dbTester.qualityProfiles().insert(defaultOrganization); + RuleKey ruleKey = RuleTesting.randomRuleKey(); + TestRequest request = ws.newRequest() + .setMethod("POST") + .setParam(PARAM_RULE, ruleKey.toString()) + .setParam(PARAM_PROFILE, qualityProfile.getKee()) + .setParam("severity", "BLOCKER") + .setParam("params", "key1=v1;key2=v2") + .setParam("reset", "false"); + + TestResponse response = request.execute(); + + assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT); + ArgumentCaptor captor = ArgumentCaptor.forClass(RuleActivation.class); + verify(ruleActivator).activate(any(DbSession.class), captor.capture(), any(QProfileDto.class)); + RuleActivation value = captor.getValue(); + assertThat(value.getRuleKey()).isEqualTo(ruleKey); + assertThat(value.getSeverity()).isEqualTo(Severity.BLOCKER); + assertThat(value.isReset()).isFalse(); + } + + @Test + public void activate_rule_in_specific_organization() { + userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization); + QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization); + RuleKey ruleKey = RuleTesting.randomRuleKey(); + TestRequest request = ws.newRequest() + .setMethod("POST") + .setParam(PARAM_RULE, ruleKey.toString()) + .setParam(PARAM_PROFILE, qualityProfile.getKee()) + .setParam("severity", "BLOCKER") + .setParam("params", "key1=v1;key2=v2") + .setParam("reset", "false"); + + TestResponse response = request.execute(); + + assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT); + ArgumentCaptor captor = ArgumentCaptor.forClass(RuleActivation.class); + verify(ruleActivator).activate(any(DbSession.class), captor.capture(), any(QProfileDto.class)); + assertThat(captor.getValue().getRuleKey()).isEqualTo(ruleKey); + assertThat(captor.getValue().getSeverity()).isEqualTo(Severity.BLOCKER); + assertThat(captor.getValue().isReset()).isFalse(); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java index 09d17df3469..5e0566d8504 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java @@ -30,7 +30,7 @@ import org.junit.Test; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; -import org.sonar.api.server.ws.WebService; +import org.sonar.api.server.ws.WebService.Param; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; @@ -52,14 +52,17 @@ import org.sonar.server.rule.index.RuleQuery; import org.sonar.server.tester.ServerTester; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; +import org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; +import static org.sonar.server.qualityprofile.ws.QProfilesWs.API_ENDPOINT; 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_PROFILE_KEY; -import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_RULE_KEY; -import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters.PARAM_SEVERITY; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RESET; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SEVERITY; import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_LANGUAGES; import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_QPROFILE; @@ -111,8 +114,8 @@ public class QProfilesWsMediumTest { // 1. Deactivate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_DEACTIVATE_RULE); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(PARAM_RULE_KEY, rule.getKey().toString()); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); + request.setParam(ActivateActionParameters.PARAM_RULE_KEY, rule.getKey().toString()); request.execute(); session.clearCache(); @@ -139,7 +142,7 @@ public class QProfilesWsMediumTest { // 1. Deactivate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); WsTester.Result result = request.execute(); session.clearCache(); @@ -165,7 +168,7 @@ public class QProfilesWsMediumTest { // 1. Deactivate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); WsTester.Result result = request.execute(); session.clearCache(); @@ -189,8 +192,8 @@ public class QProfilesWsMediumTest { // 1. Deactivate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(WebService.Param.TEXT_QUERY, "hello"); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); + request.setParam(Param.TEXT_QUERY, "hello"); WsTester.Result result = request.execute(); session.clearCache(); @@ -210,8 +213,8 @@ public class QProfilesWsMediumTest { // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(PARAM_RULE_KEY, rule.getKey().toString()); + request.setParam(PARAM_PROFILE, profile.getKee()); + request.setParam(PARAM_RULE, rule.getKey().toString()); WsTester.Result result = request.execute(); session.clearCache(); @@ -232,8 +235,8 @@ public class QProfilesWsMediumTest { try { // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(PARAM_RULE_KEY, rule.getKey().toString()); + request.setParam(PARAM_PROFILE, profile.getKee()); + request.setParam(PARAM_RULE, rule.getKey().toString()); request.execute(); session.clearCache(); fail(); @@ -254,8 +257,8 @@ public class QProfilesWsMediumTest { // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(PARAM_RULE_KEY, rule.getKey().toString()); + request.setParam(PARAM_PROFILE, profile.getKee()); + request.setParam(PARAM_RULE, rule.getKey().toString()); request.setParam(PARAM_SEVERITY, "MINOR"); WsTester.Result result = request.execute(); session.clearCache(); @@ -282,7 +285,7 @@ public class QProfilesWsMediumTest { // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); request.setParam(PARAM_LANGUAGES, "java"); request.execute().assertJson(getClass(), "bulk_activate_rule.json"); session.clearCache(); @@ -306,7 +309,7 @@ public class QProfilesWsMediumTest { // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, php.getKee()); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, php.getKee()); request.setParam(PARAM_LANGUAGES, "php"); request.execute().assertJson(getClass(), "bulk_activate_rule_not_all.json"); session.clearCache(); @@ -329,8 +332,8 @@ public class QProfilesWsMediumTest { // 1. Activate Rule with query returning 0 hits WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(WebService.Param.TEXT_QUERY, "php"); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); + request.setParam(Param.TEXT_QUERY, "php"); request.execute(); session.clearCache(); @@ -339,8 +342,8 @@ public class QProfilesWsMediumTest { // 1. Activate Rule with query returning 1 hits request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(WebService.Param.TEXT_QUERY, "world"); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); + request.setParam(Param.TEXT_QUERY, "world"); request.execute(); session.commit(); @@ -364,7 +367,7 @@ public class QProfilesWsMediumTest { new SearchOptions()).getIds()).hasSize(2); // 1. Activate Rule with query returning 2 hits - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); request.setParam(ActivateRulesAction.PROFILE_KEY, profile.getKee()); request.setParam(ActivateRulesAction.SEVERITY, "MINOR"); request.execute(); @@ -390,8 +393,8 @@ public class QProfilesWsMediumTest { session.commit(); // 1. Activate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); - request.setParam(PARAM_PROFILE_KEY, javaProfile.getKee()); + WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, javaProfile.getKee()); request.setParam(PARAM_QPROFILE, javaProfile.getKee()); request.setParam("activation", "false"); request.execute().assertJson(getClass(), "does_not_return_warnings_when_bulk_activate_on_profile_and_rules_exist_on_another_language_than_profile.json"); @@ -424,10 +427,10 @@ public class QProfilesWsMediumTest { assertThat(activeRuleDto.get().getSeverityString()).isEqualTo(Severity.MINOR); // 1. reset child rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); - request.setParam("profile_key", subProfile.getKee()); - request.setParam("rule_key", rule.getKey().toString()); - request.setParam("reset", "true"); + WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ACTION_ACTIVATE_RULE); + request.setParam(PARAM_PROFILE, subProfile.getKee()); + request.setParam(PARAM_RULE, rule.getKey().toString()); + request.setParam(PARAM_RESET, "true"); request.execute(); session.clearCache(); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java index 7c3f36cd380..74d5e20df22 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java @@ -22,42 +22,44 @@ package org.sonarqube.ws.client.qualityprofile; public class QualityProfileWsParameters { public static final String CONTROLLER_QUALITY_PROFILES = "api/qualityprofiles"; - - public static final String ACTION_ACTIVATE_RULE = "activate_rule"; public interface ActivateActionParameters { String PARAM_PROFILE_KEY = "profile_key"; String PARAM_RULE_KEY = "rule_key"; - String PARAM_SEVERITY = "severity"; - String PARAM_RESET = "reset"; - String PARAM_PARAMS = "params"; } - public static final String ACTION_RESTORE = "restore"; + public interface RestoreActionParameters { String PARAM_BACKUP = "backup"; } + public static final String ACTION_ACTIVATE_RULE = "activate_rule"; - public static final String ACTION_DEACTIVATE_RULE = "deactivate_rule"; - public static final String ACTION_SEARCH = "search"; public static final String ACTION_ADD_PROJECT = "add_project"; - public static final String ACTION_REMOVE_PROJECT = "remove_project"; - public static final String ACTION_CREATE = "create"; - public static final String ACTION_COPY = "copy"; public static final String ACTION_CHANGE_PARENT = "change_parent"; - public static final String ACTION_SET_DEFAULT = "set_default"; + public static final String ACTION_COPY = "copy"; + public static final String ACTION_CREATE = "create"; + public static final String ACTION_DEACTIVATE_RULE = "deactivate_rule"; public static final String ACTION_DELETE = "delete"; + public static final String ACTION_REMOVE_PROJECT = "remove_project"; + public static final String ACTION_SEARCH = "search"; + public static final String ACTION_SET_DEFAULT = "set_default"; - public static final String PARAM_ORGANIZATION = "organization"; public static final String PARAM_DEFAULTS = "defaults"; + public static final String PARAM_FROM_KEY = "fromKey"; + public static final String PARAM_ORGANIZATION = "organization"; public static final String PARAM_LANGUAGE = "language"; - public static final String PARAM_PROFILE_NAME = "profileName"; + public static final String PARAM_PARAMS = "params"; + public static final String PARAM_PARENT_KEY = "parentKey"; + public static final String PARAM_PARENT_NAME = "parentName"; + public static final String PARAM_PROFILE = "profile"; public static final String PARAM_PROFILE_KEY = "profileKey"; + public static final String PARAM_PROFILE_NAME = "profileName"; public static final String PARAM_PROJECT_KEY = "projectKey"; public static final String PARAM_PROJECT_UUID = "projectUuid"; - public static final String PARAM_FROM_KEY = "fromKey"; + public static final String PARAM_RESET = "reset"; + public static final String PARAM_RULE = "rule"; + public static final String PARAM_RULE_KEY = "ruleKey"; + public static final String PARAM_SEVERITY = "severity"; public static final String PARAM_TO_NAME = "toName"; - public static final String PARAM_PARENT_NAME = "parentName"; - public static final String PARAM_PARENT_KEY = "parentKey"; private QualityProfileWsParameters() { // Only static stuff diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java index c3a6d02a925..d7658b35b7b 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java @@ -45,12 +45,18 @@ import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters. import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_FROM_KEY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARAMS; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARENT_KEY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARENT_NAME; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_KEY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_NAME; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_UUID; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RESET; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE_KEY; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SEVERITY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO_NAME; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.RestoreActionParameters.PARAM_BACKUP; @@ -63,11 +69,11 @@ public class QualityProfilesService extends BaseService { public void activateRule(ActivateRuleWsRequest request) { PostRequest httpRequest = new PostRequest(path(ACTION_ACTIVATE_RULE)); httpRequest.setParam(PARAM_ORGANIZATION, request.getOrganization().orElse(null)); - httpRequest.setParam(ActivateActionParameters.PARAM_PARAMS, request.getParams().orElse(null)); - httpRequest.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, request.getProfileKey()); - httpRequest.setParam(ActivateActionParameters.PARAM_RESET, request.getReset().orElse(null)); - httpRequest.setParam(ActivateActionParameters.PARAM_RULE_KEY, request.getRuleKey()); - httpRequest.setParam(ActivateActionParameters.PARAM_SEVERITY, request.getSeverity().map(Enum::name).orElse(null)); + httpRequest.setParam(PARAM_PARAMS, request.getParams().orElse(null)); + httpRequest.setParam(PARAM_PROFILE, request.getProfileKey()); + httpRequest.setParam(PARAM_RESET, request.getReset().orElse(null)); + httpRequest.setParam(PARAM_RULE, request.getRuleKey()); + httpRequest.setParam(PARAM_SEVERITY, request.getSeverity().map(Enum::name).orElse(null)); call(httpRequest); } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java index 60052f0b4b4..81b080a4a59 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java @@ -21,6 +21,7 @@ package org.sonarqube.ws.client.qualityprofile; import org.junit.Rule; import org.junit.Test; +import org.sonarqube.ws.Common.Severity; import org.sonarqube.ws.QualityProfiles; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.PostRequest; @@ -32,9 +33,14 @@ import static org.mockito.Mockito.mock; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_FROM_KEY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARAMS; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_KEY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_NAME; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_SEVERITY; import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO_NAME; public class QualityProfilesServiceTest { @@ -151,4 +157,25 @@ public class QualityProfilesServiceTest { .hasParam(QualityProfileWsParameters.ActivateActionParameters.PARAM_RULE_KEY, "R1") .andNoOtherParam(); } + + @Test + public void activate_rule() { + underTest.activateRule(ActivateRuleWsRequest.builder() + .setRuleKey("R1") + .setProfileKey("P1") + .setOrganization("O1") + .setParams("PS1") + .setSeverity(Severity.INFO) + .build()); + PostRequest request = serviceTester.getPostRequest(); + + serviceTester.assertThat(request) + .hasPath("activate_rule") + .hasParam(PARAM_PROFILE, "P1") + .hasParam(PARAM_RULE, "R1") + .hasParam(PARAM_ORGANIZATION, "O1") + .hasParam(PARAM_PARAMS, "PS1") + .hasParam(PARAM_SEVERITY, Severity.INFO.toString()) + .andNoOtherParam(); + } } -- 2.39.5