]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 QProfileComparison is now only using dao
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 25 Feb 2016 14:59:29 +0000 (15:59 +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/QProfileComparison.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CompareAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileComparisonMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/CompareActionMediumTest.java

index 5fada8ad564dd42f6c093fd8065c60e4bff2db6c..057b1dfb5e5667b5f1b899e6987f14c98e92b0e9 100644 (file)
  */
 package org.sonar.server.qualityprofile;
 
+import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.MapDifference;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import javax.annotation.Nonnull;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.server.ServerSide;
-import org.sonar.core.util.NonNullInputFunction;
 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.qualityprofile.QualityProfileDto;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
 
 @ServerSide
 public class QProfileComparison {
 
   private final DbClient dbClient;
 
-  private final QProfileLoader profileLoader;
-
-  public QProfileComparison(DbClient dbClient, QProfileLoader profileLoader) {
+  public QProfileComparison(DbClient dbClient) {
     this.dbClient = dbClient;
-    this.profileLoader = profileLoader;
   }
 
   public QProfileComparisonResult compare(String leftKey, String rightKey) {
     QProfileComparisonResult result = new QProfileComparisonResult();
 
     DbSession dbSession = dbClient.openSession(false);
-
     try {
-      compare(leftKey, rightKey, dbSession, result);
+      compare(dbSession, leftKey, rightKey, result);
     } finally {
-      dbSession.close();
+      dbClient.closeSession(dbSession);
     }
-
     return result;
   }
 
-  private void compare(String leftKey, String rightKey, DbSession session, QProfileComparisonResult result) {
+  private void compare(DbSession session, String leftKey, String rightKey, QProfileComparisonResult result) {
     result.left = dbClient.qualityProfileDao().selectByKey(session, leftKey);
     Preconditions.checkArgument(result.left != null, String.format("Could not find left profile '%s'", leftKey));
     result.right = dbClient.qualityProfileDao().selectByKey(session, rightKey);
     Preconditions.checkArgument(result.right != null, String.format("Could not find right profile '%s'", leftKey));
 
-    Map<RuleKey, ActiveRuleDoc> leftActiveRulesByRuleKey = loadActiveRules(leftKey);
-    Map<RuleKey, ActiveRuleDoc> rightActiveRulesByRuleKey = loadActiveRules(rightKey);
+    Map<RuleKey, ActiveRuleDto> leftActiveRulesByRuleKey = loadActiveRules(session, leftKey);
+    Map<RuleKey, ActiveRuleDto> rightActiveRulesByRuleKey = loadActiveRules(session, rightKey);
 
     Set<RuleKey> allRules = Sets.newHashSet();
     allRules.addAll(leftActiveRulesByRuleKey.keySet());
@@ -79,43 +78,40 @@ public class QProfileComparison {
       } else if (!rightActiveRulesByRuleKey.containsKey(ruleKey)) {
         result.inLeft.put(ruleKey, leftActiveRulesByRuleKey.get(ruleKey));
       } else {
-        compareActivationParams(leftActiveRulesByRuleKey.get(ruleKey), rightActiveRulesByRuleKey.get(ruleKey), result);
+        compareActivationParams(session, leftActiveRulesByRuleKey.get(ruleKey), rightActiveRulesByRuleKey.get(ruleKey), result);
       }
     }
   }
 
-  private void compareActivationParams(ActiveRule leftRule, ActiveRule rightRule, QProfileComparisonResult result) {
-    RuleKey key = leftRule.key().ruleKey();
-    if (leftRule.params().equals(rightRule.params()) && leftRule.severity().equals(rightRule.severity())) {
+  private void compareActivationParams(DbSession session, ActiveRuleDto leftRule, ActiveRuleDto rightRule, QProfileComparisonResult result) {
+    RuleKey key = leftRule.getKey().ruleKey();
+    Map<String, String> leftParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleKey(session, leftRule.getKey()));
+    Map<String, String> rightParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleKey(session, rightRule.getKey()));
+    if (leftParams.equals(rightParams) && leftRule.getSeverityString().equals(rightRule.getSeverityString())) {
       result.same.put(key, leftRule);
     } else {
       ActiveRuleDiff diff = new ActiveRuleDiff();
 
-      diff.leftSeverity = leftRule.severity();
-      diff.rightSeverity = rightRule.severity();
+      diff.leftSeverity = leftRule.getSeverityString();
+      diff.rightSeverity = rightRule.getSeverityString();
 
-      diff.paramDifference = Maps.difference(leftRule.params(), rightRule.params());
+      diff.paramDifference = Maps.difference(leftParams, rightParams);
       result.modified.put(key, diff);
     }
   }
 
-  private Map<RuleKey, ActiveRuleDoc> loadActiveRules(String profileKey) {
-    return Maps.uniqueIndex(profileLoader.findActiveRulesByProfile(profileKey), new NonNullInputFunction<ActiveRuleDoc, RuleKey>() {
-      @Override
-      protected RuleKey doApply(ActiveRuleDoc input) {
-        return input.key().ruleKey();
-      }
-    });
+  private Map<RuleKey, ActiveRuleDto> loadActiveRules(DbSession session, String profileKey) {
+    return Maps.uniqueIndex(dbClient.activeRuleDao().selectByProfileKey(session, profileKey), ActiveRuleToRuleKey.INSTANCE);
   }
 
   public static class QProfileComparisonResult {
 
     private QualityProfileDto left;
     private QualityProfileDto right;
-    private Map<RuleKey, ActiveRule> inLeft = Maps.newHashMap();
-    private Map<RuleKey, ActiveRule> inRight = Maps.newHashMap();
+    private Map<RuleKey, ActiveRuleDto> inLeft = Maps.newHashMap();
+    private Map<RuleKey, ActiveRuleDto> inRight = Maps.newHashMap();
     private Map<RuleKey, ActiveRuleDiff> modified = Maps.newHashMap();
-    private Map<RuleKey, ActiveRule> same = Maps.newHashMap();
+    private Map<RuleKey, ActiveRuleDto> same = Maps.newHashMap();
 
     public QualityProfileDto left() {
       return left;
@@ -125,11 +121,11 @@ public class QProfileComparison {
       return right;
     }
 
-    public Map<RuleKey, ActiveRule> inLeft() {
+    public Map<RuleKey, ActiveRuleDto> inLeft() {
       return inLeft;
     }
 
-    public Map<RuleKey, ActiveRule> inRight() {
+    public Map<RuleKey, ActiveRuleDto> inRight() {
       return inRight;
     }
 
@@ -137,7 +133,7 @@ public class QProfileComparison {
       return modified;
     }
 
-    public Map<RuleKey, ActiveRule> same() {
+    public Map<RuleKey, ActiveRuleDto> same() {
       return same;
     }
 
@@ -169,4 +165,21 @@ public class QProfileComparison {
     }
   }
 
+  private enum ActiveRuleToRuleKey implements Function<ActiveRuleDto, RuleKey> {
+    INSTANCE;
+
+    @Override
+    public RuleKey apply(@Nonnull ActiveRuleDto input) {
+      return input.getKey().ruleKey();
+    }
+  }
+
+  private static Map<String, String> paramDtoToMap(List<ActiveRuleParamDto> params) {
+    Map<String, String> map = new HashMap<>();
+    for (ActiveRuleParamDto dto : params) {
+      map.put(dto.getKey(), dto.getValue());
+    }
+    return map;
+  }
+
 }
index f6b014d9116a46f8b3c39e5c6b5c4079870e2049..c78d3a8fdaddeaf4f9061cdcd66d7560872bc1b0 100644 (file)
@@ -21,6 +21,9 @@ package org.sonar.server.qualityprofile.ws;
 
 import com.google.common.collect.MapDifference.ValueDifference;
 import com.google.common.collect.Maps;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.rule.RuleKey;
@@ -29,9 +32,9 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.api.server.ws.WebService.NewController;
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.core.util.NonNullInputFunction;
-import org.sonar.server.qualityprofile.ActiveRule;
+import org.sonar.db.qualityprofile.ActiveRuleDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.qualityprofile.QProfileComparison;
 import org.sonar.server.qualityprofile.QProfileComparison.ActiveRuleDiff;
 import org.sonar.server.qualityprofile.QProfileComparison.QProfileComparisonResult;
@@ -40,10 +43,6 @@ import org.sonar.server.rule.RuleRepositories;
 import org.sonar.server.rule.RuleRepositories.Repository;
 import org.sonar.server.rule.RuleService;
 
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
 public class CompareAction implements QProfileWsAction {
 
   private static final String ATTRIBUTE_LEFT = "left";
@@ -145,15 +144,15 @@ public class CompareAction implements QProfileWsAction {
       .prop(ATTRIBUTE_NAME, profile.getName());
   }
 
-  private void writeRules(JsonWriter json, Map<RuleKey, ActiveRule> activeRules, Map<RuleKey, Rule> rulesByKey) {
+  private void writeRules(JsonWriter json, Map<RuleKey, ActiveRuleDto> activeRules, Map<RuleKey, Rule> rulesByKey) {
     json.beginArray();
-    for (Entry<RuleKey, ActiveRule> activeRule : activeRules.entrySet()) {
+    for (Entry<RuleKey, ActiveRuleDto> activeRule : activeRules.entrySet()) {
       RuleKey key = activeRule.getKey();
-      ActiveRule value = activeRule.getValue();
+      ActiveRuleDto value = activeRule.getValue();
 
       json.beginObject();
       writeRule(json, key, rulesByKey);
-      json.prop(ATTRIBUTE_SEVERITY, value.severity());
+      json.prop(ATTRIBUTE_SEVERITY, value.getSeverityString());
       json.endObject();
     }
     json.endArray();
index 3d71bb25dc0f662ad7327b6a1d1e7ea2541ab068..34fff148da26730a32d3b9829337b95fe0efd827 100644 (file)
@@ -24,34 +24,28 @@ import org.assertj.core.data.MapEntry;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
-import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.server.rule.RuleParamType;
+import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
+import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.qualityprofile.QProfileComparison.ActiveRuleDiff;
 import org.sonar.server.qualityprofile.QProfileComparison.QProfileComparisonResult;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
-import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.tester.ServerTester;
-import org.sonar.server.tester.UserSessionRule;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class QProfileComparisonMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().addXoo();
-  @Rule
-  public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
+  public static ServerTester tester = new ServerTester().withEsIndexes().addXoo();
 
   DbClient db;
   DbSession dbSession;
-  ActiveRuleIndex index;
   RuleActivator ruleActivator;
   QProfileComparison comparison;
 
@@ -70,17 +64,18 @@ public class QProfileComparisonMediumTest {
 
     xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR");
     xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR");
-    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").setType(RuleParamType.INTEGER.type()));
-    db.deprecatedRuleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
+    db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
       .setName("min").setType(RuleParamType.INTEGER.type()));
 
     left = QProfileTesting.newXooP1();
     right = QProfileTesting.newXooP2();
     db.qualityProfileDao().insert(dbSession, left, right);
+
     dbSession.commit();
-    dbSession.clearCache();
   }
 
   @After
index 6eb039a765aeb987c5af1792e01ca1889f290562..e019475fed4b8a4f0e5cf6639295877d7485f88d 100644 (file)
@@ -30,13 +30,13 @@ import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.api.server.rule.RulesDefinition;
+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.qualityprofile.QualityProfileDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.qualityprofile.QProfileName;
 import org.sonar.server.qualityprofile.QProfileTesting;
 import org.sonar.server.tester.ServerTester;
@@ -46,7 +46,7 @@ import org.sonar.server.ws.WsTester;
 public class CompareActionMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester().withStartupTasks().addXoo()
+  public static ServerTester tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo()
     .addComponents(new RulesDefinition() {
       @Override
       public void define(Context context) {
@@ -200,38 +200,38 @@ public class CompareActionMediumTest {
       .setLanguage(lang)
       .setSeverity(Severity.BLOCKER)
       .setStatus(RuleStatus.READY);
-    db.deprecatedRuleDao().insert(session, rule);
+    db.ruleDao().insert(session, rule);
     RuleParamDto param = RuleParamDto.createFor(rule).setName("param_" + id).setType(RuleParamType.STRING.toString());
-    db.deprecatedRuleDao().insertRuleParam(session, rule, param);
+    db.ruleDao().insertRuleParam(session, rule, param);
     return rule;
   }
 
   private RuleDto createRuleWithParam(String lang, String id) {
     RuleDto rule = createRule(lang, id);
     RuleParamDto param = RuleParamDto.createFor(rule).setName("param_" + id).setType(RuleParamType.STRING.toString());
-    db.deprecatedRuleDao().insertRuleParam(session, rule, param);
+    db.ruleDao().insertRuleParam(session, rule, param);
     return rule;
   }
 
   private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) {
     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
       .setSeverity(rule.getSeverityString());
-    db.deprecatedActiveRuleDao().insert(session, activeRule);
+    db.activeRuleDao().insert(session, activeRule);
     return activeRule;
   }
 
   private ActiveRuleDto createActiveRuleWithParam(RuleDto rule, QualityProfileDto profile, String value) {
     ActiveRuleDto activeRule = createActiveRule(rule, profile);
-    RuleParamDto paramDto = db.deprecatedRuleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
+    RuleParamDto paramDto = db.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0);
     ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value);
-    db.deprecatedActiveRuleDao().insertParam(session, activeRule, activeRuleParam);
+    db.activeRuleDao().insertParam(session, activeRule, activeRuleParam);
     return activeRule;
   }
 
   private ActiveRuleDto createActiveRuleWithSeverity(RuleDto rule, QualityProfileDto profile, String severity) {
     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
       .setSeverity(severity);
-    db.deprecatedActiveRuleDao().insert(session, activeRule);
+    db.activeRuleDao().insert(session, activeRule);
     return activeRule;
   }
 }