]> source.dussan.org Git - sonarqube.git/blob
e9582b0ae186c01e9315e6f88cf04f461badf721
[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.issue;
21
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.RuleDefinitionDto;
31 import org.sonar.db.rule.RuleDto;
32 import org.sonar.db.rule.RuleMetadataDto;
33 import org.sonar.scanner.protocol.Constants;
34 import org.sonar.scanner.protocol.output.ScannerReport;
35 import org.sonar.server.es.EsTester;
36 import org.sonar.server.rule.index.RuleIndexer;
37
38 import static org.apache.commons.lang.StringUtils.repeat;
39 import static org.assertj.core.api.Assertions.assertThat;
40
41 public class AdHocRuleCreatorTest {
42
43   @org.junit.Rule
44   public DbTester db = DbTester.create(System2.INSTANCE);
45   @org.junit.Rule
46   public EsTester es = EsTester.create();
47
48   private RuleIndexer indexer = new RuleIndexer(es.client(), db.getDbClient());
49   private AdHocRuleCreator underTest = new AdHocRuleCreator(db.getDbClient(), System2.INSTANCE, indexer, new SequenceUuidFactory());
50   private DbSession dbSession = db.getSession();
51
52   @Test
53   public void create_ad_hoc_rule_from_issue() {
54     NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build());
55
56     RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
57
58     assertThat(rule).isNotNull();
59     assertThat(rule.isExternal()).isTrue();
60     assertThat(rule.isAdHoc()).isTrue();
61     assertThat(rule.getUuid()).isNotBlank();
62     assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
63     assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
64     assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
65     assertThat(rule.getSeverity()).isNull();
66     assertThat(rule.getType()).isZero();
67     assertThat(rule.getMetadata().getAdHocName()).isNull();
68     assertThat(rule.getMetadata().getAdHocDescription()).isNull();
69     assertThat(rule.getMetadata().getAdHocSeverity()).isNull();
70     assertThat(rule.getMetadata().getAdHocType()).isNull();
71   }
72
73   @Test
74   public void create_ad_hoc_rule_from_scanner_report() {
75     NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
76       .setEngineId("eslint")
77       .setRuleId("no-cond-assign")
78       .setName("No condition assigned")
79       .setDescription("A description")
80       .setSeverity(Constants.Severity.BLOCKER)
81       .setType(ScannerReport.IssueType.BUG)
82       .build());
83
84     RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
85
86     assertThat(rule).isNotNull();
87     assertThat(rule.isExternal()).isTrue();
88     assertThat(rule.isAdHoc()).isTrue();
89     assertThat(rule.getUuid()).isNotBlank();
90     assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
91     assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
92     assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty();
93     assertThat(rule.getSeverity()).isNull();
94     assertThat(rule.getType()).isZero();
95     assertThat(rule.getMetadata().getAdHocName()).isEqualTo("No condition assigned");
96     assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo("A description");
97     assertThat(rule.getMetadata().getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
98     assertThat(rule.getMetadata().getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant());
99   }
100
101   @Test
102   public void truncate_metadata_name_and_desc_if_longer_than_max_value() {
103     NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
104       .setEngineId("eslint")
105       .setRuleId("no-cond-assign")
106       .setName(repeat("a", 201))
107       .setDescription(repeat("a", 1_000_000))
108       .setSeverity(Constants.Severity.BLOCKER)
109       .setType(ScannerReport.IssueType.BUG)
110       .build());
111
112     RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
113
114     assertThat(rule.getMetadata().getAdHocName()).isEqualTo(repeat("a", 200));
115     assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo(repeat("a", 1_000_000));
116   }
117
118   @Test
119   public void update_metadata_only() {
120     NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
121       .setEngineId("eslint")
122       .setRuleId("no-cond-assign")
123       .setName("No condition assigned")
124       .setDescription("A description")
125       .setSeverity(Constants.Severity.BLOCKER)
126       .setType(ScannerReport.IssueType.BUG)
127       .build());
128     RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
129     long creationDate = rule.getCreatedAt();
130     NewAdHocRule addHocRuleUpdated = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
131       .setEngineId("eslint")
132       .setRuleId("no-cond-assign")
133       .setName("No condition assigned updated")
134       .setDescription("A description updated")
135       .setSeverity(Constants.Severity.CRITICAL)
136       .setType(ScannerReport.IssueType.CODE_SMELL)
137       .build());
138
139     RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, addHocRuleUpdated);
140
141     assertThat(ruleUpdated).isNotNull();
142     assertThat(ruleUpdated.isExternal()).isTrue();
143     assertThat(ruleUpdated.isAdHoc()).isTrue();
144     assertThat(ruleUpdated.getUuid()).isNotBlank();
145     assertThat(ruleUpdated.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
146     assertThat(ruleUpdated.getName()).isEqualTo("eslint:no-cond-assign");
147     assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).isEmpty();
148     assertThat(ruleUpdated.getSeverity()).isNull();
149     assertThat(ruleUpdated.getType()).isZero();
150     assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo("No condition assigned updated");
151     assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo("A description updated");
152     assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(Severity.CRITICAL);
153     assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
154     assertThat(ruleUpdated.getDefinition().getCreatedAt()).isEqualTo(creationDate);
155     assertThat(ruleUpdated.getMetadata().getCreatedAt()).isEqualTo(creationDate);
156     assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isGreaterThan(creationDate);
157   }
158
159   @Test
160   public void does_not_update_rule_when_no_change() {
161     RuleDefinitionDto rule = db.rules().insert(r -> r.setRepositoryKey("external_eslint").setIsExternal(true).setIsAdHoc(true));
162     RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule);
163
164     RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
165       .setEngineId("eslint")
166       .setRuleId(rule.getKey().rule())
167       .setName(ruleMetadata.getAdHocName())
168       .setDescription(ruleMetadata.getAdHocDescription())
169       .setSeverity(Constants.Severity.valueOf(ruleMetadata.getAdHocSeverity()))
170       .setType(ScannerReport.IssueType.forNumber(ruleMetadata.getAdHocType()))
171       .build()));
172
173     assertThat(ruleUpdated).isNotNull();
174     assertThat(ruleUpdated.isExternal()).isTrue();
175     assertThat(ruleUpdated.isAdHoc()).isTrue();
176     assertThat(ruleUpdated.getKey()).isEqualTo(rule.getKey());
177     assertThat(ruleUpdated.getName()).isEqualTo(rule.getName());
178     assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator().isEqualTo(rule.getRuleDescriptionSectionDtos());
179     assertThat(ruleUpdated.getSeverity()).isEqualTo(rule.getSeverity());
180     assertThat(ruleUpdated.getType()).isEqualTo(rule.getType());
181     assertThat(ruleUpdated.getDefinition().getCreatedAt()).isEqualTo(rule.getCreatedAt());
182     assertThat(ruleUpdated.getDefinition().getUpdatedAt()).isEqualTo(rule.getUpdatedAt());
183
184     assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo(ruleMetadata.getAdHocName());
185     assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo(ruleMetadata.getAdHocDescription());
186     assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(ruleMetadata.getAdHocSeverity());
187     assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(ruleMetadata.getAdHocType());
188     assertThat(ruleUpdated.getMetadata().getCreatedAt()).isEqualTo(rule.getCreatedAt());
189     assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isEqualTo(rule.getUpdatedAt());
190   }
191
192 }