Search in sources :

Example 6 with LoadedActiveRule

use of org.sonar.api.batch.rule.LoadedActiveRule in project sonarqube by SonarSource.

the class DefaultActiveRulesLoader method readPage.

private static List<LoadedActiveRule> readPage(SearchResponse response) {
    List<LoadedActiveRule> loadedRules = new LinkedList<>();
    List<Rule> rulesList = response.getRulesList();
    Map<String, ActiveList> actives = response.getActives().getActives();
    for (Rule r : rulesList) {
        ActiveList activeList = actives.get(r.getKey());
        Active active = activeList.getActiveList(0);
        LoadedActiveRule loadedRule = new LoadedActiveRule();
        loadedRule.setRuleKey(RuleKey.parse(r.getKey()));
        loadedRule.setName(r.getName());
        loadedRule.setSeverity(active.getSeverity());
        loadedRule.setCreatedAt(DateUtils.dateToLong(DateUtils.parseDateTime(active.getCreatedAt())));
        loadedRule.setUpdatedAt(DateUtils.dateToLong(DateUtils.parseDateTime(active.getUpdatedAt())));
        loadedRule.setLanguage(r.getLang());
        loadedRule.setInternalKey(r.getInternalKey());
        if (r.hasTemplateKey()) {
            RuleKey templateRuleKey = RuleKey.parse(r.getTemplateKey());
            loadedRule.setTemplateRuleKey(templateRuleKey.rule());
        }
        Map<String, String> params = new HashMap<>();
        for (Rules.Rule.Param param : r.getParams().getParamsList()) {
            params.put(param.getKey(), param.getDefaultValue());
        }
        // overrides defaultValue if the key is the same
        for (Param param : active.getParamsList()) {
            params.put(param.getKey(), param.getValue());
        }
        loadedRule.setParams(params);
        loadedRule.setDeprecatedKeys(r.getDeprecatedKeys().getDeprecatedKeyList().stream().map(RuleKey::parse).collect(Collectors.toSet()));
        loadedRules.add(loadedRule);
    }
    return loadedRules;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) HashMap(java.util.HashMap) ActiveList(org.sonarqube.ws.Rules.ActiveList) LinkedList(java.util.LinkedList) Active(org.sonarqube.ws.Rules.Active) Param(org.sonarqube.ws.Rules.Active.Param) LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule) Rule(org.sonarqube.ws.Rules.Rule) LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule)

Example 7 with LoadedActiveRule

use of org.sonar.api.batch.rule.LoadedActiveRule in project sonarqube by SonarSource.

the class ActiveRulesProviderTest method testParamsAreTransformed.

@Test
public void testParamsAreTransformed() {
    LoadedActiveRule r1 = mockRule("rule1");
    LoadedActiveRule r2 = mockRule("rule2");
    r2.setParams(ImmutableMap.of("foo1", "bar1", "foo2", "bar2"));
    List<LoadedActiveRule> qpRules = ImmutableList.of(r1, r2);
    when(loader.load("qp")).thenReturn(qpRules);
    QualityProfiles profiles = mockProfiles("qp");
    ActiveRules activeRules = provider.provide(loader, profiles);
    assertThat(activeRules.findAll()).hasSize(2);
    assertThat(activeRules.findAll()).extracting("ruleKey", "params").containsOnly(Tuple.tuple(RuleKey.of("rule1", "rule1"), ImmutableMap.of()), Tuple.tuple(RuleKey.of("rule2", "rule2"), ImmutableMap.of("foo1", "bar1", "foo2", "bar2")));
    verify(loader).load("qp");
    verifyNoMoreInteractions(loader);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) ActiveRules(org.sonar.api.batch.rule.ActiveRules) LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule) Test(org.junit.Test)

Example 8 with LoadedActiveRule

use of org.sonar.api.batch.rule.LoadedActiveRule in project sonarqube by SonarSource.

the class ChecksMediumTest method createActiveRuleWithParam.

private LoadedActiveRule createActiveRuleWithParam(String repositoryKey, String ruleKey, @Nullable String templateRuleKey, String name, @Nullable String severity, @Nullable String internalKey, @Nullable String languag, String paramKey, String paramValue) {
    LoadedActiveRule r = new LoadedActiveRule();
    r.setInternalKey(internalKey);
    r.setRuleKey(RuleKey.of(repositoryKey, ruleKey));
    r.setName(name);
    r.setTemplateRuleKey(templateRuleKey);
    r.setLanguage(languag);
    r.setSeverity(severity);
    r.setDeprecatedKeys(emptySet());
    Map<String, String> params = new HashMap<>();
    params.put(paramKey, paramValue);
    r.setParams(params);
    return r;
}
Also used : HashMap(java.util.HashMap) LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule)

Example 9 with LoadedActiveRule

use of org.sonar.api.batch.rule.LoadedActiveRule in project sonarqube by SonarSource.

the class IssuesMediumTest method activateTODORule.

private void activateTODORule() {
    LoadedActiveRule r = new LoadedActiveRule();
    r.setRuleKey(RuleKey.of("xoo", HasTagSensor.RULE_KEY));
    r.setName("TODO");
    r.setLanguage("xoo");
    r.setSeverity("MAJOR");
    r.setDeprecatedKeys(emptySet());
    r.setParams(ImmutableMap.of("tag", "TODO"));
    tester.activateRule(r);
}
Also used : LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule)

Aggregations

LoadedActiveRule (org.sonar.api.batch.rule.LoadedActiveRule)9 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)2 RuleKey (org.sonar.api.rule.RuleKey)2 HashSet (java.util.HashSet)1 ActiveRules (org.sonar.api.batch.rule.ActiveRules)1 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)1 Active (org.sonarqube.ws.Rules.Active)1 Param (org.sonarqube.ws.Rules.Active.Param)1 ActiveList (org.sonarqube.ws.Rules.ActiveList)1 Rule (org.sonarqube.ws.Rules.Rule)1 SearchResponse (org.sonarqube.ws.Rules.SearchResponse)1 GetRequest (org.sonarqube.ws.client.GetRequest)1