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.

PopulateQualityGatesIsBuiltInTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.db.CoreDbTester;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.api.Assertions.tuple;
  34. public class PopulateQualityGatesIsBuiltInTest {
  35. private final static long PAST = 10_000_000_000L;
  36. private final static long NOW = 50_000_000_000L;
  37. @Rule
  38. public ExpectedException expectedException = ExpectedException.none();
  39. @Rule
  40. public CoreDbTester db = CoreDbTester.createForSchema(PopulateQualityGatesIsBuiltInTest.class, "quality_gates.sql");
  41. private System2 system2 = new TestSystem2().setNow(NOW);
  42. private PopulateQualityGatesIsBuiltIn underTest = new PopulateQualityGatesIsBuiltIn(db.database(), system2);
  43. @Test
  44. public void has_no_effect_if_table_is_empty() throws SQLException {
  45. underTest.execute();
  46. assertThat(db.countRowsOfTable("quality_gates")).isEqualTo(0);
  47. }
  48. @Test
  49. public void updates_sonarqube_way_is_build_in_column_to_true() throws SQLException {
  50. insertQualityGate("SonarQube way", null);
  51. underTest.execute();
  52. assertQualityGates(tuple("SonarQube way", true, new Date(PAST), new Date(NOW)));
  53. }
  54. @Test
  55. public void updates_none_sonarqube_way_is_build_in_column_to_false() throws SQLException {
  56. insertQualityGate("Other 1", null);
  57. insertQualityGate("Other 2", null);
  58. underTest.execute();
  59. assertQualityGates(
  60. tuple("Other 1", false, new Date(PAST), new Date(NOW)),
  61. tuple("Other 2", false, new Date(PAST), new Date(NOW)));
  62. }
  63. @Test
  64. public void does_nothing_when_built_in_column_is_set() throws SQLException {
  65. insertQualityGate("SonarQube way", true);
  66. insertQualityGate("Other way", false);
  67. underTest.execute();
  68. assertQualityGates(
  69. tuple("SonarQube way", true, new Date(PAST), new Date(PAST)),
  70. tuple("Other way", false, new Date(PAST), new Date(PAST)));
  71. }
  72. @Test
  73. public void execute_is_reentreant() throws SQLException {
  74. insertQualityGate("SonarQube way", null);
  75. insertQualityGate("Other way", null);
  76. underTest.execute();
  77. underTest.execute();
  78. assertQualityGates(
  79. tuple("SonarQube way", true, new Date(PAST), new Date(NOW)),
  80. tuple("Other way", false, new Date(PAST), new Date(NOW)));
  81. }
  82. private void assertQualityGates(Tuple... expectedTuples) {
  83. assertThat(db.select("SELECT NAME, IS_BUILT_IN, CREATED_AT, UPDATED_AT FROM QUALITY_GATES")
  84. .stream()
  85. .map(map -> new Tuple(map.get("NAME"), map.get("IS_BUILT_IN"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
  86. .collect(Collectors.toList()))
  87. .containsExactlyInAnyOrder(expectedTuples);
  88. }
  89. private void insertQualityGate(String name, @Nullable Boolean builtIn) {
  90. db.executeInsert(
  91. "QUALITY_GATES",
  92. "NAME", name,
  93. "IS_BUILT_IN", builtIn == null ? null : builtIn.toString(),
  94. "CREATED_AT", new Date(PAST),
  95. "UPDATED_AT", new Date(PAST));
  96. }
  97. }