]> source.dussan.org Git - sonarqube.git/blob
2b3239c50ed9c26335a75cf1f0f96d50219d6789
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.rule.RuleKey;
27 import org.sonar.api.utils.System2;
28 import org.sonar.ce.task.projectanalysis.issue.AdHocRuleCreator;
29 import org.sonar.ce.task.projectanalysis.issue.NewAdHocRule;
30 import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryImpl;
31 import org.sonar.ce.task.step.ComputationStep;
32 import org.sonar.ce.task.step.TestComputationStepContext;
33 import org.sonar.core.util.SequenceUuidFactory;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.rule.RuleDao;
37 import org.sonar.db.rule.RuleDefinitionDto;
38 import org.sonar.scanner.protocol.output.ScannerReport;
39 import org.sonar.server.es.EsTester;
40 import org.sonar.server.rule.index.RuleIndexDefinition;
41 import org.sonar.server.rule.index.RuleIndexer;
42
43 import static org.assertj.core.api.Assertions.assertThat;
44
45 public class PersistAdHocRulesStepTest extends BaseStepTest {
46
47   @Rule
48   public DbTester db = DbTester.create(System2.INSTANCE);
49
50   private DbClient dbClient = db.getDbClient();
51
52   private ComputationStep underTest;
53   private RuleRepositoryImpl ruleRepository;
54
55   @org.junit.Rule
56   public EsTester es = EsTester.create();
57
58   private RuleIndexer indexer = new RuleIndexer(es.client(), dbClient);
59   private AdHocRuleCreator adHocRuleCreator = new AdHocRuleCreator(dbClient, System2.INSTANCE, indexer, new SequenceUuidFactory());
60
61   @Override
62   protected ComputationStep step() {
63     return underTest;
64   }
65
66   @Before
67   public void setup() {
68     ruleRepository = new RuleRepositoryImpl(adHocRuleCreator, dbClient);
69     underTest = new PersistAdHocRulesStep(dbClient, ruleRepository);
70   }
71
72   @Test
73   public void persist_and_index_new_ad_hoc_rules() {
74     RuleKey ruleKey = RuleKey.of("external_eslint", "no-cond-assign");
75     ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey,
76       () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
77
78     underTest.execute(new TestComputationStepContext());
79
80     RuleDao ruleDao = dbClient.ruleDao();
81     Optional<RuleDefinitionDto> ruleDefinitionDtoOptional = ruleDao.selectDefinitionByKey(dbClient.openSession(false), ruleKey);
82     assertThat(ruleDefinitionDtoOptional).isPresent();
83
84     RuleDefinitionDto reloaded = ruleDefinitionDtoOptional.get();
85     assertThat(reloaded.getRuleKey()).isEqualTo("no-cond-assign");
86     assertThat(reloaded.getRepositoryKey()).isEqualTo("external_eslint");
87     assertThat(reloaded.isExternal()).isTrue();
88     assertThat(reloaded.isAdHoc()).isTrue();
89     assertThat(reloaded.getType()).isZero();
90     assertThat(reloaded.getSeverity()).isNull();
91     assertThat(reloaded.getName()).isEqualTo("eslint:no-cond-assign");
92
93     assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isOne();
94     assertThat(es.getDocuments(RuleIndexDefinition.TYPE_RULE).iterator().next().getId()).isEqualTo(reloaded.getUuid());
95   }
96
97   @Test
98   public void do_not_persist_existing_external_rules() {
99     RuleKey ruleKey = RuleKey.of("eslint", "no-cond-assign");
100     db.rules().insert(ruleKey, r -> r.setIsExternal(true));
101     ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey,
102       () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
103
104     underTest.execute(new TestComputationStepContext());
105
106     RuleDao ruleDao = dbClient.ruleDao();
107     assertThat(ruleDao.selectAllDefinitions(dbClient.openSession(false))).hasSize(1);
108     assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isZero();
109   }
110
111 }