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.db.CoreDbTester;
30 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
31 import static org.sonar.server.platform.db.migration.version.v95.CreateIndexForRuleDescSections.INDEX_NAME;
32 import static org.sonar.server.platform.db.migration.version.v95.CreateRuleDescSectionsTable.RULE_DESCRIPTION_SECTIONS_TABLE;
34 public class CreateIndexForRuleDescSectionsTest {
37 public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexForRuleDescSectionsTest.class, "schema.sql");
39 private final CreateIndexForRuleDescSections createIndex = new CreateIndexForRuleDescSections(db.database());
42 public void should_create_index() throws SQLException {
43 db.assertIndexDoesNotExist(RULE_DESCRIPTION_SECTIONS_TABLE, INDEX_NAME);
44 createIndex.execute();
45 db.assertUniqueIndex(RULE_DESCRIPTION_SECTIONS_TABLE, INDEX_NAME, "rule_uuid", "kee");
49 public void migration_should_be_reentrant() throws SQLException {
50 db.assertIndexDoesNotExist(RULE_DESCRIPTION_SECTIONS_TABLE, INDEX_NAME);
52 createIndex.execute();
54 createIndex.execute();
56 db.assertUniqueIndex(RULE_DESCRIPTION_SECTIONS_TABLE, INDEX_NAME, "rule_uuid", "kee");
60 public void index_should_prevent_two_descriptions_with_same_key_on_same_rule() throws SQLException {
61 createIndex.execute();
63 insertRuleDescSection("default", "rule1");
64 insertRuleDescSection("default", "rule2");
65 insertRuleDescSection("non_default", "rule2");
66 assertThatExceptionOfType(IllegalStateException.class)
67 .isThrownBy(() -> this.insertRuleDescSection("default", "rule1"));
70 private void insertRuleDescSection(String key, String ruleUuid) {
71 Map<String, Object> ruleParams = new HashMap<>();
72 ruleParams.put("uuid", RandomStringUtils.randomAlphanumeric(40));
73 ruleParams.put("rule_uuid", ruleUuid);
74 ruleParams.put("kee", key);
75 ruleParams.put("description", "descriptions");
77 db.executeInsert("rule_desc_sections", ruleParams);