}
private void reindexRule(RuleDto rule, SqlSession session) {
- Integer subCharacteristicId = rule.getSubCharacteristicId();
- CharacteristicDto subCharacteristic = subCharacteristicId != null ? characteristicDao.selectById(subCharacteristicId, session) : null;
- CharacteristicDto characteristic = subCharacteristic != null ? characteristicDao.selectById(subCharacteristic.getParentId(), session) : null;
- ruleRegistry.save(rule, characteristic, subCharacteristic, ruleDao.selectParametersByRuleId(rule.getId(), session), ruleDao.selectTagsByRuleIds(rule.getId(), session));
+ ruleRegistry.reindex(rule, session);
}
private void checkPermission(UserSession userSession) {
return reindex(ruleDao.selectEnablesAndNonManual(session), session);
}
+ /**
+ * Reindex a given list of rules
+ */
public String[] reindex(Collection<RuleDto> rules, SqlSession session) {
Multimap<Integer, RuleParamDto> paramsByRuleId = ArrayListMultimap.create();
Multimap<Integer, RuleRuleTagDto> tagsByRuleId = ArrayListMultimap.create();
return bulkIndexRules(rules, characteristicsById, paramsByRuleId, tagsByRuleId);
}
+ /**
+ * Reindex one rule
+ */
+ public void reindex(RuleDto rule) {
+ SqlSession sqlSession = myBatis.openSession();
+ try {
+ reindex(rule, sqlSession);
+ } finally {
+ sqlSession.close();
+ }
+ }
+
+ public void reindex(RuleDto rule, SqlSession session) {
+ try {
+ Integer ruleId = rule.getId();
+ Integer effectiveSubCharacteristicId = rule.getSubCharacteristicId() != null ? rule.getSubCharacteristicId() : rule.getDefaultSubCharacteristicId();
+ CharacteristicDto subCharacteristic = effectiveSubCharacteristicId != null ? characteristicDao.selectById(effectiveSubCharacteristicId, session) : null;
+ CharacteristicDto characteristic = subCharacteristic != null ? characteristicDao.selectById(subCharacteristic.getParentId(), session) : null;
+ searchIndex.putSynchronous(INDEX_RULES, TYPE_RULE, Long.toString(ruleId), ruleDocument(rule,
+ characteristic, subCharacteristic,
+ ruleDao.selectParametersByRuleIds(newArrayList(ruleId), session),
+ ruleDao.selectTagsByRuleIds(newArrayList(ruleId), session)));
+ } catch (IOException ioexception) {
+ throw new IllegalStateException("Unable to index rule with id=" + rule.getId(), ioexception);
+ }
+ }
+
+ private String[] bulkIndexRules(Collection<RuleDto> rules, Map<Integer, CharacteristicDto> characteristicsById, Multimap<Integer, RuleParamDto> paramsByRule,
+ Multimap<Integer, RuleRuleTagDto> tagsByRule) {
+ try {
+ String[] ids = new String[rules.size()];
+ BytesStream[] docs = new BytesStream[rules.size()];
+ int index = 0;
+ TimeProfiler profiler = new TimeProfiler();
+ profiler.start("Build rules documents");
+ for (RuleDto rule : rules) {
+ ids[index] = rule.getId().toString();
+ CharacteristicDto effectiveSubCharacteristic =
+ characteristicsById.get(rule.getSubCharacteristicId() != null ? rule.getSubCharacteristicId() : rule.getDefaultSubCharacteristicId());
+ CharacteristicDto effectiveCharacteristic = effectiveSubCharacteristic != null ? characteristicsById.get(effectiveSubCharacteristic.getParentId()) : null;
+ docs[index] = ruleDocument(rule, effectiveCharacteristic, effectiveSubCharacteristic, paramsByRule.get(rule.getId()), tagsByRule.get(rule.getId()));
+ index++;
+ }
+ profiler.stop();
+
+ if (!rules.isEmpty()) {
+ profiler.start("Index rules");
+ searchIndex.bulkIndex(INDEX_RULES, TYPE_RULE, ids, docs);
+ profiler.stop();
+ }
+ return ids;
+ } catch (IOException ioe) {
+ throw new IllegalStateException("Unable to index rules", ioe);
+ }
+ }
+
+ public void removeDeletedRules(String[] ids) {
+ List<String> indexIds = searchIndex.findDocumentIds(SearchQuery.create().index(INDEX_RULES).type(TYPE_RULE));
+ indexIds.removeAll(Arrays.asList(ids));
+ TimeProfiler profiler = new TimeProfiler();
+ if (!indexIds.isEmpty()) {
+ profiler.start("Remove deleted rule documents");
+ searchIndex.bulkDelete(INDEX_RULES, TYPE_RULE, indexIds.toArray(new String[0]));
+ profiler.stop();
+ }
+ }
+
+ private XContentBuilder ruleDocument(RuleDto rule, @Nullable CharacteristicDto characteristicDto, @Nullable CharacteristicDto subCharacteristicDto,
+ Collection<RuleParamDto> params, Collection<RuleRuleTagDto> tags) throws IOException {
+ XContentBuilder document = XContentFactory.jsonBuilder()
+ .startObject()
+ .field(RuleDocument.FIELD_ID, rule.getId())
+ .field(RuleDocument.FIELD_KEY, rule.getRuleKey())
+ .field(RuleDocument.FIELD_LANGUAGE, rule.getLanguage())
+ .field(RuleDocument.FIELD_NAME, rule.getName())
+ .field(RuleDocument.FIELD_DESCRIPTION, rule.getDescription())
+ .field(RuleDocument.FIELD_TEMPLATE_ID, rule.getParentId())
+ .field(RuleDocument.FIELD_REPOSITORY_KEY, rule.getRepositoryKey())
+ .field(RuleDocument.FIELD_SEVERITY, rule.getSeverityString())
+ .field(RuleDocument.FIELD_STATUS, rule.getStatus())
+ .field(RuleDocument.FIELD_CARDINALITY, rule.getCardinality())
+ .field(RuleDocument.FIELD_CREATED_AT, rule.getCreatedAt())
+ .field(RuleDocument.FIELD_UPDATED_AT, rule.getUpdatedAt());
+ if (characteristicDto != null && subCharacteristicDto != null) {
+ boolean isFunctionOverridden = rule.getRemediationFunction() != null;
+ document
+ .field(RuleDocument.FIELD_CHARACTERISTIC_ID, characteristicDto.getId())
+ .field(RuleDocument.FIELD_CHARACTERISTIC_KEY, characteristicDto.getKey())
+ .field(RuleDocument.FIELD_CHARACTERISTIC_NAME, characteristicDto.getName())
+ .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID, subCharacteristicDto.getId())
+ .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY, subCharacteristicDto.getKey())
+ .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME, subCharacteristicDto.getName())
+ .field(RuleDocument.FIELD_REMEDIATION_FUNCTION, isFunctionOverridden ? rule.getRemediationFunction() : rule.getDefaultRemediationFunction())
+ .field(RuleDocument.FIELD_REMEDIATION_COEFFICIENT, isFunctionOverridden ? rule.getRemediationCoefficient() : rule.getDefaultRemediationCoefficient())
+ .field(RuleDocument.FIELD_REMEDIATION_OFFSET, isFunctionOverridden ? rule.getRemediationOffset() : rule.getDefaultRemediationOffset());
+ }
+
+ if (rule.getNoteData() != null || rule.getNoteUserLogin() != null) {
+ document.startObject(RuleDocument.FIELD_NOTE)
+ .field(RuleDocument.FIELD_NOTE_DATA, rule.getNoteData())
+ .field(RuleDocument.FIELD_NOTE_USER_LOGIN, rule.getNoteUserLogin())
+ .field(RuleDocument.FIELD_NOTE_CREATED_AT, rule.getNoteCreatedAt())
+ .field(RuleDocument.FIELD_NOTE_UPDATED_AT, rule.getNoteUpdatedAt())
+ .endObject();
+ }
+ if (!params.isEmpty()) {
+ document.startArray(RuleDocument.FIELD_PARAMS);
+ for (RuleParamDto param : params) {
+ document.startObject()
+ .field(RuleDocument.FIELD_PARAM_KEY, param.getName())
+ .field(RuleDocument.FIELD_PARAM_TYPE, param.getType())
+ .field(RuleDocument.FIELD_PARAM_DEFAULT_VALUE, param.getDefaultValue())
+ .field(RuleDocument.FIELD_PARAM_DESCRIPTION, param.getDescription())
+ .endObject();
+ }
+ document.endArray();
+ }
+ List<String> systemTags = Lists.newArrayList();
+ List<String> adminTags = Lists.newArrayList();
+ for (RuleRuleTagDto tag : tags) {
+ if (tag.getType() == RuleTagType.SYSTEM) {
+ systemTags.add(tag.getTag());
+ } else {
+ adminTags.add(tag.getTag());
+ }
+ }
+ if (!systemTags.isEmpty()) {
+ document.array(RuleDocument.FIELD_SYSTEM_TAGS, systemTags.toArray());
+ }
+ if (!adminTags.isEmpty()) {
+ document.array(RuleDocument.FIELD_ADMIN_TAGS, adminTags.toArray());
+ }
+
+ document.endObject();
+ return document;
+ }
+
+
/**
* <p>Find rule IDs matching the given criteria.</p>
*
}
}
- public void save(RuleDto rule, @Nullable CharacteristicDto characteristicDto, @Nullable CharacteristicDto subCharacteristicDto,
- Collection<RuleParamDto> params, Collection<RuleRuleTagDto> tags) {
- try {
- searchIndex.putSynchronous(INDEX_RULES, TYPE_RULE, Long.toString(rule.getId()), ruleDocument(rule, characteristicDto, subCharacteristicDto, params, tags));
- } catch (IOException ioexception) {
- throw new IllegalStateException("Unable to index rule with id=" + rule.getId(), ioexception);
- }
- }
-
@CheckForNull
public Rule findByKey(RuleKey key) {
final SearchHits hits = searchIndex.executeRequest(searchIndex.client().prepareSearch(INDEX_RULES).setTypes(TYPE_RULE)
return RuleDocumentParser.parse(hits.hits()[0].sourceAsMap());
}
}
-
- private String[] bulkIndexRules(Collection<RuleDto> rules, Map<Integer, CharacteristicDto> characteristicsById, Multimap<Integer, RuleParamDto> paramsByRule,
- Multimap<Integer, RuleRuleTagDto> tagsByRule) {
- try {
- String[] ids = new String[rules.size()];
- BytesStream[] docs = new BytesStream[rules.size()];
- int index = 0;
- TimeProfiler profiler = new TimeProfiler();
- profiler.start("Build rules documents");
- for (RuleDto rule : rules) {
- ids[index] = rule.getId().toString();
- CharacteristicDto effectiveSubCharacteristic =
- characteristicsById.get(rule.getSubCharacteristicId() != null ? rule.getSubCharacteristicId() : rule.getDefaultSubCharacteristicId());
- CharacteristicDto effectiveCharacteristic = effectiveSubCharacteristic != null ? characteristicsById.get(effectiveSubCharacteristic.getParentId()) : null;
- docs[index] = ruleDocument(rule, effectiveCharacteristic, effectiveSubCharacteristic, paramsByRule.get(rule.getId()), tagsByRule.get(rule.getId()));
- index++;
- }
- profiler.stop();
-
- if (!rules.isEmpty()) {
- profiler.start("Index rules");
- searchIndex.bulkIndex(INDEX_RULES, TYPE_RULE, ids, docs);
- profiler.stop();
- }
- return ids;
- } catch (IOException ioe) {
- throw new IllegalStateException("Unable to index rules", ioe);
- }
- }
-
- public void removeDeletedRules(String[] ids) {
- List<String> indexIds = searchIndex.findDocumentIds(SearchQuery.create().index(INDEX_RULES).type(TYPE_RULE));
- indexIds.removeAll(Arrays.asList(ids));
- TimeProfiler profiler = new TimeProfiler();
- if (!indexIds.isEmpty()) {
- profiler.start("Remove deleted rule documents");
- searchIndex.bulkDelete(INDEX_RULES, TYPE_RULE, indexIds.toArray(new String[0]));
- profiler.stop();
- }
- }
-
- private XContentBuilder ruleDocument(RuleDto rule, @Nullable CharacteristicDto characteristicDto, @Nullable CharacteristicDto subCharacteristicDto,
- Collection<RuleParamDto> params, Collection<RuleRuleTagDto> tags) throws IOException {
- XContentBuilder document = XContentFactory.jsonBuilder()
- .startObject()
- .field(RuleDocument.FIELD_ID, rule.getId())
- .field(RuleDocument.FIELD_KEY, rule.getRuleKey())
- .field(RuleDocument.FIELD_LANGUAGE, rule.getLanguage())
- .field(RuleDocument.FIELD_NAME, rule.getName())
- .field(RuleDocument.FIELD_DESCRIPTION, rule.getDescription())
- .field(RuleDocument.FIELD_TEMPLATE_ID, rule.getParentId())
- .field(RuleDocument.FIELD_REPOSITORY_KEY, rule.getRepositoryKey())
- .field(RuleDocument.FIELD_SEVERITY, rule.getSeverityString())
- .field(RuleDocument.FIELD_STATUS, rule.getStatus())
- .field(RuleDocument.FIELD_CARDINALITY, rule.getCardinality())
- .field(RuleDocument.FIELD_CREATED_AT, rule.getCreatedAt())
- .field(RuleDocument.FIELD_UPDATED_AT, rule.getUpdatedAt());
- if (characteristicDto != null && subCharacteristicDto != null) {
- boolean isFunctionOverridden = rule.getRemediationFunction() != null;
- document
- .field(RuleDocument.FIELD_CHARACTERISTIC_ID, characteristicDto.getId())
- .field(RuleDocument.FIELD_CHARACTERISTIC_KEY, characteristicDto.getKey())
- .field(RuleDocument.FIELD_CHARACTERISTIC_NAME, characteristicDto.getName())
- .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID, subCharacteristicDto.getId())
- .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY, subCharacteristicDto.getKey())
- .field(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME, subCharacteristicDto.getName())
- .field(RuleDocument.FIELD_REMEDIATION_FUNCTION, isFunctionOverridden ? rule.getRemediationFunction() : rule.getDefaultRemediationFunction())
- .field(RuleDocument.FIELD_REMEDIATION_COEFFICIENT, isFunctionOverridden ? rule.getRemediationCoefficient() : rule.getDefaultRemediationCoefficient())
- .field(RuleDocument.FIELD_REMEDIATION_OFFSET, isFunctionOverridden ? rule.getRemediationOffset() : rule.getDefaultRemediationOffset());
- }
-
- if (rule.getNoteData() != null || rule.getNoteUserLogin() != null) {
- document.startObject(RuleDocument.FIELD_NOTE)
- .field(RuleDocument.FIELD_NOTE_DATA, rule.getNoteData())
- .field(RuleDocument.FIELD_NOTE_USER_LOGIN, rule.getNoteUserLogin())
- .field(RuleDocument.FIELD_NOTE_CREATED_AT, rule.getNoteCreatedAt())
- .field(RuleDocument.FIELD_NOTE_UPDATED_AT, rule.getNoteUpdatedAt())
- .endObject();
- }
- if (!params.isEmpty()) {
- document.startArray(RuleDocument.FIELD_PARAMS);
- for (RuleParamDto param : params) {
- document.startObject()
- .field(RuleDocument.FIELD_PARAM_KEY, param.getName())
- .field(RuleDocument.FIELD_PARAM_TYPE, param.getType())
- .field(RuleDocument.FIELD_PARAM_DEFAULT_VALUE, param.getDefaultValue())
- .field(RuleDocument.FIELD_PARAM_DESCRIPTION, param.getDescription())
- .endObject();
- }
- document.endArray();
- }
- List<String> systemTags = Lists.newArrayList();
- List<String> adminTags = Lists.newArrayList();
- for (RuleRuleTagDto tag : tags) {
- if (tag.getType() == RuleTagType.SYSTEM) {
- systemTags.add(tag.getTag());
- } else {
- adminTags.add(tag.getTag());
- }
- }
- if (!systemTags.isEmpty()) {
- document.array(RuleDocument.FIELD_SYSTEM_TAGS, systemTags.toArray());
- }
- if (!adminTags.isEmpty()) {
- document.array(RuleDocument.FIELD_ADMIN_TAGS, adminTags.toArray());
- }
-
- document.endObject();
- return document;
- }
}
assertThat(argumentCaptor.getValue().getNoteUpdatedAt()).isEqualTo(now);
verify(session).commit();
- verify(ruleRegistry).save(eq(rule), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(ruleParams), eq(ruleTags));
+ verify(ruleRegistry).reindex(eq(rule), eq(session));
}
@Test
assertThat(argumentCaptor.getValue().getNoteUpdatedAt()).isEqualTo(now);
verify(session).commit();
- verify(ruleRegistry).save(eq(rule), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(ruleParams), eq(ruleTags));
+ verify(ruleRegistry).reindex(eq(rule), eq(session));
}
@Test
assertThat(argumentCaptor.getValue().getNoteUpdatedAt()).isNull();
verify(session).commit();
- verify(ruleRegistry).save(eq(rule), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(ruleParams), eq(ruleTags));
+ verify(ruleRegistry).reindex(eq(rule), eq(session));
}
@Test
assertThat(ruleTagArgument.getValue().getType()).isEqualTo(RuleTagType.SYSTEM);
verify(session).commit();
- verify(ruleRegistry).save(eq(ruleArgument.getValue()), isNull(CharacteristicDto.class), isNull(CharacteristicDto.class),
- anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry).reindex(eq(ruleArgument.getValue()), eq(session));
}
@Test
assertThat(ruleParamArgument.getValue().getDefaultValue()).isEqualTo("21");
verify(session).commit();
- verify(ruleRegistry).save(eq(ruleArgument.getValue()), any(CharacteristicDto.class), any(CharacteristicDto.class),
- eq(newArrayList(ruleParamArgument.getValue())), eq(ruleTags));
+ verify(ruleRegistry).reindex(eq(ruleArgument.getValue()), eq(session));
}
@Test
assertThat(ruleCaptor.getValue().getStatus()).isEqualTo(Rule.STATUS_REMOVED);
assertThat(ruleCaptor.getValue().getUpdatedAt()).isEqualTo(now);
- verify(ruleRegistry).save(eq(ruleCaptor.getValue()), any(CharacteristicDto.class), any(CharacteristicDto.class), eq(newArrayList(param)), eq(ruleTags));
+ verify(ruleRegistry).reindex(eq(ruleCaptor.getValue()), eq(session));
verify(activeRuleDao).deleteParameters(eq(activeRuleId), eq(session));
verify(activeRuleDao).deleteFromRule(eq(ruleId), eq(session));
verify(session, times(2)).commit();
assertThat(result.getUpdatedAt()).isEqualTo(now);
verify(session).commit();
- verify(ruleRegistry).save(eq(result), eq(characteristic), eq(subCharacteristic), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry).reindex(eq(result), eq(session));
}
@Test
verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
verify(session, never()).commit();
- verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
}
@Test
assertThat(result.getUpdatedAt()).isEqualTo(now);
verify(session).commit();
- verify(ruleRegistry).save(eq(result), isNull(CharacteristicDto.class), isNull(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry).reindex(eq(result), eq(session));
}
@Test
verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
verify(session, never()).commit();
- verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
}
@Test
verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
verify(session, never()).commit();
- verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
}
@Test
verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
verify(session, never()).commit();
- verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
}
@Test
verify(ruleDao, never()).update(any(RuleDto.class), eq(session));
verify(session, never()).commit();
- verify(ruleRegistry, never()).save(any(RuleDto.class), any(CharacteristicDto.class), any(CharacteristicDto.class), anyListOf(RuleParamDto.class), anyListOf(RuleRuleTagDto.class));
+ verify(ruleRegistry, never()).reindex(any(RuleDto.class), eq(session));
}
}
}
@Test
- public void index_rules() {
+ public void index_new_rules() {
when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
new RuleDto().setId(3).setRepositoryKey("repo").setRuleKey("key").setSeverity(Severity.MINOR).setNoteData("noteData").setNoteUserLogin("userLogin")
.setDefaultSubCharacteristicId(11).setDefaultRemediationFunction("LINEAR_OFFSET").setDefaultRemediationCoefficient("1h").setDefaultRemediationOffset("15min")
}
@Test
- public void index_overridden_characteristic_if_both_default_and_overridden_characteristics_exists() {
+ public void reindex_existing_rules() {
+ assertThat(esSetup.exists("rules", "rule", "3")).isTrue();
+
+ // Update severity to MAJOR
+ when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
+ new RuleDto().setId(3).setRepositoryKey("repo").setRuleKey("key").setSeverity(Severity.MAJOR)
+ ));
+
+ registry.reindex();
+
+ Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(3)).execute().actionGet().getSourceAsMap();
+ assertThat(ruleDocument.get(RuleDocument.FIELD_ID)).isEqualTo(3);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REPOSITORY_KEY)).isEqualTo("repo");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_KEY)).isEqualTo("key");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SEVERITY)).isEqualTo("MAJOR");
+ }
+
+ @Test
+ public void index_overridden_characteristic_if_both_default_and_overridden_characteristics_exists_when_indexing_rules() {
when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
new RuleDto().setId(10).setRepositoryKey("repo").setRuleKey("key1").setSeverity(Severity.MINOR)
// default and overridden debt values are set
.setSubCharacteristicId(13).setRemediationFunction("LINEAR_OFFSET").setRemediationCoefficient("1h").setRemediationOffset("15min")
));
- when(characteristicDao.selectCharacteristicsByIds(newHashSet(11, 13), session)).thenReturn(newArrayList(
- new CharacteristicDto().setId(11).setKey("MODULARITY").setName("Modularity").setParentId(10),
- new CharacteristicDto().setId(13).setKey("COMPILER").setName("Compiler").setParentId(12)
- ));
+ // Characteristics
when(characteristicDao.selectCharacteristicsByIds(newHashSet(10, 12), session)).thenReturn(newArrayList(
new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability"),
new CharacteristicDto().setId(12).setKey("PORTABILITY").setName("Portability")
));
+ // Sub-characteristics
+ when(characteristicDao.selectCharacteristicsByIds(newHashSet(11, 13), session)).thenReturn(newArrayList(
+ new CharacteristicDto().setId(11).setKey("MODULARITY").setName("Modularity").setParentId(10),
+ new CharacteristicDto().setId(13).setKey("COMPILER").setName("Compiler").setParentId(12)
+ ));
+
registry.reindex();
Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(10)).execute().actionGet().getSourceAsMap();
}
@Test
- public void index_overridden_function_if_both_default_and_overridden_functions_exists() {
+ public void index_overridden_function_if_both_default_and_overridden_functions_exists_when_indexing_rules() {
when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
new RuleDto().setId(10).setRepositoryKey("repo").setRuleKey("key1").setSeverity(Severity.MINOR)
// default and overridden debt values are set
assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isNull();
}
+ @Test
+ public void index_one_rule() {
+ RuleDto ruleDto = new RuleDto().setId(3).setRepositoryKey("repo").setRuleKey("key").setSeverity(Severity.MINOR).setNoteData("noteData").setNoteUserLogin("userLogin")
+ .setDefaultSubCharacteristicId(11).setDefaultRemediationFunction("LINEAR_OFFSET").setDefaultRemediationCoefficient("1h").setDefaultRemediationOffset("15min");
+
+ when(ruleDao.selectParametersByRuleIds(newArrayList(3), session)).thenReturn(newArrayList(
+ new RuleParamDto().setRuleId(3).setName("name")
+ ));
+
+ when(ruleDao.selectTagsByRuleIds(newArrayList(3), session)).thenReturn(newArrayList(
+ new RuleRuleTagDto().setRuleId(3).setTag("tag1").setType(RuleTagType.SYSTEM),
+ new RuleRuleTagDto().setRuleId(3).setTag("tag2").setType(RuleTagType.SYSTEM),
+ new RuleRuleTagDto().setRuleId(3).setTag("tag").setType(RuleTagType.ADMIN)));
+
+ when(characteristicDao.selectById(11, session)).thenReturn(new CharacteristicDto().setId(11).setKey("MODULARITY").setName("Modularity").setParentId(10));
+ when(characteristicDao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability"));
+
+ registry.reindex(ruleDto);
+
+ Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(3)).execute().actionGet().getSourceAsMap();
+ assertThat(ruleDocument.get(RuleDocument.FIELD_ID)).isEqualTo(3);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REPOSITORY_KEY)).isEqualTo("repo");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_KEY)).isEqualTo("key");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SEVERITY)).isEqualTo("MINOR");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_NOTE)).isNotNull();
+
+ assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_PARAMS)).hasSize(1);
+ assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_SYSTEM_TAGS)).hasSize(2);
+ assertThat((List<String>) ruleDocument.get(RuleDocument.FIELD_ADMIN_TAGS)).hasSize(1);
+
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(10);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_NAME)).isEqualTo("Reusability");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(11);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME)).isEqualTo("Modularity");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_FUNCTION)).isEqualTo("LINEAR_OFFSET");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_COEFFICIENT)).isEqualTo("1h");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isEqualTo("15min");
+ }
+
+ @Test
+ public void reindex_existing_rule() {
+ // Update severity to MAJOR
+ RuleDto ruleDto = new RuleDto().setId(3).setRepositoryKey("repo").setRuleKey("key").setSeverity(Severity.MAJOR);
+
+ assertThat(esSetup.exists("rules", "rule", "3")).isTrue();
+
+ registry.reindex(ruleDto);
+
+ Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(3)).execute().actionGet().getSourceAsMap();
+ assertThat(ruleDocument.get(RuleDocument.FIELD_ID)).isEqualTo(3);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REPOSITORY_KEY)).isEqualTo("repo");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_KEY)).isEqualTo("key");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SEVERITY)).isEqualTo("MAJOR");
+ }
+
+ @Test
+ public void index_overridden_characteristic_if_both_default_and_overridden_characteristics_exists_when_indexing_one_rule() {
+ RuleDto ruleDto = new RuleDto().setId(10).setRepositoryKey("repo").setRuleKey("key1").setSeverity(Severity.MINOR)
+ // default and overridden debt values are set
+ .setDefaultSubCharacteristicId(11).setDefaultRemediationFunction("LINEAR").setDefaultRemediationCoefficient("2h")
+ .setSubCharacteristicId(13).setRemediationFunction("LINEAR_OFFSET").setRemediationCoefficient("1h").setRemediationOffset("15min");
+
+ when(characteristicDao.selectById(12, session)).thenReturn(new CharacteristicDto().setId(12).setKey("REUSABILITY").setName("Reusability"));
+ when(characteristicDao.selectById(13, session)).thenReturn(new CharacteristicDto().setId(13).setKey("MODULARITY").setName("Modularity").setParentId(12));
+
+ registry.reindex(ruleDto);
+
+ Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(10)).execute().actionGet().getSourceAsMap();
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(12);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_NAME)).isEqualTo("Reusability");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(13);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME)).isEqualTo("Modularity");
+
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_FUNCTION)).isEqualTo("LINEAR_OFFSET");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_COEFFICIENT)).isEqualTo("1h");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isEqualTo("15min");
+ }
+
+ @Test
+ public void index_overridden_function_if_both_default_and_overridden_functions_exists_when_indexing_one_rule() {
+ RuleDto ruleDto = new RuleDto().setId(10).setRepositoryKey("repo").setRuleKey("key1").setSeverity(Severity.MINOR)
+ // default and overridden debt values are set
+ .setDefaultSubCharacteristicId(11).setDefaultRemediationFunction("CONSTANT_ISSUE").setDefaultRemediationOffset("15min")
+ .setSubCharacteristicId(11).setRemediationFunction("LINEAR").setRemediationCoefficient("1h");
+
+ when(characteristicDao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setKey("REUSABILITY").setName("Reusability"));
+ when(characteristicDao.selectById(11, session)).thenReturn(new CharacteristicDto().setId(11).setKey("MODULARITY").setName("Modularity").setParentId(10));
+
+ registry.reindex(ruleDto);
+
+ Map<String, Object> ruleDocument = esSetup.client().prepareGet("rules", "rule", Integer.toString(10)).execute().actionGet().getSourceAsMap();
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_ID)).isEqualTo(10);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_KEY)).isEqualTo("REUSABILITY");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_CHARACTERISTIC_NAME)).isEqualTo("Reusability");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_ID)).isEqualTo(11);
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_KEY)).isEqualTo("MODULARITY");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_SUB_CHARACTERISTIC_NAME)).isEqualTo("Modularity");
+
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_FUNCTION)).isEqualTo("LINEAR");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_COEFFICIENT)).isEqualTo("1h");
+ assertThat(ruleDocument.get(RuleDocument.FIELD_REMEDIATION_OFFSET)).isNull();
+ }
@Test
public void remove_all_rules_when_ro_rule_registered() {