From 6f2ee1969a1283ec5462a4527f2da8651bf2338d Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Fri, 16 May 2014 18:58:39 +0200 Subject: [PATCH] SONAR-5007 - ActiveRule2 on ES 1.1.1 --- .../core/qualityprofile/db/ActiveRuleKey.java | 2 +- .../qualityprofile/index/ActiveRuleDoc.java | 24 ++-- .../qualityprofile/index/ActiveRuleIndex.java | 54 ++++++++- .../index/ActiveRuleNormalizer.java | 4 +- .../index/ActiveRuleIndexMediumTest.java | 103 ++++++++++++------ 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 fields; private final ActiveRuleKey key; - public ActiveRuleDoc(ActiveRuleKey key, Map fields) { + public ActiveRuleDoc(Map 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 params() { Map params = new HashMap(); if (this.fields.containsKey(ActiveRuleNormalizer.ActiveRuleField.PARAMS.key())) { - Map> allParams = (Map>) this.fields.get(ActiveRuleNormalizer.ActiveRuleField.PARAMS.key()); - for (Map.Entry> param : allParams.entrySet()) { - params.put(param.getKey(), param.getValue().get(ActiveRuleNormalizer.ActiveRuleParamField.VALUE.key())); + List> allParams = (List>) this.fields.get(ActiveRuleNormalizer.ActiveRuleField.PARAMS.key()); + for (Map 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 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 activeRules = new ArrayList(); + 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 activeRules = (Map) 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 _activeRules = (Map) hit.getField(RuleNormalizer.RuleField.ACTIVE.key()); -// assertThat(_activeRules).isNotNull().hasSize(1); -// -// Map _activeRule = (Map) Iterables.getFirst(_activeRules.values(),null); -// assertThat(_activeRule.get(RuleNormalizer.RuleField.SEVERITY.key())).isEqualTo(Severity.BLOCKER); -// -// Map _activeRuleParams = (Map) _activeRule.get(RuleNormalizer.RuleField.PARAMS.key()); -// assertThat(_activeRuleParams).isNotNull().hasSize(2); -// -// Map _activeRuleParamValue = (Map) _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 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 hits = index.findByRule(RuleKey.of("javascript", "S001")); + + assertThat(hits).isNotNull(); + assertThat(hits).hasSize(2); } -- 2.39.5