diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-06-21 15:14:04 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-06-26 09:07:48 +0200 |
commit | 43fcd8577624dbb4bab8e52784ce663ce80e85ae (patch) | |
tree | 0bd37197241a923de5c1e015ffd22052dd4d91b6 /server | |
parent | 7ac92581bbed6357a781bb77a5147d0a141e620e (diff) | |
download | sonarqube-43fcd8577624dbb4bab8e52784ce663ce80e85ae.tar.gz sonarqube-43fcd8577624dbb4bab8e52784ce663ce80e85ae.zip |
SONAR-9448 Sanitize api/qualityprofiles/deactivate_rule
Diffstat (limited to 'server')
3 files changed, 125 insertions, 115 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRuleAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRuleAction.java index 7a000f2fc0a..fa7f99d76b7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRuleAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/DeactivateRuleAction.java @@ -20,22 +20,20 @@ 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.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.user.UserSession; +import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; 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.PARAM_PROFILE; +import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE; -@ServerSide public class DeactivateRuleAction implements QProfileWsAction { private final DbClient dbClient; @@ -53,26 +51,29 @@ public class DeactivateRuleAction implements QProfileWsAction { public void define(WebService.NewController controller) { WebService.NewAction deactivate = controller .createAction(ACTION_DEACTIVATE_RULE) - .setDescription("Deactivate a rule on a Quality profile") + .setDescription("Deactivate a rule on a Quality profile.<br> " + + "Requires to be logged in and the 'Administer Quality Profiles' permission.") .setHandler(this) .setPost(true) .setSince("4.4"); - deactivate.createParam(PARAM_PROFILE_KEY) - .setDescription("Key of Quality profile, can be obtained through <code>api/qualityprofiles/search</code>") + deactivate.createParam(PARAM_PROFILE) + .setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>") + .setDeprecatedKey("profile_key", "6.5") .setRequired(true) - .setExampleValue(Uuids.UUID_EXAMPLE_01); + .setExampleValue(UUID_EXAMPLE_01); - deactivate.createParam(PARAM_RULE_KEY) - .setDescription("Key of the rule") + deactivate.createParam(PARAM_RULE) + .setDescription("Rule key") + .setDeprecatedKey("rule_key", "6.5") .setRequired(true) .setExampleValue("squid:AvoidCycles"); } @Override public void handle(Request request, Response response) throws Exception { - RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE_KEY)); - String qualityProfileKey = request.mandatoryParam(PARAM_PROFILE_KEY); + RuleKey ruleKey = RuleKey.parse(request.mandatoryParam(PARAM_RULE)); + String qualityProfileKey = request.mandatoryParam(PARAM_PROFILE); userSession.checkLoggedIn(); try (DbSession dbSession = dbClient.openSession(false)) { QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(qualityProfileKey)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/DeactivateRuleActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/DeactivateRuleActionTest.java index aea31bf4349..9ea0717b041 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/DeactivateRuleActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/DeactivateRuleActionTest.java @@ -25,7 +25,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; import org.sonar.api.rule.RuleKey; import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; @@ -49,7 +48,10 @@ 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 DeactivateRuleActionTest { @Rule @@ -57,7 +59,7 @@ public class DeactivateRuleActionTest { @Rule public UserSessionRule userSession = UserSessionRule.standalone(); @Rule - public ExpectedException thrown = ExpectedException.none(); + public ExpectedException expectedException = ExpectedException.none(); private DbClient dbClient = db.getDbClient(); private RuleActivator ruleActivator = mock(RuleActivator.class); @@ -78,17 +80,22 @@ public class DeactivateRuleActionTest { WebService.Action definition = wsActionTester.getDef(); assertThat(definition).isNotNull(); assertThat(definition.isPost()).isTrue(); - assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("profile_key", "rule_key"); + assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("profile", "rule"); + 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() .setMethod("POST") - .setParam("rule_key", RuleTesting.newRuleDto().getKey().toString()) - .setParam("profile_key", randomAlphanumeric(UUID_SIZE)); + .setParam(PARAM_RULE, RuleTesting.newRuleDto().getKey().toString()) + .setParam(PARAM_PROFILE, randomAlphanumeric(UUID_SIZE)); + + expectedException.expect(UnauthorizedException.class); - thrown.expect(UnauthorizedException.class); request.execute(); } @@ -98,10 +105,11 @@ public class DeactivateRuleActionTest { QProfileDto qualityProfile = db.qualityProfiles().insert(organization); TestRequest request = wsActionTester.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); - thrown.expect(ForbiddenException.class); request.execute(); } @@ -112,10 +120,10 @@ public class DeactivateRuleActionTest { QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true)); TestRequest request = wsActionTester.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()); - thrown.expect(BadRequestException.class); + expectedException.expect(BadRequestException.class); request.execute(); } @@ -127,15 +135,15 @@ public class DeactivateRuleActionTest { RuleKey ruleKey = RuleTesting.randomRuleKey(); TestRequest request = wsActionTester.newRequest() .setMethod("POST") - .setParam("rule_key", ruleKey.toString()) - .setParam("profile_key", qualityProfile.getKee()); + .setParam(PARAM_RULE, ruleKey.toString()) + .setParam(PARAM_PROFILE, qualityProfile.getKee()); TestResponse response = request.execute(); assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT); ArgumentCaptor<RuleKey> ruleKeyCaptor = ArgumentCaptor.forClass(RuleKey.class); ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class); - Mockito.verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), ruleKeyCaptor.capture()); + verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), ruleKeyCaptor.capture()); assertThat(ruleKeyCaptor.getValue()).isEqualTo(ruleKey); assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee()); } @@ -148,15 +156,15 @@ public class DeactivateRuleActionTest { TestRequest request = wsActionTester.newRequest() .setMethod("POST") .setParam("organization", organization.getKey()) - .setParam("rule_key", ruleKey.toString()) - .setParam("profile_key", qualityProfile.getKee()); + .setParam(PARAM_RULE, ruleKey.toString()) + .setParam(PARAM_PROFILE, qualityProfile.getKee()); TestResponse response = request.execute(); assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT); ArgumentCaptor<RuleKey> captor = ArgumentCaptor.forClass(RuleKey.class); ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class); - Mockito.verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), captor.capture()); + verify(ruleActivator).deactivateAndUpdateIndex(any(DbSession.class), qProfileDtoCaptor.capture(), captor.capture()); assertThat(captor.getValue()).isEqualTo(ruleKey); assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee()); } 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 5e0566d8504..3e5bc43204f 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 @@ -75,29 +75,30 @@ public class QProfilesWsMediumTest { public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester) .logIn().setRoot(); - private DbClient db; - private DbSession session; - private WsTester wsTester; + private DbClient dbClient; + private DbSession dbSession; private RuleIndexer ruleIndexer = tester.get(RuleIndexer.class); private ActiveRuleIndexer activeRuleIndexer = tester.get(ActiveRuleIndexer.class); private OrganizationDto organization; + private WsTester ws; + @Before public void setUp() { tester.clearDbAndIndexes(); - db = tester.get(DbClient.class); - wsTester = tester.get(WsTester.class); - session = db.openSession(false); + dbClient = tester.get(DbClient.class); + ws = tester.get(WsTester.class); + dbSession = dbClient.openSession(false); ruleIndexer = tester.get(RuleIndexer.class); activeRuleIndexer = tester.get(ActiveRuleIndexer.class); organization = OrganizationTesting.newOrganizationDto().setKey("org-123"); - db.organizationDao().insert(session, organization, false); + dbClient.organizationDao().insert(dbSession, organization, false); } @After public void after() { - session.close(); + dbSession.close(); } @Test @@ -105,22 +106,22 @@ public class QProfilesWsMediumTest { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto"); createActiveRule(rule, profile); - session.commit(); + dbSession.commit(); ruleIndexer.indexRuleDefinition(rule.getKey()); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); // 1. Deactivate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_DEACTIVATE_RULE); - request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); - request.setParam(ActivateActionParameters.PARAM_RULE_KEY, rule.getKey().toString()); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_DEACTIVATE_RULE); + request.setParam(PARAM_PROFILE, profile.getKee()); + request.setParam(PARAM_RULE, rule.getKey().toString()); request.execute(); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); } @Test @@ -134,20 +135,20 @@ public class QProfilesWsMediumTest { createActiveRule(rule2, profile); createActiveRule(rule3, profile); createActiveRule(rule1, profile); - session.commit(); + dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(4); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(4); // 1. Deactivate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); WsTester.Result result = request.execute(); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); } @Test @@ -160,21 +161,21 @@ public class QProfilesWsMediumTest { createActiveRule(rule1, profile); createActiveRule(rule0, php); createActiveRule(rule1, php); - session.commit(); + dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(2); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(2); // 1. Deactivate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); WsTester.Result result = request.execute(); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(0); - assertThat(db.activeRuleDao().selectByProfileUuid(session, php.getKee())).hasSize(2); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(0); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).hasSize(2); } @Test @@ -184,61 +185,61 @@ public class QProfilesWsMediumTest { RuleDefinitionDto rule1 = createRule(profile.getLanguage(), "world"); createActiveRule(rule0, profile); createActiveRule(rule1, profile); - session.commit(); + dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(2); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(2); // 1. Deactivate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, DeactivateRulesAction.DEACTIVATE_RULES_ACTION); request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); request.setParam(Param.TEXT_QUERY, "hello"); WsTester.Result result = request.execute(); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); } @Test public void activate_rule() throws Exception { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto"); - session.commit(); + dbSession.commit(); ruleIndexer.indexRuleDefinition(rule.getKey()); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); request.setParam(PARAM_PROFILE, profile.getKee()); request.setParam(PARAM_RULE, rule.getKey().toString()); WsTester.Result result = request.execute(); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); } @Test public void activate_rule_diff_languages() throws Exception { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule("php", "toto"); - session.commit(); + dbSession.commit(); ruleIndexer.indexRuleDefinition(rule.getKey()); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); try { // 1. Activate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); request.setParam(PARAM_PROFILE, profile.getKee()); request.setParam(PARAM_RULE, rule.getKey().toString()); request.execute(); - session.clearCache(); + dbSession.clearCache(); fail(); } catch (BadRequestException e) { assertThat(e.getMessage()).isEqualTo("Rule blah:toto and profile pjava have different languages"); @@ -249,24 +250,24 @@ public class QProfilesWsMediumTest { public void activate_rule_override_severity() throws Exception { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule = createRule(profile.getLanguage(), "toto"); - session.commit(); + dbSession.commit(); ruleIndexer.indexRuleDefinition(rule.getKey()); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ACTION_ACTIVATE_RULE); 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(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile, rule.getKey()); - Optional<ActiveRuleDto> activeRuleDto = db.activeRuleDao().selectByKey(session, activeRuleKey); + Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, activeRuleKey); assertThat(activeRuleDto.isPresent()).isTrue(); assertThat(activeRuleDto.get().getSeverityString()).isEqualTo(Severity.MINOR); } @@ -278,20 +279,20 @@ public class QProfilesWsMediumTest { createRule(profile.getLanguage(), "tata"); createRule(profile.getLanguage(), "hello"); createRule(profile.getLanguage(), "world"); - session.commit(); + dbSession.commit(); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); request.setParam(PARAM_LANGUAGES, "java"); request.execute().assertJson(getClass(), "bulk_activate_rule.json"); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(4); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(4); } @Test @@ -302,20 +303,20 @@ public class QProfilesWsMediumTest { createRule(java.getLanguage(), "tata"); createRule(php.getLanguage(), "hello"); createRule(php.getLanguage(), "world"); - session.commit(); + dbSession.commit(); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, php.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).isEmpty(); // 1. Activate Rule - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); 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(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, php.getKee())).hasSize(2); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, php.getKee())).hasSize(2); } @Test @@ -325,30 +326,30 @@ public class QProfilesWsMediumTest { createRule(profile.getLanguage(), "tata"); createRule(profile.getLanguage(), "hello"); createRule(profile.getLanguage(), "world"); - session.commit(); + dbSession.commit(); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 1. Activate Rule with query returning 0 hits - WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); request.setParam(Param.TEXT_QUERY, "php"); request.execute(); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(0); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(0); // 1. Activate Rule with query returning 1 hits - request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + request = ws.newPostRequest(QProfilesWs.API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); request.setParam(ActivateActionParameters.PARAM_PROFILE_KEY, profile.getKee()); request.setParam(Param.TEXT_QUERY, "world"); request.execute(); - session.commit(); + dbSession.commit(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).hasSize(1); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).hasSize(1); } @Test @@ -356,10 +357,10 @@ public class QProfilesWsMediumTest { QProfileDto profile = createProfile("java"); RuleDefinitionDto rule0 = createRule(profile.getLanguage(), "toto"); RuleDefinitionDto rule1 = createRule(profile.getLanguage(), "tata"); - session.commit(); + dbSession.commit(); // 0. Assert No Active Rule for profile - assertThat(db.activeRuleDao().selectByProfileUuid(session, profile.getKee())).isEmpty(); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, profile.getKee())).isEmpty(); // 2. Assert ActiveRule with BLOCKER severity assertThat(tester.get(RuleIndex.class).search( @@ -367,14 +368,14 @@ public class QProfilesWsMediumTest { new SearchOptions()).getIds()).hasSize(2); // 1. Activate Rule with query returning 2 hits - WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); request.setParam(ActivateRulesAction.PROFILE_KEY, profile.getKee()); request.setParam(ActivateRulesAction.SEVERITY, "MINOR"); request.execute(); - session.commit(); + dbSession.commit(); // 2. Assert ActiveRule with MINOR severity - assertThat(tester.get(ActiveRuleDao.class).selectByRuleId(session, organization, rule0.getId()).get(0).getSeverityString()).isEqualTo("MINOR"); + assertThat(tester.get(ActiveRuleDao.class).selectByRuleId(dbSession, organization, rule0.getId()).get(0).getSeverityString()).isEqualTo("MINOR"); assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery() .setQProfile(profile) .setKey(rule0.getKey().toString()) @@ -390,59 +391,59 @@ public class QProfilesWsMediumTest { QProfileDto phpProfile = createProfile("php"); createRule(phpProfile.getLanguage(), "hello"); createRule(phpProfile.getLanguage(), "world"); - session.commit(); + dbSession.commit(); // 1. Activate Rule - WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ActivateRulesAction.ACTIVATE_RULES_ACTION); + WsTester.TestRequest request = ws.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"); - session.clearCache(); + dbSession.clearCache(); // 2. Assert ActiveRule in DAO - assertThat(db.activeRuleDao().selectByProfileUuid(session, javaProfile.getKee())).hasSize(2); + assertThat(dbClient.activeRuleDao().selectByProfileUuid(dbSession, javaProfile.getKee())).hasSize(2); } @Test public void reset() throws Exception { QProfileDto profile = QProfileTesting.newXooP1(organization); QProfileDto subProfile = QProfileTesting.newXooP2(organization).setParentKee(QProfileTesting.XOO_P1_KEY); - db.qualityProfileDao().insert(session, profile, subProfile); + dbClient.qualityProfileDao().insert(dbSession, profile, subProfile); RuleDefinitionDto rule = createRule(profile.getLanguage(), "rule"); ActiveRuleDto active1 = ActiveRuleDto.createFor(profile, rule) .setSeverity(rule.getSeverityString()); ActiveRuleDto active2 = ActiveRuleDto.createFor(subProfile, rule) .setSeverity("MINOR"); - db.activeRuleDao().insert(session, active1); - db.activeRuleDao().insert(session, active2); + dbClient.activeRuleDao().insert(dbSession, active1); + dbClient.activeRuleDao().insert(dbSession, active2); - session.commit(); + dbSession.commit(); activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); // 0. assert rule child rule is minor - Optional<ActiveRuleDto> activeRuleDto = db.activeRuleDao().selectByKey(session, active2.getKey()); + Optional<ActiveRuleDto> activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, active2.getKey()); assertThat(activeRuleDto.isPresent()).isTrue(); assertThat(activeRuleDto.get().getSeverityString()).isEqualTo(Severity.MINOR); // 1. reset child rule - WsTester.TestRequest request = wsTester.newPostRequest(API_ENDPOINT, ACTION_ACTIVATE_RULE); + WsTester.TestRequest request = ws.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(); + dbSession.clearCache(); // 2. assert rule child rule is NOT minor - activeRuleDto = db.activeRuleDao().selectByKey(session, active2.getKey()); + activeRuleDto = dbClient.activeRuleDao().selectByKey(dbSession, active2.getKey()); assertThat(activeRuleDto.isPresent()).isTrue(); assertThat(activeRuleDto.get().getSeverityString()).isNotEqualTo(Severity.MINOR); } private QProfileDto createProfile(String lang) { QProfileDto profile = QProfileTesting.newQProfileDto(organization, new QProfileName(lang, "P" + lang), "p" + lang); - db.qualityProfileDao().insert(session, profile); + dbClient.qualityProfileDao().insert(dbSession, profile); return profile; } @@ -451,8 +452,8 @@ public class QProfilesWsMediumTest { .setLanguage(lang) .setSeverity(Severity.BLOCKER) .setStatus(RuleStatus.READY); - db.ruleDao().insert(session, rule); - session.commit(); + dbClient.ruleDao().insert(dbSession, rule); + dbSession.commit(); ruleIndexer.indexRuleDefinition(rule.getKey()); return rule; } @@ -460,7 +461,7 @@ public class QProfilesWsMediumTest { private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) { ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule) .setSeverity(rule.getSeverityString()); - db.activeRuleDao().insert(session, activeRule); + dbClient.activeRuleDao().insert(dbSession, activeRule); return activeRule; } } |