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.issue;
22 import org.junit.Test;
23 import org.sonar.api.rule.RuleKey;
24 import org.sonar.api.rule.Severity;
25 import org.sonar.api.rules.RuleType;
26 import org.sonar.api.utils.System2;
27 import org.sonar.core.util.SequenceUuidFactory;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.rule.RuleDto;
31 import org.sonar.db.rule.RuleMetadataDto;
32 import org.sonar.scanner.protocol.Constants;
33 import org.sonar.scanner.protocol.output.ScannerReport;
34 import org.sonar.server.es.EsTester;
35 import org.sonar.server.rule.index.RuleIndexer;
37 import static org.apache.commons.lang.StringUtils.repeat;
38 import static org.assertj.core.api.Assertions.assertThat;
40 public class AdHocRuleCreatorTest {
43 public DbTester db = DbTester.create(System2.INSTANCE);
45 public EsTester es = EsTester.create();
47 private RuleIndexer indexer = new RuleIndexer(es.client(), db.getDbClient());
48 private AdHocRuleCreator underTest = new AdHocRuleCreator(db.getDbClient(), System2.INSTANCE, indexer, new SequenceUuidFactory());
49 private DbSession dbSession = db.getSession();
52 public void create_ad_hoc_rule_from_issue() {
53 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build());
55 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
57 assertThat(rule).isNotNull();
58 assertThat(rule.isExternal()).isTrue();
59 assertThat(rule.isAdHoc()).isTrue();
60 assertThat(rule.getUuid()).isNotBlank();
61 assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
62 assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
63 assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
64 assertThat(rule.getSeverity()).isNull();
65 assertThat(rule.getType()).isZero();
66 assertThat(rule.getMetadata().getAdHocName()).isNull();
67 assertThat(rule.getMetadata().getAdHocDescription()).isNull();
68 assertThat(rule.getMetadata().getAdHocSeverity()).isNull();
69 assertThat(rule.getMetadata().getAdHocType()).isNull();
73 public void create_ad_hoc_rule_from_scanner_report() {
74 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
75 .setEngineId("eslint")
76 .setRuleId("no-cond-assign")
77 .setName("No condition assigned")
78 .setDescription("A description")
79 .setSeverity(Constants.Severity.BLOCKER)
80 .setType(ScannerReport.IssueType.BUG)
83 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
85 assertThat(rule).isNotNull();
86 assertThat(rule.isExternal()).isTrue();
87 assertThat(rule.isAdHoc()).isTrue();
88 assertThat(rule.getUuid()).isNotBlank();
89 assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
90 assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
91 assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
92 assertThat(rule.getSeverity()).isNull();
93 assertThat(rule.getType()).isZero();
94 assertThat(rule.getMetadata().getAdHocName()).isEqualTo("No condition assigned");
95 assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo("A description");
96 assertThat(rule.getMetadata().getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
97 assertThat(rule.getMetadata().getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant());
101 public void truncate_metadata_name_and_desc_if_longer_than_max_value() {
102 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
103 .setEngineId("eslint")
104 .setRuleId("no-cond-assign")
105 .setName(repeat("a", 201))
106 .setDescription(repeat("a", 16_777_216))
107 .setSeverity(Constants.Severity.BLOCKER)
108 .setType(ScannerReport.IssueType.BUG)
111 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
113 assertThat(rule.getMetadata().getAdHocName()).isEqualTo(repeat("a", 200));
114 assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo(repeat("a", 16_777_215));
118 public void update_metadata_only() {
119 NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
120 .setEngineId("eslint")
121 .setRuleId("no-cond-assign")
122 .setName("No condition assigned")
123 .setDescription("A description")
124 .setSeverity(Constants.Severity.BLOCKER)
125 .setType(ScannerReport.IssueType.BUG)
127 RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
128 long creationDate = rule.getCreatedAt();
129 NewAdHocRule addHocRuleUpdated = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
130 .setEngineId("eslint")
131 .setRuleId("no-cond-assign")
132 .setName("No condition assigned updated")
133 .setDescription("A description updated")
134 .setSeverity(Constants.Severity.CRITICAL)
135 .setType(ScannerReport.IssueType.CODE_SMELL)
138 RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, addHocRuleUpdated);
140 assertThat(ruleUpdated).isNotNull();
141 assertThat(ruleUpdated.isExternal()).isTrue();
142 assertThat(ruleUpdated.isAdHoc()).isTrue();
143 assertThat(ruleUpdated.getUuid()).isNotBlank();
144 assertThat(ruleUpdated.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
145 assertThat(ruleUpdated.getName()).isEqualTo("eslint:no-cond-assign");
146 assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).isEmpty();
147 assertThat(ruleUpdated.getSeverity()).isNull();
148 assertThat(ruleUpdated.getType()).isZero();
149 assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo("No condition assigned updated");
150 assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo("A description updated");
151 assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(Severity.CRITICAL);
152 assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
153 assertThat(ruleUpdated.getCreatedAt()).isEqualTo(creationDate);
154 assertThat(ruleUpdated.getUpdatedAt()).isGreaterThan(creationDate);
158 public void does_not_update_rule_when_no_change() {
159 RuleDto rule = db.rules().insert(r -> r.setRepositoryKey("external_eslint").setIsExternal(true).setIsAdHoc(true));
160 RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule);
162 RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
163 .setEngineId("eslint")
164 .setRuleId(rule.getKey().rule())
165 .setName(ruleMetadata.getAdHocName())
166 .setDescription(ruleMetadata.getAdHocDescription())
167 .setSeverity(Constants.Severity.valueOf(ruleMetadata.getAdHocSeverity()))
168 .setType(ScannerReport.IssueType.forNumber(ruleMetadata.getAdHocType()))
171 assertThat(ruleUpdated).isNotNull();
172 assertThat(ruleUpdated.isExternal()).isTrue();
173 assertThat(ruleUpdated.isAdHoc()).isTrue();
174 assertThat(ruleUpdated.getKey()).isEqualTo(rule.getKey());
175 assertThat(ruleUpdated.getName()).isEqualTo(rule.getName());
176 assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator().isEqualTo(rule.getRuleDescriptionSectionDtos());
177 assertThat(ruleUpdated.getSeverity()).isEqualTo(rule.getSeverity());
178 assertThat(ruleUpdated.getType()).isEqualTo(rule.getType());
179 assertThat(ruleUpdated.getCreatedAt()).isEqualTo(rule.getCreatedAt());
180 assertThat(ruleUpdated.getUpdatedAt()).isEqualTo(rule.getUpdatedAt());
182 assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo(ruleMetadata.getAdHocName());
183 assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo(ruleMetadata.getAdHocDescription());
184 assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(ruleMetadata.getAdHocSeverity());
185 assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(ruleMetadata.getAdHocType());