diff options
author | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-01 19:50:02 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-01 19:50:02 +0200 |
commit | 2f413656329e579ebc61ce9fbbf034755d5c69c4 (patch) | |
tree | 4d25f7635bcff694f1a6621fb0cc6244da089a37 | |
parent | 2248191e14451c2b9d2f90e3309e982622bab7b1 (diff) | |
download | sonarqube-2f413656329e579ebc61ce9fbbf034755d5c69c4.tar.gz sonarqube-2f413656329e579ebc61ce9fbbf034755d5c69c4.zip |
AddedSONAR-5237 - Added all tags and updated IndexMediumTest
3 files changed, 77 insertions, 19 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java index f05e69d30bc..4301c850094 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java @@ -170,12 +170,12 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> { .endObject() .endObject(); - mapping.startObject("active") + mapping.startObject(RuleField.ACTIVE.key()) .field("type", "nested") .field("dynamic", true) .endObject(); - mapping.startObject("params") + mapping.startObject(RuleField.PARAMS.key()) .field("type", "nested") .field("dynamic", true) .endObject(); 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 1ec6e8ada15..dbd380c89bf 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 @@ -27,9 +27,11 @@ import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.rule.RuleRuleTagDto; +import org.sonar.core.rule.RuleTagType; import org.sonar.server.search.BaseNormalizer; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -149,29 +151,29 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { /* Normalize the tags */ List<RuleRuleTagDto> tags = ruleDao.selectTagsByRuleId(rule.getId()); - if (!tags.isEmpty()) { - XContentBuilder sysTags = document.startArray(RuleField.SYSTEM_TAGS.key()); - XContentBuilder adminTags = document.startArray(RuleField.TAGS.key()); - + if (tags != null && !tags.isEmpty()) { + ArrayList<String> sys = new ArrayList<String>(); + ArrayList<String> admin = new ArrayList<String>(); for (RuleRuleTagDto tag : tags) { - switch (tag.getType()) { - case SYSTEM: - sysTags.startObject(tag.getTag()).endObject(); - break; - case ADMIN: - adminTags.startObject(tag.getTag()).endObject(); - break; + if (tag.getType().equals(RuleTagType.SYSTEM)) { + sys.add(tag.getTag()); + } else { + admin.add(tag.getTag()); } } - sysTags.endArray(); - adminTags.endArray(); + if (!admin.isEmpty()) { + document.array(RuleField.TAGS.key(), admin.toArray(new String[admin.size()])); + } + if (!sys.isEmpty()) { + document.array(RuleField.SYSTEM_TAGS.key(), sys.toArray(new String[sys.size()])); + } } /* Normalize the params */ List<RuleParamDto> params = ruleDao.selectParametersByRuleId(rule.getId()); if (!params.isEmpty()) { document.startArray(RuleField.PARAMS.key()); - for (RuleParamDto param :params) { + for (RuleParamDto param : params) { document.startObject(); indexField(RuleParamField.NAME.key(), param.getName(), document); indexField(RuleParamField.TYPE.key(), param.getType(), document); @@ -184,7 +186,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { /* Normalize activeRules */ List<ActiveRuleDto> activeRules = activeRuleDao.selectByRuleId(rule.getId()); - if(!activeRules.isEmpty()) { + if (!activeRules.isEmpty()) { document.startArray(RuleField.ACTIVE.key()); for (ActiveRuleDto activeRule : activeRules) { document.startObject(); 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 51828128531..93ddfb70424 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 @@ -35,6 +35,10 @@ import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; +import org.sonar.core.rule.RuleRuleTagDto; +import org.sonar.core.rule.RuleTagDao; +import org.sonar.core.rule.RuleTagDto; +import org.sonar.core.rule.RuleTagType; import org.sonar.server.search.Hit; import org.sonar.server.tester.ServerTester; @@ -129,8 +133,6 @@ public class RuleServiceMediumTest { assertThat(Iterables.getLast(rule.params(), null).key()).isEqualTo("max"); } - //TODO test delete, update, tags, params - @Test public void insert_and_index_activeRules() { DbSession dbSession = tester.get(MyBatis.class).openSession(false); @@ -162,6 +164,60 @@ public class RuleServiceMediumTest { Rule rule = service.getByKey(ruleKey); } + //TODO test delete, update, tags, params + + @Test + public void insert_and_index_tags() { + DbSession dbSession = tester.get(MyBatis.class).openSession(false); + RuleTagDao ruleTagDao = tester.get(RuleTagDao.class); + + // insert db + RuleKey ruleKey = RuleKey.of("javascript", "S001"); + RuleDto ruleDto = newRuleDto(ruleKey); + dao.insert(ruleDto, dbSession); + RuleTagDto tag1 = new RuleTagDto() + .setTag("hello"); + RuleTagDto tag2 = new RuleTagDto() + .setTag("world"); + RuleTagDto tag3 = new RuleTagDto() + .setTag("AdMiN"); + ruleTagDao.insert(tag1,dbSession); + ruleTagDao.insert(tag2,dbSession); + ruleTagDao.insert(tag3,dbSession); + + RuleRuleTagDto rTag1 = new RuleRuleTagDto() + .setTagId(tag1.getId()) + .setRuleId(ruleDto.getId()) + .setType(RuleTagType.ADMIN); + RuleRuleTagDto rTag2 = new RuleRuleTagDto() + .setTagId(tag2.getId()) + .setRuleId(ruleDto.getId()) + .setType(RuleTagType.ADMIN); + RuleRuleTagDto rTag3 = new RuleRuleTagDto() + .setTagId(tag3.getId()) + .setRuleId(ruleDto.getId()) + .setType(RuleTagType.SYSTEM); + dao.insert(rTag1, dbSession); + dao.insert(rTag2, dbSession); + dao.insert(rTag3, dbSession); + dbSession.commit(); + + // verify that tags are persisted in db + List<RuleRuleTagDto> persistedDtos = dao.selectTagsByRuleId(ruleDto.getId()); + assertThat(persistedDtos).hasSize(3); + + // verify that tags are indexed in es + index.refresh(); + Hit hit = index.getByKey(ruleKey); + assertThat(hit).isNotNull(); + assertThat(hit.getField(RuleNormalizer.RuleField.TAGS.key())).isNotNull(); + + RuleService service = tester.get(RuleService.class); + Rule rule = service.getByKey(ruleKey); + assertThat(rule.tags()).containsExactly("hello","world"); + assertThat(rule.systemTags()).containsExactly("AdMiN"); + } + private RuleDto newRuleDto(RuleKey ruleKey) { return new RuleDto() |