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.v104;
22 import java.sql.SQLException;
23 import javax.annotation.Nullable;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.db.MigrationDbTester;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.tuple;
31 public class PopulateRuleTagsTableIT {
34 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(PopulateRuleTagsTable.class);
36 private final PopulateRuleTagsTable migration = new PopulateRuleTagsTable(db.database());
39 public void execute_whenTagsExist_shouldPopulateProperly() throws SQLException {
40 insertRule("uuid-1", null, "tag_1,tag_2");
41 insertRule("uuid-2", "systag_1,systag_2", null);
42 insertRule("uuid-3", "systag_3,systag_4", "tag_3,tag_4");
46 assertThat(db.select("select value, is_system_tag, rule_uuid from rule_tags"))
47 .extracting(t -> t.get("value"), t -> t.get("is_system_tag"), t -> t.get("rule_uuid"))
48 .containsExactlyInAnyOrder(
49 tuple("systag_1", true, "uuid-2"),
50 tuple("systag_2", true, "uuid-2"),
51 tuple("tag_1", false, "uuid-1"),
52 tuple("tag_2", false, "uuid-1"),
53 tuple("systag_3", true, "uuid-3"),
54 tuple("systag_4", true, "uuid-3"),
55 tuple("tag_3", false, "uuid-3"),
56 tuple("tag_4", false, "uuid-3")
61 public void execute_whenEmptyOrDuplicateTagsExist_shouldNotBeMigrated() throws SQLException {
62 insertRule("uuid-1", null, "tag_1,,tag_2");
63 insertRule("uuid-2", "systag_1,,systag_2,systag_2,", null);
67 assertThat(db.select("select value, is_system_tag, rule_uuid from rule_tags"))
68 .extracting(t -> t.get("value"), t -> t.get("is_system_tag"), t -> t.get("rule_uuid"))
69 .containsExactlyInAnyOrder(
70 tuple("systag_1", true, "uuid-2"),
71 tuple("systag_2", true, "uuid-2"),
72 tuple("tag_1", false, "uuid-1"),
73 tuple("tag_2", false, "uuid-1")
78 public void execute_whenRunMoreThanOnce_shouldBeReentrant() throws SQLException {
79 insertRule("uuid-3", "sys_tag", "tag");
86 private void verifyMapping() {
87 assertThat(db.select("select value, is_system_tag, rule_uuid from rule_tags"))
88 .extracting(t -> t.get("value"), t -> t.get("is_system_tag"), t -> t.get("rule_uuid"))
90 tuple("sys_tag", true, "uuid-3"),
91 tuple("tag", false, "uuid-3")
95 private void insertRule(String uuid, @Nullable String systemTags, @Nullable String tags) {
96 db.executeInsert("rules",
98 "PLUGIN_RULE_KEY", uuid,
101 "IS_TEMPLATE", false,
103 "SYSTEM_TAGS", systemTags,
105 "IS_EXTERNAL", false);