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.

ReplaceIndexInProjectBranchesTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.v71;
  21. import java.sql.SQLException;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.db.CoreDbTester;
  26. import static org.sonar.server.platform.db.migration.version.v71.ReplaceIndexInProjectBranches.NEW_INDEX_NAME;
  27. import static org.sonar.server.platform.db.migration.version.v71.ReplaceIndexInProjectBranches.KEE_COLUMN;
  28. import static org.sonar.server.platform.db.migration.version.v71.ReplaceIndexInProjectBranches.KEY_TYPE_COLUMN;
  29. import static org.sonar.server.platform.db.migration.version.v71.ReplaceIndexInProjectBranches.PROJECT_UUID_COLUMN;
  30. import static org.sonar.server.platform.db.migration.version.v71.ReplaceIndexInProjectBranches.TABLE_NAME;
  31. public class ReplaceIndexInProjectBranchesTest {
  32. @Rule
  33. public final CoreDbTester dbTester = CoreDbTester.createForSchema(ReplaceIndexInProjectBranchesTest.class, "project_branches.sql");
  34. @Rule
  35. public ExpectedException expectedException = ExpectedException.none();
  36. private ReplaceIndexInProjectBranches underTest = new ReplaceIndexInProjectBranches(dbTester.database());
  37. @Test
  38. public void column_is_part_of_index() throws SQLException {
  39. underTest.execute();
  40. dbTester.assertUniqueIndex(TABLE_NAME, NEW_INDEX_NAME, PROJECT_UUID_COLUMN.getName(), KEE_COLUMN.getName(), KEY_TYPE_COLUMN.getName());
  41. }
  42. @Test
  43. public void adding_pr_with_same_key_as_existing_branch_fails_before_migration() {
  44. expectedException.expect(IllegalStateException.class);
  45. String key = "feature/foo";
  46. insertBranch(1, key);
  47. insertPullRequest(2, key);
  48. }
  49. @Test
  50. public void adding_pr_with_same_key_as_existing_branch_works_after_migration() throws SQLException {
  51. underTest.execute();
  52. String key = "feature/foo";
  53. insertBranch(1, key);
  54. insertPullRequest(2, key);
  55. }
  56. private void insertBranch(int id, String name) {
  57. insertRow(id, "SHORT", name, "BRANCH");
  58. }
  59. private void insertPullRequest(int id, String pullRequestId) {
  60. insertRow(id, "PULL_REQUEST", pullRequestId, "PULL_REQUEST");
  61. }
  62. private void insertRow(int id, String branchType, String key, String keyType) {
  63. dbTester.executeInsert(
  64. "PROJECT_BRANCHES",
  65. "UUID", "dummy_uuid" + id,
  66. "PROJECT_UUID", "dummy_project_uuid",
  67. "KEE", key,
  68. "KEY_TYPE", keyType,
  69. "CREATED_AT", 456789 + id,
  70. "UPDATED_AT", 456789 + id,
  71. "BRANCH_TYPE", branchType);
  72. }
  73. }