3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.step;
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;
43 import static org.assertj.core.api.Assertions.assertThat;
45 public class PersistAdHocRulesStepTest extends BaseStepTest {
48 public DbTester db = DbTester.create(System2.INSTANCE);
50 private DbClient dbClient = db.getDbClient();
52 private ComputationStep underTest;
53 private RuleRepositoryImpl ruleRepository;
56 public EsTester es = EsTester.create();
58 private RuleIndexer indexer = new RuleIndexer(es.client(), dbClient);
59 private AdHocRuleCreator adHocRuleCreator = new AdHocRuleCreator(dbClient, System2.INSTANCE, indexer, new SequenceUuidFactory());
62 protected ComputationStep step() {
68 ruleRepository = new RuleRepositoryImpl(adHocRuleCreator, dbClient);
69 underTest = new PersistAdHocRulesStep(dbClient, ruleRepository);
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()));
78 underTest.execute(new TestComputationStepContext());
80 RuleDao ruleDao = dbClient.ruleDao();
81 Optional<RuleDefinitionDto> ruleDefinitionDtoOptional = ruleDao.selectDefinitionByKey(dbClient.openSession(false), ruleKey);
82 assertThat(ruleDefinitionDtoOptional).isPresent();
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");
93 assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isOne();
94 assertThat(es.getDocuments(RuleIndexDefinition.TYPE_RULE).iterator().next().getId()).isEqualTo(reloaded.getUuid());
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()));
104 underTest.execute(new TestComputationStepContext());
106 RuleDao ruleDao = dbClient.ruleDao();
107 assertThat(ruleDao.selectAllDefinitions(dbClient.openSession(false))).hasSize(1);
108 assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isZero();