3 * Copyright (C) 2009-2023 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.v102;
22 import java.sql.SQLException;
23 import javax.annotation.Nullable;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.rules.CleanCodeAttribute;
27 import org.sonar.db.CoreDbTester;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.assertThatCode;
32 public class PopulateCleanCodeAttributeColumnInRulesTest {
34 private static final String TABLE_NAME = "rules";
37 public final CoreDbTester db = CoreDbTester.createForSchema(PopulateCleanCodeAttributeColumnInRulesTest.class, "schema.sql");
39 private final PopulateCleanCodeAttributeColumnInRules underTest = new PopulateCleanCodeAttributeColumnInRules(db.database());
42 public void execute_whenRulesDoNotExist_shouldNotFail() {
43 assertThatCode(underTest::execute).doesNotThrowAnyException();
47 public void execute_whenRuleWithUndefinedCleanCodeAttribute_shouldUpdate() throws SQLException {
48 insertRule("1", null);
50 assertThat(db.select("select UUID, CLEAN_CODE_ATTRIBUTE from rules"))
51 .extracting(stringObjectMap -> stringObjectMap.get("CLEAN_CODE_ATTRIBUTE")).containsExactly(CleanCodeAttribute.CONVENTIONAL.name());
55 public void execute_whenRuleWithUndefinedCleanCodeAttribute_shouldBeReentrant() throws SQLException {
56 insertRule("1", null);
59 assertThat(db.select("select UUID, CLEAN_CODE_ATTRIBUTE from rules"))
60 .extracting(stringObjectMap -> stringObjectMap.get("CLEAN_CODE_ATTRIBUTE")).containsExactly(CleanCodeAttribute.CONVENTIONAL.name());
64 public void execute_whenRuleWithDefinedCleanCodeAttribute_shouldNotUpdate() throws SQLException {
65 insertRule("1", CleanCodeAttribute.FOCUSED);
67 assertThat(db.select("select UUID, CLEAN_CODE_ATTRIBUTE from rules"))
68 .extracting(stringObjectMap -> stringObjectMap.get("CLEAN_CODE_ATTRIBUTE")).containsExactly(CleanCodeAttribute.FOCUSED.name());
71 private void insertRule(String uuid, @Nullable CleanCodeAttribute cleanCodeAttribute) {
72 db.executeInsert(TABLE_NAME,
74 "PLUGIN_RULE_KEY", "key",
75 "PLUGIN_NAME", "name",
77 "CLEAN_CODE_ATTRIBUTE", cleanCodeAttribute != null ? cleanCodeAttribute.name() : null,
80 "IS_EXTERNAL", false);