]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 Replace usage of deprecateActiveRuleDao by activeRuleDao
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 23 Feb 2016 16:24:05 +0000 (17:24 +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/main/java/org/sonar/server/qualityprofile/QProfileReset.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivatorContextFactory.java
server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java
server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexerTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java

index 07e88d83649dcdd075513f6e5946d8044dd4a5e5..ecb01928244028c7f796f97aa97d63773ff8c12c 100644 (file)
@@ -116,7 +116,7 @@ public class QProfileFactory {
 
   private void doDelete(DbSession session, QualityProfileDto profile) {
     db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session);
-    db.deprecatedActiveRuleDao().deleteByProfileKey(session, profile.getKey());
+    db.activeRuleDao().deleteByProfileKey(session, profile.getKey());
     db.qualityProfileDao().delete(session, profile);
   }
 
index d598028dbca133d76b8e092e028acbed5a34401f..9b9bbc64297f81cb86143f98c36f12b4394a3a69 100644 (file)
@@ -128,7 +128,7 @@ public class QProfileReset {
     BulkChangeResult result = new BulkChangeResult(profile);
     Set<RuleKey> ruleToBeDeactivated = Sets.newHashSet();
     // Keep reference to all the activated rules before backup restore
-    for (ActiveRuleDto activeRuleDto : db.deprecatedActiveRuleDao().selectByProfileKey(dbSession, profile.getKee())) {
+    for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfileKey(dbSession, profile.getKee())) {
       if (activeRuleDto.getInheritance() == null) {
         // inherited rules can't be deactivated
         ruleToBeDeactivated.add(activeRuleDto.getKey().ruleKey());
index fb0325aae358172b5d87736cfd664abd441b2976..b9089ffce2cfbcc4f93bc02a410c155d02b638c6 100644 (file)
@@ -24,30 +24,28 @@ import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.Multimaps;
 import com.google.common.collect.Sets;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
-import org.sonar.api.server.ServerSide;
 import org.sonar.api.profiles.ProfileDefinition;
 import org.sonar.api.profiles.RulesProfile;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rules.ActiveRuleParam;
+import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.ValidationMessages;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.api.utils.log.Profiler;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.loadedtemplate.LoadedTemplateDto;
-import org.sonar.db.DbClient;
-import org.sonar.server.platform.PersistentSettings;
-
-import javax.annotation.Nullable;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import org.sonar.db.qualityprofile.QualityProfileDto;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
 
 /**
  * Synchronize Quality profiles during server startup
@@ -64,24 +62,26 @@ public class RegisterQualityProfiles {
   private final QProfileFactory profileFactory;
   private final RuleActivator ruleActivator;
   private final Languages languages;
+  private final ActiveRuleIndexer activeRuleIndexer;
 
   /**
    * To be kept when no ProfileDefinition are injected
    */
-  public RegisterQualityProfiles(PersistentSettings settings, BuiltInProfiles builtInProfiles,
-    DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages) {
-    this(settings, builtInProfiles, dbClient, profileFactory, ruleActivator, Collections.<ProfileDefinition>emptyList(), languages);
+  public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
+    DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
+    this(builtInProfiles, dbClient, profileFactory, ruleActivator, Collections.<ProfileDefinition>emptyList(), languages, activeRuleIndexer);
   }
 
-  public RegisterQualityProfiles(PersistentSettings settings, BuiltInProfiles builtInProfiles,
+  public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
     DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator,
-    List<ProfileDefinition> definitions, Languages languages) {
+    List<ProfileDefinition> definitions, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
     this.builtInProfiles = builtInProfiles;
     this.dbClient = dbClient;
     this.profileFactory = profileFactory;
     this.ruleActivator = ruleActivator;
     this.definitions = definitions;
     this.languages = languages;
+    this.activeRuleIndexer = activeRuleIndexer;
   }
 
   public void start() {
@@ -95,6 +95,7 @@ public class RegisterQualityProfiles {
           registerProfilesForLanguage(session, language, defs);
         }
       }
+      activeRuleIndexer.setEnabled(true).index();
       profiler.stopDebug();
 
     } finally {
index 84902c83c39e44d6997bb4ffdf053670eacd57cf..392cfdcfe2fc1431533e2d194e23e73c4b5bff3a 100644 (file)
@@ -29,7 +29,10 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.server.rule.RuleParamType;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.ActiveRuleDao;
 import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.ActiveRuleKey;
 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
@@ -37,9 +40,7 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
 import org.sonar.server.activity.ActivityService;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.qualityprofile.db.ActiveRuleDao;
 import org.sonar.server.rule.Rule;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleNormalizer;
@@ -58,6 +59,7 @@ import static com.google.common.collect.Lists.newArrayList;
 @ServerSide
 public class RuleActivator {
 
+  private final System2 system2;
   private final DbClient db;
   private final TypeValidations typeValidations;
   private final RuleActivatorContextFactory contextFactory;
@@ -65,9 +67,10 @@ public class RuleActivator {
   private final ActivityService activityService;
   private final UserSession userSession;
 
-  public RuleActivator(DbClient db, IndexClient index,
+  public RuleActivator(System2 system2, DbClient db, IndexClient index,
     RuleActivatorContextFactory contextFactory, TypeValidations typeValidations,
     ActivityService activityService, UserSession userSession) {
+    this.system2 = system2;
     this.db = db;
     this.index = index;
     this.contextFactory = contextFactory;
@@ -233,21 +236,21 @@ public class RuleActivator {
     ActiveRuleDto activeRule = null;
     if (change.getType() == ActiveRuleChange.Type.ACTIVATED) {
       activeRule = doInsert(change, context, dbSession);
-
     } else if (change.getType() == ActiveRuleChange.Type.DEACTIVATED) {
-      ActiveRuleDao dao = db.deprecatedActiveRuleDao();
-      dao.deleteByKey(dbSession, change.getKey());
+      ActiveRuleDao dao = db.activeRuleDao();
+      dao.delete(dbSession, change.getKey());
 
     } else if (change.getType() == ActiveRuleChange.Type.UPDATED) {
       activeRule = doUpdate(change, context, dbSession);
     }
+
     activityService.save(change.toActivity());
     return activeRule;
   }
 
   private ActiveRuleDto doInsert(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
     ActiveRuleDto activeRule;
-    ActiveRuleDao dao = db.deprecatedActiveRuleDao();
+    ActiveRuleDao dao = db.activeRuleDao();
     activeRule = ActiveRuleDto.createFor(context.profile(), context.rule());
     String severity = change.getSeverity();
     if (severity != null) {
@@ -257,6 +260,8 @@ public class RuleActivator {
     if (inheritance != null) {
       activeRule.setInheritance(inheritance.name());
     }
+    activeRule.setUpdatedAtInMs(system2.now());
+    activeRule.setCreatedAtInMs(system2.now());
     dao.insert(dbSession, activeRule);
     for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
       if (param.getValue() != null) {
@@ -269,7 +274,7 @@ public class RuleActivator {
   }
 
   private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
-    ActiveRuleDao dao = db.deprecatedActiveRuleDao();
+    ActiveRuleDao dao = db.activeRuleDao();
     ActiveRuleDto activeRule = context.activeRule();
     if (activeRule != null) {
       String severity = change.getSeverity();
@@ -280,6 +285,7 @@ public class RuleActivator {
       if (inheritance != null) {
         activeRule.setInheritance(inheritance.name());
       }
+      activeRule.setUpdatedAtInMs(system2.now());
       dao.update(dbSession, activeRule);
 
       for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
@@ -331,7 +337,7 @@ public class RuleActivator {
    */
   public List<ActiveRuleChange> deactivate(DbSession dbSession, RuleDto ruleDto) {
     List<ActiveRuleChange> changes = Lists.newArrayList();
-    List<ActiveRuleDto> activeRules = db.deprecatedActiveRuleDao().selectByRule(dbSession, ruleDto);
+    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByRule(dbSession, ruleDto);
     for (ActiveRuleDto activeRule : activeRules) {
       changes.addAll(deactivate(dbSession, activeRule.getKey(), true));
     }
@@ -478,7 +484,7 @@ public class RuleActivator {
       // set new parent
       profile.setParentKee(parentKey);
       db.qualityProfileDao().update(dbSession, profile);
-      for (ActiveRuleDto parentActiveRule : db.deprecatedActiveRuleDao().selectByProfileKey(dbSession, parentKey)) {
+      for (ActiveRuleDto parentActiveRule : db.activeRuleDao().selectByProfileKey(dbSession, parentKey)) {
         try {
           RuleActivation activation = new RuleActivation(parentActiveRule.getKey().ruleKey());
           activate(dbSession, activation, profileKey);
@@ -497,12 +503,13 @@ public class RuleActivator {
     if (profileDto.getParentKee() != null) {
       profileDto.setParentKee(null);
       db.qualityProfileDao().update(dbSession, profileDto);
-      for (ActiveRuleDto activeRule : db.deprecatedActiveRuleDao().selectByProfileKey(dbSession, profileDto.getKey())) {
+      for (ActiveRuleDto activeRule : db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey())) {
         if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
           deactivate(dbSession, activeRule.getKey(), true);
         } else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
           activeRule.setInheritance(null);
-          db.deprecatedActiveRuleDao().update(dbSession, activeRule);
+          activeRule.setUpdatedAtInMs(system2.now());
+          db.activeRuleDao().update(dbSession, activeRule);
         }
       }
     }
index b981461b834008405f4f1c368bad6aa8115a0d04..8ae46b3c39de016a5cfeb8efc228ed54bde5b0f9 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile;
 
+import com.google.common.base.Optional;
 import java.util.Collection;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.ServerSide;
@@ -86,16 +87,16 @@ public class RuleActivatorContextFactory {
 
   private void initActiveRules(String profileKey, RuleKey ruleKey, RuleActivatorContext context, DbSession session, boolean parent) {
     ActiveRuleKey key = ActiveRuleKey.of(profileKey, ruleKey);
-    ActiveRuleDto activeRule = db.deprecatedActiveRuleDao().getNullableByKey(session, key);
+    Optional<ActiveRuleDto> activeRule = db.activeRuleDao().selectByKey(session, key);
     Collection<ActiveRuleParamDto> activeRuleParams = null;
-    if (activeRule != null) {
-      activeRuleParams = db.deprecatedActiveRuleDao().selectParamsByActiveRuleKey(session, key);
+    if (activeRule.isPresent()) {
+      activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleKey(session, key);
     }
     if (parent) {
-      context.setParentActiveRule(activeRule);
+      context.setParentActiveRule(activeRule.orNull());
       context.setParentActiveRuleParams(activeRuleParams);
     } else {
-      context.setActiveRule(activeRule);
+      context.setActiveRule(activeRule.orNull());
       context.setActiveRuleParams(activeRuleParams);
     }
   }
index e76bfee84b5adc75c832ec939be2fb150d257045..b53ffdd268a4799c979380238dbd31a2a1978252 100644 (file)
@@ -46,13 +46,13 @@ import org.sonar.api.utils.System2;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.api.utils.log.Profiler;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleDto.Format;
 import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.qualityprofile.RuleActivator;
 import org.sonar.server.rule.index.RuleIndexer;
 
@@ -284,7 +284,7 @@ public class RegisterRules implements Startable {
     for (RuleParamDto paramDto : paramDtos) {
       RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
       if (paramDef == null) {
-        dbClient.deprecatedActiveRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName());
+        dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName());
         dbClient.ruleDao().deleteRuleParam(session, paramDto.getId());
       } else {
         if (mergeParam(paramDto, paramDef)) {
@@ -306,9 +306,9 @@ public class RegisterRules implements Startable {
         dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
         if (!StringUtils.isEmpty(param.defaultValue())) {
           // Propagate the default value to existing active rules
-          for (ActiveRuleDto activeRule : dbClient.deprecatedActiveRuleDao().selectByRule(session, rule)) {
+          for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRule(session, rule)) {
             ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
-            dbClient.deprecatedActiveRuleDao().insertParam(session, activeRule, activeParam);
+            dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
           }
         }
       }
index 8152cab7337e3b32b0ae5972a698b367f5537edd..0acc2497660cf87361f988db123a5293754ab0b5 100644 (file)
@@ -251,9 +251,9 @@ public class RuleUpdater {
       // Load active rules and its parameters in cache
       Multimap<RuleDto, ActiveRuleDto> activeRules = ArrayListMultimap.create();
       Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParams = ArrayListMultimap.create();
-      for (ActiveRuleDto activeRuleDto : dbClient.deprecatedActiveRuleDao().selectByRule(dbSession, customRule)) {
+      for (ActiveRuleDto activeRuleDto : dbClient.activeRuleDao().selectByRule(dbSession, customRule)) {
         activeRules.put(customRule, activeRuleDto);
-        for (ActiveRuleParamDto activeRuleParamDto : dbClient.deprecatedActiveRuleDao().selectParamsByActiveRuleKey(dbSession, activeRuleDto.getKey())) {
+        for (ActiveRuleParamDto activeRuleParamDto : dbClient.activeRuleDao().selectParamsByActiveRuleKey(dbSession, activeRuleDto.getKey())) {
           activeRuleParams.put(activeRuleDto, activeRuleParamDto);
         }
       }
@@ -278,9 +278,9 @@ public class RuleUpdater {
         for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) {
           for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) {
             if (activeRuleParamDto.getKey().equals(key)) {
-              dbClient.deprecatedActiveRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto.setValue(value));
+              dbClient.activeRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto.setValue(value));
             } else {
-              dbClient.deprecatedActiveRuleDao().insertParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(ruleParamDto).setValue(value));
+              dbClient.activeRuleDao().insertParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(ruleParamDto).setValue(value));
             }
           }
         }
@@ -289,7 +289,7 @@ public class RuleUpdater {
         for (ActiveRuleDto activeRuleDto : activeRules.get(customRule)) {
           for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams.get(activeRuleDto)) {
             if (activeRuleParamDto.getKey().equals(key)) {
-              dbClient.deprecatedActiveRuleDao().deleteParam(dbSession, activeRuleDto, activeRuleParamDto);
+              dbClient.activeRuleDao().deleteParam(dbSession, activeRuleDto, activeRuleParamDto);
             }
           }
         }
index 9a192525ece3904da463d060af5cb8ce77ae40f1..1c0ccb8d0158f8267fbc9ed914fa7d09389fbde5 100644 (file)
@@ -32,21 +32,24 @@ import org.sonar.api.rules.RulePriority;
 import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.api.server.rule.RulesDefinition;
 import org.sonar.api.utils.ValidationMessages;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.loadedtemplate.LoadedTemplateDto;
+import org.sonar.db.qualityprofile.ActiveRuleDao;
 import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.ActiveRuleKey;
 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
 import org.sonar.db.qualityprofile.QualityProfileDao;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.db.DbClient;
+import org.sonar.server.es.SearchOptions;
 import org.sonar.server.platform.Platform;
-import org.sonar.server.qualityprofile.db.ActiveRuleDao;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
+import org.sonar.server.rule.index.RuleIndex2;
+import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.tester.ServerTester;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
 
 public class RegisterQualityProfilesMediumTest {
 
@@ -76,34 +79,38 @@ public class RegisterQualityProfilesMediumTest {
     assertThat(profile).isNotNull();
 
     // Check ActiveRules in DB
-    ActiveRuleDao activeRuleDao = dbClient().deprecatedActiveRuleDao();
+    ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
     assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
+
     RuleKey ruleKey = RuleKey.of("xoo", "x1");
     ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
+    assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
+
+    // Check in ES
+    assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
 
-    // 0. Check and clear ES
-    assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNotNull();
-    tester.clearIndexes();
-    assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNull();
     tester.get(Platform.class).restart();
-    assertThat(tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey)).isNotNull();
 
-    // Check ActiveRules in ES
-    org.sonar.server.qualityprofile.ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getNullableByKey(activeRuleKey);
-    assertThat(activeRule.key().qProfile()).isEqualTo(profile.getKee());
-    assertThat(activeRule.key().ruleKey()).isEqualTo(ruleKey);
-    assertThat(activeRule.severity()).isEqualTo(Severity.CRITICAL);
+    assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
+
+    // Check ActiveRules
+    ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, activeRuleKey).get();
+    assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKee());
+    assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
+    assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
+
+    // Check in ES
+    assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
 
     // TODO
     // Check ActiveRuleParameters in DB
     Map<String, ActiveRuleParamDto> params =
-      ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.key()));
+      ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
     assertThat(params).hasSize(2);
     // set by profile
     assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
     // default value
     assertThat(params.get("max").getValue()).isEqualTo("10");
-
   }
 
   @Test
@@ -122,11 +129,11 @@ public class RegisterQualityProfilesMediumTest {
     verifyDefaultProfile("xoo", "Basic");
 
     // Check ActiveRules in DB
-    ActiveRuleDao activeRuleDao = dbClient().deprecatedActiveRuleDao();
+    ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
     assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
     RuleKey ruleKey = RuleKey.of("xoo", "x1");
 
-    ActiveRuleDto activeRule = activeRuleDao.getNullableByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey));
+    ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey)).get();
     assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
     assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
     assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
index 3fbd501a52b8f8bb736d63330fc332e3d1ad7c16..cfcf852ebf4b46f744e3104ca0c8959eb5517216 100644 (file)
@@ -158,7 +158,7 @@ public class ActiveRuleIndexerTest {
     dbTester.getDbClient().qualityProfileDao().insert(dbTester.getSession(), profile);
     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule).setSeverity(Severity.BLOCKER)
       .setCreatedAtInMs(yesterday).setUpdatedAtInMs(yesterday);
-//    dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule);
+    dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule);
     dbTester.getSession().commit();
 
     indexer.index();
@@ -170,7 +170,7 @@ public class ActiveRuleIndexerTest {
     dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), rule2);
     ActiveRuleDto activeRule2 = ActiveRuleDto.createFor(profile, rule2).setSeverity(Severity.CRITICAL)
       .setCreatedAtInMs(now).setUpdatedAtInMs(now);
-//    dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule2);
+    dbTester.getDbClient().activeRuleDao().insert(dbTester.getSession(), activeRule2);
     dbTester.getSession().commit();
 
     indexer.index(singletonList(ActiveRuleChange.createFor(ACTIVATED, activeRule2.getKey())));
index 6bc6cf0cae792e38f0e554172fd139ca8f1551c2..6c888b585352a377fd7f9181b4e282c22c11835b 100644 (file)
  */
 package org.sonar.server.rule;
 
+import com.google.common.base.Function;
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableMap;
 import java.util.List;
 import java.util.Map;
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
@@ -35,21 +37,19 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.api.server.rule.RulesDefinition;
 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.ActiveRuleParamDto;
 import org.sonar.db.rule.RuleDao;
 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.es.SearchIdResult;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.platform.Platform;
-import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.qualityprofile.QProfileService;
 import org.sonar.server.qualityprofile.QProfileTesting;
 import org.sonar.server.qualityprofile.RuleActivation;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.rule.index.RuleIndex2;
 import org.sonar.server.rule.index.RuleQuery;
 import org.sonar.server.tester.ServerTester;
@@ -57,16 +57,15 @@ import org.sonar.server.tester.UserSessionRule;
 
 import static com.google.common.collect.Sets.newHashSet;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
 
-// TODO tests should be moved to RegisterRulesTest
+// TODO remaining tests should be moved to RegisterRulesTest
 public class RegisterRulesMediumTest {
 
   static final XooRulesDefinition RULE_DEFS = new XooRulesDefinition();
 
   @ClassRule
   public static final ServerTester TESTER = new ServerTester().addXoo().addComponents(RULE_DEFS);
-  public static final RuleKey X1_KEY = RuleKey.of("xoo", "x1");
+
   @org.junit.Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(TESTER);
 
@@ -74,8 +73,6 @@ public class RegisterRulesMediumTest {
   DbSession dbSession = TESTER.get(DbClient.class).openSession(false);
 
   RuleIndex2 ruleIndex = TESTER.get(RuleIndex2.class);
-  ActiveRuleIndex activeRuleIndex = TESTER.get(ActiveRuleIndex.class);
-
   RuleDao ruleDao = db.ruleDao();
 
   @Before
@@ -103,36 +100,6 @@ public class RegisterRulesMediumTest {
     ruleIndex = TESTER.get(RuleIndex2.class);
   }
 
-  /**
-   * Use-case:
-   * 1. start server
-   * 2. stop server
-   * 3. drop elasticsearch index: rm -rf data/es
-   * 4. start server -> db is up-to-date (no changes) but rules must be re-indexed
-   */
-  @Test
-  @Ignore
-  public void index_rules_even_if_no_changes() {
-    Rules rules = new Rules() {
-      @Override
-      public void init(RulesDefinition.NewRepository repository) {
-        repository.createRule("x1")
-          .setName("x1 name")
-          .setHtmlDescription("x1 desc");
-      }
-    };
-    register(rules);
-
-    // clear ES but keep db
-    TESTER.clearIndexes();
-    register(rules);
-
-    // verify that rules are indexed
-    SearchIdResult<RuleKey> searchResult = ruleIndex.search(new RuleQuery().setKey("xoo:x1"), new SearchOptions());
-    assertThat(searchResult.getTotal()).isEqualTo(1);
-    assertThat(searchResult.getIds()).containsOnly(RuleKey.of("xoo", "x1"));
-  }
-
   @Test
   public void deactivate_removed_rules_only_if_repository_still_exists() {
     register(new Rules() {
@@ -159,7 +126,7 @@ public class RegisterRulesMediumTest {
     });
     assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
     assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X2.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
-    assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(0);
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).isEmpty();
   }
 
   @Test
@@ -182,13 +149,16 @@ public class RegisterRulesMediumTest {
 
     // Restart without xoo
     register(null);
+    dbSession.commit();
+    dbSession.clearCache();
+
     assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
-    assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).isEmpty();
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(1);
 
     // Re-install
     register(rules);
     assertThat(ruleIndex.search(new RuleQuery().setKey(RuleTesting.XOO_X1.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
-    assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(1);
+    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(1);
   }
 
   @Test
@@ -224,15 +194,16 @@ public class RegisterRulesMediumTest {
       }
     });
 
-    ActiveRule activeRule = activeRuleIndex.getByKey(ActiveRuleKey.of(QProfileTesting.XOO_P1_KEY, RuleTesting.XOO_X1));
-    Map<String, String> params = activeRule.params();
+    List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleKey(dbSession, ActiveRuleKey.of(QProfileTesting.XOO_P1_KEY, RuleTesting.XOO_X1));
     assertThat(params).hasSize(2);
 
+    Map<String, ActiveRuleParamDto> parmsByKey = FluentIterable.from(params).uniqueIndex(ActiveRuleParamToKey.INSTANCE);
+
     // do not change default value on existing active rules -> keep min=5
-    assertThat(params.get("min")).isEqualTo("5");
+    assertThat(parmsByKey.get("min").getValue()).isEqualTo("5");
 
     // new param with default value
-    assertThat(params.get("max")).isEqualTo("10");
+    assertThat(parmsByKey.get("max").getValue()).isEqualTo("10");
   }
 
   @Test
@@ -485,4 +456,13 @@ public class RegisterRulesMediumTest {
     }
   }
 
+  private enum ActiveRuleParamToKey implements Function<ActiveRuleParamDto, String> {
+    INSTANCE;
+
+    @Override
+    public String apply(@Nonnull ActiveRuleParamDto input) {
+      return input.getKey();
+    }
+  }
+
 }
index e33a3ffe6fe57ee3f10a2ccb2b429cab8d5dd0f8..57aa3b0d02b0eab85f54f7801a1b515db9f8e1ca 100644 (file)
@@ -35,16 +35,13 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.server.rule.RulesDefinition;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
-import org.sonar.db.qualityprofile.QualityProfileDao;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.qualityprofile.RuleActivator;
-import org.sonar.server.qualityprofile.db.ActiveRuleDao;
-import org.sonar.server.rule.db.RuleDao;
 import org.sonar.server.rule.index.RuleIndex2;
 import org.sonar.server.rule.index.RuleIndexDefinition;
 import org.sonar.server.rule.index.RuleIndexer;
@@ -68,7 +65,7 @@ public class RegisterRulesTest {
   static final RuleKey RULE_KEY2 = RuleKey.of("fake", "rule2");
   static final RuleKey RULE_KEY3 = RuleKey.of("fake", "rule3");
 
-  System2 system;
+  System2 system = mock(System2.class);;
 
   @org.junit.Rule
   public DbTester dbTester = DbTester.create(system);
@@ -78,7 +75,7 @@ public class RegisterRulesTest {
 
   RuleActivator ruleActivator = mock(RuleActivator.class);
 
-  DbClient dbClient;
+  DbClient dbClient = dbTester.getDbClient();
 
   RuleIndexer ruleIndexer;
 
@@ -87,12 +84,7 @@ public class RegisterRulesTest {
   @Before
   public void before() {
     esTester.truncateIndices();
-    system = mock(System2.class);
     when(system.now()).thenReturn(DATE1.getTime());
-    RuleDao ruleDao = new RuleDao(system);
-    ActiveRuleDao activeRuleDao = new ActiveRuleDao(new QualityProfileDao(dbTester.myBatis(), system), ruleDao, system);
-    dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), ruleDao, activeRuleDao, new org.sonar.db.rule.RuleDao(),
-      new QualityProfileDao(dbTester.myBatis(), system));
     ruleIndexer = new RuleIndexer(dbClient, esTester.client());
     ruleIndexer.setEnabled(true);
     ruleIndex = new RuleIndex2(esTester.client());