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.

RenameOldSonarQubeWayQualityGateTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 org.assertj.core.groups.Tuple;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.sonar.api.impl.utils.TestSystem2;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.api.utils.log.LogTester;
  31. import org.sonar.db.CoreDbTester;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.api.Assertions.tuple;
  34. public class RenameOldSonarQubeWayQualityGateTest {
  35. private final static long PAST = 10_000_000_000L;
  36. private final static long NOW = 50_000_000_000L;
  37. private final static String SONARQUBE_WAY_QUALITY_GATE = "SonarQube way";
  38. private final static String SONAR_WAY_OUTDATED_QUALITY_GATE = "Sonar way (outdated copy)";
  39. private final static String SONAR_WAY_QUALITY_GATE = "Sonar way";
  40. @Rule
  41. public ExpectedException expectedException = ExpectedException.none();
  42. @Rule
  43. public CoreDbTester db = CoreDbTester.createForSchema(PopulateQualityGatesIsBuiltInTest.class, "quality_gates.sql");
  44. @Rule
  45. public LogTester logTester = new LogTester();
  46. private System2 system2 = new TestSystem2().setNow(NOW);
  47. private RenameOldSonarQubeWayQualityGate underTest = new RenameOldSonarQubeWayQualityGate(db.database(), system2);
  48. @Test
  49. public void has_no_effect_if_table_is_empty() throws SQLException {
  50. underTest.execute();
  51. assertThat(db.countRowsOfTable("quality_gates")).isEqualTo(0);
  52. }
  53. @Test
  54. public void should_rename_SonarQubeWay_quality_gate() throws SQLException {
  55. insertQualityGate(SONARQUBE_WAY_QUALITY_GATE, false);
  56. underTest.execute();
  57. assertQualityGates(
  58. tuple(SONAR_WAY_OUTDATED_QUALITY_GATE, false, new Date(PAST), new Date(NOW))
  59. );
  60. }
  61. @Test
  62. public void should_set_builtin_to_false_when_renaming() throws SQLException {
  63. insertQualityGate(SONARQUBE_WAY_QUALITY_GATE, true);
  64. underTest.execute();
  65. assertQualityGates(
  66. tuple(SONAR_WAY_OUTDATED_QUALITY_GATE, false, new Date(PAST), new Date(NOW))
  67. );
  68. }
  69. @Test
  70. public void should_log_a_meaningful_info_if_outdated_copy_exists() {
  71. insertQualityGate(SONARQUBE_WAY_QUALITY_GATE, false);
  72. insertQualityGate(SONAR_WAY_OUTDATED_QUALITY_GATE, false);
  73. try {
  74. underTest.execute();
  75. } catch (Exception ex) {
  76. logTester.logs().contains("There is already a quality profile with name [Sonar way (outdated copy)]");
  77. }
  78. }
  79. @Test
  80. public void should_update_only_SonarQubeWay() throws SQLException {
  81. insertQualityGate("Whatever", true);
  82. insertQualityGate("Whatever2", false);
  83. insertQualityGate(SONAR_WAY_QUALITY_GATE, true);
  84. insertQualityGate(SONARQUBE_WAY_QUALITY_GATE, false);
  85. underTest.execute();
  86. assertQualityGates(
  87. tuple("Whatever", true, new Date(PAST), new Date(PAST)),
  88. tuple("Whatever2", false, new Date(PAST), new Date(PAST)),
  89. tuple(SONAR_WAY_QUALITY_GATE, true, new Date(PAST), new Date(PAST)),
  90. tuple(SONAR_WAY_OUTDATED_QUALITY_GATE, false, new Date(PAST), new Date(NOW))
  91. );
  92. }
  93. @Test
  94. public void is_reentrant() throws SQLException {
  95. insertQualityGate(SONARQUBE_WAY_QUALITY_GATE, false);
  96. underTest.execute();
  97. underTest.execute();
  98. assertQualityGates(
  99. tuple(SONAR_WAY_OUTDATED_QUALITY_GATE, false, new Date(PAST), new Date(NOW))
  100. );
  101. }
  102. private void assertQualityGates(Tuple... expectedTuples) {
  103. assertThat(db.select("SELECT NAME, IS_BUILT_IN, CREATED_AT, UPDATED_AT FROM QUALITY_GATES")
  104. .stream()
  105. .map(map -> new Tuple(map.get("NAME"), map.get("IS_BUILT_IN"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
  106. .collect(Collectors.toList()))
  107. .containsExactlyInAnyOrder(expectedTuples);
  108. }
  109. private void insertQualityGate(String name, boolean builtIn) {
  110. db.executeInsert(
  111. "QUALITY_GATES",
  112. "NAME", name,
  113. "IS_BUILT_IN", String.valueOf(builtIn),
  114. "CREATED_AT", new Date(PAST),
  115. "UPDATED_AT", new Date(PAST));
  116. }
  117. }