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.

PopulateMainProjectBranchesTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.v66;
  21. import java.sql.SQLException;
  22. import java.util.stream.Collectors;
  23. import javax.annotation.Nullable;
  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.core.util.Uuids;
  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 PopulateMainProjectBranchesTest {
  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(PopulateMainProjectBranchesTest.class, "initial.sql");
  41. private System2 system2 = new TestSystem2().setNow(NOW);
  42. private PopulateMainProjectBranches underTest = new PopulateMainProjectBranches(db.database(), system2);
  43. @Test
  44. public void migrate() throws SQLException {
  45. String project = insertProject();
  46. underTest.execute();
  47. assertProjectBranches(tuple("master", project, project, "LONG", NOW, NOW));
  48. }
  49. @Test
  50. public void does_nothing_on_non_projects() throws SQLException {
  51. insertProject(null, "BRC");
  52. insertProject(null, "VW");
  53. underTest.execute();
  54. assertThat(db.countRowsOfTable("project_branches")).isZero();
  55. }
  56. @Test
  57. public void does_nothing_on_empty_table() throws SQLException {
  58. underTest.execute();
  59. assertThat(db.countRowsOfTable("project_branches")).isZero();
  60. }
  61. @Test
  62. public void does_nothing_if_already_migrated() throws SQLException {
  63. String project = insertProject();
  64. insertMainBranch(project);
  65. underTest.execute();
  66. assertProjectBranches(tuple("master", project, project, "LONG", PAST, PAST));
  67. }
  68. private void assertProjectBranches(Tuple... expectedTuples) {
  69. assertThat(db.select("SELECT KEE, UUID, PROJECT_UUID, BRANCH_TYPE, CREATED_AT, UPDATED_AT FROM PROJECT_BRANCHES")
  70. .stream()
  71. .map(map -> new Tuple(map.get("KEE"), map.get("UUID"), map.get("PROJECT_UUID"), map.get("BRANCH_TYPE"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
  72. .collect(Collectors.toList()))
  73. .containsExactlyInAnyOrder(expectedTuples);
  74. }
  75. private String insertProject() {
  76. return insertProject(null, "PRJ");
  77. }
  78. private String insertProject(@Nullable String mainBranchUuid, String scope) {
  79. String uuid = Uuids.createFast();
  80. db.executeInsert("PROJECTS",
  81. "ORGANIZATION_UUID", "default-org",
  82. "KEE", uuid + "-key",
  83. "UUID", uuid,
  84. "PROJECT_UUID", uuid,
  85. "main_branch_project_uuid", mainBranchUuid,
  86. "UUID_PATH", ".",
  87. "ROOT_UUID", uuid,
  88. "PRIVATE", "true",
  89. "qualifier", "TRK",
  90. "scope", scope);
  91. return uuid;
  92. }
  93. private void insertMainBranch(String uuid) {
  94. db.executeInsert("PROJECT_BRANCHES",
  95. "uuid", uuid,
  96. "project_uuid", uuid,
  97. "kee", "master",
  98. "branch_type", "LONG",
  99. "created_at", PAST,
  100. "updated_at", PAST);
  101. }
  102. }