use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.
the class RegisterRulesTest method update_if_repository_and_key_changed_and_deprecated_key_declared_among_others.
@Test
public void update_if_repository_and_key_changed_and_deprecated_key_declared_among_others() {
String ruleKey1 = "rule1";
String ruleKey2 = "rule2";
String repository1 = "fake1";
String repository2 = "fake2";
system.setNow(DATE1.getTime());
execute(context -> {
NewRepository repo = context.createRepository(repository1, "java");
repo.createRule(ruleKey1).setName("Name1").setHtmlDescription("Description");
repo.done();
});
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository1, ruleKey1));
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getUuids()).containsOnly(rule1.getUuid());
system.setNow(DATE2.getTime());
execute(context -> {
NewRepository repo = context.createRepository(repository2, "java");
repo.createRule(ruleKey2).setName("Name2").setHtmlDescription("Description").addDeprecatedRuleKey("foo", "bar").addDeprecatedRuleKey(repository1, ruleKey1).addDeprecatedRuleKey("some", "noise");
repo.done();
});
// rule2 is actually rule1
RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository2, ruleKey2));
assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid());
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions()).getUuids()).containsOnly(rule1.getUuid());
}
use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.
the class RegisterRulesTest method update_only_rule_description.
@Test
public void update_only_rule_description() {
system.setNow(DATE1.getTime());
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
repo.createRule("rule").setName("Name").setHtmlDescription("Desc1");
repo.done();
});
system.setNow(DATE2.getTime());
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
repo.createRule("rule").setName("Name").setHtmlDescription("Desc2");
repo.done();
});
// rule1 has been updated
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of("fake", "rule"));
assertThat(rule1.getName()).isEqualTo("Name");
assertThat(rule1.getDescription()).isEqualTo("Desc2");
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc2"), new SearchOptions()).getTotal()).isOne();
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc1"), new SearchOptions()).getTotal()).isZero();
}
use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.
the class RegisterRulesTest method declaring_a_rule_with_a_deprecated_RuleKey_still_used_should_throw_ISE.
@Test
public void declaring_a_rule_with_a_deprecated_RuleKey_still_used_should_throw_ISE() {
assertThatThrownBy(() -> {
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
createRule(repo, "newKey1");
createRule(repo, "newKey2").addDeprecatedRuleKey("fake", "newKey1");
repo.done();
});
}).isInstanceOf(IllegalStateException.class).hasMessage("The following rule keys are declared both as deprecated and used key [fake:newKey1]");
}
use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.
the class RegisterRulesTest method insert_then_remove_rule.
@Test
public void insert_then_remove_rule() {
String ruleKey = randomAlphanumeric(5);
// register one rule
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
repo.createRule(ruleKey).setName(randomAlphanumeric(5)).setHtmlDescription(randomAlphanumeric(20));
repo.done();
});
// verify db
List<RuleDefinitionDto> rules = dbClient.ruleDao().selectAllDefinitions(db.getSession());
assertThat(rules).extracting(RuleDefinitionDto::getKey).extracting(RuleKey::rule).containsExactly(ruleKey);
RuleDefinitionDto rule = rules.iterator().next();
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).containsExactly(rule.getUuid());
verifyIndicesMarkedAsInitialized();
// register no rule
execute(context -> context.createRepository("fake", "java").done());
// verify db
assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).extracting(RuleDefinitionDto::getKey).extracting(RuleKey::rule).containsExactly(ruleKey);
assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).extracting(RuleDefinitionDto::getStatus).containsExactly(REMOVED);
// verify index
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).isEmpty();
verifyIndicesNotMarkedAsInitialized();
}
use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.
the class RegisterRulesTest method update_if_rule_key_renamed_and_deprecated_key_declared.
@Test
public void update_if_rule_key_renamed_and_deprecated_key_declared() {
String ruleKey1 = "rule1";
String ruleKey2 = "rule2";
String repository = "fake";
system.setNow(DATE1.getTime());
execute(context -> {
NewRepository repo = context.createRepository(repository, "java");
repo.createRule(ruleKey1).setName("Name1").setHtmlDescription("Description");
repo.done();
});
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository, ruleKey1));
SearchIdResult<String> searchRule1 = ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions());
assertThat(searchRule1.getUuids()).containsOnly(rule1.getUuid());
assertThat(searchRule1.getTotal()).isOne();
system.setNow(DATE2.getTime());
execute(context -> {
NewRepository repo = context.createRepository(repository, "java");
repo.createRule(ruleKey2).setName("Name2").setHtmlDescription("Description").addDeprecatedRuleKey(repository, ruleKey1);
repo.done();
});
// rule2 is actually rule1
RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository, ruleKey2));
assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid());
assertThat(rule2.getName()).isEqualTo("Name2");
assertThat(rule2.getDescription()).isEqualTo(rule1.getDescription());
SearchIdResult<String> searchRule2 = ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions());
assertThat(searchRule2.getUuids()).containsOnly(rule2.getUuid());
assertThat(searchRule2.getTotal()).isOne();
assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getTotal()).isZero();
}
Aggregations