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.

FixMissingQualityProfilesOnOrganizationsTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.v73;
  21. import java.sql.SQLException;
  22. import java.sql.Timestamp;
  23. import java.util.stream.Collectors;
  24. import org.assertj.core.groups.Tuple;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.api.impl.config.MapSettings;
  28. import org.sonar.api.impl.utils.TestSystem2;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.core.util.UuidFactoryFast;
  31. import org.sonar.core.util.Uuids;
  32. import org.sonar.db.CoreDbTester;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.tuple;
  35. public class FixMissingQualityProfilesOnOrganizationsTest {
  36. private final static long PAST = 10_000_000_000L;
  37. private final static long NOW = 50_000_000_000L;
  38. @Rule
  39. public CoreDbTester db = CoreDbTester.createForSchema(FixMissingQualityProfilesOnOrganizationsTest.class, "schema.sql");
  40. private MapSettings settings = new MapSettings();
  41. private System2 system2 = new TestSystem2().setNow(NOW);
  42. private FixMissingQualityProfilesOnOrganizations underTest = new FixMissingQualityProfilesOnOrganizations(db.database(), system2,
  43. UuidFactoryFast.getInstance(), settings.asConfig());
  44. @Test
  45. public void migration_is_reentrant_on_sonarqube() throws SQLException {
  46. underTest.execute();
  47. underTest.execute();
  48. }
  49. @Test
  50. public void create_missing_links_with_builtin() throws SQLException {
  51. setSonarCloud();
  52. String orgUuid = insertOrganization();
  53. String qProfileUuid = insertRulesProfiles("xoo profile", "xoo", true);
  54. underTest.execute();
  55. assertDefaultQProfiles(tuple(orgUuid, "xoo", retrieveOrgQProfile(orgUuid, qProfileUuid)));
  56. assertOrgQProfiles(tuple(orgUuid, qProfileUuid));
  57. }
  58. @Test
  59. public void does_nothing_when_no_missing_built_in_profile() throws SQLException {
  60. setSonarCloud();
  61. insertOrganization();
  62. insertRulesProfiles("xoo profile", "xoo", false);
  63. underTest.execute();
  64. assertDefaultQProfiles();
  65. assertOrgQProfiles();
  66. }
  67. @Test
  68. public void prefer_SonarWay_BuiltIn_quality_profile_as_default() throws SQLException {
  69. setSonarCloud();
  70. String orgUuid = insertOrganization();
  71. String anotherRuleProfileUuid = insertRulesProfiles("xoo profile", "xoo", true);
  72. String sonarWayQProfileUuid = insertRulesProfiles("Sonar way", "xoo", true);
  73. underTest.execute();
  74. assertOrgQProfiles(
  75. tuple(orgUuid, anotherRuleProfileUuid),
  76. tuple(orgUuid, sonarWayQProfileUuid)
  77. );
  78. assertDefaultQProfiles(tuple(orgUuid, "xoo", retrieveOrgQProfile(orgUuid, sonarWayQProfileUuid)));
  79. }
  80. @Test
  81. public void dont_create_duplicates_when_records_exist() throws SQLException {
  82. setSonarCloud();
  83. String orgUuid = insertOrganization();
  84. String qProfileUuid = insertRulesProfiles("xoo profile","xoo", true);
  85. insertDefaultQProfiles(orgUuid, "xoo", qProfileUuid);
  86. insertOrgQProfiles(orgUuid, qProfileUuid);
  87. underTest.execute();
  88. assertDefaultQProfiles(tuple(orgUuid, "xoo", qProfileUuid));
  89. assertOrgQProfiles(tuple(orgUuid, qProfileUuid));
  90. }
  91. @Test
  92. public void create_missing_default_qprofiles() throws SQLException {
  93. setSonarCloud();
  94. String orgUuid = insertOrganization();
  95. String qProfileUuid = insertRulesProfiles("xoo profile","xoo", true);
  96. String orgQProfileUuid = insertOrgQProfiles(orgUuid, qProfileUuid);
  97. underTest.execute();
  98. assertDefaultQProfiles(tuple(orgUuid, "xoo", orgQProfileUuid));
  99. assertOrgQProfiles(tuple(orgUuid, qProfileUuid));
  100. }
  101. @Test
  102. public void migration_is_reentrant_on_sonarcloud() throws SQLException {
  103. setSonarCloud();
  104. underTest.execute();
  105. underTest.execute();
  106. }
  107. private String insertOrganization() {
  108. String uuid = Uuids.createFast();
  109. db.executeInsert("ORGANIZATIONS",
  110. "UUID", uuid,
  111. "KEE", uuid,
  112. "NAME", uuid,
  113. "SUBSCRIPTION", "PAID",
  114. "GUARDED", false,
  115. "DEFAULT_QUALITY_GATE_UUID", "QGATE_UUID",
  116. "NEW_PROJECT_PRIVATE", false,
  117. "CREATED_AT", PAST,
  118. "UPDATED_AT", PAST);
  119. return uuid;
  120. }
  121. private String insertRulesProfiles(String name, String language, Boolean isBuiltIn) {
  122. String uuid = Uuids.createFast();
  123. db.executeInsert("RULES_PROFILES",
  124. "NAME", name,
  125. "LANGUAGE", language,
  126. "KEE", uuid,
  127. "CREATED_AT", new Timestamp(PAST),
  128. "UPDATED_AT", new Timestamp(PAST),
  129. "IS_BUILT_IN", isBuiltIn);
  130. return uuid;
  131. }
  132. private String insertOrgQProfiles(String organizationUuid, String rulesProfileUuid) {
  133. String uuid = Uuids.createFast();
  134. db.executeInsert("ORG_QPROFILES",
  135. "UUID", uuid,
  136. "ORGANIZATION_UUID", organizationUuid,
  137. "RULES_PROFILE_UUID", rulesProfileUuid,
  138. "CREATED_AT", PAST,
  139. "UPDATED_AT", PAST);
  140. return uuid;
  141. }
  142. private void insertDefaultQProfiles(String organizationUuid, String language, String qProfileUuid) {
  143. db.executeInsert("DEFAULT_QPROFILES",
  144. "ORGANIZATION_UUID", organizationUuid,
  145. "LANGUAGE", language,
  146. "QPROFILE_UUID", qProfileUuid,
  147. "CREATED_AT", PAST,
  148. "UPDATED_AT", PAST);
  149. }
  150. private String retrieveOrgQProfile(String organizationUuid, String qProfileUuid) {
  151. return (String) db.select("SELECT UUID FROM ORG_QPROFILES " +
  152. "WHERE ORGANIZATION_UUID='" + organizationUuid + "' AND RULES_PROFILE_UUID='" + qProfileUuid + "'")
  153. .iterator()
  154. .next()
  155. .get("UUID");
  156. }
  157. private void assertOrgQProfiles(Tuple... expectedTuples) {
  158. assertThat(db.select("SELECT ORGANIZATION_UUID, RULES_PROFILE_UUID FROM ORG_QPROFILES")
  159. .stream()
  160. .map(row -> new Tuple(row.get("ORGANIZATION_UUID"), row.get("RULES_PROFILE_UUID")))
  161. .collect(Collectors.toList()))
  162. .containsExactlyInAnyOrder(expectedTuples);
  163. }
  164. private void assertDefaultQProfiles(Tuple... expectedTuples) {
  165. assertThat(db.select("SELECT ORGANIZATION_UUID, LANGUAGE, QPROFILE_UUID FROM DEFAULT_QPROFILES")
  166. .stream()
  167. .map(row -> new Tuple(row.get("ORGANIZATION_UUID"), row.get("LANGUAGE"), row.get("QPROFILE_UUID")))
  168. .collect(Collectors.toList()))
  169. .containsExactlyInAnyOrder(expectedTuples);
  170. }
  171. private void setSonarCloud() {
  172. settings.setProperty("sonar.sonarcloud.enabled", true);
  173. }
  174. }