Skip to content

Commit

Permalink
AddedSONAR-5237 - Added all tags and updated IndexMediumTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane Gamard committed May 1, 2014
1 parent 2248191 commit 2f41365
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ protected XContentBuilder getMapping() throws IOException {
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
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;
Expand Down Expand Up @@ -149,29 +151,29 @@ public XContentBuilder normalize(RuleDto rule) throws IOException {

/* 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);
Expand All @@ -184,7 +186,7 @@ public XContentBuilder normalize(RuleDto rule) throws IOException {

/* 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
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;

Expand Down Expand Up @@ -129,8 +133,6 @@ public void insert_and_index_rule_parameters() {
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);
Expand Down Expand Up @@ -162,6 +164,60 @@ public void insert_and_index_activeRules() {
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()
Expand Down

0 comments on commit 2f41365

Please sign in to comment.