]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - Put back ActiveRule in RuleResult with ES 1.1.1
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 16 May 2014 17:55:06 +0000 (19:55 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Mon, 19 May 2014 05:44:54 +0000 (07:44 +0200)
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java
sonar-server/src/main/java/org/sonar/server/rule2/index/RuleResult.java
sonar-server/src/test/java/org/sonar/server/rule2/ws/RulesWebServiceTest.java
sonar-server/src/test/resources/org/sonar/server/rule2/ws/RulesWebServiceTest/search_active_rules_params.json

index ccfcb59e422d20811090af9ac76888aac160a541..a2f6e8329f666da09c7fb7a4b6ed01a02937bd64 100644 (file)
@@ -54,11 +54,12 @@ public class ActiveRuleDoc implements ActiveRule {
   @Override
   public ActiveRule.Inheritance inheritance() {
     String inheritance = (String) this.fields.get(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.key());
-    if(inheritance == null || inheritance.isEmpty()){
+    if(inheritance == null || inheritance.isEmpty() ||
+      inheritance.toLowerCase().contains("none")){
       return Inheritance.NONE;
-    } else if(inheritance.toLowerCase().indexOf("herit") > 0) {
+    } else if(inheritance.toLowerCase().contains("herit")) {
       return Inheritance.INHERIT;
-    } else if(inheritance.toLowerCase().indexOf("over") > 0) {
+    } else if(inheritance.toLowerCase().contains("over")) {
       return Inheritance.OVERRIDE;
     } else {
       throw new IllegalStateException("Value \"" +inheritance+"\" is not valid for rule's inheritance");
index 123ae3142c900f67c7619ae39bacbcf6127c250b..48c55f912364081078a49077e1a49c3cd7eef85a 100644 (file)
@@ -26,6 +26,8 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.server.db.DbClient;
+import org.sonar.server.qualityprofile.ActiveRule;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
 import org.sonar.server.rule2.index.RuleIndex;
 import org.sonar.server.rule2.index.RuleNormalizer;
 import org.sonar.server.rule2.index.RuleQuery;
@@ -44,10 +46,12 @@ import java.util.Set;
 public class RuleService implements ServerComponent {
 
   private final RuleIndex index;
+  private final ActiveRuleIndex activeRuleIndex;
   private final DbClient db;
 
-  public RuleService(RuleIndex index, DbClient db) {
+  public RuleService(ActiveRuleIndex activeRuleIndex, RuleIndex index, DbClient db) {
     this.index = index;
+    this.activeRuleIndex = activeRuleIndex;
     this.db = db;
   }
 
@@ -64,7 +68,14 @@ public class RuleService implements ServerComponent {
     // keep only supported fields and add the fields to always return
     options.filterFieldsToReturn(RuleIndex.PUBLIC_FIELDS);
     options.addFieldsToReturn(RuleNormalizer.RuleField.REPOSITORY.key(), RuleNormalizer.RuleField.KEY.key());
-    return index.search(query, options);
+
+    RuleResult result =  index.search(query, options);
+    for(Rule rule:result.getHits()){
+      for(ActiveRule activeRule:activeRuleIndex.findByRule(rule.key())){
+        result.getActiveRules().put(rule.key().toString(),activeRule);
+      }
+    }
+    return result;
   }
 
   /**
index 1689bd64f482cf0d894d1460c704baed06aaa7ac..d96551f649e15bd9ff055edd4d53e804f604a800 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.server.rule2.index;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Multimap;
 import org.elasticsearch.action.search.SearchResponse;
-import org.sonar.server.qualityprofile.index.ActiveRuleDoc;
+import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.rule2.Rule;
 import org.sonar.server.search.Result;
 
@@ -31,7 +31,7 @@ import java.util.Map;
 
 public class RuleResult extends Result<Rule> {
 
-  private Multimap<String,ActiveRuleDoc> activeRules;
+  private Multimap<String,ActiveRule> activeRules;
 
   public RuleResult(SearchResponse response) {
     super(response);
@@ -47,7 +47,7 @@ public class RuleResult extends Result<Rule> {
     return super.getHits();
   }
 
-  public  Multimap<String,ActiveRuleDoc> getActiveRules() {
+  public  Multimap<String,ActiveRule> getActiveRules() {
     return this.activeRules;
   }
 }
index 398063e15005e30aa6c5db6d0aa1a078ce9ae64b..66d9733aae9ec93c697a133d92f09c8405f410ad 100644 (file)
@@ -23,7 +23,6 @@ import com.google.common.collect.ImmutableSet;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
@@ -139,8 +138,6 @@ public class RulesWebServiceTest {
 
 
   @Test
-  @Ignore
-  //FIXME
   public void search_active_rules() throws Exception {
     QualityProfileDto profile = newQualityProfile();
     tester.get(QualityProfileDao.class).insert(profile, session);
@@ -165,8 +162,6 @@ public class RulesWebServiceTest {
   }
 
   @Test
-  @Ignore
-  //FIXME
   public void search_active_rules_params() throws Exception {
     QualityProfileDto profile = newQualityProfile();
     tester.get(QualityProfileDao.class).insert(profile, session);
@@ -197,6 +192,10 @@ public class RulesWebServiceTest {
     ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param)
       .setValue("The VALUE");
     tester.get(ActiveRuleDao.class).addParam(activeRule, activeRuleParam, session);
+
+    ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2)
+      .setValue("The Other Value");
+    tester.get(ActiveRuleDao.class).addParam(activeRule, activeRuleParam2, session);
     session.commit();
 
     tester.get(RuleService.class).refresh();
index dc72a0b1a7af7fb4d70e533ac25a162edbeb5abd..f0fa30a25fbb936b35f0ef9549622bdf7a13d498 100644 (file)
@@ -1,38 +1,52 @@
-{"total": 1,"p":1,"ps":25, "rules": [
-    {
-        "key": "java:S001",
-        "repo": "java",
-        "lang": "js",
-        "name": "Rule S001",
-        "htmlDesc": "Description S001",
-        "status": "READY",
-        "template": false,
-        "internalKey": "InternalKeyS001",
-        "severity": "INFO",
-        "tags": [],
-        "sysTags": [],
-        "debtRemediationFunctionType": "LINEAR",
-        "debtRemediationFunctionCoefficient": "1h",
-        "debtRemediationFunctionOffset": "5min",
-        "params": [
-            {
-                "key": "my_var",
-                "desc": "My small description",
-                "defaultValue": "some value"
-            }
-        ],
-        "actives": [
-            {
-                "key": "My Profile:java:java:S001",
-                "inherit": "NONE",
-                "severity": "BLOCKER",
-                "params": [
-                    {
-                        "key": "my_var",
-                        "value": "The VALUE"
-                    }
-                ]
-            }
-        ]
-    }
-]}
+{
+    "p": 1,
+    "ps": 25,
+    "rules": [
+        {
+            "actives": [
+                {
+                    "inherit": "NONE",
+                    "key": "My Profile:java:java:S001",
+                    "params": [
+                        {
+                            "key": "the_var",
+                            "value": "The Other Value"
+                        },
+                        {
+                            "key": "my_var",
+                            "value": "The VALUE"
+                        }
+                    ],
+                    "severity": "BLOCKER"
+                }
+            ],
+            "debtRemediationFunctionCoefficient": "1h",
+            "debtRemediationFunctionOffset": "5min",
+            "debtRemediationFunctionType": "LINEAR",
+            "htmlDesc": "Description S001",
+            "internalKey": "InternalKeyS001",
+            "key": "java:S001",
+            "lang": "js",
+            "name": "Rule S001",
+            "params": [
+                {
+                    "defaultValue": "some value",
+                    "desc": "My small description",
+                    "key": "my_var"
+                },
+                {
+                    "defaultValue": "other value",
+                    "desc": "My small description",
+                    "key": "the_var"
+                }
+            ],
+            "repo": "java",
+            "severity": "INFO",
+            "status": "READY",
+            "sysTags": [],
+            "tags": [],
+            "template": false
+        }
+    ],
+    "total": 1
+}