]> source.dussan.org Git - sonarqube.git/blob
eb717af0e07473cc91f5718b9e602ea80672bd63
[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.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;
32
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;
37
38 public class InsertRuleDescriptionIntoRuleDescSectionsTest {
39
40   @Rule
41   public final CoreDbTester db = CoreDbTester.createForSchema(InsertRuleDescriptionIntoRuleDescSectionsTest.class, "schema.sql");
42
43   private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
44
45   private final DataChange insertRuleDescriptions = new InsertRuleDescriptionIntoRuleDescSections(db.database(), uuidFactory);
46
47   @Test
48   public void insertRuleDescriptions_doesNotFailIfRulesTableIsEmpty() {
49     assertThatCode(insertRuleDescriptions::execute)
50       .doesNotThrowAnyException();
51   }
52
53   @Test
54   public void insertRuleDescriptions_whenDifferentRules_createsRelevantSectionDescription() throws SQLException {
55     String description1 = RandomStringUtils.randomAlphanumeric(5000);
56     String uuid1 = "uuid1";
57     insertRule(uuid1, description1);
58
59     String description2 = RandomStringUtils.randomAlphanumeric(5000);
60     String uuid2 = "uuid2";
61     insertRule(uuid2, description2);
62
63     insertRuleDescriptions.execute();
64
65     assertThat(db.countRowsOfTable(RULE_DESCRIPTION_SECTIONS_TABLE)).isEqualTo(2);
66     assertRuleDescriptionCreated(uuid1, description1);
67     assertRuleDescriptionCreated(uuid2, description2);
68   }
69
70   @Test
71   public void insertRuleDescriptions_whenReentrant_doesNotFail() throws SQLException {
72     String description1 = RandomStringUtils.randomAlphanumeric(5000);
73     String uuid1 = "uuid1";
74     insertRule(uuid1, description1);
75
76     insertRuleDescriptions.execute();
77     insertRuleDescriptions.execute();
78     insertRuleDescriptions.execute();
79
80     assertThat(db.countRowsOfTable(RULE_DESCRIPTION_SECTIONS_TABLE)).isEqualTo(1);
81     assertRuleDescriptionCreated(uuid1, description1);
82   }
83
84   @Test
85   public void insertRuleDescriptions_whenNoDescription_doesNotCreateRuleDescriptionSection() throws SQLException {
86     String uuid1 = "uuid1";
87     insertRule(uuid1, null);
88
89     insertRuleDescriptions.execute();
90
91     assertThat(db.countRowsOfTable(RULE_DESCRIPTION_SECTIONS_TABLE)).isZero();
92   }
93
94   private void assertRuleDescriptionCreated(String uuid1, String description1) {
95     Map<String, Object> result1 = findRuleSectionDescription(uuid1);
96     assertThat(result1)
97       .containsEntry("RULE_UUID", uuid1)
98       .containsEntry("KEE", DEFAULT_DESCRIPTION_KEY)
99       .containsEntry("DESCRIPTION", description1)
100       .extractingByKey("UUID").isNotNull();
101   }
102
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 + "'");
106   }
107
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);
118
119     db.executeInsert("rules", ruleParams);
120   }
121
122 }