You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuleDtoTest.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.db.rule;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.Collections;
  23. import java.util.Set;
  24. import java.util.TreeSet;
  25. import org.jetbrains.annotations.NotNull;
  26. import org.junit.Test;
  27. import org.sonar.api.issue.impact.Severity;
  28. import org.sonar.api.issue.impact.SoftwareQuality;
  29. import org.sonar.core.util.UuidFactoryFast;
  30. import org.sonar.api.rules.RuleType;
  31. import org.sonar.core.util.Uuids;
  32. import org.sonar.db.issue.ImpactDto;
  33. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  34. import static org.apache.commons.lang.StringUtils.repeat;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  37. import static org.assertj.core.api.Assertions.tuple;
  38. import static org.sonar.db.rule.RuleDto.ERROR_MESSAGE_SECTION_ALREADY_EXISTS;
  39. import static org.sonar.db.rule.RuleTesting.newRule;
  40. public class RuleDtoTest {
  41. private static final String SECTION_KEY = "section key";
  42. @Test
  43. public void fail_if_key_is_too_long() {
  44. assertThatThrownBy(() -> new RuleDto().setRuleKey(repeat("x", 250)))
  45. .isInstanceOf(IllegalArgumentException.class)
  46. .hasMessageContaining("Rule key is too long: ");
  47. }
  48. @Test
  49. public void fail_if_name_is_too_long() {
  50. assertThatThrownBy(() -> new RuleDto().setName(repeat("x", 300)))
  51. .isInstanceOf(IllegalArgumentException.class)
  52. .hasMessageContaining("Rule name is too long: ");
  53. }
  54. @Test
  55. public void fail_if_tags_are_too_long() {
  56. assertThatThrownBy(() -> {
  57. Set<String> tags = ImmutableSet.of(repeat("a", 2000), repeat("b", 1000), repeat("c", 2000));
  58. new RuleDto().setTags(tags);
  59. })
  60. .isInstanceOf(IllegalArgumentException.class)
  61. .hasMessageContaining("Rule tags are too long: ");
  62. }
  63. @Test
  64. public void tags_are_optional() {
  65. RuleDto dto = new RuleDto().setTags(Collections.emptySet());
  66. assertThat(dto.getTags()).isEmpty();
  67. }
  68. @Test
  69. public void tags_are_joined_with_comma() {
  70. Set<String> tags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
  71. RuleDto dto = new RuleDto().setTags(tags);
  72. assertThat(dto.getTags()).isEqualTo(tags);
  73. assertThat(dto.getTagsAsString()).isEqualTo("first_tag,second_tag,third_tag");
  74. }
  75. @Test
  76. public void system_tags_are_joined_with_comma() {
  77. Set<String> systemTags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
  78. RuleDto dto = new RuleDto().setSystemTags(systemTags);
  79. assertThat(dto.getSystemTags()).isEqualTo(systemTags);
  80. }
  81. @Test
  82. public void security_standards_are_joined_with_comma() {
  83. Set<String> securityStandards = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
  84. RuleDto dto = new RuleDto().setSecurityStandards(securityStandards);
  85. assertThat(dto.getSecurityStandards()).isEqualTo(securityStandards);
  86. }
  87. @Test
  88. public void equals_is_based_on_uuid() {
  89. String uuid = Uuids.createFast();
  90. RuleDto dto = newRule().setUuid(uuid);
  91. assertThat(dto)
  92. .isEqualTo(dto)
  93. .isEqualTo(newRule().setUuid(uuid))
  94. .isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(uuid))
  95. .isNotNull()
  96. .isNotEqualTo(new Object())
  97. .isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(Uuids.createFast()))
  98. .isNotEqualTo(newRule().setUuid(Uuids.createFast()));
  99. }
  100. @Test
  101. public void hashcode_is_based_on_uuid() {
  102. String uuid = Uuids.createFast();
  103. RuleDto dto = newRule().setUuid(uuid);
  104. assertThat(dto)
  105. .hasSameHashCodeAs(dto)
  106. .hasSameHashCodeAs(newRule().setUuid(uuid))
  107. .hasSameHashCodeAs(newRule().setRuleKey(dto.getRuleKey()).setUuid(uuid));
  108. assertThat(dto.hashCode())
  109. .isNotEqualTo(new Object().hashCode())
  110. .isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(Uuids.createFast()).hashCode())
  111. .isNotEqualTo(newRule().setUuid(Uuids.createFast()).hashCode());
  112. }
  113. @Test
  114. public void add_rule_description_section_same_key_should_throw_error() {
  115. RuleDto dto = new RuleDto();
  116. RuleDescriptionSectionDto section1 = createSection(SECTION_KEY);
  117. dto.addRuleDescriptionSectionDto(section1);
  118. RuleDescriptionSectionDto section2 = createSection(SECTION_KEY);
  119. assertThatThrownBy(() -> dto.addRuleDescriptionSectionDto(section2))
  120. .isInstanceOf(IllegalArgumentException.class)
  121. .hasMessage(String.format(ERROR_MESSAGE_SECTION_ALREADY_EXISTS, SECTION_KEY, "null"));
  122. }
  123. @Test
  124. public void add_rule_description_section_with_different_context() {
  125. RuleDto dto = new RuleDto();
  126. RuleDescriptionSectionDto section1 = createSection(RuleDtoTest.SECTION_KEY, "context key 1", "context display Name 1");
  127. dto.addRuleDescriptionSectionDto(section1);
  128. RuleDescriptionSectionDto section2 = createSection(RuleDtoTest.SECTION_KEY, "context key 2", "context display Name 2");
  129. dto.addRuleDescriptionSectionDto(section2);
  130. assertThat(dto.getRuleDescriptionSectionDtos())
  131. .usingRecursiveFieldByFieldElementComparator()
  132. .containsExactlyInAnyOrder(section1, section2);
  133. }
  134. @Test
  135. public void add_rule_description_section_with_same_section_and_context_should_throw_error() {
  136. RuleDto dto = new RuleDto();
  137. String contextKey = randomAlphanumeric(50);
  138. String displayName = randomAlphanumeric(50);
  139. RuleDescriptionSectionDto section1 = createSection(SECTION_KEY, contextKey, displayName);
  140. dto.addRuleDescriptionSectionDto(section1);
  141. RuleDescriptionSectionDto section2 = createSection(SECTION_KEY, contextKey, displayName);
  142. assertThatThrownBy(() -> dto.addRuleDescriptionSectionDto(section2))
  143. .isInstanceOf(IllegalArgumentException.class)
  144. .hasMessage(String.format(ERROR_MESSAGE_SECTION_ALREADY_EXISTS, SECTION_KEY, contextKey));
  145. }
  146. @Test
  147. public void addDefaultImpact_whenSoftwareQualityAlreadyDefined_shouldThrowISE() {
  148. RuleDto dto = new RuleDto();
  149. dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.LOW));
  150. ImpactDto duplicatedImpact = newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);
  151. assertThatThrownBy(() -> dto.addDefaultImpact(duplicatedImpact))
  152. .isInstanceOf(IllegalStateException.class)
  153. .hasMessage("Impact already defined on rule for Software Quality [MAINTAINABILITY]");
  154. }
  155. @Test
  156. public void replaceAllDefaultImpacts_whenSoftwareQualityAlreadyDuplicated_shouldThrowISE() {
  157. RuleDto dto = new RuleDto();
  158. dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM));
  159. dto.addDefaultImpact(newImpactDto(SoftwareQuality.SECURITY, Severity.HIGH));
  160. dto.addDefaultImpact(newImpactDto(SoftwareQuality.RELIABILITY, Severity.LOW));
  161. Set<ImpactDto> duplicatedImpacts = Set.of(
  162. newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
  163. newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.LOW));
  164. assertThatThrownBy(() -> dto.replaceAllDefaultImpacts(duplicatedImpacts))
  165. .isInstanceOf(IllegalStateException.class)
  166. .hasMessage("Impacts must have unique Software Quality values");
  167. }
  168. @Test
  169. public void replaceAllImpacts_shouldReplaceExistingImpacts() {
  170. RuleDto dto = new RuleDto();
  171. dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM));
  172. dto.addDefaultImpact(newImpactDto(SoftwareQuality.SECURITY, Severity.HIGH));
  173. dto.addDefaultImpact(newImpactDto(SoftwareQuality.RELIABILITY, Severity.LOW));
  174. Set<ImpactDto> duplicatedImpacts = Set.of(
  175. newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
  176. newImpactDto(SoftwareQuality.SECURITY, Severity.LOW));
  177. dto.replaceAllDefaultImpacts(duplicatedImpacts);
  178. assertThat(dto.getDefaultImpacts())
  179. .extracting(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity)
  180. .containsExactlyInAnyOrder(
  181. tuple(SoftwareQuality.MAINTAINABILITY, Severity.HIGH),
  182. tuple(SoftwareQuality.SECURITY, Severity.LOW));
  183. }
  184. @Test
  185. public void getEnumType_shouldReturnCorrectValue() {
  186. RuleDto ruleDto = new RuleDto();
  187. ruleDto.setType(RuleType.CODE_SMELL);
  188. RuleType enumType = ruleDto.getEnumType();
  189. assertThat(enumType).isEqualTo(RuleType.CODE_SMELL);
  190. }
  191. @NotNull
  192. private static RuleDescriptionSectionDto createSection(String section_key, String contextKey, String contextDisplayName) {
  193. return RuleDescriptionSectionDto.builder()
  194. .key(section_key)
  195. .context(RuleDescriptionSectionContextDto.of(contextKey, contextDisplayName))
  196. .build();
  197. }
  198. @NotNull
  199. private static RuleDescriptionSectionDto createSection(String section_key) {
  200. return RuleDescriptionSectionDto.builder()
  201. .key(section_key)
  202. .build();
  203. }
  204. public static ImpactDto newImpactDto(SoftwareQuality softwareQuality, Severity severity) {
  205. return new ImpactDto(UuidFactoryFast.getInstance().create(), softwareQuality, severity);
  206. }
  207. }