3 * Copyright (C) 2009-2024 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.registration;
22 import com.google.common.collect.ImmutableSet;
24 import org.assertj.core.groups.Tuple;
25 import org.junit.Test;
26 import org.sonar.api.rule.RuleKey;
27 import org.sonar.api.server.rule.RulesDefinition;
28 import org.sonar.db.rule.DeprecatedRuleKeyDto;
29 import org.sonar.server.rule.registration.SingleDeprecatedRuleKey;
31 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.groups.Tuple.tuple;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 public class SingleDeprecatedRuleKeyTest {
40 public void test_creation_from_DeprecatedRuleKeyDto() {
41 // Creation from DeprecatedRuleKeyDto
42 DeprecatedRuleKeyDto deprecatedRuleKeyDto = new DeprecatedRuleKeyDto()
43 .setOldRuleKey(randomAlphanumeric(50))
44 .setOldRepositoryKey(randomAlphanumeric(50))
45 .setRuleUuid(randomAlphanumeric(50))
46 .setUuid(randomAlphanumeric(40));
48 SingleDeprecatedRuleKey singleDeprecatedRuleKey = SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto);
50 assertThat(singleDeprecatedRuleKey.getOldRepositoryKey()).isEqualTo(deprecatedRuleKeyDto.getOldRepositoryKey());
51 assertThat(singleDeprecatedRuleKey.getOldRuleKey()).isEqualTo(deprecatedRuleKeyDto.getOldRuleKey());
52 assertThat(singleDeprecatedRuleKey.getNewRepositoryKey()).isEqualTo(deprecatedRuleKeyDto.getNewRepositoryKey());
53 assertThat(singleDeprecatedRuleKey.getNewRuleKey()).isEqualTo(deprecatedRuleKeyDto.getNewRuleKey());
54 assertThat(singleDeprecatedRuleKey.getUuid()).isEqualTo(deprecatedRuleKeyDto.getUuid());
55 assertThat(singleDeprecatedRuleKey.getRuleUuid()).isEqualTo(deprecatedRuleKeyDto.getRuleUuid());
56 assertThat(singleDeprecatedRuleKey.getOldRuleKeyAsRuleKey())
57 .isEqualTo(RuleKey.of(deprecatedRuleKeyDto.getOldRepositoryKey(), deprecatedRuleKeyDto.getOldRuleKey()));
61 public void test_creation_from_RulesDefinitionRule() {
62 // Creation from RulesDefinition.Rule
63 ImmutableSet<RuleKey> deprecatedRuleKeys = ImmutableSet.of(
64 RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)),
65 RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)),
66 RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)));
68 RulesDefinition.Repository repository = mock(RulesDefinition.Repository.class);
69 when(repository.key()).thenReturn(randomAlphanumeric(50));
71 RulesDefinition.Rule rule = mock(RulesDefinition.Rule.class);
72 when(rule.key()).thenReturn(randomAlphanumeric(50));
73 when(rule.deprecatedRuleKeys()).thenReturn(deprecatedRuleKeys);
74 when(rule.repository()).thenReturn(repository);
76 Set<SingleDeprecatedRuleKey> singleDeprecatedRuleKeys = SingleDeprecatedRuleKey.from(rule);
77 assertThat(singleDeprecatedRuleKeys).hasSize(deprecatedRuleKeys.size());
78 assertThat(singleDeprecatedRuleKeys)
79 .extracting(SingleDeprecatedRuleKey::getUuid, SingleDeprecatedRuleKey::getOldRepositoryKey, SingleDeprecatedRuleKey::getOldRuleKey,
80 SingleDeprecatedRuleKey::getNewRepositoryKey, SingleDeprecatedRuleKey::getNewRuleKey, SingleDeprecatedRuleKey::getOldRuleKeyAsRuleKey)
81 .containsExactlyInAnyOrder(
82 deprecatedRuleKeys.stream().map(
83 r -> tuple(null, r.repository(), r.rule(), rule.repository().key(), rule.key(), RuleKey.of(r.repository(), r.rule())))
84 .toList().toArray(new Tuple[deprecatedRuleKeys.size()]));
88 public void test_equality() {
89 DeprecatedRuleKeyDto deprecatedRuleKeyDto1 = new DeprecatedRuleKeyDto()
90 .setOldRuleKey(randomAlphanumeric(50))
91 .setOldRepositoryKey(randomAlphanumeric(50))
92 .setUuid(randomAlphanumeric(40))
93 .setRuleUuid("some-uuid");
95 DeprecatedRuleKeyDto deprecatedRuleKeyDto1WithoutUuid = new DeprecatedRuleKeyDto()
96 .setOldRuleKey(deprecatedRuleKeyDto1.getOldRuleKey())
97 .setOldRepositoryKey(deprecatedRuleKeyDto1.getOldRepositoryKey());
99 DeprecatedRuleKeyDto deprecatedRuleKeyDto2 = new DeprecatedRuleKeyDto()
100 .setOldRuleKey(randomAlphanumeric(50))
101 .setOldRepositoryKey(randomAlphanumeric(50))
102 .setUuid(randomAlphanumeric(40));
104 SingleDeprecatedRuleKey singleDeprecatedRuleKey1 = SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto1);
105 SingleDeprecatedRuleKey singleDeprecatedRuleKey2 = SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto2);
107 assertThat(singleDeprecatedRuleKey1)
108 .isEqualTo(singleDeprecatedRuleKey1)
109 .isEqualTo(SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto1))
110 .isEqualTo(SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto1WithoutUuid));
111 assertThat(singleDeprecatedRuleKey2).isEqualTo(SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto2));
113 assertThat(singleDeprecatedRuleKey1)
114 .hasSameHashCodeAs(singleDeprecatedRuleKey1)
115 .hasSameHashCodeAs(SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto1))
116 .hasSameHashCodeAs(SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto1WithoutUuid));
117 assertThat(singleDeprecatedRuleKey2).hasSameHashCodeAs(SingleDeprecatedRuleKey.from(deprecatedRuleKeyDto2));
119 assertThat(singleDeprecatedRuleKey1)
123 .isNotEqualTo(singleDeprecatedRuleKey2);
124 assertThat(singleDeprecatedRuleKey2).isNotEqualTo(singleDeprecatedRuleKey1);
126 assertThat(singleDeprecatedRuleKey1.hashCode()).isNotEqualTo(singleDeprecatedRuleKey2.hashCode());
127 assertThat(singleDeprecatedRuleKey2.hashCode()).isNotEqualTo(singleDeprecatedRuleKey1.hashCode());