From: Julien Lancelot Date: Mon, 22 Feb 2016 11:38:05 +0000 (+0100) Subject: SONAR-7330 RuleDeleter is now using RuleIndexer X-Git-Tag: 5.5-M6~86 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b9929d2d04cc950e68894fe85c39847e3bc813b5;p=sonarqube.git SONAR-7330 RuleDeleter is now using RuleIndexer --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java index 61f1250b031..48afa7f3f11 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java @@ -19,22 +19,25 @@ */ package org.sonar.server.rule; -import org.sonar.api.server.ServerSide; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; +import org.sonar.api.server.ServerSide; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.rule.RuleDto; -import org.sonar.server.db.DbClient; import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.rule.index.RuleDoc; +import org.sonar.server.rule.index.RuleIndexer; @ServerSide public class RuleDeleter { + private final RuleIndexer ruleIndexer; private final DbClient dbClient; private final RuleActivator ruleActivator; - public RuleDeleter(DbClient dbClient, RuleActivator ruleActivator) { + public RuleDeleter(RuleIndexer ruleIndexer, DbClient dbClient, RuleActivator ruleActivator) { + this.ruleIndexer = ruleIndexer; this.dbClient = dbClient; this.ruleActivator = ruleActivator; } @@ -42,7 +45,7 @@ public class RuleDeleter { public void delete(RuleKey ruleKey) { DbSession dbSession = dbClient.openSession(false); try { - RuleDto rule = dbClient.deprecatedRuleDao().getByKey(dbSession, ruleKey); + RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbSession, ruleKey); if (rule.getTemplateId() == null && !rule.getRepositoryKey().equals(RuleDoc.MANUAL_REPOSITORY)) { throw new IllegalStateException("Only custom rules and manual rules can be deleted"); } @@ -53,9 +56,11 @@ public class RuleDeleter { } rule.setStatus(RuleStatus.REMOVED); - dbClient.deprecatedRuleDao().update(dbSession, rule); + dbClient.ruleDao().update(dbSession, rule); dbSession.commit(); + ruleIndexer.setEnabled(true).index(); + } finally { dbSession.close(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java index 258bc4a94a6..629dff2646b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java @@ -20,6 +20,7 @@ package org.sonar.server.rule; import com.google.common.collect.Lists; +import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -27,26 +28,26 @@ 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.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualityprofile.QualityProfileDto; +import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleTesting; -import org.sonar.server.db.DbClient; +import org.sonar.server.es.SearchOptions; import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.qualityprofile.QProfileTesting; import org.sonar.server.qualityprofile.RuleActivation; import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.index.ActiveRuleIndex; -import org.sonar.server.rule.db.RuleDao; -import org.sonar.server.rule.index.RuleIndex; -import org.sonar.server.search.BaseIndex; +import org.sonar.server.rule.index.RuleIndex2; +import org.sonar.server.rule.index.RuleQuery; import org.sonar.server.tester.ServerTester; - -import java.util.List; import org.sonar.server.tester.UserSessionRule; import static org.assertj.core.api.Assertions.assertThat; +// TODO replace ServerTester by EsTester / DbTester public class RuleDeleterMediumTest { @ClassRule @@ -56,14 +57,13 @@ public class RuleDeleterMediumTest { DbClient db = tester.get(DbClient.class); RuleDao dao = tester.get(RuleDao.class); - BaseIndex index = tester.get(RuleIndex.class); + RuleIndex2 index = tester.get(RuleIndex2.class); RuleDeleter deleter = tester.get(RuleDeleter.class); - DbSession dbSession; + DbSession dbSession = tester.get(DbClient.class).openSession(false); @Before public void before() { tester.clearDbAndIndexes(); - dbSession = tester.get(DbClient.class).openSession(false); } @After @@ -94,13 +94,16 @@ public class RuleDeleterMediumTest { deleter.delete(customRule.getKey()); // Verify custom rule have status REMOVED - Rule customRuleReloaded = index.getByKey(customRule.getKey()); + RuleDto customRuleReloaded = dao.selectOrFailByKey(dbSession, customRule.getKey()); assertThat(customRuleReloaded).isNotNull(); - assertThat(customRuleReloaded.status()).isEqualTo(RuleStatus.REMOVED); + assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.REMOVED); // Verify there's no more active rule from custom rule List activeRules = Lists.newArrayList(tester.get(ActiveRuleIndex.class).findByProfile(profileDto.getKey())); assertThat(activeRules).isEmpty(); + + // Verify in index + assertThat(index.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(templateRule.getKey()); } @Test @@ -115,9 +118,12 @@ public class RuleDeleterMediumTest { deleter.delete(manualRule.getKey()); // Verify custom rule have status REMOVED - Rule result = index.getByKey(manualRule.getKey()); + RuleDto result = dao.selectOrFailByKey(dbSession, manualRule.getKey()); assertThat(result).isNotNull(); - assertThat(result.status()).isEqualTo(RuleStatus.REMOVED); + assertThat(result.getStatus()).isEqualTo(RuleStatus.REMOVED); + + // Verify in index + assertThat(index.search(new RuleQuery(), new SearchOptions()).getIds()).isEmpty(); } @Test