You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanBrokenProjectToQGReferencesTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.v71;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.sql.SQLException;
  25. import java.util.Random;
  26. import java.util.stream.IntStream;
  27. import java.util.stream.Stream;
  28. import javax.annotation.Nullable;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.sonar.db.CoreDbTester;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. @RunWith(DataProviderRunner.class)
  35. public class CleanBrokenProjectToQGReferencesTest {
  36. private static final String PROPERTY_SONAR_QUALITYGATE = "sonar.qualitygate";
  37. @Rule
  38. public CoreDbTester db = CoreDbTester.createForSchema(CleanBrokenProjectToQGReferencesTest.class, "properties_and_quality_gates.sql");
  39. private CleanBrokenProjectToQGReferences underTest = new CleanBrokenProjectToQGReferences(db.database());
  40. @Test
  41. public void do_nothing_when_no_data() throws SQLException {
  42. assertThat(db.countRowsOfTable("PROPERTIES")).isEqualTo(0);
  43. underTest.execute();
  44. assertThat(db.countRowsOfTable("PROPERTIES")).isEqualTo(0);
  45. }
  46. @Test
  47. public void execute_deletes_all_qualitygate_component_properties_when_there_is_no_qualitygate() throws SQLException {
  48. insertProperty(PROPERTY_SONAR_QUALITYGATE, 30, "12");
  49. insertProperty(PROPERTY_SONAR_QUALITYGATE, 42, "val1");
  50. insertProperty(PROPERTY_SONAR_QUALITYGATE, null, "val2");
  51. underTest.execute();
  52. assertThat(selectPropertyValues()).containsOnly("val2");
  53. assertThat(db.countRowsOfTable("PROPERTIES")).isEqualTo(1);
  54. }
  55. @Test
  56. @UseDataProvider("DP_execute_deletes_qualitygate_component_properties_for_non_existing_qualitygate")
  57. public void execute_deletes_qualitygate_component_properties_for_non_existing_qualitygate(int existingQualityGateCount, int missingQualityGateCount) throws SQLException {
  58. String[] qualityGateIds = IntStream.range(0, existingQualityGateCount)
  59. .mapToObj(i -> insertQualityGate())
  60. .map(s -> {
  61. int componentId = 2 + s;
  62. String qualityGateId = String.valueOf(s);
  63. insertProperty(PROPERTY_SONAR_QUALITYGATE, componentId, qualityGateId);
  64. return qualityGateId;
  65. })
  66. .toArray(String[]::new);
  67. IntStream.range(0, missingQualityGateCount)
  68. .forEach(i -> {
  69. int componentId = 3_000 + i;
  70. insertProperty(PROPERTY_SONAR_QUALITYGATE, componentId, "non_existing_" + i);
  71. });
  72. underTest.execute();
  73. assertThat(selectPropertyValues()).containsOnly(qualityGateIds);
  74. assertThat(db.countRowsOfTable("PROPERTIES")).isEqualTo(qualityGateIds.length);
  75. }
  76. @DataProvider
  77. public static Object[][] DP_execute_deletes_qualitygate_component_properties_for_non_existing_qualitygate() {
  78. Random random = new Random();
  79. return new Object[][] {
  80. {1, 1},
  81. {1, 2},
  82. {2, 1},
  83. {2 + random.nextInt(5), 1 + random.nextInt(5)},
  84. };
  85. }
  86. @Test
  87. public void execute_deletes_only_project_qualitygate_property() throws SQLException {
  88. String qualityGateId = String.valueOf(insertQualityGate());
  89. insertProperty(PROPERTY_SONAR_QUALITYGATE, 84651, qualityGateId);
  90. insertProperty(PROPERTY_SONAR_QUALITYGATE, 7_323, "does_not_exist");
  91. insertProperty(PROPERTY_SONAR_QUALITYGATE, null, "not a project property");
  92. insertProperty(PROPERTY_SONAR_QUALITYGATE, null, "not_a_qualitygate_id_either");
  93. underTest.execute();
  94. assertThat(selectPropertyValues()).containsExactly(qualityGateId, "not a project property", "not_a_qualitygate_id_either");
  95. assertThat(db.countRowsOfTable("PROPERTIES")).isEqualTo(3);
  96. }
  97. @Test
  98. public void execute_deletes_only_qualitygate_property_for_project() throws SQLException {
  99. String qualityGateId = String.valueOf(insertQualityGate());
  100. insertProperty(PROPERTY_SONAR_QUALITYGATE, 84651, qualityGateId);
  101. insertProperty("FOO", 84651, "does_not_exist");
  102. underTest.execute();
  103. assertThat(selectPropertyValues()).containsExactly(qualityGateId, "does_not_exist");
  104. assertThat(db.countRowsOfTable("PROPERTIES")).isEqualTo(2);
  105. }
  106. private Stream<String> selectPropertyValues() {
  107. return db.select("select text_value as \"value\" from properties").stream().map(s -> (String) s.get("value"));
  108. }
  109. private void insertProperty(String key, @Nullable Integer componentId, String value) {
  110. db.executeInsert(
  111. "PROPERTIES",
  112. "PROP_KEY", key,
  113. "RESOURCE_ID", componentId,
  114. "IS_EMPTY", value.isEmpty(),
  115. "TEXT_VALUE", value);
  116. }
  117. private static int qualityGateIdGenerator = 2_999_567 + new Random().nextInt(56);
  118. private int insertQualityGate() {
  119. int id = qualityGateIdGenerator++;
  120. db.executeInsert(
  121. "QUALITY_GATES",
  122. "ID", id,
  123. "UUID", "uuid_" + id,
  124. "NAME", "name_" + id,
  125. "IS_BUILT_IN", new Random().nextBoolean());
  126. return id;
  127. }
  128. }