選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

PopulateTableProjectLinks2Test.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 java.util.stream.Collectors;
  23. import org.assertj.core.groups.Tuple;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.api.utils.internal.TestSystem2;
  29. import org.sonar.core.util.SequenceUuidFactory;
  30. import org.sonar.core.util.UuidFactory;
  31. import org.sonar.db.CoreDbTester;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.assertj.core.groups.Tuple.tuple;
  34. public class PopulateTableProjectLinks2Test {
  35. private static final long PAST = 5_000_000_000L;
  36. private static final long NOW = 10_000_000_000L;
  37. @Rule
  38. public ExpectedException expectedException = ExpectedException.none();
  39. @Rule
  40. public CoreDbTester db = CoreDbTester.createForSchema(PopulateTableProjectLinks2Test.class, "project_links2.sql");
  41. private System2 system2 = new TestSystem2().setNow(NOW);
  42. private UuidFactory uuidFactory = new SequenceUuidFactory();
  43. private PopulateTableProjectLinks2 underTest = new PopulateTableProjectLinks2(db.database(), uuidFactory, system2);
  44. @Test
  45. public void copy_custom_links() throws SQLException {
  46. String project = insertProject();
  47. insertProjectLink("Name1", "custom1", "http://link1", project);
  48. insertProjectLink("Name2", "custom2", "http://link2", project);
  49. underTest.execute();
  50. assertProjectLinks2(
  51. tuple("Name1", "custom1", "http://link1", project, NOW, NOW),
  52. tuple("Name2", "custom2", "http://link2", project, NOW, NOW));
  53. }
  54. @Test
  55. public void remove_name_of_provided_links() throws SQLException {
  56. String project = insertProject();
  57. insertProjectLink("Home", "homepage", "http://homepage", project);
  58. insertProjectLink("CI", "ci", "http://ci", project);
  59. insertProjectLink("Jira", "issue", "http://issue", project);
  60. insertProjectLink("SCM", "scm", "http://scm", project);
  61. underTest.execute();
  62. assertProjectLinks2(
  63. tuple(null, "homepage", "http://homepage", project, NOW, NOW),
  64. tuple(null, "ci", "http://ci", project, NOW, NOW),
  65. tuple(null, "issue", "http://issue", project, NOW, NOW),
  66. tuple(null, "scm", "http://scm", project, NOW, NOW));
  67. }
  68. @Test
  69. public void copy_links_from_different_projects() throws SQLException {
  70. String project1 = insertProject();
  71. insertProjectLink("Name", "custom", "http://link1", project1);
  72. String project2 = insertProject();
  73. insertProjectLink("Name", "custom", "http://link2", project2);
  74. underTest.execute();
  75. assertProjectLinks2(
  76. tuple("Name", "custom", "http://link1", project1, NOW, NOW),
  77. tuple("Name", "custom", "http://link2", project2, NOW, NOW));
  78. }
  79. @Test
  80. public void do_not_copy_links_from_developer_connection_link() throws SQLException {
  81. insertProjectLink("Dev", "scm_dev", "http://link1", insertProject());
  82. underTest.execute();
  83. assertNoProjectLinks2();
  84. }
  85. @Test
  86. public void do_not_copy_links_from_components_that_are_not_projects() throws SQLException {
  87. insertProjectLink("Name", "custom", "http://link1", insertComponent("PRJ", "BRC"));
  88. insertProjectLink("Name", "custom", "http://link2", insertComponent("PRJ", "VW"));
  89. insertProjectLink("Name", "custom", "http://link1", insertComponent("DIR", "DIR"));
  90. insertProjectLink("Name", "custom", "http://link1", "UNKNOWN");
  91. underTest.execute();
  92. assertNoProjectLinks2();
  93. }
  94. @Test
  95. public void do_not_copy_already_copied_data() throws SQLException {
  96. String project = insertProject();
  97. insertProjectLink("Name", "custom", "http://link", project);
  98. insertProjectLink("Home", "homepage", "http://homepage", project);
  99. insertProjectLink2("UUID1", "Name", "custom", "http://link", project, PAST);
  100. insertProjectLink2("UUID2", null, "homepage", "http://homepage", project, PAST);
  101. underTest.execute();
  102. assertThat(db.select("SELECT UUID, NAME, LINK_TYPE, HREF, PROJECT_UUID, CREATED_AT FROM PROJECT_LINKS2")
  103. .stream()
  104. .map(map -> new Tuple(map.get("UUID"), map.get("NAME"), map.get("LINK_TYPE"), map.get("HREF"), map.get("PROJECT_UUID"), map.get("CREATED_AT")))
  105. .collect(Collectors.toList()))
  106. .containsExactlyInAnyOrder(
  107. tuple("UUID1", "Name", "custom", "http://link", project, PAST),
  108. tuple("UUID2", null, "homepage", "http://homepage", project, PAST));
  109. }
  110. @Test
  111. public void migration_is_reentrant() throws SQLException {
  112. String project = insertProject();
  113. insertProjectLink("Name", "custom", "http://link", project);
  114. underTest.execute();
  115. underTest.execute();
  116. assertProjectLinks2(tuple("Name", "custom", "http://link", project, NOW, NOW));
  117. }
  118. @Test
  119. public void has_no_effect_if_table_is_empty() throws SQLException {
  120. underTest.execute();
  121. assertThat(db.countRowsOfTable("project_links2")).isZero();
  122. }
  123. private void assertNoProjectLinks2() {
  124. assertProjectLinks2();
  125. }
  126. private void assertProjectLinks2(Tuple... expectedTuples) {
  127. assertThat(db.select("SELECT NAME, LINK_TYPE, HREF, PROJECT_UUID, CREATED_AT, UPDATED_AT FROM PROJECT_LINKS2")
  128. .stream()
  129. .map(map -> new Tuple(map.get("NAME"), map.get("LINK_TYPE"), map.get("HREF"), map.get("PROJECT_UUID"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
  130. .collect(Collectors.toList()))
  131. .containsExactlyInAnyOrder(expectedTuples);
  132. }
  133. private void insertProjectLink(String name, String linkType, String href, String componentUuid) {
  134. db.executeInsert(
  135. "PROJECT_LINKS",
  136. "COMPONENT_UUID", componentUuid,
  137. "NAME", name,
  138. "LINK_TYPE", linkType,
  139. "HREF", href);
  140. }
  141. private void insertProjectLink2(String uuid, String name, String linkType, String href, String componentUuid, Long createdAt) {
  142. db.executeInsert(
  143. "PROJECT_LINKS2",
  144. "UUID", uuid,
  145. "PROJECT_UUID", componentUuid,
  146. "NAME", name,
  147. "LINK_TYPE", linkType,
  148. "HREF", href,
  149. "CREATED_AT", createdAt,
  150. "UPDATED_AT", createdAt);
  151. }
  152. private String insertProject() {
  153. return insertComponent("PRJ", "TRK");
  154. }
  155. private String insertComponent(String scope, String qualifier) {
  156. String uuid = uuidFactory.create();
  157. db.executeInsert("PROJECTS",
  158. "ORGANIZATION_UUID", "O1",
  159. "KEE", uuid,
  160. "UUID", uuid,
  161. "PROJECT_UUID", uuid,
  162. "MAIN_BRANCH_PROJECT_UUID", uuid,
  163. "UUID_PATH", ".",
  164. "ROOT_UUID", uuid,
  165. "PRIVATE", "true",
  166. "QUALIFIER", qualifier,
  167. "SCOPE", scope);
  168. return uuid;
  169. }
  170. }