assertThat(index.search(query, new QueryContext()).getHits()).hasSize(1);
}
@Test
public void search_by_characteristics_with_default_and_overridden_char() throws InterruptedException {
CharacteristicDto char1 = DebtTesting.newCharacteristicDto("RELIABILITY");
db.debtCharacteristicDao().insert(char1, dbSession);
CharacteristicDto char11 = DebtTesting.newCharacteristicDto("SOFT_RELIABILITY")
.setParentId(char1.getId());
db.debtCharacteristicDao().insert(char11, dbSession);
dbSession.commit();
CharacteristicDto char2 = DebtTesting.newCharacteristicDto("TESTABILITY");
db.debtCharacteristicDao().insert(char2, dbSession);
CharacteristicDto char21 = DebtTesting.newCharacteristicDto("UNIT_TESTABILITY")
.setParentId(char2.getId());
db.debtCharacteristicDao().insert(char21, dbSession);
dbSession.commit();
// Rule with only default sub characteristic -> should be find by char11 and char1
dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S001"))
.setSubCharacteristicId(char11.getId())
.setDefaultSubCharacteristicId(null));
// Rule with only sub characteristic -> should be find by char11 and char1
dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S002"))
.setSubCharacteristicId(null)
.setDefaultSubCharacteristicId(char11.getId()));
// Rule with both default sub characteristic and overridden sub characteristic -> should only be find by char21 and char2
dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S003"))
.setSubCharacteristicId(char21.getId()))
.setDefaultSubCharacteristicId(char11.getId());
// Rule with both default sub characteristic and overridden sub characteristic and with same values -> should be find by char11 and char1
dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("java", "S004"))
.setSubCharacteristicId(char11.getId()))
.setDefaultSubCharacteristicId(char11.getId());
dbSession.commit();
dbSession.clearCache();
RuleQuery query;
Result<Rule> results;
// 0. we have 4 rules in index
results = index.search(new RuleQuery(), new QueryContext());
assertThat(results.getHits()).hasSize(4);
// filter by subChar
query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char11.getKey()));
assertThat(ruleKeys(index.search(query, new QueryContext()).getHits())).containsOnly("S001", "S002", "S004");
query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char21.getKey()));
assertThat(ruleKeys(index.search(query, new QueryContext()).getHits())).containsOnly("S003");
// filter by Char
query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char1.getKey()));
assertThat(ruleKeys(index.search(query, new QueryContext()).getHits())).containsOnly("S001", "S002", "S004");
query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char2.getKey()));
assertThat(ruleKeys(index.search(query, new QueryContext()).getHits())).containsOnly("S003");
// filter by Char and SubChar
query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char11.getKey(), char1.getKey(), char2.getKey(), char21.getKey()));
assertThat(ruleKeys(index.search(query, new QueryContext()).getHits())).containsOnly("S001", "S002", "S003", "S004");
}