Search in sources :

Example 21 with Rule

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

the class CachingRuleFinderTest method findAll_returns_all_rules_with_exact_same_repository_key_and_order_them_most_recent_first.

@Test
public void findAll_returns_all_rules_with_exact_same_repository_key_and_order_them_most_recent_first() {
    String repoKey = "ABCD";
    RuleDefinitionDto[] sameRepoKey = { dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setRepositoryKey(repoKey).setUpdatedAt(system2.now())) };
    RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now()));
    CachingRuleFinder underTest = new CachingRuleFinder(dbClient);
    assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(repoKey))).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(sameRepoKey[1].getKey(), sameRepoKey[0].getKey());
    assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(otherRule.getRepositoryKey()))).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(otherRule.getKey());
    assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(repoKey.toLowerCase()))).isEmpty();
    assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(randomAlphabetic(3)))).isEmpty();
}
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) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 22 with Rule

use of org.sonar.api.rules.Rule 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 23 with Rule

use of org.sonar.api.rules.Rule 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)

Example 24 with Rule

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

the class CachingRuleFinderTest method findAll_returns_all_rules_with_exact_same_configkey_and_order_them_most_recent_first.

@Test
public void findAll_returns_all_rules_with_exact_same_configkey_and_order_them_most_recent_first() {
    String configKey = "ABCD";
    RuleDefinitionDto[] sameConfigKey = { dbTester.rules().insert(rule -> rule.setConfigKey(configKey).setUpdatedAt(system2.now())), dbTester.rules().insert(rule -> rule.setConfigKey(configKey).setUpdatedAt(system2.now())) };
    RuleDefinitionDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now()));
    CachingRuleFinder underTest = new CachingRuleFinder(dbClient);
    assertThat(underTest.findAll(RuleQuery.create().withConfigKey(configKey))).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(sameConfigKey[1].getKey(), sameConfigKey[0].getKey());
    assertThat(underTest.findAll(RuleQuery.create().withConfigKey(otherRule.getConfigKey()))).extracting(CachingRuleFinderTest::toRuleKey).containsExactly(otherRule.getKey());
    assertThat(underTest.findAll(RuleQuery.create().withConfigKey(configKey.toLowerCase()))).isEmpty();
    assertThat(underTest.findAll(RuleQuery.create().withConfigKey(randomAlphabetic(3)))).isEmpty();
}
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) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 25 with Rule

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

the class CachingRuleFinderTest method find_returns_most_recent_rule_when_RuleQuery_has_no_non_null_field.

@Test
public void find_returns_most_recent_rule_when_RuleQuery_has_no_non_null_field() {
    Rule rule = underTest.find(RuleQuery.create());
    assertThat(toRuleKey(rule)).isEqualTo(ruleDefinitions[5].getKey());
}
Also used : Rule(org.sonar.api.rules.Rule) Test(org.junit.Test)

Aggregations

Rule (org.sonar.api.rules.Rule)31 Test (org.junit.Test)22 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)11 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 List (java.util.List)9 DbSession (org.sonar.db.DbSession)9 Arrays (java.util.Arrays)8 Consumer (java.util.function.Consumer)8 Collectors.toList (java.util.stream.Collectors.toList)8 Nullable (javax.annotation.Nullable)8 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Before (org.junit.Before)8 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.verify (org.mockito.Mockito.verify)8 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)8 Mockito.when (org.mockito.Mockito.when)8 AlwaysIncreasingSystem2 (org.sonar.api.impl.utils.AlwaysIncreasingSystem2)8 RuleKey (org.sonar.api.rule.RuleKey)8