]> source.dussan.org Git - sonarqube.git/blob
2b9639ca815cf141f05bf1a6d9f5ff838b6a4607
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.util.cache;
21
22 import java.util.Date;
23 import java.util.Map;
24 import org.junit.Test;
25 import org.sonar.api.issue.impact.Severity;
26 import org.sonar.api.issue.impact.SoftwareQuality;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.rules.CleanCodeAttribute;
29 import org.sonar.api.rules.RuleType;
30 import org.sonar.core.issue.DefaultIssue;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33
34 public class ProtobufIssueDiskCacheTest {
35
36   private static final String TEST_CONTEXT_KEY = "test_context_key";
37
38   @Test
39   public void toDefaultIssue_whenRuleDescriptionContextKeyPresent_shouldSetItInDefaultIssue() {
40     IssueCache.Issue issue = prepareIssueWithCompulsoryFields()
41       .setRuleDescriptionContextKey(TEST_CONTEXT_KEY)
42       .build();
43
44     DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
45
46     assertThat(defaultIssue.getRuleDescriptionContextKey()).contains(TEST_CONTEXT_KEY);
47   }
48
49   @Test
50   public void toDefaultIssue_whenRuleDescriptionContextKeyAbsent_shouldNotSetItInDefaultIssue() {
51     IssueCache.Issue issue = prepareIssueWithCompulsoryFields()
52       .build();
53
54     DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
55
56     assertThat(defaultIssue.getRuleDescriptionContextKey()).isEmpty();
57   }
58
59   @Test
60   public void toDefaultIssue_whenImpactIsSet_shouldSetItInDefaultIssue() {
61     IssueCache.Issue issue = prepareIssueWithCompulsoryFields()
62       .addImpacts(toImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH))
63       .addImpacts(toImpact(SoftwareQuality.RELIABILITY, Severity.LOW))
64       .build();
65
66     DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
67
68     assertThat(defaultIssue.impacts()).containsExactlyInAnyOrderEntriesOf(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, SoftwareQuality.RELIABILITY, Severity.LOW));
69   }
70
71   @Test
72   public void toDefaultIssue_whenCleanCodeAttributeIsSet_shouldSetItInDefaultIssue() {
73     IssueCache.Issue issue = prepareIssueWithCompulsoryFields()
74       .setCleanCodeAttribute(CleanCodeAttribute.FOCUSED.name())
75       .build();
76
77     DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
78
79     assertThat(defaultIssue.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.FOCUSED);
80   }
81
82   @Test
83   public void toProto_whenRuleDescriptionContextKeySet_shouldCopyToIssueProto() {
84     DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
85     defaultIssue.setRuleDescriptionContextKey(TEST_CONTEXT_KEY);
86
87     IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
88
89     assertThat(issue.hasRuleDescriptionContextKey()).isTrue();
90     assertThat(issue.getRuleDescriptionContextKey()).isEqualTo(TEST_CONTEXT_KEY);
91   }
92
93   @Test
94   public void toProto_whenRuleDescriptionContextKeyNotSet_shouldCopyToIssueProto() {
95     DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
96     defaultIssue.setRuleDescriptionContextKey(null);
97
98     IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
99
100     assertThat(issue.hasRuleDescriptionContextKey()).isFalse();
101   }
102
103   @Test
104   public void toProto_whenRuleDescriptionContextKeyIsSet_shouldCopyToIssueProto() {
105     DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
106     defaultIssue.addImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);
107     defaultIssue.addImpact(SoftwareQuality.RELIABILITY, Severity.LOW);
108
109     IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
110
111     assertThat(issue.getImpactsList()).containsExactly(
112       toImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
113       toImpact(SoftwareQuality.RELIABILITY, Severity.LOW)
114     );
115   }
116
117   @Test
118   public void toProto_whenCleanCodeAttributeIsSet_shouldCopyToIssueProto() {
119     DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
120     defaultIssue.setCleanCodeAttribute(CleanCodeAttribute.FOCUSED);
121
122     IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
123
124     assertThat(issue.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.FOCUSED.name());
125   }
126
127   private IssueCache.Impact toImpact(SoftwareQuality softwareQuality, Severity severity) {
128     return IssueCache.Impact.newBuilder().setSoftwareQuality(softwareQuality.name()).setSeverity(severity.name()).build();
129   }
130
131   private static DefaultIssue createDefaultIssueWithMandatoryFields() {
132     DefaultIssue defaultIssue = new DefaultIssue();
133     defaultIssue.setKey("test_issue:key");
134     defaultIssue.setType(RuleType.CODE_SMELL);
135     defaultIssue.setComponentKey("component_key");
136     defaultIssue.setProjectUuid("project_uuid");
137     defaultIssue.setProjectKey("project_key");
138     defaultIssue.setRuleKey(RuleKey.of("ruleRepo", "rule1"));
139     defaultIssue.setStatus("open");
140     defaultIssue.setCreationDate(new Date());
141     return defaultIssue;
142   }
143
144   private static IssueCache.Issue.Builder prepareIssueWithCompulsoryFields() {
145     return IssueCache.Issue.newBuilder()
146       .setRuleType(RuleType.CODE_SMELL.getDbConstant())
147       .setRuleKey("test_issue:key")
148       .setStatus("open");
149   }
150
151 }