]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 QProfileFactory is now using ActiveRuleIndexer
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 25 Feb 2016 10:47:33 +0000 (11:47 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 29 Feb 2016 12:26:54 +0000 (13:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java

index ecb01928244028c7f796f97aa97d63773ff8c12c..f5e458a27771418a182dbfd3d2e806cbe141e634 100644 (file)
 package org.sonar.server.qualityprofile;
 
 import com.google.common.collect.Lists;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import javax.annotation.CheckForNull;
 import org.apache.commons.lang.RandomStringUtils;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.core.util.Slug;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.exceptions.Verifications;
 
+import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
+
 /**
  * Create, delete, rename and set as default profile.
  */
@@ -84,21 +88,11 @@ public class QProfileFactory {
 
   // ------------- DELETION
 
-  void delete(String key) {
-    DbSession session = db.openSession(false);
-    try {
-      delete(session, key, false);
-      session.commit();
-    } finally {
-      session.close();
-    }
-  }
-
   /**
    * Session is NOT committed. Profiles marked as "default" for a language can't be deleted,
    * except if the parameter <code>force</code> is true.
    */
-  public void delete(DbSession session, String key, boolean force) {
+  public List<ActiveRuleChange> delete(DbSession session, String key, boolean force) {
     QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(session, key);
     List<QualityProfileDto> descendants = db.qualityProfileDao().selectDescendants(session, key);
     if (!force) {
@@ -108,16 +102,23 @@ public class QProfileFactory {
       }
     }
     // delete bottom-up
+    List<ActiveRuleChange> changes = new ArrayList<>();
     for (QualityProfileDto descendant : Lists.reverse(descendants)) {
-      doDelete(session, descendant);
+      changes.addAll(doDelete(session, descendant));
     }
-    doDelete(session, profile);
+    changes.addAll(doDelete(session, profile));
+    return changes;
   }
 
-  private void doDelete(DbSession session, QualityProfileDto profile) {
+  private List<ActiveRuleChange> doDelete(DbSession session, QualityProfileDto profile) {
     db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session);
-    db.activeRuleDao().deleteByProfileKey(session, profile.getKey());
+    List<ActiveRuleChange> changes = new ArrayList<>();
+    for (ActiveRuleDto activeRule : db.activeRuleDao().selectByProfileKey(session, profile.getKey())) {
+      db.activeRuleDao().delete(session, activeRule.getKey());
+      changes.add(ActiveRuleChange.createFor(DEACTIVATED, activeRule.getKey()));
+    }
     db.qualityProfileDao().delete(session, profile);
+    return changes;
   }
 
   // ------------- DEFAULT PROFILE
index d94a6bd6abafa73b87821f9ea41819024b1f5afa..7567c3b52309627ab8f135a20a6e0edf6467eecf 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile;
 
+import java.util.List;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -27,6 +28,7 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.RowNotFoundException;
 import org.sonar.db.component.ComponentDto;
@@ -34,11 +36,11 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
 import org.sonar.db.rule.RuleTesting;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
-import org.sonar.server.search.IndexClient;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex2;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.tester.MockUserSession;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
@@ -52,7 +54,7 @@ import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P3_KEY;
 public class QProfileFactoryMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester();
+  public static ServerTester tester = new ServerTester().withEsIndexes();
   @Rule
   public ExpectedException thrown = ExpectedException.none();
   @Rule
@@ -60,7 +62,9 @@ public class QProfileFactoryMediumTest {
 
   DbClient db;
   DbSession dbSession;
-  IndexClient index;
+  ActiveRuleIndex2 activeRuleIndex;
+  ActiveRuleIndexer activeRuleIndexer;
+  RuleIndexer ruleIndexer;
   QProfileFactory factory;
 
   @Before
@@ -68,8 +72,12 @@ public class QProfileFactoryMediumTest {
     tester.clearDbAndIndexes();
     db = tester.get(DbClient.class);
     dbSession = db.openSession(false);
-    index = tester.get(IndexClient.class);
     factory = tester.get(QProfileFactory.class);
+    activeRuleIndex = tester.get(ActiveRuleIndex2.class);
+    activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
+    activeRuleIndexer.setEnabled(true);
+    ruleIndexer = tester.get(RuleIndexer.class);
+    ruleIndexer.setEnabled(true);
   }
 
   @After
@@ -208,13 +216,15 @@ public class QProfileFactoryMediumTest {
     dbSession.commit();
     dbSession.clearCache();
 
-    factory.delete(XOO_P1_KEY);
+    List<ActiveRuleChange> changes = factory.delete(dbSession, XOO_P1_KEY, false);
+    dbSession.commit();
+    activeRuleIndexer.index(changes);
 
     dbSession.clearCache();
     assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
-    assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).isEmpty();
-    assertThat(db.deprecatedActiveRuleDao().selectAllParams(dbSession)).isEmpty();
-    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty();
+    assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
+    assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
+    assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).isEmpty();
   }
 
   @Test
@@ -223,23 +233,27 @@ public class QProfileFactoryMediumTest {
 
     // create parent and child profiles
     db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3());
-    tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
-    tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY);
-    tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY);
+    List<ActiveRuleChange> changes = tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
+    changes.addAll(tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY));
+    changes.addAll(tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY));
     dbSession.commit();
     dbSession.clearCache();
+    activeRuleIndexer.index(changes);
+
     assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(3);
-    assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).hasSize(3);
+    assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(3);
 
-    factory.delete(XOO_P1_KEY);
+    changes = factory.delete(dbSession, XOO_P1_KEY, false);
+    dbSession.commit();
+    activeRuleIndexer.index(changes);
 
     dbSession.clearCache();
     assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty();
-    assertThat(db.deprecatedActiveRuleDao().selectAll(dbSession)).isEmpty();
-    assertThat(db.deprecatedActiveRuleDao().selectAllParams(dbSession)).isEmpty();
-    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty();
-    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P2_KEY)).isEmpty();
-    assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P3_KEY)).isEmpty();
+    assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty();
+    assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty();
+    assertThat(activeRuleIndex.findByProfile(XOO_P1_KEY)).isEmpty();
+    assertThat(activeRuleIndex.findByProfile(XOO_P2_KEY)).isEmpty();
+    assertThat(activeRuleIndex.findByProfile(XOO_P3_KEY)).isEmpty();
   }
 
   @Test
@@ -250,7 +264,9 @@ public class QProfileFactoryMediumTest {
     dbSession.clearCache();
 
     try {
-      factory.delete(XOO_P1_KEY);
+      List<ActiveRuleChange> changes = factory.delete(dbSession, XOO_P1_KEY, false);
+      dbSession.commit();
+      activeRuleIndexer.index(changes);
       fail();
     } catch (BadRequestException e) {
       assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P1");
@@ -261,14 +277,18 @@ public class QProfileFactoryMediumTest {
   @Test
   public void do_not_delete_if_default_descendant() {
     db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3());
-    tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
-    tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY);
+
+    List<ActiveRuleChange> changes = tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY);
+    changes.addAll(tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY));
     factory.setDefault(dbSession, XOO_P3_KEY);
     dbSession.commit();
     dbSession.clearCache();
+    activeRuleIndexer.index(changes);
 
     try {
-      factory.delete(XOO_P1_KEY);
+      changes = factory.delete(dbSession, XOO_P1_KEY, false);
+      dbSession.commit();
+      activeRuleIndexer.index(changes);
       fail();
     } catch (BadRequestException e) {
       assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P3");
@@ -281,7 +301,9 @@ public class QProfileFactoryMediumTest {
     thrown.expect(RowNotFoundException.class);
     thrown.expectMessage("Quality profile not found: XOO_P1");
 
-    factory.delete(XOO_P1_KEY);
+    List<ActiveRuleChange> changes = factory.delete(dbSession, XOO_P1_KEY, false);
+    dbSession.commit();
+    activeRuleIndexer.index(changes);
   }
 
   @Test
@@ -348,10 +370,12 @@ public class QProfileFactoryMediumTest {
     // create pre-defined rules
     RuleDto xooRule1 = RuleTesting.newXooX1();
     RuleDto xooRule2 = RuleTesting.newXooX2();
-    db.deprecatedRuleDao().insert(dbSession, xooRule1, xooRule2);
-    db.deprecatedRuleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
+    db.ruleDao().insert(dbSession, xooRule1);
+    db.ruleDao().insert(dbSession, xooRule2);
+    db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
       .setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
     dbSession.commit();
     dbSession.clearCache();
+    ruleIndexer.index();
   }
 }
index 808b85f593edf0c47eaf2025f5903f9fd9ac88ed..d85a54b7a41fc96b40c337e65f4ed09d4eaafb6a 100644 (file)
@@ -57,6 +57,11 @@ public class ActiveRuleDao implements Dao {
     return mapper(dbSession).selectByRuleId(rule.getId());
   }
 
+  // TODO As it's only used by MediumTest, it should be replaced by DbTester.countRowsOfTable()
+  public List<ActiveRuleDto> selectAll(DbSession dbSession) {
+    return mapper(dbSession).selectAll();
+  }
+
   public List<ActiveRuleParamDto> selectAllParams(DbSession dbSession) {
     return mapper(dbSession).selectAllParams();
   }
@@ -122,13 +127,6 @@ public class ActiveRuleDao implements Dao {
     mapper(session).deleteParameter(activeRuleParam.getId());
   }
 
-  public void deleteByProfileKey(DbSession session, String profileKey) {
-    /** Functional cascade for params */
-    for (ActiveRuleDto activeRule : selectByProfileKey(session, profileKey)) {
-      delete(session, activeRule.getKey());
-    }
-  }
-
   public List<ActiveRuleDto> selectByProfileKey(DbSession session, String profileKey) {
     return mapper(session).selectByProfileKey(profileKey);
   }