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.platform.db.migration.version.v100;
22 import java.sql.SQLException;
23 import java.util.HashMap;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.utils.System2;
29 import org.sonar.core.util.SequenceUuidFactory;
30 import org.sonar.core.util.UuidFactory;
31 import org.sonar.db.MigrationDbTester;
32 import org.sonar.server.platform.db.migration.step.DataChange;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38 import static org.sonar.core.util.SequenceUuidFactory.UUID_1;
40 public class RemoveOrphanRulesFromQualityProfilesIT {
43 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(RemoveOrphanRulesFromQualityProfiles.class);
44 private final System2 system2 = mock(System2.class);
45 private final UuidFactory instance = new SequenceUuidFactory();
46 private final DataChange underTest = new RemoveOrphanRulesFromQualityProfiles(db.database(), instance, system2);
49 public void before() {
50 when(system2.now()).thenReturn(1L);
54 public void migration_should_remove_orphans() throws SQLException {
59 assertOrphanRuleRemoved();
60 assertQualityProfileChanges();
64 public void migration_should_be_reentrant() throws SQLException {
71 assertOrphanRuleRemoved();
72 assertQualityProfileChanges();
75 private void insertData() {
76 insertRule("uuid-rule-1", "language-1", "rule1");
77 insertRule("uuid-rule-2", "language-2", "rule2");
78 insertProfile("uuid-profile-1", "language-1");
79 insertProfile("uuid-profile-2", "language-2");
80 activateRule("uuid-active-rule-1", "uuid-profile-1", "uuid-rule-1");
81 activateRule("uuid-active-rule-2", "uuid-profile-1", "uuid-rule-2");
82 activateRule("uuid-active-rule-3", "uuid-profile-2", "uuid-rule-2");
85 private void insertRule(String uuid, String language, String ruleKey) {
86 Map<String, Object> rule = new HashMap<>();
87 rule.put("uuid", uuid);
88 rule.put("plugin_rule_key", language + ":" + ruleKey);
89 rule.put("plugin_name", "plugin-name-1");
90 rule.put("scope", "MAIN");
91 rule.put("language", language);
92 rule.put("is_template", false);
93 rule.put("is_ad_hoc", false);
94 rule.put("is_external", false);
95 db.executeInsert("rules", rule);
98 private void insertProfile(String uuid, String language) {
99 Map<String, Object> profile = new HashMap<>();
100 profile.put("uuid", uuid);
101 profile.put("name", "profile-name-1");
102 profile.put("language", language);
103 profile.put("is_built_in", false);
104 db.executeInsert("rules_profiles", profile);
107 private void activateRule(String activeRuleUuid, String profileUuid, String ruleUuid) {
108 Map<String, Object> active_rule = new HashMap<>();
109 active_rule.put("uuid", activeRuleUuid);
110 active_rule.put("failure_level", 3);
111 active_rule.put("profile_uuid", profileUuid);
112 active_rule.put("rule_uuid", ruleUuid);
113 db.executeInsert("active_rules", active_rule);
116 private void assertOrphanRuleRemoved() {
117 assertThat(db.select("SELECT * from active_rules"))
118 .extracting(r -> r.get("UUID"))
119 .containsExactly("uuid-active-rule-1", "uuid-active-rule-3");
122 private void assertQualityProfileChanges() {
123 assertThat(db.select("SELECT * from qprofile_changes"))
124 .extracting(r -> r.get("KEE"), r -> r.get("RULES_PROFILE_UUID"), r -> r.get("CHANGE_TYPE"), r -> r.get("USER_UUID"), r -> r.get("CHANGE_DATA"), r -> r.get("CREATED_AT"))
125 .containsExactly(tuple(UUID_1, "uuid-profile-1", "DEACTIVATED", null, "ruleUuid=uuid-rule-2", 1L));