From 244c8531a02d1c4714761455a2aeff0c622eee18 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 22 Feb 2016 16:12:20 +0100 Subject: [PATCH] SONAR-7330 IssuesNotification is now using only RuleDao --- .../notification/MyNewIssuesNotification.java | 5 +- .../notification/NewIssuesNotification.java | 49 +++++++++---------- .../NewIssuesNotificationFactory.java | 9 ++-- .../MyNewIssuesNotificationTest.java | 3 +- .../NewIssuesNotificationTest.java | 22 +++------ 5 files changed, 37 insertions(+), 51 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesNotification.java b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesNotification.java index bc8b6c7faaa..c9af90739ff 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesNotification.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/MyNewIssuesNotification.java @@ -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) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotification.java b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotification.java index 9f4d8ab4825..afd0f5f593b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotification.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotification.java @@ -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> 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> 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())); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotificationFactory.java b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotificationFactory.java index f0d8969e533..2979f0a1eac 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotificationFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotificationFactory.java @@ -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); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesNotificationTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesNotificationTest.java index c9c79cea1a5..6660e6c1a6f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesNotificationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/MyNewIssuesNotificationTest.java @@ -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() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesNotificationTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesNotificationTest.java index 2ea940ce2b3..95cf12389be 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesNotificationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/notification/NewIssuesNotificationTest.java @@ -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.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); } } -- 2.39.5