From 0e4c8a968c38395d18825bcd19dc3d894661f3b6 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 25 Feb 2016 11:17:59 +0100 Subject: [PATCH] SONAR-7330 QProfileService is now using ActiveRuleIndexer --- .../qualityprofile/QProfileService.java | 23 ++++++++++--- .../QProfileServiceMediumTest.java | 32 +++++++++++++------ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java index 9a218b71944..dccab17f092 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java @@ -19,6 +19,7 @@ */ package org.sonar.server.qualityprofile; +import com.google.common.base.Optional; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; @@ -32,14 +33,15 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.search.SearchHit; import org.sonar.api.server.ServerSide; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.db.qualityprofile.QualityProfileDto; import org.sonar.db.rule.RuleDto; import org.sonar.db.user.UserDto; import org.sonar.server.activity.index.ActivityIndex; -import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; +import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.rule.index.RuleQuery; import org.sonar.server.search.Result; import org.sonar.server.user.UserSession; @@ -49,6 +51,7 @@ public class QProfileService { private final DbClient db; private final ActivityIndex activityIndex; + private final ActiveRuleIndexer activeRuleIndexer; private final RuleActivator ruleActivator; private final QProfileFactory factory; private final QProfileBackuper backuper; @@ -57,11 +60,12 @@ public class QProfileService { private final QProfileExporters exporters; private final UserSession userSession; - public QProfileService(DbClient db, ActivityIndex activityIndex, RuleActivator ruleActivator, QProfileFactory factory, + public QProfileService(DbClient db, ActivityIndex activityIndex, ActiveRuleIndexer activeRuleIndexer, RuleActivator ruleActivator, QProfileFactory factory, QProfileBackuper backuper, QProfileCopier copier, QProfileReset reset, QProfileExporters exporters, UserSession userSession) { this.db = db; this.activityIndex = activityIndex; + this.activeRuleIndexer = activeRuleIndexer; this.ruleActivator = ruleActivator; this.factory = factory; this.backuper = backuper; @@ -84,6 +88,7 @@ public class QProfileService { } } dbSession.commit(); + activeRuleIndexer.index(result.getChanges()); return result; } finally { dbSession.close(); @@ -100,6 +105,7 @@ public class QProfileService { try { List changes = ruleActivator.activate(dbSession, activation, profileKey); dbSession.commit(); + activeRuleIndexer.index(changes); return changes; } finally { dbSession.close(); @@ -172,7 +178,14 @@ public class QProfileService { public void delete(String key) { verifyAdminPermission(); - factory.delete(key); + DbSession session = db.openSession(false); + try { + List changes = factory.delete(session, key, false); + session.commit(); + activeRuleIndexer.index(changes); + } finally { + db.closeSession(session); + } } public void rename(String key, String newName) { @@ -219,8 +232,8 @@ public class QProfileService { Result result = new Result<>(response); for (SearchHit hit : response.getHits().getHits()) { QProfileActivity profileActivity = new QProfileActivity(hit.getSource()); - RuleDto ruleDto = db.deprecatedRuleDao().getNullableByKey(session, profileActivity.ruleKey()); - profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null); + Optional ruleDto = db.ruleDao().selectByKey(session, profileActivity.ruleKey()); + profileActivity.ruleName(ruleDto.isPresent() ? ruleDto.get().getName() : null); String login = profileActivity.getLogin(); if (login != null) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java index dbb70389642..63dddf763f2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java @@ -41,6 +41,7 @@ import org.sonar.api.rules.Rule; import org.sonar.api.rules.RulePriority; import org.sonar.api.utils.ValidationMessages; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.db.qualityprofile.QualityProfileDto; @@ -49,10 +50,11 @@ import org.sonar.db.rule.RuleTesting; import org.sonar.db.user.UserDto; import org.sonar.server.activity.Activity; import org.sonar.server.activity.ActivityService; -import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; import org.sonar.server.qualityprofile.index.ActiveRuleDoc; +import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer; +import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.search.FacetValue; import org.sonar.server.search.Result; import org.sonar.server.tester.ServerTester; @@ -66,6 +68,7 @@ public class QProfileServiceMediumTest { @ClassRule public static ServerTester tester = new ServerTester().withStartupTasks().withEsIndexes().addComponents(XooProfileImporter.class, XooExporter.class); + @org.junit.Rule public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); @@ -74,6 +77,8 @@ public class QProfileServiceMediumTest { QProfileService service; QProfileLoader loader; RuleActivator activator; + RuleIndexer ruleIndexer; + ActiveRuleIndexer activeRuleIndexer; @Before public void before() { @@ -83,15 +88,19 @@ public class QProfileServiceMediumTest { service = tester.get(QProfileService.class); loader = tester.get(QProfileLoader.class); activator = tester.get(RuleActivator.class); + ruleIndexer = tester.get(RuleIndexer.class); + activeRuleIndexer = tester.get(ActiveRuleIndexer.class); // create pre-defined rules RuleDto xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR"); - db.deprecatedRuleDao().insert(dbSession, xooRule1); + db.ruleDao().insert(dbSession, xooRule1); // create pre-defined profiles P1 and P2 db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2()); + dbSession.commit(); dbSession.clearCache(); + ruleIndexer.index(); } @After @@ -114,8 +123,9 @@ public class QProfileServiceMediumTest { public void create_profile_with_xml() { userSessionRule.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); - db.deprecatedRuleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "R1")).setLanguage("xoo").setSeverity("MINOR")); + db.ruleDao().insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "R1")).setLanguage("xoo").setSeverity("MINOR")); dbSession.commit(); + ruleIndexer.index(); QProfileResult result = service.create(QProfileName.createFor("xoo", "New Profile"), ImmutableMap.of("XooProfileImporter", "")); QualityProfileDto profile = result.profile(); @@ -134,8 +144,8 @@ public class QProfileServiceMediumTest { service.activate(XOO_P1_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("BLOCKER")); service.activate(XOO_P2_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("BLOCKER")); - dbSession.clearCache(); + activeRuleIndexer.index(); Map counts = loader.countAllActiveRules(); assertThat(counts).hasSize(2); @@ -150,6 +160,7 @@ public class QProfileServiceMediumTest { service.activate(XOO_P1_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("MINOR")); service.activate(XOO_P2_KEY, new RuleActivation(RuleTesting.XOO_X1).setSeverity("BLOCKER")); dbSession.clearCache(); + activeRuleIndexer.index(); Map> stats = loader.getAllProfileStats(); @@ -167,8 +178,9 @@ public class QProfileServiceMediumTest { // create deprecated rule RuleDto deprecatedXooRule = RuleTesting.newDto(RuleKey.of("xoo", "deprecated1")) .setSeverity("MINOR").setLanguage("xoo").setStatus(RuleStatus.DEPRECATED); - db.deprecatedRuleDao().insert(dbSession, deprecatedXooRule); + db.ruleDao().insert(dbSession, deprecatedXooRule); dbSession.commit(); + ruleIndexer.index(); // active some rules service.activate(XOO_P1_KEY, new RuleActivation(deprecatedXooRule.getKey()).setSeverity("BLOCKER")); @@ -184,11 +196,10 @@ public class QProfileServiceMediumTest { UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis()); db.userDao().insert(dbSession, user); + dbSession.commit(); // We need an actual rule in DB to test RuleName in Activity - RuleDto rule = db.deprecatedRuleDao().getByKey(dbSession, RuleTesting.XOO_X1); - - dbSession.commit(); + RuleDto rule = db.ruleDao().selectOrFailByKey(dbSession, RuleTesting.XOO_X1); tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)) .setSeverity(Severity.MAJOR) @@ -235,8 +246,9 @@ public class QProfileServiceMediumTest { userSessionRule.login("david").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); // We need an actual rule in DB to test RuleName in Activity - db.deprecatedRuleDao().getByKey(dbSession, RuleTesting.XOO_X1); - dbSession.commit(); + // TODO ??? + //db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1); + //dbSession.commit(); tester.get(ActivityService.class).save( ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)) -- 2.39.5