From 40051708d3a157674243f7f80721433075db66ea Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Wed, 21 May 2014 12:23:40 +0200 Subject: [PATCH] SONAR-5238 - Added ActivRuleService#findByRuleKey() method --- .../qualityprofile/ActiveRuleService.java | 11 +++++- .../ActiveRuleServiceMediumTest.java | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) 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 a32aee5d980..b1921e419da 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 @@ -23,6 +23,7 @@ import com.google.common.base.Splitter; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; +import org.sonar.api.rule.RuleKey; import org.sonar.api.server.rule.RuleParamType; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.persistence.DbSession; @@ -32,7 +33,9 @@ import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.server.db.DbClient; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex; import org.sonar.server.qualityprofile.persistence.ActiveRuleDao; +import org.sonar.server.search.IndexClient; import org.sonar.server.user.UserSession; import org.sonar.server.util.TypeValidations; @@ -49,11 +52,13 @@ public class ActiveRuleService implements ServerComponent { private final TypeValidations typeValidations; private final RuleActivationContextFactory contextFactory; private final PreviewCache previewCache; + private final IndexClient index; - public ActiveRuleService(DbClient db, + public ActiveRuleService(DbClient db, IndexClient index, RuleActivationContextFactory contextFactory, TypeValidations typeValidations, PreviewCache previewCache) { this.db = db; + this.index = index; this.contextFactory = contextFactory; this.typeValidations = typeValidations; this.previewCache = previewCache; @@ -187,4 +192,8 @@ public class ActiveRuleService implements ServerComponent { } } } + + public List findByRuleKey(RuleKey ruleKey){ + return index.get(ActiveRuleIndex.class).findByRule(ruleKey); + } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleServiceMediumTest.java index 88a040594b5..e949da77da7 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleServiceMediumTest.java @@ -32,12 +32,16 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; +import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.qualityprofile.db.QualityProfileKey; +import org.sonar.core.rule.RuleDto; import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.qualityprofile.index.ActiveRuleIndex; +import org.sonar.server.qualityprofile.persistence.ActiveRuleDao; +import org.sonar.server.rule2.persistence.RuleDao; import org.sonar.server.tester.ServerTester; import org.sonar.server.user.MockUserSession; @@ -352,6 +356,41 @@ public class ActiveRuleServiceMediumTest { } } + @Test + public void find_by_ruleKey() throws Exception { + QualityProfileDto profile = QualityProfileDto.createFor("name","java"); + dbClient.getDao(QualityProfileDao.class).insert(profile, dbSession); + + RuleDto rule = RuleDto.createFor(RuleKey.of("java", "r1")) + .setSeverity("MAJOR"); + dbClient.getDao(RuleDao.class).insert(rule, dbSession); + + + RuleDto rule2 = RuleDto.createFor(RuleKey.of("java", "r2")) + .setSeverity("MAJOR"); + dbClient.getDao(RuleDao.class).insert(rule2, dbSession); + + dbClient.getDao(ActiveRuleDao.class).insert( + ActiveRuleDto.createFor(profile, rule).setSeverity("MINOR"), + dbSession); + + dbClient.getDao(ActiveRuleDao.class).insert( + ActiveRuleDto.createFor(profile, rule2).setSeverity("BLOCKER"), + dbSession); + + dbSession.commit(); + + assertThat(service.findByRuleKey(rule.getKey())).hasSize(1); + assertThat(service.findByRuleKey(rule.getKey()).get(0) + .severity()).isEqualTo("MINOR"); + + assertThat(service.findByRuleKey(rule2.getKey())).hasSize(1); + assertThat(service.findByRuleKey(rule2.getKey()).get(0) + .severity()).isEqualTo("BLOCKER"); + + + } + private void grantPermission() { MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("marius"); } -- 2.39.5