]> source.dussan.org Git - sonarqube.git/blob
7022cf49b608086f5ddd5152f27e9605011bef67
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.db.migration.version.v95;
21
22 import java.sql.SQLException;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.apache.commons.lang.RandomStringUtils;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.db.CoreDbTester;
29
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;
33
34 public class CreateIndexForRuleDescSectionsTest {
35
36   @Rule
37   public final CoreDbTester db = CoreDbTester.createForSchema(CreateIndexForRuleDescSectionsTest.class, "schema.sql");
38
39   private final CreateIndexForRuleDescSections createIndex = new CreateIndexForRuleDescSections(db.database());
40
41   @Test
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");
46   }
47
48   @Test
49   public void migration_should_be_reentrant() throws SQLException {
50     db.assertIndexDoesNotExist(RULE_DESCRIPTION_SECTIONS_TABLE, INDEX_NAME);
51
52     createIndex.execute();
53     //re-entrant
54     createIndex.execute();
55
56     db.assertUniqueIndex(RULE_DESCRIPTION_SECTIONS_TABLE, INDEX_NAME, "rule_uuid", "kee");
57   }
58
59   @Test
60   public void index_should_prevent_two_descriptions_with_same_key_on_same_rule() throws SQLException {
61     createIndex.execute();
62
63     insertRuleDescSection("default", "rule1");
64     insertRuleDescSection("default", "rule2");
65     insertRuleDescSection("non_default", "rule2");
66     assertThatExceptionOfType(IllegalStateException.class)
67       .isThrownBy(() -> this.insertRuleDescSection("default", "rule1"));
68   }
69
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");
76
77     db.executeInsert("rule_desc_sections", ruleParams);
78   }
79
80 }