Search in sources :

Example 1 with ActiveList

use of org.sonarqube.ws.Rules.ActiveList 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(dateToLong(parseDateTime(active.getCreatedAt())));
        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);
        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) Rule(org.sonarqube.ws.Rules.Rule)

Example 2 with ActiveList

use of org.sonarqube.ws.Rules.ActiveList in project sonarqube by SonarSource.

the class DefaultActiveRulesLoaderTest method responseOfSize.

/**
   * Generates an imaginary protobuf result.
   *
   * @param numberOfRules the number of rules, that the response should contain
   * @param total the number of results on all pages
   * @return the binary stream
   */
private InputStream responseOfSize(int numberOfRules, int total) {
    Builder rules = SearchResponse.newBuilder();
    Actives.Builder actives = Actives.newBuilder();
    IntStream.rangeClosed(1, numberOfRules).mapToObj(i -> RuleKey.of("squid", "S" + i)).forEach(key -> {
        Rule.Builder ruleBuilder = Rule.newBuilder();
        ruleBuilder.setKey(key.toString());
        rules.addRules(ruleBuilder);
        Active.Builder activeBuilder = Active.newBuilder();
        activeBuilder.setCreatedAt("2014-05-27T15:50:45+0100");
        if (EXAMPLE_KEY.equals(key)) {
            activeBuilder.addParams(Rules.Active.Param.newBuilder().setKey(FORMAT_KEY).setValue(FORMAT_VALUE));
            activeBuilder.setSeverity(SEVERITY_VALUE);
        }
        ActiveList activeList = Rules.ActiveList.newBuilder().addActiveList(activeBuilder).build();
        actives.putAllActives(ImmutableSortedMap.of(key.toString(), activeList));
    });
    rules.setActives(actives);
    rules.setPs(numberOfRules);
    rules.setTotal(total);
    return new ByteArrayInputStream(rules.build().toByteArray());
}
Also used : IntStream(java.util.stream.IntStream) Rule(org.sonarqube.ws.Rules.Rule) WsTestUtil(org.sonar.scanner.WsTestUtil) Builder(org.sonarqube.ws.Rules.SearchResponse.Builder) SearchResponse(org.sonarqube.ws.Rules.SearchResponse) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Test(org.junit.Test) Active(org.sonarqube.ws.Rules.Active) ActiveList(org.sonarqube.ws.Rules.ActiveList) Actives(org.sonarqube.ws.Rules.Actives) Rules(org.sonarqube.ws.Rules) ByteArrayInputStream(java.io.ByteArrayInputStream) Severity(org.sonar.api.rule.Severity) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) RuleKey(org.sonar.api.rule.RuleKey) ScannerWsClient(org.sonar.scanner.bootstrap.ScannerWsClient) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) InputStream(java.io.InputStream) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) Active(org.sonarqube.ws.Rules.Active) Actives(org.sonarqube.ws.Rules.Actives) ByteArrayInputStream(java.io.ByteArrayInputStream) ActiveList(org.sonarqube.ws.Rules.ActiveList) Builder(org.sonarqube.ws.Rules.SearchResponse.Builder) Rule(org.sonarqube.ws.Rules.Rule)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)2 Active (org.sonarqube.ws.Rules.Active)2 ActiveList (org.sonarqube.ws.Rules.ActiveList)2 Rule (org.sonarqube.ws.Rules.Rule)2 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 IntStream (java.util.stream.IntStream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)1 Severity (org.sonar.api.rule.Severity)1 WsTestUtil (org.sonar.scanner.WsTestUtil)1 ScannerWsClient (org.sonar.scanner.bootstrap.ScannerWsClient)1