Search in sources :

Example 36 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RegisterRulesTest method disable_then_enable_rule.

@Test
public void disable_then_enable_rule() throws Exception {
    // Install rule
    when(system.now()).thenReturn(DATE1.getTime());
    execute(new FakeRepositoryV1());
    // Uninstall rule
    when(system.now()).thenReturn(DATE2.getTime());
    execute();
    RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
    assertThat(rule.getStatus()).isEqualTo(RuleStatus.REMOVED);
    assertThat(ruleIndex.search(new RuleQuery().setKey(RULE_KEY1.toString()), new SearchOptions()).getTotal()).isEqualTo(0);
    // Re-install rule
    when(system.now()).thenReturn(DATE3.getTime());
    execute(new FakeRepositoryV1());
    rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
    assertThat(rule.getStatus()).isEqualTo(RuleStatus.BETA);
    assertThat(ruleIndex.search(new RuleQuery().setKey(RULE_KEY1.toString()), new SearchOptions()).getTotal()).isEqualTo(1);
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 37 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RegisterRulesTest method update_only_rule_name.

@Test
public void update_only_rule_name() throws Exception {
    when(system.now()).thenReturn(DATE1.getTime());
    execute(new RulesDefinition() {

        @Override
        public void define(Context context) {
            NewRepository repo = context.createRepository("fake", "java");
            repo.createRule("rule").setName("Name1").setHtmlDescription("Description");
            repo.done();
        }
    });
    when(system.now()).thenReturn(DATE2.getTime());
    execute(new RulesDefinition() {

        @Override
        public void define(Context context) {
            NewRepository repo = context.createRepository("fake", "java");
            repo.createRule("rule").setName("Name2").setHtmlDescription("Description");
            repo.done();
        }
    });
    // rule1 has been updated
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RuleKey.of("fake", "rule"));
    assertThat(rule1.getName()).isEqualTo("Name2");
    assertThat(rule1.getDescription()).isEqualTo("Description");
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions()).getTotal()).isEqualTo(1);
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getTotal()).isEqualTo(0);
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleDto(org.sonar.db.rule.RuleDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 38 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RegisterRulesTest method insert_new_rules.

@Test
public void insert_new_rules() {
    execute(new FakeRepositoryV1());
    // verify db
    assertThat(dbClient.ruleDao().selectAll(dbTester.getSession())).hasSize(2);
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
    assertThat(rule1.getName()).isEqualTo("One");
    assertThat(rule1.getDescription()).isEqualTo("Description of One");
    assertThat(rule1.getSeverityString()).isEqualTo(BLOCKER);
    assertThat(rule1.getTags()).isEmpty();
    assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag2", "tag3");
    assertThat(rule1.getConfigKey()).isEqualTo("config1");
    assertThat(rule1.getStatus()).isEqualTo(RuleStatus.BETA);
    assertThat(rule1.getCreatedAt()).isEqualTo(DATE1.getTime());
    assertThat(rule1.getUpdatedAt()).isEqualTo(DATE1.getTime());
    assertThat(rule1.getDefaultRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
    assertThat(rule1.getDefaultRemediationGapMultiplier()).isEqualTo("5d");
    assertThat(rule1.getDefaultRemediationBaseEffort()).isEqualTo("10h");
    assertThat(rule1.getType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
    List<RuleParamDto> params = dbClient.ruleDao().selectRuleParamsByRuleKey(dbTester.getSession(), RULE_KEY1);
    assertThat(params).hasSize(2);
    RuleParamDto param = getParam(params, "param1");
    assertThat(param.getDescription()).isEqualTo("parameter one");
    assertThat(param.getDefaultValue()).isEqualTo("default1");
    // verify index
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY2);
    // verify repositories
    assertThat(dbClient.ruleRepositoryDao().selectAll(dbTester.getSession())).extracting(RuleRepositoryDto::getKey).containsOnly("fake");
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleParamDto(org.sonar.db.rule.RuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 39 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RuleCreatorMediumTest method create_custom_rule.

@Test
public void create_custom_rule() {
    // insert template rule
    RuleDto templateRule = createTemplateRule();
    // Create custom rule
    NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setMarkdownDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", "a.*"));
    RuleKey customRuleKey = creator.create(newRule);
    dbSession.clearCache();
    RuleDto rule = db.ruleDao().selectOrFailByKey(dbSession, customRuleKey);
    assertThat(rule).isNotNull();
    assertThat(rule.getKey()).isEqualTo(RuleKey.of("java", "CUSTOM_RULE"));
    assertThat(rule.getTemplateId()).isEqualTo(templateRule.getId());
    assertThat(rule.getName()).isEqualTo("My custom");
    assertThat(rule.getDescription()).isEqualTo("Some description");
    assertThat(rule.getSeverityString()).isEqualTo("MAJOR");
    assertThat(rule.getStatus()).isEqualTo(RuleStatus.READY);
    assertThat(rule.getLanguage()).isEqualTo("java");
    assertThat(rule.getConfigKey()).isEqualTo("S001");
    assertThat(rule.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET");
    assertThat(rule.getDefaultRemediationGapMultiplier()).isEqualTo("1h");
    assertThat(rule.getDefaultRemediationBaseEffort()).isEqualTo("5min");
    assertThat(rule.getGapDescription()).isEqualTo("desc");
    assertThat(rule.getTags()).containsOnly("usertag1", "usertag2");
    assertThat(rule.getSystemTags()).containsOnly("tag1", "tag4");
    List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
    assertThat(params).hasSize(1);
    RuleParamDto param = params.get(0);
    // From template rule
    assertThat(param.getName()).isEqualTo("regex");
    assertThat(param.getDescription()).isEqualTo("Reg ex");
    assertThat(param.getType()).isEqualTo("STRING");
    // From user
    assertThat(param.getDefaultValue()).isEqualTo("a.*");
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(customRuleKey, templateRule.getKey());
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleParamDto(org.sonar.db.rule.RuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Aggregations

RuleQuery (org.sonar.server.rule.index.RuleQuery)39 Test (org.junit.Test)34 SearchOptions (org.sonar.server.es.SearchOptions)22 RuleDto (org.sonar.db.rule.RuleDto)16 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)14 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)14 RuleIndex (org.sonar.server.rule.index.RuleIndex)7 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RuleKey (org.sonar.api.rule.RuleKey)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)4 RuleParamDto (org.sonar.db.rule.RuleParamDto)4 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)3 RuleActivation (org.sonar.server.qualityprofile.RuleActivation)3 RuleActivator (org.sonar.server.qualityprofile.RuleActivator)3 ActiveRuleIndex (org.sonar.server.qualityprofile.index.ActiveRuleIndex)3 StringReader (java.io.StringReader)2 QProfileService (org.sonar.server.qualityprofile.QProfileService)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1