summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-05-16 18:58:39 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-05-19 07:44:53 +0200
commit6f2ee1969a1283ec5462a4527f2da8651bf2338d (patch)
tree22a39f637c68da1dbb71c4648b30f0e4f77583ad
parentb5a43ea82cb32a0c58db22ae4b081f35eb6e6cf5 (diff)
downloadsonarqube-6f2ee1969a1283ec5462a4527f2da8651bf2338d.tar.gz
sonarqube-6f2ee1969a1283ec5462a4527f2da8651bf2338d.zip
SONAR-5007 - ActiveRule2 on ES 1.1.1
-rw-r--r--sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java2
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java24
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java54
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java4
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java103
5 files changed, 138 insertions, 49 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java
index f6d7a1483f4..5421b67c5ee 100644
--- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java
+++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/ActiveRuleKey.java
@@ -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());
}
}
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java
index 28ddaf44cc8..ccfcb59e422 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleDoc.java
@@ -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;
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
index 94841e32396..6b4a3148818 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
@@ -39,16 +39,24 @@
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;
}
}
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java
index 3124ac9a6d6..1fc29a110d8 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleNormalizer.java
@@ -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)
diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java
index 8c8070bfac0..9b879315305 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexMediumTest.java
@@ -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);
}