Search in sources :

Example 16 with ActiveRuleKey

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

the class QProfileResetMediumTest method reset_language_profile.

@Test
public void reset_language_profile() {
    RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
    defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1").setParams(newArrayList(new RuleParam().setKey("acceptWhitespace"))), RulePriority.CRITICAL).setParameter("acceptWhitespace", "true");
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MINOR);
            x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
        }
    }, defProfile);
    RuleKey ruleKey = RuleKey.of("xoo", "x1");
    QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
    // Change the severity and the value of the parameter in the active rule
    tester.get(RuleActivator.class).activate(dbSession, new RuleActivation(ruleKey).setSeverity(BLOCKER).setParameter("acceptWhitespace", "false"), profile.getKey());
    dbSession.commit();
    // Verify severity and param has changed
    ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(BLOCKER);
    List<ActiveRuleParamDto> activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("false");
    reset.resetLanguage(ServerTester.Xoo.KEY);
    dbSession.commit();
    // Severity and parameter value come back to origin after reset
    activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    assertThat(activeRuleDto.getSeverityString()).isEqualTo(CRITICAL);
    activeRuleParamDtos = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(activeRuleParamDtos.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(activeRuleParamDtos.get(0).getValue()).isEqualTo("true");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) RuleParam(org.sonar.api.rules.RuleParam) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 17 with ActiveRuleKey

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

the class QProfileResetMediumTest method reset_language_profile_param_when_rule_definition_has_changed.

@Test
public void reset_language_profile_param_when_rule_definition_has_changed() {
    RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
    defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1"), null);
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
            x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
        }
    }, defProfile);
    QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), RuleKey.of("xoo", "x1"));
    // Change param in the rule def
    register(new Rules() {

        @Override
        public void init(RulesDefinition.NewRepository repository) {
            RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
            x1.createParam("acceptWhitespace").setDefaultValue("true").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
        }
    }, defProfile);
    reset.resetLanguage(ServerTester.Xoo.KEY);
    // Parameter value come back to origin after reset
    ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
    List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
    assertThat(params).hasSize(1);
    assertThat(params.get(0).getKey()).isEqualTo("acceptWhitespace");
    assertThat(params.get(0).getValue()).isEqualTo("true");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 18 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey 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 19 with ActiveRuleKey

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

the class QProfilesWsMediumTest method activate_rule_override_severity.

@Test
public void activate_rule_override_severity() throws Exception {
    QualityProfileDto profile = createProfile("java");
    RuleDto rule = createRule(profile.getLanguage(), "toto");
    session.commit();
    ruIndexer.index();
    // 0. Assert No Active Rule for profile
    assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty();
    // 1. Activate Rule
    WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION);
    request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey());
    request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString());
    request.setParam(RuleActivationActions.SEVERITY, "MINOR");
    WsTester.Result result = request.execute();
    session.clearCache();
    // 2. Assert ActiveRule in DAO
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), rule.getKey());
    assertThat(db.activeRuleDao().selectOrFailByKey(session, activeRuleKey).getSeverityString()).isEqualTo("MINOR");
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 20 with ActiveRuleKey

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

the class ActiveRuleCompleter method activeRuleDtosToActiveRuleParamDtos.

private ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleDtosToActiveRuleParamDtos(DbSession dbSession, List<ActiveRuleDto> activeRuleDtos) {
    Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
    for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
        activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
    }
    List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId));
    ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRuleDtos.size(), 10);
    for (ActiveRuleParamDto activeRuleParamDto : activeRuleParamDtos) {
        ActiveRuleKey activeRuleKey = activeRuleIdsByKey.get(activeRuleParamDto.getActiveRuleId());
        activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto);
    }
    return activeRuleParamsByActiveRuleKey;
}
Also used : HashMap(java.util.HashMap) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)26 Test (org.junit.Test)18 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)9 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)7 RuleKey (org.sonar.api.rule.RuleKey)6 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)5 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 BadRequestException (org.sonar.server.exceptions.BadRequestException)3 HashMap (java.util.HashMap)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 RuleDto (org.sonar.db.rule.RuleDto)2 SearchOptions (org.sonar.server.es.SearchOptions)2 RuleQuery (org.sonar.server.rule.index.RuleQuery)2 Multimap (com.google.common.collect.Multimap)1 RuleParam (org.sonar.api.rules.RuleParam)1 DbSession (org.sonar.db.DbSession)1 Platform (org.sonar.server.platform.Platform)1 ServerTester (org.sonar.server.tester.ServerTester)1