Search in sources :

Example 1 with RuleQuery

use of org.sonar.api.rules.RuleQuery in project sonarqube by SonarSource.

the class CachingRuleFinderTest method findAll_returns_all_rules_which_match_exactly_all_criteria_and_order_then_by_most_recent_first.

@Test
public void findAll_returns_all_rules_which_match_exactly_all_criteria_and_order_then_by_most_recent_first() {
    String repoKey = "ABCD";
    String ruleKey = "EFGH";
    String configKey = "IJKL";
    RuleDefinitionDto[] rules = { dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setRuleKey(ruleKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())) };
    RuleQuery allQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey).withConfigKey(configKey);
    RuleQuery ruleAndConfigKeyQuery = RuleQuery.create().withKey(ruleKey).withConfigKey(configKey);
    RuleQuery repoAndConfigKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withConfigKey(configKey);
    RuleQuery repoAndKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey);
    RuleQuery configKeyQuery = RuleQuery.create().withConfigKey(configKey);
    RuleQuery ruleKeyQuery = RuleQuery.create().withKey(ruleKey);
    RuleQuery repoKeyQuery = RuleQuery.create().withRepositoryKey(repoKey);
    CachingRuleFinder underTest = new CachingRuleFinder(dbClient);
    assertThat(underTest.findAll(allQuery)).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(rules[0].getKey());
    assertThat(underTest.findAll(ruleAndConfigKeyQuery)).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(rules[1].getKey(), rules[0].getKey());
    assertThat(underTest.findAll(repoAndConfigKeyQuery)).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(rules[2].getKey(), rules[0].getKey());
    assertThat(underTest.findAll(repoAndKeyQuery)).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(rules[0].getKey());
    assertThat(underTest.findAll(repoKeyQuery)).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(rules[2].getKey(), rules[0].getKey());
    assertThat(underTest.findAll(ruleKeyQuery)).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(rules[1].getKey(), rules[0].getKey());
    assertThat(underTest.findAll(configKeyQuery)).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(rules[2].getKey(), rules[1].getKey(), rules[0].getKey());
}
Also used : Rule(org.sonar.api.rules.Rule) Arrays(java.util.Arrays) RuleQuery(org.sonar.api.rules.RuleQuery) RuleDao(org.sonar.db.rule.RuleDao) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) DbSession(org.sonar.db.DbSession) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Nullable(javax.annotation.Nullable) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RuleParamDto(org.sonar.db.rule.RuleParamDto) RuleKey(org.sonar.api.rule.RuleKey) Mockito.mock(org.mockito.Mockito.mock) RuleQuery(org.sonar.api.rules.RuleQuery) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 2 with RuleQuery

use of org.sonar.api.rules.RuleQuery in project sonarqube by SonarSource.

the class CachingRuleFinderTest method find_searches_by_exact_match_and_match_on_all_criterias_and_returns_most_recent_match.

@Test
public void find_searches_by_exact_match_and_match_on_all_criterias_and_returns_most_recent_match() {
    String repoKey = "ABCD";
    String ruleKey = "EFGH";
    String configKey = "IJKL";
    RuleDefinitionDto[] rules = { dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setRuleKey(ruleKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRuleKey(ruleKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())) };
    RuleQuery allQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey).withConfigKey(configKey);
    RuleQuery ruleAndConfigKeyQuery = RuleQuery.create().withKey(ruleKey).withConfigKey(configKey);
    RuleQuery repoAndConfigKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withConfigKey(configKey);
    RuleQuery repoAndKeyQuery = RuleQuery.create().withRepositoryKey(repoKey).withKey(ruleKey);
    RuleQuery configKeyQuery = RuleQuery.create().withConfigKey(configKey);
    RuleQuery ruleKeyQuery = RuleQuery.create().withKey(ruleKey);
    RuleQuery repoKeyQuery = RuleQuery.create().withRepositoryKey(repoKey);
    CachingRuleFinder underTest = new CachingRuleFinder(dbClient);
    assertThat(toRuleKey(underTest.find(allQuery))).isEqualTo(rules[0].getKey());
    assertThat(toRuleKey(underTest.find(ruleAndConfigKeyQuery))).isEqualTo(rules[1].getKey());
    assertThat(toRuleKey(underTest.find(repoAndConfigKeyQuery))).isEqualTo(rules[2].getKey());
    assertThat(toRuleKey(underTest.find(repoAndKeyQuery))).isEqualTo(rules[0].getKey());
    assertThat(toRuleKey(underTest.find(repoKeyQuery))).isEqualTo(rules[2].getKey());
    assertThat(toRuleKey(underTest.find(ruleKeyQuery))).isEqualTo(rules[1].getKey());
    assertThat(toRuleKey(underTest.find(configKeyQuery))).isEqualTo(rules[2].getKey());
}
Also used : Rule(org.sonar.api.rules.Rule) Arrays(java.util.Arrays) RuleQuery(org.sonar.api.rules.RuleQuery) RuleDao(org.sonar.db.rule.RuleDao) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) DbSession(org.sonar.db.DbSession) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Nullable(javax.annotation.Nullable) Before(org.junit.Before) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) AlwaysIncreasingSystem2(org.sonar.api.impl.utils.AlwaysIncreasingSystem2) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RuleParamDto(org.sonar.db.rule.RuleParamDto) RuleKey(org.sonar.api.rule.RuleKey) Mockito.mock(org.mockito.Mockito.mock) RuleQuery(org.sonar.api.rules.RuleQuery) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Aggregations

Arrays (java.util.Arrays)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Collectors.toList (java.util.stream.Collectors.toList)2 Nullable (javax.annotation.Nullable)2 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Before (org.junit.Before)2 Test (org.junit.Test)2 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)2 Mockito.when (org.mockito.Mockito.when)2 AlwaysIncreasingSystem2 (org.sonar.api.impl.utils.AlwaysIncreasingSystem2)2 RuleKey (org.sonar.api.rule.RuleKey)2 Rule (org.sonar.api.rules.Rule)2 RuleQuery (org.sonar.api.rules.RuleQuery)2 System2 (org.sonar.api.utils.System2)2 DbClient (org.sonar.db.DbClient)2