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.

PopulateUuidOnQualityGatesTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Arrays;
  23. import java.util.Date;
  24. import java.util.List;
  25. import javax.annotation.Nullable;
  26. import org.assertj.core.groups.Tuple;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.sonar.api.impl.utils.TestSystem2;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.core.util.UuidFactoryFast;
  33. import org.sonar.db.CoreDbTester;
  34. import static java.util.stream.Collectors.toList;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.assertj.core.api.Assertions.tuple;
  37. public class PopulateUuidOnQualityGatesTest {
  38. private final static long PAST = 10_000_000_000L;
  39. private final static long NOW = 50_000_000_000L;
  40. @Rule
  41. public ExpectedException expectedException = ExpectedException.none();
  42. @Rule
  43. public CoreDbTester db = CoreDbTester.createForSchema(PopulateUuidOnQualityGatesTest.class, "quality_gates.sql");
  44. private System2 system2 = new TestSystem2().setNow(NOW);
  45. private PopulateUuidOnQualityGates underTest = new PopulateUuidOnQualityGates(db.database(), system2, UuidFactoryFast.getInstance());
  46. @Test
  47. public void has_no_effect_if_table_is_empty() throws SQLException {
  48. underTest.execute();
  49. assertThat(db.countRowsOfTable("quality_gates")).isEqualTo(0);
  50. }
  51. @Test
  52. public void updates_uuid_when_uuid_is_null() throws SQLException {
  53. insertQualityGate("Test 1", null);
  54. insertQualityGate("Test 2", null);
  55. underTest.execute();
  56. List<Tuple> tuples = selectAllQualityGates("NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT");
  57. assertThat(selectAllQualityGates("NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT"))
  58. .containsExactlyInAnyOrder(
  59. tuple("Test 1", false, new Date(PAST), new Date(NOW)),
  60. tuple("Test 2", false, new Date(PAST), new Date(NOW)));
  61. selectAllQualityGates("UUID").forEach(c -> assertThat(c).isNotNull());
  62. }
  63. @Test
  64. public void does_not_update_uuid_when_uuid_is_not_null() throws SQLException {
  65. insertQualityGate("Test 1", "1");
  66. insertQualityGate("Test 2", "2");
  67. underTest.execute();
  68. assertThat(selectAllQualityGates("UUID", "NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT"))
  69. .containsExactlyInAnyOrder(
  70. tuple("1", "Test 1", false, new Date(PAST), new Date(PAST)),
  71. tuple("2", "Test 2", false, new Date(PAST), new Date(PAST)));
  72. }
  73. @Test
  74. public void execute_is_reentreant() throws SQLException {
  75. insertQualityGate("Test 1", null);
  76. insertQualityGate("Test 2", null);
  77. underTest.execute();
  78. underTest.execute();
  79. assertThat(selectAllQualityGates("NAME", "IS_BUILT_IN", "CREATED_AT", "UPDATED_AT"))
  80. .containsExactlyInAnyOrder(
  81. tuple("Test 1", false, new Date(PAST), new Date(NOW)),
  82. tuple("Test 2", false, new Date(PAST), new Date(NOW)));
  83. selectAllQualityGates("UUID").forEach(c -> assertThat(c).isNotNull());
  84. }
  85. private List<Tuple> selectAllQualityGates(String... columns) {
  86. return db.select("SELECT UUID, NAME, IS_BUILT_IN, CREATED_AT, UPDATED_AT FROM QUALITY_GATES")
  87. .stream()
  88. .map(map -> new Tuple(Arrays.stream(columns).map(c -> map.get(c)).collect(toList()).toArray()))
  89. .collect(toList());
  90. }
  91. private void insertQualityGate(String name, @Nullable String uuid) {
  92. db.executeInsert(
  93. "QUALITY_GATES",
  94. "NAME", name,
  95. "UUID", uuid,
  96. "IS_BUILT_IN", false,
  97. "CREATED_AT", new Date(PAST),
  98. "UPDATED_AT", new Date(PAST));
  99. }
  100. }