From 14afcfa29577ad02ab85f176ca507a9a4b11964d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 26 Feb 2016 16:38:28 +0100 Subject: [PATCH] SONAR-7330 Rename date properties in RuleDoc and ActiveRuleDoc --- .../server/qualityprofile/ActiveRule.java | 8 +--- .../qualityprofile/QProfileExporters.java | 39 +++++++++++-------- .../qualityprofile/index/ActiveRuleDoc.java | 25 +----------- .../index/ActiveRuleIndexer.java | 2 +- .../org/sonar/server/rule/index/RuleDoc.java | 6 +-- .../sonar/server/rule/index/RuleIndexer.java | 2 +- .../RuleActivatorMediumTest.java | 4 +- .../ActiveRuleResultSetIteratorTest.java | 16 ++++---- .../qualityprofile/ws/ExportActionTest.java | 4 +- .../rule/index/RuleResultSetIteratorTest.java | 12 +++--- 10 files changed, 49 insertions(+), 69 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRule.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRule.java index 4d497c06eb7..3ec041ee2c5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRule.java @@ -20,9 +20,7 @@ package org.sonar.server.qualityprofile; import com.google.common.collect.ImmutableList; -import java.util.Date; import java.util.List; -import java.util.Map; import javax.annotation.CheckForNull; import org.sonar.db.qualityprofile.ActiveRuleKey; @@ -33,9 +31,9 @@ public interface ActiveRule { public static final List ALL = ImmutableList.of(NONE, OVERRIDES, INHERITED); } - Date createdAt(); + long createdAt(); - Date updatedAt(); + long updatedAt(); ActiveRuleKey key(); @@ -46,6 +44,4 @@ public interface ActiveRule { @CheckForNull ActiveRuleKey parentKey(); - Map params(); - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java index 3f30e0f3feb..2cefe47bbbe 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java @@ -27,9 +27,7 @@ import java.io.StringWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import java.util.Map; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.profiles.ProfileExporter; @@ -41,22 +39,26 @@ import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RulePriority; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.ValidationMessages; +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.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.qualityprofile.index.ActiveRuleDoc; @ServerSide public class QProfileExporters { + private final DbClient dbClient; private final QProfileLoader loader; private final RuleFinder ruleFinder; private final RuleActivator ruleActivator; private final ProfileExporter[] exporters; private final ProfileImporter[] importers; - public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters, ProfileImporter[] importers) { + public QProfileExporters(DbClient dbClient, QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters, ProfileImporter[] importers) { + this.dbClient = dbClient; this.loader = loader; this.ruleFinder = ruleFinder; this.ruleActivator = ruleActivator; @@ -67,22 +69,22 @@ public class QProfileExporters { /** * Used by Pico if no {@link ProfileImporter} is found */ - public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters) { - this(loader, ruleFinder, ruleActivator, exporters, new ProfileImporter[0]); + public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileExporter[] exporters, DbClient dbClient) { + this(dbClient, loader, ruleFinder, ruleActivator, exporters, new ProfileImporter[0]); } /** * Used by Pico if no {@link ProfileExporter} is found */ - public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileImporter[] importers) { - this(loader, ruleFinder, ruleActivator, new ProfileExporter[0], importers); + public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, ProfileImporter[] importers, DbClient dbClient) { + this(dbClient, loader, ruleFinder, ruleActivator, new ProfileExporter[0], importers); } /** * Used by Pico if no {@link ProfileImporter} nor {@link ProfileExporter} is found */ - public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator) { - this(loader, ruleFinder, ruleActivator, new ProfileExporter[0], new ProfileImporter[0]); + public QProfileExporters(QProfileLoader loader, RuleFinder ruleFinder, RuleActivator ruleActivator, DbClient dbClient) { + this(dbClient, loader, ruleFinder, ruleActivator, new ProfileExporter[0], new ProfileImporter[0]); } public List exportersForLanguage(String language) { @@ -119,14 +121,19 @@ public class QProfileExporters { } private RulesProfile wrap(QualityProfileDto profile) { + DbSession dbSession = dbClient.openSession(false); RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); - for (Iterator activeRuleIterator = loader.findActiveRulesByProfile(profile.getKey()); activeRuleIterator.hasNext();) { - ActiveRule activeRule = activeRuleIterator.next(); - Rule rule = ruleFinder.findByKey(activeRule.key().ruleKey()); - org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.severity())); - for (Map.Entry entry : activeRule.params().entrySet()) { - wrappedActiveRule.setParameter(entry.getKey(), entry.getValue()); + try { + for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByProfileKey(dbSession, profile.getKey())) { + Rule rule = ruleFinder.findByKey(activeRule.getKey().ruleKey()); + org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString())); + List paramDtos = dbClient.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId()); + for (ActiveRuleParamDto activeRuleParamDto : paramDtos) { + wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue()); + } } + } finally { + dbClient.closeSession(dbSession); } return target; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java index 57fe9150c09..f79c5b32e78 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java @@ -21,15 +21,12 @@ package org.sonar.server.qualityprofile.index; import com.google.common.base.Preconditions; import com.google.common.collect.Maps; -import java.util.Collections; -import java.util.Date; import java.util.Map; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.search.BaseDoc; -import org.sonar.server.search.IndexUtils; import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_CREATED_AT; import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_INHERITANCE; @@ -112,19 +109,7 @@ public class ActiveRuleDoc extends BaseDoc implements ActiveRule { } @Override - @Deprecated - public Map params() { - return Collections.emptyMap(); - } - - @Override - @Deprecated - public Date createdAt() { - return IndexUtils.parseDateTime((String) getNullableField(FIELD_ACTIVE_RULE_CREATED_AT)); - } - - @CheckForNull - public Long createdAtAsLong() { + public long createdAt() { return (Long) getField(FIELD_ACTIVE_RULE_CREATED_AT); } @@ -134,13 +119,7 @@ public class ActiveRuleDoc extends BaseDoc implements ActiveRule { } @Override - @Deprecated - public Date updatedAt() { - return IndexUtils.parseDateTime((String) getNullableField(FIELD_ACTIVE_RULE_UPDATED_AT)); - } - - @CheckForNull - public Long updatedAtAsLong() { + public long updatedAt() { return (Long) getField(FIELD_ACTIVE_RULE_UPDATED_AT); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java index 2a3b5f4ec42..e7f6e5aa3ca 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java @@ -81,7 +81,7 @@ public class ActiveRuleIndexer extends BaseIndexer { bulk.add(newIndexRequest(activeRule)); // it's more efficient to sort programmatically than in SQL on some databases (MySQL for instance) - maxDate = Math.max(maxDate, activeRule.updatedAtAsLong()); + maxDate = Math.max(maxDate, activeRule.updatedAt()); } bulk.stop(); return maxDate; diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java index fe3376a163e..7f12dd6836d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java @@ -168,8 +168,7 @@ public class RuleDoc extends BaseDoc { return this; } - @CheckForNull - public Long createdAtAsLong() { + public long createdAt() { return (Long) getField(RuleIndexDefinition.FIELD_RULE_CREATED_AT); } @@ -178,8 +177,7 @@ public class RuleDoc extends BaseDoc { return this; } - @CheckForNull - public Long updatedAtAtAsLong() { + public long updatedAt() { return (Long) getField(RuleIndexDefinition.FIELD_RULE_UPDATED_AT); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java index 88ce05d3153..7592ac2557f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java @@ -75,7 +75,7 @@ public class RuleIndexer extends BaseIndexer { bulk.add(newIndexRequest(rule)); // it's more efficient to sort programmatically than in SQL on some databases (MySQL for instance) - maxDate = Math.max(maxDate, rule.updatedAtAtAsLong()); + maxDate = Math.max(maxDate, rule.updatedAt()); } bulk.stop(); return maxDate; diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java index 409e5a9191b..3f4c4fcd16f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java @@ -1123,8 +1123,8 @@ public class RuleActivatorMediumTest { ActiveRule.Inheritance.valueOf(expectedInheritance)); // Dates should be set - assertThat(activeRule.createdAtAsLong()).isNotNull(); - assertThat(activeRule.updatedAtAsLong()).isNotNull(); + assertThat(activeRule.createdAt()).isNotNull(); + assertThat(activeRule.updatedAt()).isNotNull(); } } assertThat(found).as("Rule is not activated in index").isTrue(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleResultSetIteratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleResultSetIteratorTest.java index 6631d172052..3a5010b690b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleResultSetIteratorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleResultSetIteratorTest.java @@ -66,8 +66,8 @@ public class ActiveRuleResultSetIteratorTest { assertThat(activeRule.severity()).isEqualTo(CRITICAL); assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE); assertThat(activeRule.parentKey()).isNull(); - assertThat(activeRule.createdAtAsLong()).isEqualTo(1500000000000L); - assertThat(activeRule.updatedAtAsLong()).isEqualTo(1600000000000L); + assertThat(activeRule.createdAt()).isEqualTo(1500000000000L); + assertThat(activeRule.updatedAt()).isEqualTo(1600000000000L); } @Test @@ -85,8 +85,8 @@ public class ActiveRuleResultSetIteratorTest { assertThat(activeRule.severity()).isEqualTo(CRITICAL); assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE); assertThat(activeRule.parentKey()).isNull(); - assertThat(activeRule.createdAtAsLong()).isEqualTo(2000000000000L); - assertThat(activeRule.updatedAtAsLong()).isEqualTo(2100000000000L); + assertThat(activeRule.createdAt()).isEqualTo(2000000000000L); + assertThat(activeRule.updatedAt()).isEqualTo(2100000000000L); key = ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001")); activeRule = activeRulesByKey.get(key); @@ -94,8 +94,8 @@ public class ActiveRuleResultSetIteratorTest { assertThat(activeRule.severity()).isEqualTo(INFO); assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE); assertThat(activeRule.parentKey()).isNull(); - assertThat(activeRule.createdAtAsLong()).isEqualTo(1700000000000L); - assertThat(activeRule.updatedAtAsLong()).isEqualTo(1800000000000L); + assertThat(activeRule.createdAt()).isEqualTo(1700000000000L); + assertThat(activeRule.updatedAt()).isEqualTo(1800000000000L); key = ActiveRuleKey.of("child", RuleKey.of("xoo", "S001")); activeRule = activeRulesByKey.get(key); @@ -103,8 +103,8 @@ public class ActiveRuleResultSetIteratorTest { assertThat(activeRule.severity()).isEqualTo(BLOCKER); assertThat(activeRule.inheritance()).isEqualTo(INHERITED); assertThat(activeRule.parentKey()).isEqualTo(ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001"))); - assertThat(activeRule.createdAtAsLong()).isEqualTo(1500000000000L); - assertThat(activeRule.updatedAtAsLong()).isEqualTo(1600000000000L); + assertThat(activeRule.createdAt()).isEqualTo(1500000000000L); + assertThat(activeRule.updatedAt()).isEqualTo(1600000000000L); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java index a641ab0d048..e66cd4a096d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java @@ -89,7 +89,7 @@ public class ExportActionTest { ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class); when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.newHashSet().iterator()); - exporters = new QProfileExporters(new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2}, null); + exporters = new QProfileExporters(dbClient, new QProfileLoader(dbClient, activeRuleIndex, mock(RuleIndex.class)), null, null, new ProfileExporter[] {exporter1, exporter2}, null); wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), mock(ProjectAssociationActions.class), @@ -181,7 +181,7 @@ public class ExportActionTest { @Test public void do_not_fail_when_no_exporters() throws Exception { - QProfileExporters myExporters = new QProfileExporters(null, null, null, new ProfileExporter[0], null); + QProfileExporters myExporters = new QProfileExporters(dbClient, null, null, null, new ProfileExporter[0], null); WsTester myWsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), mock(ProjectAssociationActions.class), diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleResultSetIteratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleResultSetIteratorTest.java index f6abb47f65b..5bcf35f40a4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleResultSetIteratorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleResultSetIteratorTest.java @@ -69,8 +69,8 @@ public class RuleResultSetIteratorTest { assertThat(rule.status()).isEqualTo(RuleStatus.READY); assertThat(rule.isTemplate()).isFalse(); assertThat(rule.allTags()).containsOnly("bug", "performance", "cwe"); - assertThat(rule.createdAtAsLong()).isEqualTo(1500000000000L); - assertThat(rule.updatedAtAtAsLong()).isEqualTo(1600000000000L); + assertThat(rule.createdAt()).isEqualTo(1500000000000L); + assertThat(rule.updatedAt()).isEqualTo(1600000000000L); } @Test @@ -109,8 +109,8 @@ public class RuleResultSetIteratorTest { assertThat(rule.status()).isEqualTo(RuleStatus.READY); assertThat(rule.isTemplate()).isFalse(); assertThat(rule.allTags()).containsOnly("bug", "performance", "cwe"); - assertThat(rule.createdAtAsLong()).isEqualTo(1500000000000L); - assertThat(rule.updatedAtAtAsLong()).isEqualTo(1600000000000L); + assertThat(rule.createdAt()).isEqualTo(1500000000000L); + assertThat(rule.updatedAt()).isEqualTo(1600000000000L); rule = rulesByKey.get("S002"); assertThat(rule.key()).isEqualTo(RuleKey.of("xoo", "S002")); @@ -125,8 +125,8 @@ public class RuleResultSetIteratorTest { assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.isTemplate()).isTrue(); assertThat(rule.allTags()).isEmpty(); - assertThat(rule.createdAtAsLong()).isEqualTo(2000000000000L); - assertThat(rule.updatedAtAtAsLong()).isEqualTo(2100000000000L); + assertThat(rule.createdAt()).isEqualTo(2000000000000L); + assertThat(rule.updatedAt()).isEqualTo(2100000000000L); } @Test -- 2.39.5