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.

UpdateIsMainColumnInProjectBranchesTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.v101;
  21. import java.sql.SQLException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.stream.Collectors;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.core.util.UuidFactory;
  28. import org.sonar.core.util.UuidFactoryFast;
  29. import org.sonar.db.CoreDbTester;
  30. import org.sonar.server.platform.db.migration.step.DataChange;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. public class UpdateIsMainColumnInProjectBranchesTest {
  33. private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
  34. @Rule
  35. public CoreDbTester db = CoreDbTester.createForSchema(UpdateIsMainColumnInProjectBranchesTest.class, "schema.sql");
  36. private final DataChange underTest = new UpdateIsMainColumnInProjectBranches(db.database());
  37. private static int not_random_value_always_incremented = 0;
  38. @Test
  39. public void migration_updates_is_main_if_row_has_the_same_uuids() throws SQLException {
  40. String branchUuid1 = insertProjectBranch(true);
  41. String branchUuid2 = insertProjectBranch(false);
  42. underTest.execute();
  43. assertBranchIsMain(branchUuid1);
  44. assertBranchIsNotMain(branchUuid2);
  45. }
  46. @Test
  47. public void migration_should_be_reentrant() throws SQLException {
  48. String branchUuid1 = insertProjectBranch(true);
  49. String branchUuid2 = insertProjectBranch(false);
  50. underTest.execute();
  51. // re-entrant
  52. underTest.execute();
  53. assertBranchIsMain(branchUuid1);
  54. assertBranchIsNotMain(branchUuid2);
  55. }
  56. private void assertBranchIsMain(String branchUuid) {
  57. assertBranchIs(branchUuid, true);
  58. }
  59. private void assertBranchIsNotMain(String branchUuid) {
  60. assertBranchIs(branchUuid, false);
  61. }
  62. private void assertBranchIs(String branchUuid, boolean isMain) {
  63. String selectSql = String.format("select is_main from project_branches where uuid='%s'", branchUuid);
  64. assertThat(db.select(selectSql).stream()
  65. .map(row -> row.get("IS_MAIN"))
  66. .toList())
  67. .containsExactlyInAnyOrder(isMain);
  68. }
  69. private String insertProjectBranch(boolean sameUuids) {
  70. Map<String, Object> map = new HashMap<>();
  71. String uuid = uuidFactory.create();
  72. map.put("UUID", uuid);
  73. if(sameUuids) {
  74. map.put("PROJECT_UUID", uuid);
  75. } else {
  76. map.put("PROJECT_UUID", "uuid" + not_random_value_always_incremented++);
  77. }
  78. map.put("KEE", "randomKey");
  79. map.put("BRANCH_TYPE", "BRANCH");
  80. map.put("MERGE_BRANCH_UUID", null);
  81. map.put("CREATED_AT", System.currentTimeMillis());
  82. map.put("UPDATED_AT", System.currentTimeMillis());
  83. map.put("PULL_REQUEST_BINARY", null);
  84. map.put("EXCLUDE_FROM_PURGE", true);
  85. map.put("NEED_ISSUE_SYNC", false);
  86. db.executeInsert("project_branches", map);
  87. return uuid;
  88. }
  89. }