]> source.dussan.org Git - sonarqube.git/blob
d651c2da65484d4f731d2bfaea655997a9ac14f9
[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.server.rule;
21
22 import java.util.List;
23 import java.util.Set;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.sonar.api.server.rule.Context;
31 import org.sonar.api.server.rule.RuleDescriptionSection;
32 import org.sonar.api.server.rule.RuleDescriptionSectionBuilder;
33 import org.sonar.api.server.rule.RulesDefinition;
34 import org.sonar.core.util.UuidFactory;
35 import org.sonar.db.rule.RuleDescriptionSectionContextDto;
36 import org.sonar.db.rule.RuleDescriptionSectionDto;
37
38 import static java.util.Collections.emptyList;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.HOW_TO_FIX_SECTION_KEY;
42 import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.RESOURCES_SECTION_KEY;
43 import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ROOT_CAUSE_SECTION_KEY;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class AdvancedRuleDescriptionSectionsGeneratorTest {
47   private static final String UUID_1 = "uuid1";
48   private static final String UUID_2 = "uuid2";
49
50   private static final String HTML_CONTENT = "html content";
51
52   private static final RuleDescriptionSection SECTION_1 = new RuleDescriptionSectionBuilder().sectionKey(HOW_TO_FIX_SECTION_KEY).htmlContent(HTML_CONTENT).build();
53   private static final RuleDescriptionSection SECTION_2 = new RuleDescriptionSectionBuilder().sectionKey(ROOT_CAUSE_SECTION_KEY).htmlContent(HTML_CONTENT + "2").build();
54
55   private static final Context CONTEXT_1 = new Context("ctx_1", "ctx 1 display name");
56   private static final RuleDescriptionSection SECTION_3_WITH_CTX_1 = new RuleDescriptionSectionBuilder()
57     .sectionKey(RESOURCES_SECTION_KEY)
58     .htmlContent(HTML_CONTENT)
59     .context(CONTEXT_1)
60     .build();
61
62   private static final Context CONTEXT_2 = new Context("ctx_2", "ctx 2 display name");
63   private static final RuleDescriptionSection SECTION_3_WITH_CTX_2 = new RuleDescriptionSectionBuilder()
64     .sectionKey(RESOURCES_SECTION_KEY)
65     .htmlContent(HTML_CONTENT + "2")
66     .context(CONTEXT_2)
67     .build();
68
69   private static final RuleDescriptionSectionDto EXPECTED_SECTION_1 = RuleDescriptionSectionDto.builder().uuid(UUID_1).key(HOW_TO_FIX_SECTION_KEY).content(HTML_CONTENT).build();
70   private static final RuleDescriptionSectionDto EXPECTED_SECTION_2 = RuleDescriptionSectionDto.builder().uuid(UUID_2).key(ROOT_CAUSE_SECTION_KEY)
71     .content(HTML_CONTENT + "2").build();
72
73   private static final RuleDescriptionSectionDto EXPECTED_SECTION_3_WITH_CTX_1 = RuleDescriptionSectionDto.builder()
74     .uuid(UUID_1)
75     .key(SECTION_3_WITH_CTX_1.getKey())
76     .content(SECTION_3_WITH_CTX_1.getHtmlContent())
77     .context(RuleDescriptionSectionContextDto.of(CONTEXT_1.getKey(), CONTEXT_1.getDisplayName()))
78     .build();
79
80   private static final RuleDescriptionSectionDto EXPECTED_SECTION_3_WITH_CTX_2 = RuleDescriptionSectionDto.builder()
81     .uuid(UUID_2)
82     .key(SECTION_3_WITH_CTX_2.getKey())
83     .content(SECTION_3_WITH_CTX_2.getHtmlContent())
84     .context(RuleDescriptionSectionContextDto.of(CONTEXT_2.getKey(), CONTEXT_2.getDisplayName()))
85     .build();
86
87
88   @Mock
89   private UuidFactory uuidFactory;
90
91   @Mock
92   private RulesDefinition.Rule rule;
93
94   @Mock
95   private RuleDescriptionSectionDto LEGACY_SECTION;
96
97   @Mock
98   private LegacyIssueRuleDescriptionSectionsGenerator legacyIssueRuleDescriptionSectionsGenerator;
99
100   @InjectMocks
101   private AdvancedRuleDescriptionSectionsGenerator generator;
102
103   @Before
104   public void before() {
105     when(uuidFactory.create()).thenReturn(UUID_1).thenReturn(UUID_2);
106     when(legacyIssueRuleDescriptionSectionsGenerator.generateSections(rule)).thenReturn(Set.of(LEGACY_SECTION));
107   }
108
109   @Test
110   public void generateSections_whenOneSection_createsOneSectionAndDefault() {
111     when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_1));
112
113     Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
114
115     assertThat(ruleDescriptionSectionDtos)
116       .usingRecursiveFieldByFieldElementComparator()
117       .containsExactlyInAnyOrder(EXPECTED_SECTION_1, LEGACY_SECTION);
118   }
119
120   @Test
121   public void generateSections_whenTwoSections_createsTwoSectionsAndDefault() {
122     when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_1, SECTION_2));
123
124     Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
125
126     assertThat(ruleDescriptionSectionDtos)
127       .usingRecursiveFieldByFieldElementComparator()
128       .containsExactlyInAnyOrder(EXPECTED_SECTION_1, EXPECTED_SECTION_2, LEGACY_SECTION);
129   }
130
131   @Test
132   public void generateSections_whenTwoContextSpecificSections_createsTwoSectionsAndDefault() {
133     when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_3_WITH_CTX_1, SECTION_3_WITH_CTX_2));
134
135     Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
136
137     assertThat(ruleDescriptionSectionDtos)
138       .usingRecursiveFieldByFieldElementComparator()
139       .containsExactlyInAnyOrder(EXPECTED_SECTION_3_WITH_CTX_1, EXPECTED_SECTION_3_WITH_CTX_2, LEGACY_SECTION);
140   }
141
142   @Test
143   public void generateSections_whenContextSpecificSectionsAndNonContextSpecificSection_createsTwoSectionsAndDefault() {
144     when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_1, SECTION_3_WITH_CTX_2));
145
146     Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
147
148     assertThat(ruleDescriptionSectionDtos)
149       .usingRecursiveFieldByFieldElementComparator()
150       .containsExactlyInAnyOrder(EXPECTED_SECTION_1, EXPECTED_SECTION_3_WITH_CTX_2, LEGACY_SECTION);
151   }
152
153   @Test
154   public void generateSections_whenNoSections_returnsDefault() {
155     when(rule.ruleDescriptionSections()).thenReturn(emptyList());
156
157     Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
158
159     assertThat(ruleDescriptionSectionDtos).containsOnly(LEGACY_SECTION);
160   }
161
162 }