Search in sources :

Example 1 with ActiveRuleDao

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

the class RuleActivator method doInsert.

private ActiveRuleDto doInsert(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
    ActiveRuleDto activeRule;
    ActiveRuleDao dao = db.activeRuleDao();
    activeRule = ActiveRuleDto.createFor(context.profile(), context.rule());
    String severity = change.getSeverity();
    if (severity != null) {
        activeRule.setSeverity(severity);
    }
    ActiveRule.Inheritance inheritance = change.getInheritance();
    if (inheritance != null) {
        activeRule.setInheritance(inheritance.name());
    }
    activeRule.setUpdatedAt(system2.now());
    activeRule.setCreatedAt(system2.now());
    dao.insert(dbSession, activeRule);
    for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
        if (param.getValue() != null) {
            ActiveRuleParamDto paramDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
            paramDto.setValue(param.getValue());
            dao.insertParam(dbSession, activeRule, paramDto);
        }
    }
    return activeRule;
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Map(java.util.Map)

Example 2 with ActiveRuleDao

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

the class RegisterQualityProfilesMediumTest method register_profile_definitions.

@Test
public void register_profile_definitions() {
    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
    tester.start();
    dbSession = dbClient().openSession(false);
    // Check Profile in DB
    QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
    assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
    QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
    assertThat(profile).isNotNull();
    // Check Default Profile
    verifyDefaultProfile("xoo", "Basic");
    // Check ActiveRules in DB
    ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
    assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
    RuleKey ruleKey = RuleKey.of("xoo", "x1");
    ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, ActiveRuleKey.of(profile.getKey(), ruleKey)).get();
    assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKey());
    assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
    assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
    // Check ActiveRuleParameters in DB
    Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
    assertThat(params).hasSize(2);
    // set by profile
    assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
    // default value
    assertThat(params.get("max").getValue()).isEqualTo("10");
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ServerTester(org.sonar.server.tester.ServerTester) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 3 with ActiveRuleDao

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

the class RegisterQualityProfilesMediumTest method register_existing_profile_definitions.

@Test
public void register_existing_profile_definitions() {
    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
    tester.start();
    dbSession = dbClient().openSession(false);
    // Check Profile in DB
    QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
    assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
    QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
    assertThat(profile).isNotNull();
    // Check ActiveRules in DB
    ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
    assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
    RuleKey ruleKey = RuleKey.of("xoo", "x1");
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
    assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
    // Check in ES
    assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
    tester.get(Platform.class).restart();
    assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
    // Check ActiveRules
    ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, activeRuleKey).get();
    assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKee());
    assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
    assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
    // Check in ES
    assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
    // TODO
    // Check ActiveRuleParameters in DB
    Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
    assertThat(params).hasSize(2);
    // set by profile
    assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
    // default value
    assertThat(params.get("max").getValue()).isEqualTo("10");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) Platform(org.sonar.server.platform.Platform) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) ServerTester(org.sonar.server.tester.ServerTester) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 4 with ActiveRuleDao

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

the class RuleActivator method doUpdate.

private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
    ActiveRuleDao dao = db.activeRuleDao();
    ActiveRuleDto activeRule = context.activeRule();
    if (activeRule != null) {
        String severity = change.getSeverity();
        if (severity != null) {
            activeRule.setSeverity(severity);
        }
        ActiveRule.Inheritance inheritance = change.getInheritance();
        if (inheritance != null) {
            activeRule.setInheritance(inheritance.name());
        }
        activeRule.setUpdatedAt(system2.now());
        dao.update(dbSession, activeRule);
        for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
            ActiveRuleParamDto activeRuleParamDto = context.activeRuleParamsAsMap().get(param.getKey());
            if (activeRuleParamDto == null) {
                // did not exist
                if (param.getValue() != null) {
                    activeRuleParamDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
                    activeRuleParamDto.setValue(param.getValue());
                    dao.insertParam(dbSession, activeRule, activeRuleParamDto);
                }
            } else {
                if (param.getValue() != null) {
                    activeRuleParamDto.setValue(param.getValue());
                    dao.updateParam(dbSession, activeRule, activeRuleParamDto);
                } else {
                    dao.deleteParam(dbSession, activeRule, activeRuleParamDto);
                }
            }
        }
    }
    return activeRule;
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Map(java.util.Map)

Example 5 with ActiveRuleDao

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

the class RuleActivator method persist.

private ActiveRuleDto persist(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
    ActiveRuleDto activeRule = null;
    if (change.getType() == ActiveRuleChange.Type.ACTIVATED) {
        activeRule = doInsert(change, context, dbSession);
    } else if (change.getType() == ActiveRuleChange.Type.DEACTIVATED) {
        ActiveRuleDao dao = db.activeRuleDao();
        dao.delete(dbSession, change.getKey());
    } else if (change.getType() == ActiveRuleChange.Type.UPDATED) {
        activeRule = doUpdate(change, context, dbSession);
    }
    db.qProfileChangeDao().insert(dbSession, change.toDto(userSession.getLogin()));
    return activeRule;
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)8 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)7 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)6 Map (java.util.Map)4 Test (org.junit.Test)2 RuleKey (org.sonar.api.rule.RuleKey)2 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)2 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)2 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)2 ActiveRuleInheritance (org.sonar.server.qualityprofile.ActiveRuleInheritance)2 ActiveRuleWrapper (org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper)2 ServerTester (org.sonar.server.tester.ServerTester)2 SearchOptions (org.sonar.server.es.SearchOptions)1 Platform (org.sonar.server.platform.Platform)1 RuleWrapper (org.sonar.server.qualityprofile.builtin.RuleActivationContext.RuleWrapper)1 RuleQuery (org.sonar.server.rule.index.RuleQuery)1