aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java
blob: e043370f38157f62f890d3acabb92b0423ca60c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.rule;

import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.core.rule.RuleType;
import org.sonar.core.util.Uuids;
import org.sonar.db.issue.ImpactDto;

import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.apache.commons.lang3.StringUtils.repeat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.db.rule.RuleDto.ERROR_MESSAGE_SECTION_ALREADY_EXISTS;
import static org.sonar.db.rule.RuleTesting.newRule;

class RuleDtoTest {

  private static final String SECTION_KEY = "section key";

  @Test
  void setRuleKey_whenTooLong_shouldFail() {
    assertThatThrownBy(() -> new RuleDto().setRuleKey(repeat("x", 250)))
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessageContaining("Rule key is too long: ");
  }

  @Test
  void setName_whenTooLong_shouldFail() {
    assertThatThrownBy(() -> new RuleDto().setName(repeat("x", 300)))
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessageContaining("Rule name is too long: ");
  }

  @Test
  void setTags_whenTooLong_shouldFail() {
    assertThatThrownBy(() -> {
      Set<String> tags = ImmutableSet.of(repeat("a", 25), repeat("b", 20), repeat("c", 41));
      new RuleDto().setTags(tags);
    })
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessageContaining("Rule tag is too long: ccccccccccccccccccccccccccccccccccccccccc");
  }

  @Test
  void setTags_shouldBeOptional() {
    RuleDto dto = new RuleDto().setTags(Collections.emptySet());
    assertThat(dto.getTags()).isEmpty();
  }

  @Test
  void setTags_shouldBeSet() {
    Set<String> tags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
    RuleDto dto = new RuleDto().setTags(tags);
    assertThat(dto.getTags()).isEqualTo(tags);
  }

  @Test
  void setSystemTags_shouldBeSet() {
    Set<String> systemTags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
    RuleDto dto = new RuleDto().setSystemTags(systemTags);
    assertThat(dto.getSystemTags()).isEqualTo(systemTags);
  }

  @Test
  void setSecurityStandards_shouldBeJoinedByCommas() {
    Set<String> securityStandards = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
    RuleDto dto = new RuleDto().setSecurityStandards(securityStandards);
    assertThat(dto.getSecurityStandards()).isEqualTo(securityStandards);
  }

  @Test
  void equals_shouldBeBasedOnUuid() {
    String uuid = Uuids.createFast();
    RuleDto dto = newRule().setUuid(uuid);

    assertThat(dto)
      .isEqualTo(dto)
      .isEqualTo(newRule().setUuid(uuid))
      .isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(uuid))
      .isNotNull()
      .isNotEqualTo(new Object())
      .isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(Uuids.createFast()))
      .isNotEqualTo(newRule().setUuid(Uuids.createFast()));
  }

  @Test
  void hashcode_shouldBeBasedOnUuid() {
    String uuid = Uuids.createFast();
    RuleDto dto = newRule().setUuid(uuid);

    assertThat(dto)
      .hasSameHashCodeAs(dto)
      .hasSameHashCodeAs(newRule().setUuid(uuid))
      .hasSameHashCodeAs(newRule().setRuleKey(dto.getRuleKey()).setUuid(uuid));
    assertThat(dto.hashCode())
      .isNotEqualTo(new Object().hashCode())
      .isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(Uuids.createFast()).hashCode())
      .isNotEqualTo(newRule().setUuid(Uuids.createFast()).hashCode());
  }

  @Test
  void addRuleDescriptionSectionDto_whenSameKey_shouldThrowError() {
    RuleDto dto = new RuleDto();

    RuleDescriptionSectionDto section1 = createSection(SECTION_KEY);
    dto.addRuleDescriptionSectionDto(section1);

    RuleDescriptionSectionDto section2 = createSection(SECTION_KEY);
    assertThatThrownBy(() -> dto.addRuleDescriptionSectionDto(section2))
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage(String.format(ERROR_MESSAGE_SECTION_ALREADY_EXISTS, SECTION_KEY, "null"));
  }

  @Test
  void addRuleDescriptionSectionDto_whenDifferentContext() {
    RuleDto dto = new RuleDto();

    RuleDescriptionSectionDto section1 = createSection(RuleDtoTest.SECTION_KEY, "context key 1", "context display Name 1");
    dto.addRuleDescriptionSectionDto(section1);

    RuleDescriptionSectionDto section2 = createSection(RuleDtoTest.SECTION_KEY, "context key 2", "context display Name 2");
    dto.addRuleDescriptionSectionDto(section2);

    assertThat(dto.getRuleDescriptionSectionDtos())
      .usingRecursiveFieldByFieldElementComparator()
      .containsExactlyInAnyOrder(section1, section2);

  }

  @Test
  void addRuleDescriptionSectionDto_whenSameSectionAndContext_shouldThrowError() {
    RuleDto dto = new RuleDto();
    String contextKey = secure().nextAlphanumeric(50);
    String displayName = secure().nextAlphanumeric(50);
    RuleDescriptionSectionDto section1 = createSection(SECTION_KEY, contextKey, displayName);
    dto.addRuleDescriptionSectionDto(section1);
    RuleDescriptionSectionDto section2 = createSection(SECTION_KEY, contextKey, displayName);

    assertThatThrownBy(() -> dto.addRuleDescriptionSectionDto(section2))
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage(String.format(ERROR_MESSAGE_SECTION_ALREADY_EXISTS, SECTION_KEY, contextKey));
  }

  @Test
  void addDefaultImpact_whenSoftwareQualityAlreadyDefined_shouldThrowISE() {
    RuleDto dto = new RuleDto();
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.LOW));

    ImpactDto duplicatedImpact = newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);

    assertThatThrownBy(() -> dto.addDefaultImpact(duplicatedImpact))
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Impact already defined on rule for Software Quality [MAINTAINABILITY]");
  }

  @Test
  void replaceAllDefaultImpacts_whenSoftwareQualityAlreadyDuplicated_shouldThrowISE() {
    RuleDto dto = new RuleDto();
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM));
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.SECURITY, Severity.HIGH));
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.RELIABILITY, Severity.LOW));

    Set<ImpactDto> duplicatedImpacts = Set.of(
      newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
      newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.LOW));
    assertThatThrownBy(() -> dto.replaceAllDefaultImpacts(duplicatedImpacts))
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Impacts must have unique Software Quality values");
  }

  @Test
  void replaceAllImpacts_shouldReplaceExistingImpacts() {
    RuleDto dto = new RuleDto();
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM));
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.SECURITY, Severity.HIGH));
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.RELIABILITY, Severity.LOW));

    Set<ImpactDto> duplicatedImpacts = Set.of(
      newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
      newImpactDto(SoftwareQuality.SECURITY, Severity.LOW));

    dto.replaceAllDefaultImpacts(duplicatedImpacts);

    assertThat(dto.getDefaultImpacts())
      .extracting(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity)
      .containsExactlyInAnyOrder(
        tuple(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
        tuple(SoftwareQuality.SECURITY, Severity.LOW));
  }

  @Test
  void getDefaultImpactsMap_shouldReturnExpectedResult() {
    RuleDto dto = new RuleDto();
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM));
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.SECURITY, Severity.HIGH));
    dto.addDefaultImpact(newImpactDto(SoftwareQuality.RELIABILITY, Severity.LOW));

    assertThat(dto.getDefaultImpactsMap())
      .containsExactlyInAnyOrderEntriesOf(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM,
        SoftwareQuality.SECURITY, Severity.HIGH,
        SoftwareQuality.RELIABILITY, Severity.LOW));
  }

  @Test
  void getDefaultImpactsMap_whenIsEmpty_shouldReturnEmptyMap() {
    RuleDto dto = new RuleDto();
    assertThat(dto.getDefaultImpactsMap()).isEmpty();
  }

  @Test
  void getEnumType_shouldReturnCorrectValue() {
    RuleDto ruleDto = new RuleDto();
    ruleDto.setType(RuleType.CODE_SMELL);

    RuleType enumType = ruleDto.getEnumType();

    assertThat(enumType).isEqualTo(RuleType.CODE_SMELL);
  }

  @NotNull
  private static RuleDescriptionSectionDto createSection(String section_key, String contextKey, String contextDisplayName) {
    return RuleDescriptionSectionDto.builder()
      .key(section_key)
      .context(RuleDescriptionSectionContextDto.of(contextKey, contextDisplayName))
      .build();
  }

  @NotNull
  private static RuleDescriptionSectionDto createSection(String section_key) {
    return RuleDescriptionSectionDto.builder()
      .key(section_key)
      .build();
  }

  static ImpactDto newImpactDto(SoftwareQuality softwareQuality, Severity severity) {
    return new ImpactDto(softwareQuality, severity);
  }
}