From 71e261c73e1487a06ed723415df692b02fd244a0 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Tue, 27 May 2014 13:27:07 +0200 Subject: [PATCH] SONAR-5246 - Added medium tests for Deactivation WS --- .../qualityprofile/ActiveRuleService.java | 6 +- .../ws/BulkRuleActivationActions.java | 1 + .../ws/QProfilesWsMediumTest.java | 109 ++++++++++++++++-- 3 files changed, 107 insertions(+), 9 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java index cdcf5bd6290..5ef3c403eeb 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleService.java @@ -242,7 +242,9 @@ public class ActiveRuleService implements ServerComponent { try { RuleResult result = ruleIndex.search(ruleQuery, QueryOptions.DEFAULT.setOffset(0) - .setLimit(Integer.MAX_VALUE)); + .setLimit(Integer.MAX_VALUE) + .setFieldsToReturn(ImmutableSet.of("template","severity")) + ); for (Rule rule : result.getHits()) { if(!rule.template()) { @@ -273,7 +275,7 @@ public class ActiveRuleService implements ServerComponent { RuleResult result = ruleIndex.search(ruleQuery, QueryOptions.DEFAULT.setOffset(0) .setLimit(Integer.MAX_VALUE) - .setFieldsToReturn(ImmutableSet.of("")) + .setFieldsToReturn(ImmutableSet.of("template","severity")) ); for (Rule rule : result.getHits()) { diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java index 3ddbd4eaab0..5583074931a 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/BulkRuleActivationActions.java @@ -87,6 +87,7 @@ public class BulkRuleActivationActions implements ServerComponent { } }); + SearchAction.defineRuleSearchParameters(deactivate); defineProfileKeyParameters(deactivate); } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java index 3ca39f875a5..6a8ff33c16d 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java @@ -97,6 +97,101 @@ public class QProfilesWsMediumTest { assertThat(controller.action(API_BUILT_IN_METHOD)).isNotNull(); } + @Test + public void deactivate_rule() throws Exception { + QualityProfileDto profile = getProfile("java"); + RuleDto rule = getRule(profile.getLanguage(), "toto"); + ActiveRuleDto activeRule = getActiveRule(rule, profile); + session.commit(); + + // 0. Assert No Active Rule for profile + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(1); + + // 1. Deactivate Rule + WsTester.TestRequest request = wsTester.newGetRequest(API_ENDPOINT, API_RULE_DEACTIVATE_METHOD); + request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); + request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString()); + WsTester.Result result = request.execute(); + session.clearCache(); + + // 2. Assert ActiveRule in DAO + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).isEmpty(); + } + + @Test + public void bulk_deactivate_rule() throws Exception { + QualityProfileDto profile = getProfile("java"); + RuleDto rule0 = getRule(profile.getLanguage(), "toto1"); + RuleDto rule1 = getRule(profile.getLanguage(), "toto2"); + RuleDto rule2 = getRule(profile.getLanguage(), "toto3"); + RuleDto rule3 = getRule(profile.getLanguage(), "toto4"); + getActiveRule(rule0, profile); + getActiveRule(rule2, profile); + getActiveRule(rule3, profile); + getActiveRule(rule1, profile); + session.commit(); + + // 0. Assert No Active Rule for profile + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(4); + + // 1. Deactivate Rule + WsTester.TestRequest request = wsTester.newGetRequest(API_ENDPOINT, API_BULK_RULE_DEACTIVATE_METHOD); + request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); + WsTester.Result result = request.execute(); + session.clearCache(); + + // 2. Assert ActiveRule in DAO + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).isEmpty(); + } + + @Test + public void bulk_deactivate_rule_not_all() throws Exception { + QualityProfileDto profile = getProfile("java"); + QualityProfileDto php = getProfile("php"); + RuleDto rule0 = getRule(profile.getLanguage(), "toto1"); + RuleDto rule1 = getRule(profile.getLanguage(), "toto2"); + getActiveRule(rule0, profile); + getActiveRule(rule1, profile); + getActiveRule(rule0, php); + getActiveRule(rule1, php); + session.commit(); + + // 0. Assert No Active Rule for profile + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(2); + + // 1. Deactivate Rule + WsTester.TestRequest request = wsTester.newGetRequest(API_ENDPOINT, API_BULK_RULE_DEACTIVATE_METHOD); + request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); + WsTester.Result result = request.execute(); + session.clearCache(); + + // 2. Assert ActiveRule in DAO + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(0); + assertThat(dbClient.activeRuleDao().findByProfileKey(php.getKey(), session)).hasSize(2); + } + + @Test + public void bulk_deactivate_rule_by_profile() throws Exception { + QualityProfileDto profile = getProfile("java"); + RuleDto rule0 = getRule(profile.getLanguage(), "hello"); + RuleDto rule1 = getRule(profile.getLanguage(), "world"); + getActiveRule(rule0, profile); + getActiveRule(rule1, profile);; + session.commit(); + + // 0. Assert No Active Rule for profile + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(2); + + // 1. Deactivate Rule + WsTester.TestRequest request = wsTester.newGetRequest(API_ENDPOINT, API_BULK_RULE_DEACTIVATE_METHOD); + request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); + request.setParam(SearchOptions.PARAM_TEXT_QUERY, "hello"); + WsTester.Result result = request.execute(); + session.clearCache(); + + // 2. Assert ActiveRule in DAO + assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(1); + } @Test public void activate_rule() throws Exception { @@ -112,7 +207,7 @@ public class QProfilesWsMediumTest { request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString()); WsTester.Result result = request.execute(); - session.commit(); + session.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(1); @@ -133,7 +228,7 @@ public class QProfilesWsMediumTest { request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString()); WsTester.Result result = request.execute(); - session.commit(); + session.clearCache(); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).isEqualTo("Rule blah:toto and profile test:java have different languages"); @@ -156,7 +251,7 @@ public class QProfilesWsMediumTest { request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString()); request.setParam(RuleActivationActions.SEVERITY, "MINOR"); WsTester.Result result = request.execute(); - session.commit(); + session.clearCache(); // 2. Assert ActiveRule in DAO ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), rule.getKey()); @@ -182,7 +277,7 @@ public class QProfilesWsMediumTest { request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); request.setParam(SearchAction.PARAM_LANGUAGES, "java"); WsTester.Result result = request.execute(); - session.commit(); + session.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(4); @@ -204,7 +299,7 @@ public class QProfilesWsMediumTest { request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); request.setParam(SearchAction.PARAM_LANGUAGES, "java"); WsTester.Result result = request.execute(); - session.commit(); + session.clearCache(); // 2. assert replied ignored list result.assertJson("{\"ignored\":[{\"key\":\"blah:toto\"}],\"activated\":[{\"key\":\"blah:tata\"}]}"); @@ -231,7 +326,7 @@ public class QProfilesWsMediumTest { request.setParam(RuleActivationActions.PROFILE_KEY, php.getKey().toString()); request.setParam(SearchAction.PARAM_LANGUAGES, "php"); WsTester.Result result = request.execute(); - session.commit(); + session.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().findByProfileKey(php.getKey(), session)).hasSize(2); @@ -254,7 +349,7 @@ public class QProfilesWsMediumTest { request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey().toString()); request.setParam(SearchOptions.PARAM_TEXT_QUERY, "php"); WsTester.Result result = request.execute(); - session.commit(); + session.clearCache(); // 2. Assert ActiveRule in DAO assertThat(dbClient.activeRuleDao().findByProfileKey(profile.getKey(), session)).hasSize(0); -- 2.39.5