]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 Rule was not re-indexed in ES when beeing deleted
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 16 Mar 2016 12:31:51 +0000 (13:31 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 16 Mar 2016 12:31:51 +0000 (13:31 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java
server/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java

index 48afa7f3f1125b89b41a6284b42dabb609455e4b..e96d2db66daa3f33eb91382c77c6540af0e6a901 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.rule;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.rule.RuleDto;
@@ -32,11 +33,13 @@ import org.sonar.server.rule.index.RuleIndexer;
 @ServerSide
 public class RuleDeleter {
 
+  private final System2 system2;
   private final RuleIndexer ruleIndexer;
   private final DbClient dbClient;
   private final RuleActivator ruleActivator;
 
-  public RuleDeleter(RuleIndexer ruleIndexer, DbClient dbClient, RuleActivator ruleActivator) {
+  public RuleDeleter(System2 system2, RuleIndexer ruleIndexer, DbClient dbClient, RuleActivator ruleActivator) {
+    this.system2 = system2;
     this.ruleIndexer = ruleIndexer;
     this.dbClient = dbClient;
     this.ruleActivator = ruleActivator;
@@ -56,10 +59,11 @@ public class RuleDeleter {
       }
 
       rule.setStatus(RuleStatus.REMOVED);
+      rule.setUpdatedAt(system2.now());
       dbClient.ruleDao().update(dbSession, rule);
 
       dbSession.commit();
-      ruleIndexer.setEnabled(true).index();
+      ruleIndexer.index();
 
     } finally {
       dbSession.close();
index e72bf59befc1237008e6f638a68e0ca3d220a05a..7bdff24f5933c5a2bc143cd422b77f552cb6de23 100644 (file)
@@ -40,7 +40,9 @@ import org.sonar.server.qualityprofile.RuleActivation;
 import org.sonar.server.qualityprofile.RuleActivator;
 import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
@@ -50,6 +52,8 @@ import static org.assertj.core.api.Assertions.assertThat;
 // TODO replace ServerTester by EsTester / DbTester
 public class RuleDeleterMediumTest {
 
+  static final long PAST = 10000L;
+
   @ClassRule
   public static ServerTester tester = new ServerTester().withEsIndexes();
 
@@ -59,12 +63,16 @@ public class RuleDeleterMediumTest {
   DbClient db = tester.get(DbClient.class);
   RuleDao dao = tester.get(RuleDao.class);
   RuleIndex index = tester.get(RuleIndex.class);
+  RuleIndexer ruleIndexer = tester.get(RuleIndexer.class);
+  ActiveRuleIndexer activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
   RuleDeleter deleter = tester.get(RuleDeleter.class);
   DbSession dbSession = tester.get(DbClient.class).openSession(false);
 
   @Before
   public void before() {
     tester.clearDbAndIndexes();
+    ruleIndexer.setEnabled(true);
+    activeRuleIndexer.setEnabled(true);
   }
 
   @After
@@ -75,11 +83,17 @@ public class RuleDeleterMediumTest {
   @Test
   public void delete_custom_rule() {
     // Create template rule
-    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1")).setLanguage("xoo");
+    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("xoo", "T1"))
+      .setLanguage("xoo")
+      .setCreatedAt(PAST)
+      .setUpdatedAt(PAST);
     dao.insert(dbSession, templateRule);
 
     // Create custom rule
-    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setLanguage("xoo");
+    RuleDto customRule = RuleTesting.newCustomRule(templateRule)
+      .setLanguage("xoo")
+      .setCreatedAt(PAST)
+      .setUpdatedAt(PAST);
     dao.insert(dbSession, customRule);
 
     // Create a quality profile
@@ -87,6 +101,8 @@ public class RuleDeleterMediumTest {
     db.qualityProfileDao().insert(dbSession, profileDto);
     dbSession.commit();
     dbSession.clearCache();
+    ruleIndexer.index();
+    activeRuleIndexer.index();
 
     // Activate the custom rule
     activate(new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER), QProfileTesting.XOO_P1_KEY);
@@ -98,6 +114,7 @@ public class RuleDeleterMediumTest {
     RuleDto customRuleReloaded = dao.selectOrFailByKey(dbSession, customRule.getKey());
     assertThat(customRuleReloaded).isNotNull();
     assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.REMOVED);
+    assertThat(customRuleReloaded.getUpdatedAt()).isNotEqualTo(PAST);
 
     // Verify there's no more active rule from custom rule
     List<ActiveRuleDoc> activeRules = Lists.newArrayList(tester.get(ActiveRuleIndex.class).findByProfile(profileDto.getKey()));
@@ -110,10 +127,12 @@ public class RuleDeleterMediumTest {
   @Test
   public void delete_manual_rule() {
     // Create manual rule
-    RuleDto manualRule = RuleTesting.newManualRule("Manual_Rule");
+    RuleDto manualRule = RuleTesting.newManualRule("Manual_Rule")
+      .setCreatedAt(PAST)
+      .setUpdatedAt(PAST);
     dao.insert(dbSession, manualRule);
-
     dbSession.commit();
+    ruleIndexer.index();
 
     // Delete manual rule
     deleter.delete(manualRule.getKey());
@@ -122,6 +141,7 @@ public class RuleDeleterMediumTest {
     RuleDto result = dao.selectOrFailByKey(dbSession, manualRule.getKey());
     assertThat(result).isNotNull();
     assertThat(result.getStatus()).isEqualTo(RuleStatus.REMOVED);
+    assertThat(result.getUpdatedAt()).isNotEqualTo(PAST);
 
     // Verify in index
     assertThat(index.search(new RuleQuery(), new SearchOptions()).getIds()).isEmpty();