123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /*
- * SonarQube
- * Copyright (C) 2009-2023 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.Set;
- import java.util.TreeSet;
- import org.jetbrains.annotations.NotNull;
- import org.junit.Test;
- import org.sonar.api.issue.impact.Severity;
- import org.sonar.api.issue.impact.SoftwareQuality;
- import org.sonar.core.util.UuidFactoryFast;
- import org.sonar.api.rules.RuleType;
- import org.sonar.core.util.Uuids;
- import org.sonar.db.issue.ImpactDto;
-
- import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
- import static org.apache.commons.lang.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;
-
- public class RuleDtoTest {
-
- private static final String SECTION_KEY = "section key";
-
- @Test
- public void fail_if_key_is_too_long() {
- assertThatThrownBy(() -> new RuleDto().setRuleKey(repeat("x", 250)))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessageContaining("Rule key is too long: ");
- }
-
- @Test
- public void fail_if_name_is_too_long() {
- assertThatThrownBy(() -> new RuleDto().setName(repeat("x", 300)))
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessageContaining("Rule name is too long: ");
- }
-
- @Test
- public void fail_if_tags_are_too_long() {
- assertThatThrownBy(() -> {
- Set<String> tags = ImmutableSet.of(repeat("a", 2000), repeat("b", 1000), repeat("c", 2000));
- new RuleDto().setTags(tags);
- })
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessageContaining("Rule tags are too long: ");
- }
-
- @Test
- public void tags_are_optional() {
- RuleDto dto = new RuleDto().setTags(Collections.emptySet());
- assertThat(dto.getTags()).isEmpty();
- }
-
- @Test
- public void tags_are_joined_with_comma() {
- Set<String> tags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag"));
- RuleDto dto = new RuleDto().setTags(tags);
- assertThat(dto.getTags()).isEqualTo(tags);
- assertThat(dto.getTagsAsString()).isEqualTo("first_tag,second_tag,third_tag");
- }
-
- @Test
- public void system_tags_are_joined_with_comma() {
- 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
- public void security_standards_are_joined_with_comma() {
- 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
- public void equals_is_based_on_uuid() {
- 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
- public void hashcode_is_based_on_uuid() {
- 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
- public void add_rule_description_section_same_key_should_throw_error() {
- 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
- public void add_rule_description_section_with_different_context() {
- 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
- public void add_rule_description_section_with_same_section_and_context_should_throw_error() {
- RuleDto dto = new RuleDto();
- String contextKey = randomAlphanumeric(50);
- String displayName = randomAlphanumeric(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
- public 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
- public 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
- public 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
- public 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();
- }
-
- public static ImpactDto newImpactDto(SoftwareQuality softwareQuality, Severity severity) {
- return new ImpactDto(UuidFactoryFast.getInstance().create(), softwareQuality, severity);
- }
- }
|