3 * Copyright (C) 2009-2024 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.issue;
22 import org.assertj.core.groups.Tuple;
23 import org.junit.Test;
24 import org.sonar.api.issue.impact.SoftwareQuality;
25 import org.sonar.api.rule.RuleKey;
26 import org.sonar.api.rule.Severity;
27 import org.sonar.api.rules.CleanCodeAttribute;
28 import org.sonar.api.rules.RuleType;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.util.SequenceUuidFactory;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.rule.RuleDto;
34 import org.sonar.scanner.protocol.Constants;
35 import org.sonar.scanner.protocol.output.ScannerReport;
36 import org.sonar.server.es.EsTester;
37 import org.sonar.server.rule.index.RuleIndexer;
39 import static org.apache.commons.lang.StringUtils.repeat;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.tuple;
43 public class AdHocRuleCreatorIT {
46 public DbTester db = DbTester.create(System2.INSTANCE);
48 public EsTester es = EsTester.create();
50 private RuleIndexer indexer = new RuleIndexer(es.client(), db.getDbClient());
51 private AdHocRuleCreator underTest = new AdHocRuleCreator(db.getDbClient(), System2.INSTANCE, indexer, new SequenceUuidFactory());
52 private DbSession dbSession = db.getSession();
55 public void create_ad_hoc_rule_from_issue() {
56 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build());
58 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
60 assertThat(rule).isNotNull();
61 assertThat(rule.isExternal()).isTrue();
62 assertThat(rule.isAdHoc()).isTrue();
63 assertThat(rule.getUuid()).isNotBlank();
64 assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
65 assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
66 assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
67 assertThat(rule.getSeverity()).isNull();
68 assertThat(rule.getType()).isZero();
69 assertThat(rule.getAdHocName()).isNull();
70 assertThat(rule.getAdHocDescription()).isNull();
71 assertThat(rule.getAdHocSeverity()).isNull();
72 assertThat(rule.getAdHocType()).isNull();
73 assertThat(rule.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.defaultCleanCodeAttribute());
74 assertThat(rule.getDefaultImpacts()).extracting(i -> i.getSoftwareQuality(), i -> i.getSeverity())
75 .containsExactly(Tuple.tuple(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
79 public void create_ad_hoc_rule_from_scanner_report() {
80 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
81 .setEngineId("eslint")
82 .setRuleId("no-cond-assign")
83 .setName("No condition assigned")
84 .setDescription("A description")
85 .setSeverity(Constants.Severity.BLOCKER)
86 .setType(ScannerReport.IssueType.BUG)
87 .setCleanCodeAttribute(CleanCodeAttribute.DISTINCT.name())
88 .addDefaultImpacts(ScannerReport.Impact.newBuilder().setSoftwareQuality(SoftwareQuality.RELIABILITY.name())
89 .setSeverity(org.sonar.api.issue.impact.Severity.LOW.name()).build())
92 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
94 assertThat(rule).isNotNull();
95 assertThat(rule.isExternal()).isTrue();
96 assertThat(rule.isAdHoc()).isTrue();
97 assertThat(rule.getUuid()).isNotBlank();
98 assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
99 assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
100 assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
101 assertThat(rule.getSeverity()).isNull();
102 assertThat(rule.getType()).isZero();
103 assertThat(rule.getAdHocName()).isEqualTo("No condition assigned");
104 assertThat(rule.getAdHocDescription()).isEqualTo("A description");
105 assertThat(rule.getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
106 assertThat(rule.getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant());
107 assertThat(rule.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.DISTINCT);
108 assertThat(rule.getDefaultImpacts()).extracting(i -> i.getSoftwareQuality(), i -> i.getSeverity())
109 .containsExactly(tuple(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW));
113 public void truncate_metadata_name_and_desc_if_longer_than_max_value() {
114 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
115 .setEngineId("eslint")
116 .setRuleId("no-cond-assign")
117 .setName(repeat("a", 201))
118 .setDescription(repeat("a", 16_777_216))
119 .setSeverity(Constants.Severity.BLOCKER)
120 .setType(ScannerReport.IssueType.BUG)
123 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
125 assertThat(rule.getAdHocName()).isEqualTo(repeat("a", 200));
126 assertThat(rule.getAdHocDescription()).isEqualTo(repeat("a", 16_777_215));
130 public void update_metadata_only() {
131 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
132 .setEngineId("eslint")
133 .setRuleId("no-cond-assign")
134 .setName("No condition assigned")
135 .setDescription("A description")
136 .setSeverity(Constants.Severity.BLOCKER)
137 .setType(ScannerReport.IssueType.BUG)
139 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
140 long creationDate = rule.getCreatedAt();
141 NewAdHocRule addHocRuleUpdated = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
142 .setEngineId("eslint")
143 .setRuleId("no-cond-assign")
144 .setName("No condition assigned updated")
145 .setDescription("A description updated")
146 .setSeverity(Constants.Severity.CRITICAL)
147 .setType(ScannerReport.IssueType.CODE_SMELL)
150 RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, addHocRuleUpdated);
152 assertThat(ruleUpdated).isNotNull();
153 assertThat(ruleUpdated.isExternal()).isTrue();
154 assertThat(ruleUpdated.isAdHoc()).isTrue();
155 assertThat(ruleUpdated.getUuid()).isNotBlank();
156 assertThat(ruleUpdated.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
157 assertThat(ruleUpdated.getName()).isEqualTo("eslint:no-cond-assign");
158 assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).isEmpty();
159 assertThat(ruleUpdated.getSeverity()).isNull();
160 assertThat(ruleUpdated.getType()).isZero();
161 assertThat(ruleUpdated.getAdHocName()).isEqualTo("No condition assigned updated");
162 assertThat(ruleUpdated.getAdHocDescription()).isEqualTo("A description updated");
163 assertThat(ruleUpdated.getAdHocSeverity()).isEqualTo(Severity.CRITICAL);
164 assertThat(ruleUpdated.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
165 assertThat(ruleUpdated.getCreatedAt()).isEqualTo(creationDate);
166 assertThat(ruleUpdated.getUpdatedAt()).isGreaterThan(creationDate);
170 public void does_not_update_rule_when_no_change() {
171 RuleDto rule = db.rules().insert(r -> r.setRepositoryKey("external_eslint").setIsExternal(true).setIsAdHoc(true));
173 RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
174 .setEngineId("eslint")
175 .setRuleId(rule.getKey().rule())
176 .setName(rule.getAdHocName())
177 .setDescription(rule.getAdHocDescription())
178 .setSeverity(Constants.Severity.valueOf(rule.getAdHocSeverity()))
179 .setType(ScannerReport.IssueType.forNumber(rule.getAdHocType()))
180 .addDefaultImpacts(ScannerReport.Impact.newBuilder().setSoftwareQuality(SoftwareQuality.MAINTAINABILITY.name())
181 .setSeverity(org.sonar.api.issue.impact.Severity.HIGH.name()).build())
182 .setCleanCodeAttribute(CleanCodeAttribute.CLEAR.name())
185 assertThat(ruleUpdated).isNotNull();
186 assertThat(ruleUpdated.isExternal()).isTrue();
187 assertThat(ruleUpdated.isAdHoc()).isTrue();
188 assertThat(ruleUpdated.getKey()).isEqualTo(rule.getKey());
189 assertThat(ruleUpdated.getName()).isEqualTo(rule.getName());
190 assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator().isEqualTo(rule.getRuleDescriptionSectionDtos());
191 assertThat(ruleUpdated.getSeverity()).isEqualTo(rule.getSeverity());
192 assertThat(ruleUpdated.getType()).isEqualTo(rule.getType());
193 assertThat(ruleUpdated.getCreatedAt()).isEqualTo(rule.getCreatedAt());
194 assertThat(ruleUpdated.getUpdatedAt()).isEqualTo(rule.getUpdatedAt());
196 assertThat(ruleUpdated.getAdHocName()).isEqualTo(rule.getAdHocName());
197 assertThat(ruleUpdated.getAdHocDescription()).isEqualTo(rule.getAdHocDescription());
198 assertThat(ruleUpdated.getAdHocSeverity()).isEqualTo(rule.getAdHocSeverity());
199 assertThat(ruleUpdated.getAdHocType()).isEqualTo(rule.getAdHocType());