]> source.dussan.org Git - sonarqube.git/blob
680a1e675b18382e728b7f4c7b867cf8dc164582
[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.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;
36
37 import static org.apache.commons.lang.StringUtils.repeat;
38 import static org.assertj.core.api.Assertions.assertThat;
39
40 public class AdHocRuleCreatorTest {
41
42   @org.junit.Rule
43   public DbTester db = DbTester.create(System2.INSTANCE);
44   @org.junit.Rule
45   public EsTester es = EsTester.create();
46
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();
50
51   @Test
52   public void create_ad_hoc_rule_from_issue() {
53     NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build());
54
55     RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
56
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();
70   }
71
72   @Test
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)
81       .build());
82
83     RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
84
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());
98   }
99
100   @Test
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", 1_000_000))
107       .setSeverity(Constants.Severity.BLOCKER)
108       .setType(ScannerReport.IssueType.BUG)
109       .build());
110
111     RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule);
112
113     assertThat(rule.getMetadata().getAdHocName()).isEqualTo(repeat("a", 200));
114     assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo(repeat("a", 1_000_000));
115   }
116
117   @Test
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)
126       .build());
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)
136       .build());
137
138     RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, addHocRuleUpdated);
139
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.getMetadata().getCreatedAt()).isEqualTo(creationDate);
155     assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isGreaterThan(creationDate);
156   }
157
158   @Test
159   public void does_not_update_rule_when_no_change() {
160     RuleDto rule = db.rules().insert(r -> r.setRepositoryKey("external_eslint").setIsExternal(true).setIsAdHoc(true));
161     RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule);
162
163     RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder()
164       .setEngineId("eslint")
165       .setRuleId(rule.getKey().rule())
166       .setName(ruleMetadata.getAdHocName())
167       .setDescription(ruleMetadata.getAdHocDescription())
168       .setSeverity(Constants.Severity.valueOf(ruleMetadata.getAdHocSeverity()))
169       .setType(ScannerReport.IssueType.forNumber(ruleMetadata.getAdHocType()))
170       .build()));
171
172     assertThat(ruleUpdated).isNotNull();
173     assertThat(ruleUpdated.isExternal()).isTrue();
174     assertThat(ruleUpdated.isAdHoc()).isTrue();
175     assertThat(ruleUpdated.getKey()).isEqualTo(rule.getKey());
176     assertThat(ruleUpdated.getName()).isEqualTo(rule.getName());
177     assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator().isEqualTo(rule.getRuleDescriptionSectionDtos());
178     assertThat(ruleUpdated.getSeverity()).isEqualTo(rule.getSeverity());
179     assertThat(ruleUpdated.getType()).isEqualTo(rule.getType());
180     assertThat(ruleUpdated.getCreatedAt()).isEqualTo(rule.getCreatedAt());
181     assertThat(ruleUpdated.getUpdatedAt()).isEqualTo(ruleMetadata.getUpdatedAt());
182
183     assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo(ruleMetadata.getAdHocName());
184     assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo(ruleMetadata.getAdHocDescription());
185     assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(ruleMetadata.getAdHocSeverity());
186     assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(ruleMetadata.getAdHocType());
187     assertThat(ruleUpdated.getMetadata().getCreatedAt()).isEqualTo(rule.getCreatedAt());
188     assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isEqualTo(ruleMetadata.getUpdatedAt());
189   }
190
191 }