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;
39 public class RemoveOrphanRulesFromQualityProfilesIT {
42 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(RemoveOrphanRulesFromQualityProfiles.class);
43 private final System2 system2 = mock(System2.class);
44 private final UuidFactory instance = new SequenceUuidFactory();
45 private final DataChange underTest = new RemoveOrphanRulesFromQualityProfiles(db.database(), instance, system2);
48 public void before() {
49 when(system2.now()).thenReturn(1L);
53 public void migration_should_remove_orphans() throws SQLException {
58 assertOrphanRuleRemoved();
59 assertQualityProfileChanges();
63 public void migration_should_be_reentrant() throws SQLException {
70 assertOrphanRuleRemoved();
71 assertQualityProfileChanges();
74 private void insertData() {
75 insertRule("uuid-rule-1", "language-1", "rule1");
76 insertRule("uuid-rule-2", "language-2", "rule2");
77 insertProfile("uuid-profile-1", "language-1");
78 insertProfile("uuid-profile-2", "language-2");
79 activateRule("uuid-active-rule-1", "uuid-profile-1", "uuid-rule-1");
80 activateRule("uuid-active-rule-2", "uuid-profile-1", "uuid-rule-2");
81 activateRule("uuid-active-rule-3", "uuid-profile-2", "uuid-rule-2");
84 private void insertRule(String uuid, String language, String ruleKey) {
85 Map<String, Object> rule = new HashMap<>();
86 rule.put("uuid", uuid);
87 rule.put("plugin_rule_key", language + ":" + ruleKey);
88 rule.put("plugin_name", "plugin-name-1");
89 rule.put("scope", "MAIN");
90 rule.put("language", language);
91 rule.put("is_template", false);
92 rule.put("is_ad_hoc", false);
93 rule.put("is_external", false);
94 db.executeInsert("rules", rule);
97 private void insertProfile(String uuid, String language) {
98 Map<String, Object> profile = new HashMap<>();
99 profile.put("uuid", uuid);
100 profile.put("name", "profile-name-1");
101 profile.put("language", language);
102 profile.put("is_built_in", false);
103 db.executeInsert("rules_profiles", profile);
106 private void activateRule(String activeRuleUuid, String profileUuid, String ruleUuid) {
107 Map<String, Object> active_rule = new HashMap<>();
108 active_rule.put("uuid", activeRuleUuid);
109 active_rule.put("failure_level", 3);
110 active_rule.put("profile_uuid", profileUuid);
111 active_rule.put("rule_uuid", ruleUuid);
112 db.executeInsert("active_rules", active_rule);
115 private void assertOrphanRuleRemoved() {
116 assertThat(db.select("SELECT * from active_rules"))
117 .extracting(r -> r.get("UUID"))
118 .containsExactly("uuid-active-rule-1", "uuid-active-rule-3");
121 private void assertQualityProfileChanges() {
122 assertThat(db.select("SELECT * from qprofile_changes"))
123 .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"))
124 .containsExactly(tuple("1", "uuid-profile-1", "DEACTIVATED", null, "ruleUuid=uuid-rule-2", 1L));