Search in sources :

Example 51 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class CompareActionMediumTest method createActiveRule.

private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) {
    ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString());
    db.activeRuleDao().insert(session, activeRule);
    return activeRule;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 52 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class RuleActivatorMediumTest method verifyHasActiveRuleInDb.

private void verifyHasActiveRuleInDb(ActiveRuleKey activeRuleKey, String expectedSeverity, @Nullable String expectedInheritance, Map<String, String> expectedParams) {
    // verify db
    boolean found = false;
    List<ActiveRuleDto> activeRuleDtos = db.activeRuleDao().selectByProfileKey(dbSession, activeRuleKey.qProfile());
    for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
        if (activeRuleDto.getKey().equals(activeRuleKey)) {
            found = true;
            assertThat(activeRuleDto.getSeverityString()).isEqualTo(expectedSeverity);
            assertThat(activeRuleDto.getInheritance()).isEqualTo(expectedInheritance);
            // Dates should be set
            assertThat(activeRuleDto.getCreatedAt()).isNotNull();
            assertThat(activeRuleDto.getUpdatedAt()).isNotNull();
            List<ActiveRuleParamDto> paramDtos = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
            assertThat(paramDtos).hasSize(expectedParams.size());
            for (Map.Entry<String, String> entry : expectedParams.entrySet()) {
                ActiveRuleParamDto paramDto = db.activeRuleDao().selectParamByKeyAndName(activeRuleDto.getKey(), entry.getKey(), dbSession);
                assertThat(paramDto).isNotNull();
                assertThat(paramDto.getValue()).isEqualTo(entry.getValue());
            }
        }
    }
    assertThat(found).as("Rule is not activated in db").isTrue();
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 53 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class ShowActionMediumTest method show_rule_when_activated.

@Test
public void show_rule_when_activated() throws Exception {
    RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("java", "S001")).setName("Rule S001").setDescription("Rule S001 <b>description</b>").setDescriptionFormat(Format.HTML).setSeverity(MINOR).setStatus(RuleStatus.BETA).setLanguage("xoo").setType(RuleType.BUG).setCreatedAt(new Date().getTime()).setUpdatedAt(new Date().getTime());
    ruleDao.insert(session, ruleDto);
    RuleParamDto regexParam = RuleParamDto.createFor(ruleDto).setName("regex").setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*");
    ruleDao.insertRuleParam(session, ruleDto, regexParam);
    QualityProfileDto profile = QualityProfileDto.createFor("profile").setOrganizationUuid(defaultOrganizationProvider.get().getUuid()).setName("Profile").setLanguage("xoo");
    tester.get(QualityProfileDao.class).insert(session, profile);
    ActiveRuleDto activeRuleDto = new ActiveRuleDto().setProfileId(profile.getId()).setRuleId(ruleDto.getId()).setSeverity(MINOR).setCreatedAt(new Date().getTime()).setUpdatedAt(new Date().getTime());
    tester.get(ActiveRuleDao.class).insert(session, activeRuleDto);
    tester.get(ActiveRuleDao.class).insertParam(session, activeRuleDto, new ActiveRuleParamDto().setRulesParameterId(regexParam.getId()).setKey(regexParam.getName()).setValue(".*?"));
    session.commit();
    session.clearCache();
    tester.get(RuleIndexer.class).index();
    tester.get(ActiveRuleIndexer.class).index();
    WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show").setParam("key", ruleDto.getKey().toString()).setParam("actives", "true");
    request.execute().assertJson(getClass(), "show_rule_when_activated.json");
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) RuleIndexer(org.sonar.server.rule.index.RuleIndexer) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Date(java.util.Date) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 54 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class SearchActionMediumTest method search_profile_active_rules.

@Test
public void search_profile_active_rules() throws Exception {
    QualityProfileDto profile = QProfileTesting.newXooP1("org-123");
    tester.get(QualityProfileDao.class).insert(dbSession, profile);
    QualityProfileDto profile2 = QProfileTesting.newXooP2("org-123");
    tester.get(QualityProfileDao.class).insert(dbSession, profile2);
    dbSession.commit();
    RuleDto rule = RuleTesting.newXooX1();
    ruleDao.insert(dbSession, rule);
    RuleParamDto param = RuleParamDto.createFor(rule).setDefaultValue("some value").setType("string").setDescription("My small description").setName("my_var");
    ruleDao.insertRuleParam(dbSession, rule, param);
    RuleParamDto param2 = RuleParamDto.createFor(rule).setDefaultValue("other value").setType("integer").setDescription("My small description").setName("the_var");
    ruleDao.insertRuleParam(dbSession, rule, param2);
    // SONAR-7083
    RuleParamDto param3 = RuleParamDto.createFor(rule).setDefaultValue(null).setType("string").setDescription("Empty Param").setName("empty_var");
    ruleDao.insertRuleParam(dbSession, rule, param3);
    ActiveRuleDto activeRule = newActiveRule(profile, rule);
    tester.get(ActiveRuleDao.class).insert(dbSession, activeRule);
    ActiveRuleDto activeRule2 = newActiveRule(profile2, rule);
    tester.get(ActiveRuleDao.class).insert(dbSession, activeRule2);
    ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param).setValue("The VALUE");
    tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule2, activeRuleParam);
    ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2).setValue("The Other Value");
    tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule2, activeRuleParam2);
    ActiveRuleParamDto activeRuleParam3 = ActiveRuleParamDto.createFor(param3).setValue(null);
    tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule2, activeRuleParam3);
    dbSession.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
    request.setParam(WebService.Param.TEXT_QUERY, "x1");
    request.setParam(PARAM_ACTIVATION, "true");
    request.setParam(PARAM_QPROFILE, profile2.getKey());
    request.setParam(WebService.Param.FIELDS, "actives");
    WsTester.Result result = request.execute();
    result.assertJson(this.getClass(), "search_profile_active_rules.json");
    tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(PARAM_ACTIVATION, "true").setParam(PARAM_QPROFILE, "unknown_profile").setParam(WebService.Param.FIELDS, "actives").execute().assertJson(this.getClass(), "search_no_rules.json");
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 55 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.

the class RegisterRulesMediumTest method update_active_rules_on_param_changes.

@Test
public void update_active_rules_on_param_changes() {
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1Rule = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
            // has default value
            x1Rule.createParam("min").setType(RuleParamType.INTEGER).setDefaultValue("5");
            // no default value
            x1Rule.createParam("format").setType(RuleParamType.STRING);
        }
    });
    // Create profile and activate rule
    logInAsQProfileAdministrator();
    db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1("org-123"));
    dbSession.commit();
    dbSession.clearCache();
    RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
    activation.setParameter("format", "txt");
    TESTER.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
    // Default value of "min" is changed, "format" is removed, "format2" is added, "max" is added with a default value
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1Rule = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
            x1Rule.createParam("min").setType(RuleParamType.INTEGER).setDefaultValue("6");
            x1Rule.createParam("format2").setType(RuleParamType.STRING);
            x1Rule.createParam("max").setType(RuleParamType.INTEGER).setDefaultValue("10");
        }
    });
    ActiveRuleDto activeRuleDto = db.activeRuleDao().selectOrFailByKey(dbSession, ActiveRuleKey.of(QProfileTesting.XOO_P1_KEY, RuleTesting.XOO_X1));
    List<ActiveRuleParamDto> params = db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(params).hasSize(2);
    Map<String, ActiveRuleParamDto> parmsByKey = FluentIterable.from(params).uniqueIndex(ActiveRuleParamToKey.INSTANCE);
    // do not change default value on existing active rules -> keep min=5
    assertThat(parmsByKey.get("min").getValue()).isEqualTo("5");
    // new param with default value
    assertThat(parmsByKey.get("max").getValue()).isEqualTo("10");
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) QProfileService(org.sonar.server.qualityprofile.QProfileService) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Aggregations

ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)55 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)26 Test (org.junit.Test)25 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)23 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)17 RuleDto (org.sonar.db.rule.RuleDto)17 RuleQuery (org.sonar.server.rule.index.RuleQuery)11 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)10 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)10 RuleKey (org.sonar.api.rule.RuleKey)7 WsTester (org.sonar.server.ws.WsTester)7 RuleParamDto (org.sonar.db.rule.RuleParamDto)6 SearchOptions (org.sonar.server.es.SearchOptions)6 StringReader (java.io.StringReader)5 RuleIndex (org.sonar.server.rule.index.RuleIndex)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 RuleActivator (org.sonar.server.qualityprofile.RuleActivator)4 RulesProfile (org.sonar.api.profiles.RulesProfile)3