diff options
4 files changed, 78 insertions, 10 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java index 2348f893be4..9537f169f90 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDoc.java @@ -121,7 +121,7 @@ class RuleDoc implements Rule { @Override public String defaultValue() { - return (String) param.get(RuleNormalizer.RuleParamField.DEFAULT_VALUE.key()); + return (String) param.get(RuleNormalizer.RuleParamField.VALUE.key()); } @Override diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java index dbd380c89bf..22c5dece4f3 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java @@ -24,6 +24,7 @@ import org.sonar.api.rule.RuleKey; import org.sonar.check.Cardinality; import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.rule.RuleRuleTagDto; @@ -32,7 +33,9 @@ import org.sonar.server.search.BaseNormalizer; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -78,7 +81,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { NAME("name"), TYPE("type"), DESCRIPTION("description"), - DEFAULT_VALUE("default"); + VALUE("value"); private final String key; @@ -105,7 +108,8 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { NOTE_USER("noteUser"), PROFILE_ID("profile"), SEVERITY("severity"), - PARENT_ID("parent"); + PARENT_ID("parent"), + PARAMS("params"); private final String key; @@ -171,14 +175,16 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { /* Normalize the params */ List<RuleParamDto> params = ruleDao.selectParametersByRuleId(rule.getId()); + Map<Integer, String> paramIdNameLookup = new HashMap<Integer, String>(); if (!params.isEmpty()) { document.startArray(RuleField.PARAMS.key()); for (RuleParamDto param : params) { + paramIdNameLookup.put(param.getId(), param.getName()); document.startObject(); indexField(RuleParamField.NAME.key(), param.getName(), document); indexField(RuleParamField.TYPE.key(), param.getType(), document); indexField(RuleParamField.DESCRIPTION.key(), param.getDescription(), document); - indexField(RuleParamField.DEFAULT_VALUE.key(), param.getDefaultValue(), document); + indexField(RuleParamField.VALUE.key(), param.getDefaultValue(), document); document.endObject(); } document.endArray(); @@ -199,6 +205,19 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { indexField(ActiveRuleField.PROFILE_ID.key(), activeRule.getProfileId(), document); indexField(ActiveRuleField.SEVERITY.key(), activeRule.getSeverityString(), document); indexField(ActiveRuleField.PARENT_ID.key(), activeRule.getParentId(), document); + + /* Get all activeRuleParams */ + List<ActiveRuleParamDto> activeRuleParams = activeRuleDao.selectParamsByActiveRuleId(activeRule.getId()); + if(!activeRuleParams.isEmpty()) { + document.startArray(ActiveRuleField.PARAMS.key()); + for (ActiveRuleParamDto param : activeRuleParams) { + document.startObject(); + indexField(RuleParamField.NAME.key(), param.getKey(), document); + indexField(RuleParamField.VALUE.key(), param.getValue(), document); + document.endObject(); + } + document.endArray(); + } document.endObject(); } document.endArray(); @@ -207,5 +226,4 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { /* Done normalizing for Rule */ return document.endObject(); } - } diff --git a/sonar-server/src/main/java/org/sonar/server/search/Hit.java b/sonar-server/src/main/java/org/sonar/server/search/Hit.java index 5f1bdc09480..e31ae2904b5 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/Hit.java +++ b/sonar-server/src/main/java/org/sonar/server/search/Hit.java @@ -20,7 +20,6 @@ package org.sonar.server.search; import javax.annotation.CheckForNull; -import java.util.HashMap; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; @@ -37,7 +36,7 @@ public class Hit implements Comparable<Hit> { this.rank = rank; } - public Hit(Float score){ + public Hit(Float score) { this.fields = new TreeMap<String, Object>(); this.score = score; } diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java index 93ddfb70424..c40a88bfdc8 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java @@ -33,6 +33,7 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.rule.RuleRuleTagDto; @@ -43,6 +44,7 @@ import org.sonar.server.search.Hit; import org.sonar.server.tester.ServerTester; import java.util.List; +import java.util.Map; import static org.fest.assertions.Assertions.assertThat; @@ -86,9 +88,10 @@ public class RuleServiceMediumTest { assertThat(hit.getField(RuleNormalizer.RuleField.UPDATED_AT.key())).isNotNull(); assertThat(hit.getFieldAsString(RuleNormalizer.RuleField.INTERNAL_KEY.key())).isEqualTo("InternalKeyS001"); assertThat(hit.getFieldAsString(RuleNormalizer.RuleField.SEVERITY.key())).isEqualTo("INFO"); -//TODO assertThat((Collection) hit.getField(RuleNormalizer.RuleField.SYSTEM_TAGS.key())).isEmpty(); -//TODO assertThat((Collection) hit.getField(RuleNormalizer.RuleField.TAGS.key())).isEmpty(); assertThat((Boolean) hit.getField(RuleNormalizer.RuleField.TEMPLATE.key())).isFalse(); + + //TODO assertThat((Collection) hit.getField(RuleNormalizer.RuleField.SYSTEM_TAGS.key())).isEmpty(); + //TODO assertThat((Collection) hit.getField(RuleNormalizer.RuleField.TAGS.key())).isEmpty(); } @Test @@ -164,6 +167,54 @@ public class RuleServiceMediumTest { Rule rule = service.getByKey(ruleKey); } + @Test + public void insert_and_index_activeRuleParams() { + DbSession dbSession = tester.get(MyBatis.class).openSession(false); + ActiveRuleDao activeRuleDao = tester.get(ActiveRuleDao.class); + // insert db + RuleKey ruleKey = RuleKey.of("javascript", "S001"); + RuleDto ruleDto = newRuleDto(ruleKey); + dao.insert(ruleDto, dbSession); + + RuleParamDto ruleParam = new RuleParamDto() + .setRuleId(ruleDto.getId()) + .setName("test") + .setType("STRING"); + dao.insert(ruleParam); + + ActiveRuleDto activeRule = new ActiveRuleDto() + .setInheritance("inherited") + .setProfileId(1) + .setRuleId(ruleDto.getId()) + .setSeverity(Severity.BLOCKER); + activeRuleDao.insert(activeRule, dbSession); + + ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto() + .setActiveRuleId(activeRule.getId()) + .setKey(ruleParam.getName()) + .setValue("world") + .setRulesParameterId(ruleParam.getId()); + activeRuleDao.insert(activeRuleParam, dbSession); + + dbSession.commit(); + + // verify that activeRulesParams are persisted in db + List<ActiveRuleParamDto> persistedDtos = activeRuleDao.selectParamsByActiveRuleId(activeRule.getId()); + assertThat(persistedDtos).hasSize(1); + + // verify that activeRulesParams are indexed in es + index.refresh(); + Hit hit = index.getByKey(ruleKey); + assertThat(hit).isNotNull(); + + //Check that we have an ActiveRuleParam + List<Map<String, Object>> activeRules = (List<Map<String, Object>>) hit.getField(RuleNormalizer.RuleField.ACTIVE.key()); + assertThat(Iterables.getFirst(activeRules, null).get(RuleNormalizer.ActiveRuleField.PARAMS.key())).isNotNull(); + + List<Map<String, String>> params = (List<Map<String, String>>) activeRules.get(0).get(RuleNormalizer.ActiveRuleField.PARAMS.key()); + assertThat(Iterables.getFirst(params,null).get(RuleNormalizer.RuleParamField.NAME.key())).isEqualTo(ruleParam.getName()); + } + //TODO test delete, update, tags, params @Test @@ -183,7 +234,7 @@ public class RuleServiceMediumTest { .setTag("AdMiN"); ruleTagDao.insert(tag1,dbSession); ruleTagDao.insert(tag2,dbSession); - ruleTagDao.insert(tag3,dbSession); + ruleTagDao.insert(tag3, dbSession); RuleRuleTagDto rTag1 = new RuleRuleTagDto() .setTagId(tag1.getId()) |