]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7330 Rename date properties in RuleDoc and ActiveRuleDoc
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 26 Feb 2016 15:38:28 +0000 (16:38 +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/ActiveRule.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileExporters.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleResultSetIteratorTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ExportActionTest.java
server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleResultSetIteratorTest.java

index 4d497c06eb7a591d1e1eef79c0cee4b5d66f7be6..3ec041ee2c55ad3146c9b8e9771e5b7144229aa2 100644 (file)
@@ -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<Inheritance> 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<String, String> params();
-
 }
index 3f30e0f3febc2cb184e25efc13ed0bc710c40cd3..2cefe47bbbe87ad2cb1407e15bfe9a9e725de6d4 100644 (file)
@@ -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<ProfileExporter> 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<ActiveRuleDoc> 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<String, String> 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<ActiveRuleParamDto> paramDtos = dbClient.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId());
+        for (ActiveRuleParamDto activeRuleParamDto : paramDtos) {
+          wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue());
+        }
       }
+    } finally {
+      dbClient.closeSession(dbSession);
     }
     return target;
   }
index 57fe9150c099ddea42aa17650c01bdb98d1026de..f79c5b32e788efd92bfc14a64fabcb1dfb6a7840 100644 (file)
@@ -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<String, String> 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);
   }
 
index 2a3b5f4ec42ea5f13f521b5f9e4cdc28a77b3b61..e7f6e5aa3ca7e709cff45c226d8514f44709bd72 100644 (file)
@@ -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;
index fe3376a163ee22ba6988dd627c83d9a36841e585..7f12dd6836d6e17836d517d0001b88a6d1e6dd42 100644 (file)
@@ -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);
   }
 
index 88ce05d3153709967cf3db66f717957f1891e8c1..7592ac2557f1ec5c22974f0bc0302ef991fdd993 100644 (file)
@@ -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;
index 409e5a9191b07ea2d41b9c648acc97daa9be7600..3f4c4fcd16fcf138ad669073c3ed5b8435912dc9 100644 (file)
@@ -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();
index 6631d1720525b7e15223d4b4e61cf47e946c361b..3a5010b690b4b755221087035c1749555adf260d 100644 (file)
@@ -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
index a641ab0d04864efb77ea71ec234ea86111bbc169..e66cd4a096d263e5c24059dc26124c90adcac0d8 100644 (file)
@@ -89,7 +89,7 @@ public class ExportActionTest {
     ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class);
     when(activeRuleIndex.findByProfile(Matchers.anyString())).thenReturn(Sets.<ActiveRuleDoc>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),
index f6abb47f65b133462449c1d615914519f379ba3a..5bcf35f40a4fbdad6df27624a9dc90326d2b8e3a 100644 (file)
@@ -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