3 * Copyright (C) 2009-2022 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.server.rule;
22 import java.util.List;
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;
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;
45 @RunWith(MockitoJUnitRunner.class)
46 public class AdvancedRuleDescriptionSectionsGeneratorTest {
47 private static final String UUID_1 = "uuid1";
48 private static final String UUID_2 = "uuid2";
50 private static final String HTML_CONTENT = "html content";
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();
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)
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")
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();
73 private static final RuleDescriptionSectionDto EXPECTED_SECTION_3_WITH_CTX_1 = RuleDescriptionSectionDto.builder()
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()))
80 private static final RuleDescriptionSectionDto EXPECTED_SECTION_3_WITH_CTX_2 = RuleDescriptionSectionDto.builder()
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()))
89 private UuidFactory uuidFactory;
92 private RulesDefinition.Rule rule;
95 private RuleDescriptionSectionDto LEGACY_SECTION;
98 private LegacyIssueRuleDescriptionSectionsGenerator legacyIssueRuleDescriptionSectionsGenerator;
101 private AdvancedRuleDescriptionSectionsGenerator generator;
104 public void before() {
105 when(uuidFactory.create()).thenReturn(UUID_1).thenReturn(UUID_2);
106 when(legacyIssueRuleDescriptionSectionsGenerator.generateSections(rule)).thenReturn(Set.of(LEGACY_SECTION));
110 public void generateSections_whenOneSection_createsOneSectionAndDefault() {
111 when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_1));
113 Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
115 assertThat(ruleDescriptionSectionDtos)
116 .usingRecursiveFieldByFieldElementComparator()
117 .containsExactlyInAnyOrder(EXPECTED_SECTION_1, LEGACY_SECTION);
121 public void generateSections_whenTwoSections_createsTwoSectionsAndDefault() {
122 when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_1, SECTION_2));
124 Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
126 assertThat(ruleDescriptionSectionDtos)
127 .usingRecursiveFieldByFieldElementComparator()
128 .containsExactlyInAnyOrder(EXPECTED_SECTION_1, EXPECTED_SECTION_2, LEGACY_SECTION);
132 public void generateSections_whenTwoContextSpecificSections_createsTwoSectionsAndDefault() {
133 when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_3_WITH_CTX_1, SECTION_3_WITH_CTX_2));
135 Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
137 assertThat(ruleDescriptionSectionDtos)
138 .usingRecursiveFieldByFieldElementComparator()
139 .containsExactlyInAnyOrder(EXPECTED_SECTION_3_WITH_CTX_1, EXPECTED_SECTION_3_WITH_CTX_2, LEGACY_SECTION);
143 public void generateSections_whenContextSpecificSectionsAndNonContextSpecificSection_createsTwoSectionsAndDefault() {
144 when(rule.ruleDescriptionSections()).thenReturn(List.of(SECTION_1, SECTION_3_WITH_CTX_2));
146 Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
148 assertThat(ruleDescriptionSectionDtos)
149 .usingRecursiveFieldByFieldElementComparator()
150 .containsExactlyInAnyOrder(EXPECTED_SECTION_1, EXPECTED_SECTION_3_WITH_CTX_2, LEGACY_SECTION);
154 public void generateSections_whenNoSections_returnsDefault() {
155 when(rule.ruleDescriptionSections()).thenReturn(emptyList());
157 Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = generator.generateSections(rule);
159 assertThat(ruleDescriptionSectionDtos).containsOnly(LEGACY_SECTION);