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.

ReadGlobalSonarQualityGateSettingToDefaultOrgTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.v70;
  21. import java.sql.SQLException;
  22. import java.util.Date;
  23. import java.util.stream.Collectors;
  24. import javax.annotation.Nullable;
  25. import org.assertj.core.groups.Tuple;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.sonar.api.impl.utils.TestSystem2;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.core.util.Uuids;
  32. import org.sonar.db.CoreDbTester;
  33. import org.sonar.server.platform.db.migration.step.DataChange;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.assertj.core.api.Assertions.tuple;
  36. public class ReadGlobalSonarQualityGateSettingToDefaultOrgTest {
  37. private static final long PAST = 10_000_000_000L;
  38. private static final long NOW = 20_000_000_000L;
  39. private System2 system2 = new TestSystem2().setNow(NOW);
  40. @Rule
  41. public ExpectedException expectedException = ExpectedException.none();
  42. @Rule
  43. public CoreDbTester db = CoreDbTester.createForSchema(ReadGlobalSonarQualityGateSettingToDefaultOrgTest.class, "initial.sql");
  44. private DataChange underTest = new ReadGlobalSonarQualityGateSettingToDefaultOrg(db.database(), system2);
  45. @Test
  46. public void read_sonar_quality_gate_setting_and_update_default_organization() throws SQLException {
  47. String defaultQualityGate = insertQualityGate();
  48. String otherQualityGate = insertQualityGate();
  49. String defaultOrganization = insertOrganization(otherQualityGate);
  50. String otherOrganization = insertOrganization(otherQualityGate);
  51. insertDefaultOrgProperty(defaultOrganization);
  52. insertSetting("sonar.qualitygate", selectQualityGateId(defaultQualityGate));
  53. underTest.execute();
  54. assertDefaultQualityGate(defaultOrganization, tuple(defaultQualityGate, NOW));
  55. }
  56. @Test
  57. public void does_nothing_when_no_default_quality_gate_setting() throws Exception {
  58. String defaultQualityGate = insertQualityGate();
  59. String defaultOrganization = insertOrganization(defaultQualityGate);
  60. insertDefaultOrgProperty(defaultOrganization);
  61. insertQualityGate();
  62. underTest.execute();
  63. assertDefaultQualityGate(defaultOrganization, tuple(defaultQualityGate, PAST));
  64. }
  65. @Test
  66. public void migration_is_reentrant() throws Exception {
  67. String defaultOrganization = insertOrganization(null);
  68. insertDefaultOrgProperty(defaultOrganization);
  69. String qualityGate = insertQualityGate();
  70. insertSetting("sonar.qualitygate", selectQualityGateId(qualityGate));
  71. underTest.execute();
  72. assertDefaultQualityGate(defaultOrganization, tuple(qualityGate, NOW));
  73. underTest.execute();
  74. assertDefaultQualityGate(defaultOrganization, tuple(qualityGate, NOW));
  75. }
  76. @Test
  77. public void fail_when_no_default_organization() throws Exception {
  78. String qualityGate = insertQualityGate();
  79. insertSetting("sonar.qualitygate", selectQualityGateId(qualityGate));
  80. expectedException.expect(IllegalStateException.class);
  81. expectedException.expectMessage("Default organization uuid is missing");
  82. underTest.execute();
  83. }
  84. private void assertDefaultQualityGate(String uuid, Tuple... expectedTuples) {
  85. assertThat(db.select(String.format("SELECT DEFAULT_QUALITY_GATE_UUID, UPDATED_AT FROM ORGANIZATIONS WHERE UUID='%s'", uuid))
  86. .stream()
  87. .map(map -> new Tuple(map.get("DEFAULT_QUALITY_GATE_UUID"), map.get("UPDATED_AT")))
  88. .collect(Collectors.toList()))
  89. .containsExactlyInAnyOrder(expectedTuples);
  90. }
  91. private long selectQualityGateId(String uuid){
  92. return ((Long) db.selectFirst("select id as \"ID\" from quality_gates where uuid='" + uuid + "'").get("ID")).intValue();
  93. }
  94. private String insertOrganization(@Nullable String defaultQualityGateUuid) {
  95. String uuid = Uuids.createFast();
  96. db.executeInsert(
  97. "organizations",
  98. "UUID", uuid,
  99. "KEE", uuid,
  100. "NAME", uuid,
  101. "GUARDED", String.valueOf(false),
  102. "NEW_PROJECT_PRIVATE", String.valueOf(true),
  103. "DEFAULT_QUALITY_GATE_UUID", defaultQualityGateUuid,
  104. "CREATED_AT", PAST,
  105. "UPDATED_AT", PAST);
  106. return uuid;
  107. }
  108. private void insertDefaultOrgProperty(String uuid) {
  109. db.executeInsert(
  110. "internal_properties",
  111. "KEE", "organization.default",
  112. "IS_EMPTY", String.valueOf(false),
  113. "TEXT_VALUE", uuid,
  114. "CREATED_AT", PAST);
  115. }
  116. private String insertQualityGate() {
  117. String uuid = Uuids.createFast();
  118. db.executeInsert(
  119. "quality_gates",
  120. "UUID", uuid,
  121. "NAME", uuid,
  122. "IS_BUILT_IN", String.valueOf(false),
  123. "CREATED_AT", new Date(PAST),
  124. "UPDATED_AT", new Date(PAST));
  125. return uuid;
  126. }
  127. private void insertSetting(String key, Long value) {
  128. db.executeInsert(
  129. "properties",
  130. "PROP_KEY", key,
  131. "TEXT_VALUE", value,
  132. "IS_EMPTY", false,
  133. "CREATED_AT", PAST);
  134. }
  135. }