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.util.cache;
22 import java.util.Date;
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;
32 import static org.assertj.core.api.Assertions.assertThat;
34 public class ProtobufIssueDiskCacheTest {
36 private static final String TEST_CONTEXT_KEY = "test_context_key";
39 public void toDefaultIssue_whenRuleDescriptionContextKeyPresent_shouldSetItInDefaultIssue() {
40 IssueCache.Issue issue = prepareIssueWithCompulsoryFields()
41 .setRuleDescriptionContextKey(TEST_CONTEXT_KEY)
44 DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
46 assertThat(defaultIssue.getRuleDescriptionContextKey()).contains(TEST_CONTEXT_KEY);
50 public void toDefaultIssue_whenRuleDescriptionContextKeyAbsent_shouldNotSetItInDefaultIssue() {
51 IssueCache.Issue issue = prepareIssueWithCompulsoryFields()
54 DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
56 assertThat(defaultIssue.getRuleDescriptionContextKey()).isEmpty();
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))
66 DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
68 assertThat(defaultIssue.impacts()).containsExactlyInAnyOrderEntriesOf(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.HIGH, SoftwareQuality.RELIABILITY, Severity.LOW));
72 public void toDefaultIssue_whenCleanCodeAttributeIsSet_shouldSetItInDefaultIssue() {
73 IssueCache.Issue issue = prepareIssueWithCompulsoryFields()
74 .setCleanCodeAttribute(CleanCodeAttribute.FOCUSED.name())
77 DefaultIssue defaultIssue = ProtobufIssueDiskCache.toDefaultIssue(issue);
79 assertThat(defaultIssue.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.FOCUSED);
83 public void toProto_whenRuleDescriptionContextKeySet_shouldCopyToIssueProto() {
84 DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
85 defaultIssue.setRuleDescriptionContextKey(TEST_CONTEXT_KEY);
87 IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
89 assertThat(issue.hasRuleDescriptionContextKey()).isTrue();
90 assertThat(issue.getRuleDescriptionContextKey()).isEqualTo(TEST_CONTEXT_KEY);
94 public void toProto_whenRuleDescriptionContextKeyNotSet_shouldCopyToIssueProto() {
95 DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
96 defaultIssue.setRuleDescriptionContextKey(null);
98 IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
100 assertThat(issue.hasRuleDescriptionContextKey()).isFalse();
104 public void toProto_whenRuleDescriptionContextKeyIsSet_shouldCopyToIssueProto() {
105 DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
106 defaultIssue.addImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);
107 defaultIssue.addImpact(SoftwareQuality.RELIABILITY, Severity.LOW);
109 IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
111 assertThat(issue.getImpactsList()).containsExactly(
112 toImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
113 toImpact(SoftwareQuality.RELIABILITY, Severity.LOW)
118 public void toProto_whenCleanCodeAttributeIsSet_shouldCopyToIssueProto() {
119 DefaultIssue defaultIssue = createDefaultIssueWithMandatoryFields();
120 defaultIssue.setCleanCodeAttribute(CleanCodeAttribute.FOCUSED);
122 IssueCache.Issue issue = ProtobufIssueDiskCache.toProto(IssueCache.Issue.newBuilder(), defaultIssue);
124 assertThat(issue.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.FOCUSED.name());
127 private IssueCache.Impact toImpact(SoftwareQuality softwareQuality, Severity severity) {
128 return IssueCache.Impact.newBuilder().setSoftwareQuality(softwareQuality.name()).setSeverity(severity.name()).build();
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());
144 private static IssueCache.Issue.Builder prepareIssueWithCompulsoryFields() {
145 return IssueCache.Issue.newBuilder()
146 .setRuleType(RuleType.CODE_SMELL.getDbConstant())
147 .setRuleKey("test_issue:key")