]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 IssuesNotification is now using only RuleDao
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Feb 2016 15:12:20 +0000 (16:12 +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/issue/notification/MyNewIssuesNotification.java
server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotification.java
server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotificationFactory.java
server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesNotificationTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesNotificationTest.java

index bc8b6c7faaa6876680df45f09b249beca685ba6c..c9af90739ffd72a2c29c56d846a45535f262cb25 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.issue.notification;
 
 import org.sonar.api.utils.Durations;
 import org.sonar.db.DbClient;
-import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.user.index.UserIndex;
 
 import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate.FIELD_ASSIGNEE;
@@ -30,8 +29,8 @@ public class MyNewIssuesNotification extends NewIssuesNotification {
 
   public static final String MY_NEW_ISSUES_NOTIF_TYPE = "my-new-issues";
 
-  MyNewIssuesNotification(UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
-    super(MY_NEW_ISSUES_NOTIF_TYPE, userIndex, ruleIndex, dbClient, durations);
+  MyNewIssuesNotification(UserIndex userIndex, DbClient dbClient, Durations durations) {
+    super(MY_NEW_ISSUES_NOTIF_TYPE, userIndex, dbClient, durations);
   }
 
   public MyNewIssuesNotification setAssignee(String assignee) {
index 9f4d8ab4825d25dc76c1fc1a6773c6652cf7b939..afd0f5f593bf6eb2ea70a0878044f900c5e082d7 100644 (file)
@@ -32,9 +32,8 @@ import org.sonar.api.utils.Durations;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.MyBatis;
+import org.sonar.db.rule.RuleDto;
 import org.sonar.server.issue.notification.NewIssuesStatistics.Metric;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.user.index.UserDoc;
 import org.sonar.server.user.index.UserIndex;
 
@@ -53,18 +52,16 @@ public class NewIssuesNotification extends Notification {
   private static final String DOT = ".";
 
   private final transient UserIndex userIndex;
-  private final transient RuleIndex ruleIndex;
   private final transient DbClient dbClient;
   private final transient Durations durations;
 
-  NewIssuesNotification(UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
-    this(TYPE, userIndex, ruleIndex, dbClient, durations);
+  NewIssuesNotification(UserIndex userIndex, DbClient dbClient, Durations durations) {
+    this(TYPE, userIndex, dbClient, durations);
   }
 
-  protected NewIssuesNotification(String type, UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
+  protected NewIssuesNotification(String type, UserIndex userIndex, DbClient dbClient, Durations durations) {
     super(type);
     this.userIndex = userIndex;
-    this.ruleIndex = ruleIndex;
     this.dbClient = dbClient;
     this.durations = durations;
   }
@@ -84,40 +81,40 @@ public class NewIssuesNotification extends Notification {
   public NewIssuesNotification setStatistics(String projectName, NewIssuesStatistics.Stats stats) {
     setDefaultMessage(stats.countForMetric(SEVERITY) + " new issues on " + projectName + ".\n");
 
-    setSeverityStatistics(stats);
-    setAssigneesStatistics(stats);
-    setTagsStatistics(stats);
-    setComponentsStatistics(stats);
-    setRuleStatistics(stats);
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      setSeverityStatistics(stats);
+      setAssigneesStatistics(stats);
+      setTagsStatistics(stats);
+      setComponentsStatistics(dbSession, stats);
+      setRuleStatistics(dbSession, stats);
+    } finally {
+      MyBatis.closeQuietly(dbSession);
+    }
 
     return this;
   }
 
-  protected void setRuleStatistics(NewIssuesStatistics.Stats stats) {
+  protected void setRuleStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) {
     Metric metric = Metric.RULE;
     List<Multiset.Entry<String>> metricStats = stats.statsForMetric(metric);
     for (int i = 0; i < 5 && i < metricStats.size(); i++) {
       String ruleKey = metricStats.get(i).getElement();
-      Rule rule = ruleIndex.getByKey(RuleKey.parse(ruleKey));
-      String name = rule.name() + " (" + rule.language() + ")";
+      RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbSession, RuleKey.parse(ruleKey));
+      String name = rule.getName() + " (" + rule.getLanguage() + ")";
       setFieldValue(metric + DOT + (i + 1) + LABEL, name);
       setFieldValue(metric + DOT + (i + 1) + COUNT, String.valueOf(metricStats.get(i).getCount()));
     }
   }
 
-  protected void setComponentsStatistics(NewIssuesStatistics.Stats stats) {
+  protected void setComponentsStatistics(DbSession dbSession, NewIssuesStatistics.Stats stats) {
     Metric metric = Metric.COMPONENT;
     List<Multiset.Entry<String>> componentStats = stats.statsForMetric(metric);
-    DbSession dbSession = dbClient.openSession(false);
-    try {
-      for (int i = 0; i < 5 && i < componentStats.size(); i++) {
-        String uuid = componentStats.get(i).getElement();
-        String componentName = dbClient.componentDao().selectOrFailByUuid(dbSession, uuid).name();
-        setFieldValue(metric + DOT + (i + 1) + LABEL, componentName);
-        setFieldValue(metric + DOT + (i + 1) + COUNT, String.valueOf(componentStats.get(i).getCount()));
-      }
-    } finally {
-      MyBatis.closeQuietly(dbSession);
+    for (int i = 0; i < 5 && i < componentStats.size(); i++) {
+      String uuid = componentStats.get(i).getElement();
+      String componentName = dbClient.componentDao().selectOrFailByUuid(dbSession, uuid).name();
+      setFieldValue(metric + DOT + (i + 1) + LABEL, componentName);
+      setFieldValue(metric + DOT + (i + 1) + COUNT, String.valueOf(componentStats.get(i).getCount()));
     }
   }
 
index f0d8969e533e32380acd33dcb98da82ae7331659..2979f0a1eac42a77e56ba253148557b71a403cbf 100644 (file)
@@ -22,28 +22,25 @@ package org.sonar.server.issue.notification;
 import org.sonar.api.server.ServerSide;
 import org.sonar.api.utils.Durations;
 import org.sonar.db.DbClient;
-import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.user.index.UserIndex;
 
 @ServerSide
 public class NewIssuesNotificationFactory {
   private final UserIndex userIndex;
-  private final RuleIndex ruleIndex;
   private final DbClient dbClient;
   private final Durations durations;
 
-  public NewIssuesNotificationFactory(UserIndex userIndex, RuleIndex ruleIndex, DbClient dbClient, Durations durations) {
+  public NewIssuesNotificationFactory(UserIndex userIndex, DbClient dbClient, Durations durations) {
     this.userIndex = userIndex;
-    this.ruleIndex = ruleIndex;
     this.dbClient = dbClient;
     this.durations = durations;
   }
 
   public MyNewIssuesNotification newMyNewIssuesNotification() {
-    return new MyNewIssuesNotification(userIndex, ruleIndex, dbClient, durations);
+    return new MyNewIssuesNotification(userIndex, dbClient, durations);
   }
 
   public NewIssuesNotification newNewIssuesNotication() {
-    return new NewIssuesNotification(userIndex, ruleIndex, dbClient, durations);
+    return new NewIssuesNotification(userIndex, dbClient, durations);
   }
 }
index c9c79cea1a55a142324374a03e25fbec21391b62..6660e6c1a6f47af2f8bdead71f4c9166115e4925 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.server.issue.notification;
 import org.junit.Test;
 import org.sonar.api.utils.Durations;
 import org.sonar.db.DbClient;
-import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.user.index.UserIndex;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -31,7 +30,7 @@ import static org.sonar.server.issue.notification.AbstractNewIssuesEmailTemplate
 
 public class MyNewIssuesNotificationTest {
 
-  MyNewIssuesNotification underTest = new MyNewIssuesNotification(mock(UserIndex.class), mock(RuleIndex.class), mock(DbClient.class), mock(Durations.class));
+  MyNewIssuesNotification underTest = new MyNewIssuesNotification(mock(UserIndex.class), mock(DbClient.class), mock(Durations.class));
 
   @Test
   public void set_assignee() {
index 2ea940ce2b31ba954ef0270dd2eaabde2b491680..95cf12389be36c176b862ad48f83d541822b56fd 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.issue.notification;
 
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 import java.util.Date;
 import java.util.Locale;
@@ -33,10 +32,7 @@ import org.sonar.api.utils.Durations;
 import org.sonar.core.issue.DefaultIssue;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.index.RuleDoc;
-import org.sonar.server.rule.index.RuleIndex;
-import org.sonar.server.rule.index.RuleNormalizer;
+import org.sonar.db.rule.RuleDto;
 import org.sonar.server.user.index.UserIndex;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -55,10 +51,9 @@ public class NewIssuesNotificationTest {
 
   NewIssuesStatistics.Stats stats = new NewIssuesStatistics.Stats();
   UserIndex userIndex = mock(UserIndex.class);
-  RuleIndex ruleIndex = mock(RuleIndex.class);
   DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
   Durations durations = mock(Durations.class);
-  NewIssuesNotification underTest = new NewIssuesNotification(userIndex, ruleIndex, dbClient, durations);
+  NewIssuesNotification underTest = new NewIssuesNotification(userIndex, dbClient, durations);
 
   @Test
   public void set_project() {
@@ -84,8 +79,8 @@ public class NewIssuesNotificationTest {
     addIssueNTimes(newIssue2(), 3);
     when(dbClient.componentDao().selectOrFailByUuid(any(DbSession.class), eq("file-uuid")).name()).thenReturn("file-name");
     when(dbClient.componentDao().selectOrFailByUuid(any(DbSession.class), eq("directory-uuid")).name()).thenReturn("directory-name");
-    when(ruleIndex.getByKey(RuleKey.of("SonarQube", "rule-the-world"))).thenReturn(newRule("Rule the World", "Java"));
-    when(ruleIndex.getByKey(RuleKey.of("SonarQube", "rule-the-universe"))).thenReturn(newRule("Rule the Universe", "Clojure"));
+    when(dbClient.ruleDao().selectOrFailByKey(any(DbSession.class), eq(RuleKey.of("SonarQube", "rule-the-world")))).thenReturn(newRule("Rule the World", "Java"));
+    when(dbClient.ruleDao().selectOrFailByKey(any(DbSession.class), eq(RuleKey.of("SonarQube", "rule-the-universe")))).thenReturn(newRule("Rule the Universe", "Clojure"));
 
     underTest.setStatistics("project-long-name", stats);
 
@@ -145,10 +140,9 @@ public class NewIssuesNotificationTest {
       .setDebt(Duration.create(10L));
   }
 
-  private Rule newRule(String name, String language) {
-    return new RuleDoc(ImmutableMap.<String, Object>of(
-      RuleNormalizer.RuleField.NAME.field(), name,
-      RuleNormalizer.RuleField.LANGUAGE.field(), language
-    ));
+  private RuleDto newRule(String name, String language) {
+    return new RuleDto()
+      .setName(name)
+      .setLanguage(language);
   }
 }