3 * Copyright (C) 2009-2022 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.v95;
22 import java.sql.SQLException;
23 import java.util.HashMap;
25 import org.apache.commons.lang.RandomStringUtils;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.core.util.UuidFactory;
29 import org.sonar.core.util.UuidFactoryFast;
30 import org.sonar.db.CoreDbTester;
31 import org.sonar.server.platform.db.migration.step.DataChange;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatCode;
35 import static org.sonar.server.platform.db.migration.version.v95.CreateRuleDescSectionsTable.RULE_DESCRIPTION_SECTIONS_TABLE;
36 import static org.sonar.server.platform.db.migration.version.v95.InsertRuleDescriptionIntoRuleDescSections.DEFAULT_DESCRIPTION_KEY;
38 public class InsertRuleDescriptionIntoRuleDescSectionsTest {
41 public final CoreDbTester db = CoreDbTester.createForSchema(InsertRuleDescriptionIntoRuleDescSectionsTest.class, "schema.sql");
43 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
45 private final DataChange insertRuleDescriptions = new InsertRuleDescriptionIntoRuleDescSections(db.database(), uuidFactory);
48 public void insertRuleDescriptions_doesNotFailIfRulesTableIsEmpty() {
49 assertThatCode(insertRuleDescriptions::execute)
50 .doesNotThrowAnyException();
54 public void insertRuleDescriptions_whenDifferentRules_createsRelevantSectionDescription() throws SQLException {
55 String description1 = RandomStringUtils.randomAlphanumeric(5000);
56 String uuid1 = "uuid1";
57 insertRule(uuid1, description1);
59 String description2 = RandomStringUtils.randomAlphanumeric(5000);
60 String uuid2 = "uuid2";
61 insertRule(uuid2, description2);
63 insertRuleDescriptions.execute();
65 assertThat(db.countRowsOfTable(RULE_DESCRIPTION_SECTIONS_TABLE)).isEqualTo(2);
66 assertRuleDescriptionCreated(uuid1, description1);
67 assertRuleDescriptionCreated(uuid2, description2);
71 public void insertRuleDescriptions_whenReentrant_doesNotFail() throws SQLException {
72 String description1 = RandomStringUtils.randomAlphanumeric(5000);
73 String uuid1 = "uuid1";
74 insertRule(uuid1, description1);
76 insertRuleDescriptions.execute();
77 insertRuleDescriptions.execute();
78 insertRuleDescriptions.execute();
80 assertThat(db.countRowsOfTable(RULE_DESCRIPTION_SECTIONS_TABLE)).isEqualTo(1);
81 assertRuleDescriptionCreated(uuid1, description1);
85 public void insertRuleDescriptions_whenNoDescription_doesNotCreateRuleDescriptionSection() throws SQLException {
86 String uuid1 = "uuid1";
87 insertRule(uuid1, null);
89 insertRuleDescriptions.execute();
91 assertThat(db.countRowsOfTable(RULE_DESCRIPTION_SECTIONS_TABLE)).isZero();
94 private void assertRuleDescriptionCreated(String uuid1, String description1) {
95 Map<String, Object> result1 = findRuleSectionDescription(uuid1);
97 .containsEntry("RULE_UUID", uuid1)
98 .containsEntry("KEE", DEFAULT_DESCRIPTION_KEY)
99 .containsEntry("DESCRIPTION", description1)
100 .extractingByKey("UUID").isNotNull();
103 private Map<String, Object> findRuleSectionDescription(String uuid) {
104 return db.selectFirst("select uuid, kee, rule_uuid, description from "
105 + RULE_DESCRIPTION_SECTIONS_TABLE + " where rule_uuid = '" + uuid + "'");
108 private void insertRule(String uuid, String description) {
109 Map<String, Object> ruleParams = new HashMap<>();
110 ruleParams.put("uuid", uuid);
111 ruleParams.put("plugin_rule_key", uuid);
112 ruleParams.put("plugin_name", "plugin_name");
113 ruleParams.put("scope", "ALL");
114 ruleParams.put("is_template", false);
115 ruleParams.put("is_external", true);
116 ruleParams.put("is_ad_hoc", false);
117 ruleParams.put("description", description);
119 db.executeInsert("rules", ruleParams);