]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 - ActiveRule2 on ES 1.1.1
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 16 May 2014 16:58:39 +0000 (18:58 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Mon, 19 May 2014 05:44:53 +0000 (07:44 +0200)
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java

index f6d7a1483f42ee74c1715dea5bcb8bc103b1d638..5421b67c5ee02af82db0d204618c314855b6eb45 100644 (file)
@@ -101,7 +101,7 @@ public class ActiveRuleKey implements Serializable {
    */
   @Override
   public String toString() {
-    return String.format("%s:%s", ruleKey.toString(), qualityProfileKey.toString());
+    return String.format("%s:%s",  qualityProfileKey.toString(), ruleKey.toString());
   }
 }
 
index 28ddaf44cc818ac6782e22e91884faa3c777bc18..ccfcb59e422d20811090af9ac76888aac160a541 100644 (file)
@@ -24,6 +24,7 @@ import org.sonar.server.qualityprofile.ActiveRule;
 
 import javax.annotation.CheckForNull;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 public class ActiveRuleDoc implements ActiveRule {
@@ -31,7 +32,11 @@ public class ActiveRuleDoc implements ActiveRule {
   private final Map<String, Object> fields;
   private final ActiveRuleKey key;
 
-  public ActiveRuleDoc(ActiveRuleKey key, Map<String, Object> fields) {
+  public ActiveRuleDoc(Map<String, Object> fields) {
+    ActiveRuleKey key = ActiveRuleKey.parse((String)fields.get(ActiveRuleNormalizer.ActiveRuleField.KEY.key()));
+    if(key == null){
+      throw new IllegalStateException("Invalid ActiveRuleKey!");
+    }
     this.fields = fields;
     this.key = key;
   }
@@ -49,10 +54,14 @@ 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()){
-      return Inheritance.valueOf(inheritance);
-    } else {
+    if(inheritance == null || inheritance.isEmpty()){
       return Inheritance.NONE;
+    } else if(inheritance.toLowerCase().indexOf("herit") > 0) {
+      return Inheritance.INHERIT;
+    } else if(inheritance.toLowerCase().indexOf("over") > 0) {
+      return Inheritance.OVERRIDE;
+    } else {
+      throw new IllegalStateException("Value \"" +inheritance+"\" is not valid for rule's inheritance");
     }
   }
 
@@ -70,9 +79,10 @@ public class ActiveRuleDoc implements ActiveRule {
   public Map<String, String> params() {
     Map<String, String> params = new HashMap<String, String>();
     if (this.fields.containsKey(ActiveRuleNormalizer.ActiveRuleField.PARAMS.key())) {
-      Map<String, Map<String, String>> allParams = (Map<String, Map<String, String>>) this.fields.get(ActiveRuleNormalizer.ActiveRuleField.PARAMS.key());
-      for (Map.Entry<String, Map<String, String>> param : allParams.entrySet()) {
-        params.put(param.getKey(), param.getValue().get(ActiveRuleNormalizer.ActiveRuleParamField.VALUE.key()));
+      List<Map<String, String>> allParams = (List<Map<String, String>>) this.fields.get(ActiveRuleNormalizer.ActiveRuleField.PARAMS.key());
+      for (Map<String, String> param : allParams) {
+        params.put(param.get(ActiveRuleNormalizer.ActiveRuleParamField.NAME.key()),
+          param.get(ActiveRuleNormalizer.ActiveRuleParamField.VALUE.key()));
       }
     }
     return params;
index 94841e323965b258594c9ef1d3cb889b1e3f317f..6b4a3148818ab5d37241e3371acfb82b831bcf86 100644 (file)
 package org.sonar.server.qualityprofile.index;
 
 import org.elasticsearch.action.get.GetResponse;
+import org.elasticsearch.action.search.SearchRequestBuilder;
+import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.sonar.api.rules.ActiveRule;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.search.SearchHit;
+import org.sonar.api.rule.RuleKey;
 import org.sonar.core.cluster.WorkQueue;
 import org.sonar.core.qualityprofile.db.ActiveRuleDto;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
 import org.sonar.server.es.ESNode;
+import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.rule2.index.RuleIndexDefinition;
 import org.sonar.server.search.BaseIndex;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
 
@@ -92,8 +100,18 @@ public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, Active
     addMatchField(mapping, ActiveRuleNormalizer.ActiveRuleField.SEVERITY.key(), "string");
     addMatchField(mapping, ActiveRuleNormalizer.ActiveRuleField.PROFILE_ID.key(), "string");
     mapping.startObject(ActiveRuleNormalizer.ActiveRuleField.PARAMS.key())
-      .field("type", "nested")
-      .endObject();
+      .field("type", "object")
+      .startObject("properties")
+      .startObject("_id")
+      .field("type","string")
+      .endObject()
+      .startObject(ActiveRuleNormalizer.ActiveRuleParamField.NAME.key())
+        .field("type", "string")
+        .endObject()
+        .startObject(ActiveRuleNormalizer.ActiveRuleParamField.VALUE.key())
+        .field("type", "string")
+        .endObject()
+        .endObject();
     mapping.endObject();
 
     mapping.endObject().endObject();
@@ -101,7 +119,35 @@ public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, Active
   }
 
   @Override
-  public ActiveRule toDoc(GetResponse key) {
+  public ActiveRule toDoc(GetResponse response) {
+    return new ActiveRuleDoc(response.getSource());
+  }
+
+  /** finder methods */
+  public List<ActiveRule> findByRule(RuleKey key){
+
+    SearchRequestBuilder request = getClient().prepareSearch(this.getIndexName())
+      .setQuery(QueryBuilders
+        .hasParentQuery(this.getParentType(),
+          QueryBuilders.idsQuery(this.getParentType())
+          .addIds(key.toString())))
+      .setRouting(key.toString());
+
+    SearchResponse response = request.get();
+
+    List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
+    for(SearchHit hit:response.getHits()){
+      activeRules.add(new ActiveRuleDoc(hit.getSource()));
+    }
+
+    return activeRules;
+  }
+
+  private String getParentType() {
+    return new RuleIndexDefinition().getIndexType();
+  }
+
+  public ActiveRule getByRuleKeyAndProfileKey(RuleKey ruleKey, QualityProfileKey qualityProfileKey){
     return null;
   }
 }
index 3124ac9a6d60330222cd1e8ef2eaf69853c5b8be..1fc29a110d83c80dba95014aa02be47612c7ea92 100644 (file)
@@ -103,7 +103,8 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
     newParam.put(ActiveRuleParamField.NAME.key(), param.getKey());
     newParam.put(ActiveRuleParamField.VALUE.key(), param.getValue());
 
-    return this.nestedUpsert(ActiveRuleField.PARAMS.key(), param.getKey(), newParam);
+    return this.nestedUpsert(ActiveRuleField.PARAMS.key(), param.getKey(), newParam)
+      .routing(key.ruleKey().toString());
   }
 
   @Override
@@ -135,6 +136,7 @@ public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRu
 
     /* Creating updateRequest */
    return new UpdateRequest()
+      .routing(key.ruleKey().toString())
       .id(rule.getKey().toString())
       .parent(rule.getKey().ruleKey().toString())
       .doc(newRule)
index 8c8070bfac00e4226606c70e734945138d3d4b8d..9b87931530574b3c3835c77cb5139387ee7daa0c 100644 (file)
@@ -35,15 +35,12 @@ import org.sonar.core.qualityprofile.db.QualityProfileDao;
 import org.sonar.core.qualityprofile.db.QualityProfileDto;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
+import org.sonar.server.qualityprofile.ActiveRule;
 import org.sonar.server.qualityprofile.persistence.ActiveRuleDao;
-import org.sonar.server.rule2.Rule;
-import org.sonar.server.rule2.index.RuleIndex;
-import org.sonar.server.rule2.index.RuleQuery;
-import org.sonar.server.rule2.index.RuleResult;
 import org.sonar.server.rule2.persistence.RuleDao;
-import org.sonar.server.search.QueryOptions;
 import org.sonar.server.tester.ServerTester;
 
+import java.util.Collection;
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -57,7 +54,7 @@ public class ActiveRuleIndexMediumTest {
   QualityProfileDao qualityProfileDao = tester.get(QualityProfileDao.class);
   ActiveRuleDao activeRuleDao = tester.get(ActiveRuleDao.class);
   RuleDao dao = tester.get(RuleDao.class);
-  RuleIndex index = tester.get(RuleIndex.class);
+  ActiveRuleIndex index = tester.get(ActiveRuleIndex.class);
   DbSession dbSession;
 
   @Before
@@ -84,7 +81,7 @@ public class ActiveRuleIndexMediumTest {
     dao.insert(ruleDto, dbSession);
 
     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, ruleDto)
-      .setInheritance("inherited")
+      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
       .setSeverity(Severity.BLOCKER);
 
     activeRuleDao.insert(activeRule, dbSession);
@@ -98,14 +95,13 @@ public class ActiveRuleIndexMediumTest {
     index.refresh();
 
 
-    Rule hit = index.getByKey(ruleKey);
-
+    ActiveRule hit = index.getByKey(activeRule.getKey());
 
     assertThat(hit).isNotNull();
-//    assertThat(hit.getField(RuleNormalizer.RuleField.ACTIVE.key())).isNotNull();
-//
-//    Map<String, Object> activeRules = (Map<String, Object>) hit.getField(RuleNormalizer.RuleField.ACTIVE.key());
-//    assertThat(activeRules).hasSize(1);
+    assertThat(hit.key()).isEqualTo(activeRule.getKey());
+    assertThat(hit.inheritance().name()).isEqualTo(activeRule.getInheritance());
+    assertThat(hit.parentKey()).isEqualTo(activeRule.getParentId());
+    assertThat(hit.severity()).isEqualTo(activeRule.getSeverityString());
   }
 
   @Test
@@ -132,7 +128,7 @@ public class ActiveRuleIndexMediumTest {
 
 
     ActiveRuleDto activeRule = ActiveRuleDto.createFor(profileDto, ruleDto)
-      .setInheritance("inherited")
+      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
       .setSeverity(Severity.BLOCKER);
     activeRuleDao.insert(activeRule, dbSession);
 
@@ -153,28 +149,63 @@ public class ActiveRuleIndexMediumTest {
     // verify that activeRulesParams are indexed in es
     index.refresh();
 
-    RuleResult results = index.search(new RuleQuery(), new QueryOptions());
-
-    assertThat(results.getActiveRules().values()).hasSize(1);
-
-
-//    Hit hit = index.getByKey(ruleKey);
-//    assertThat(hit).isNotNull();
-//
-//    index.search(new RuleQuery(), new QueryOptions());
-//
-//    Map<String, Map> _activeRules = (Map<String, Map>) hit.getField(RuleNormalizer.RuleField.ACTIVE.key());
-//    assertThat(_activeRules).isNotNull().hasSize(1);
-//
-//    Map<String, Object> _activeRule = (Map<String, Object>) Iterables.getFirst(_activeRules.values(),null);
-//    assertThat(_activeRule.get(RuleNormalizer.RuleField.SEVERITY.key())).isEqualTo(Severity.BLOCKER);
-//
-//    Map<String, Map> _activeRuleParams = (Map<String, Map>) _activeRule.get(RuleNormalizer.RuleField.PARAMS.key());
-//    assertThat(_activeRuleParams).isNotNull().hasSize(2);
-//
-//    Map<String, String> _activeRuleParamValue = (Map<String, String>) _activeRuleParams.get(maxParam.getName());
-//    assertThat(_activeRuleParamValue).isNotNull().hasSize(1);
-//    assertThat(_activeRuleParamValue.get(ActiveRuleNormalizer.ActiveRuleParamField.VALUE.key())).isEqualTo("maximum");
+    ActiveRule rule = index.getByKey(activeRule.getKey());
+    assertThat(rule.params()).hasSize(2);
+    assertThat(rule.params().keySet()).containsOnly("min","max");
+    assertThat(rule.params().values()).containsOnly("minimum","maximum");
+    assertThat(rule.params().get("min")).isEqualTo("minimum");
+  }
+
+  @Test
+  public void find_activeRules_by_ruleKey() throws InterruptedException {
+    QualityProfileDto profileDto = new QualityProfileDto()
+      .setName("myprofile")
+      .setLanguage("java");
+
+    QualityProfileDto profileDto2 = new QualityProfileDto()
+      .setName("other-profile")
+      .setLanguage("java");
+    qualityProfileDao.insert(profileDto, dbSession);
+    qualityProfileDao.insert(profileDto2, dbSession);
+
+    // insert db
+    RuleDto ruleDto = newRuleDto(RuleKey.of("javascript", "S001"));
+    dao.insert(ruleDto, dbSession);
+
+    // insert db
+    RuleDto ruleDto2 = newRuleDto(RuleKey.of("javascript", "S002"));
+    dao.insert(ruleDto2, dbSession);
+
+    ActiveRuleDto find1 = ActiveRuleDto.createFor(profileDto, ruleDto)
+      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
+      .setSeverity(Severity.BLOCKER);
+
+    ActiveRuleDto find2 = ActiveRuleDto.createFor(profileDto2, ruleDto)
+      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
+      .setSeverity(Severity.BLOCKER);
+
+    ActiveRuleDto notFound = ActiveRuleDto.createFor(profileDto2, ruleDto2)
+      .setInheritance(ActiveRule.Inheritance.INHERIT.name())
+      .setSeverity(Severity.BLOCKER);
+
+    activeRuleDao.insert(find1, dbSession);
+    activeRuleDao.insert(find2, dbSession);
+    activeRuleDao.insert(notFound, dbSession);
+    dbSession.commit();
+
+    // verify that activeRules are persisted in db
+    List<ActiveRuleDto> persistedDtos = activeRuleDao.findByRule(ruleDto, dbSession);
+    assertThat(persistedDtos).hasSize(2);
+    persistedDtos = activeRuleDao.findByRule(ruleDto2, dbSession);
+    assertThat(persistedDtos).hasSize(1);
+
+    // verify that activeRules are indexed in es
+    index.refresh();
+
+    Collection<ActiveRule> hits = index.findByRule(RuleKey.of("javascript", "S001"));
+
+    assertThat(hits).isNotNull();
+    assertThat(hits).hasSize(2);
 
   }