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.

PurgeMapperTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.db.purge;
  21. import java.util.Random;
  22. import org.junit.After;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.DbTester;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.db.component.ComponentTesting;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. public class PurgeMapperTest {
  33. @Rule
  34. public DbTester db = DbTester.create(System2.INSTANCE);
  35. private DbSession dbSession;
  36. private PurgeMapper purgeMapper;
  37. @Before
  38. public void setUp() {
  39. dbSession = db.getDbClient().openSession(false);
  40. purgeMapper = dbSession.getMapper(PurgeMapper.class);
  41. }
  42. @After
  43. public void tearDown() {
  44. if (dbSession != null) {
  45. dbSession.close();
  46. }
  47. }
  48. @Test
  49. public void selectRootAndModulesOrSubviewsByProjectUuid_returns_empty_when_table_is_empty() {
  50. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid("foo")).isEmpty();
  51. }
  52. @Test
  53. public void selectRootAndModulesOrSubviewsByProjectUuid_returns_project_with_specified_uuid() {
  54. ComponentDto project = randomPublicOrPrivateProject();
  55. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(project.uuid()))
  56. .extracting(IdUuidPair::getUuid)
  57. .containsOnly(project.uuid());
  58. }
  59. @Test
  60. public void selectRootAndModulesOrSubviewsByProjectUuid_returns_modules_with_specified_project_uuid_and_project() {
  61. ComponentDto project = randomPublicOrPrivateProject();
  62. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project));
  63. ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(project));
  64. ComponentDto module3 = db.components().insertComponent(ComponentTesting.newModuleDto(project));
  65. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(project.uuid()))
  66. .extracting(IdUuidPair::getUuid)
  67. .containsOnly(project.uuid(), module1.uuid(), module2.uuid(), module3.uuid());
  68. }
  69. private ComponentDto randomPublicOrPrivateProject() {
  70. return new Random().nextBoolean() ? db.components().insertPrivateProject() : db.components().insertPublicProject();
  71. }
  72. @Test
  73. public void selectRootAndModulesOrSubviewsByProjectUuid_returns_view_with_specified_uuid() {
  74. ComponentDto view = db.components().insertView();
  75. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(view.uuid()))
  76. .extracting(IdUuidPair::getUuid)
  77. .containsOnly(view.uuid());
  78. }
  79. @Test
  80. public void selectRootAndModulesOrSubviewsByProjectUuid_returns_application_with_specified_uuid() {
  81. ComponentDto view = db.components().insertApplication(db.getDefaultOrganization());
  82. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(view.uuid()))
  83. .extracting(IdUuidPair::getUuid)
  84. .containsOnly(view.uuid());
  85. }
  86. @Test
  87. public void selectRootAndModulesOrSubviewsByProjectUuid_returns_subviews_with_specified_project_uuid_and_view() {
  88. ComponentDto view = db.components().insertView();
  89. ComponentDto subview1 = db.components().insertComponent(ComponentTesting.newSubView(view));
  90. ComponentDto subview2 = db.components().insertComponent(ComponentTesting.newSubView(view));
  91. ComponentDto subview3 = db.components().insertComponent(ComponentTesting.newSubView(view));
  92. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(view.uuid()))
  93. .extracting(IdUuidPair::getUuid)
  94. .containsOnly(view.uuid(), subview1.uuid(), subview2.uuid(), subview3.uuid());
  95. }
  96. @Test
  97. public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_project_copy_with_specified_project_uuid() {
  98. ComponentDto privateProject = db.components().insertPrivateProject();
  99. ComponentDto view = db.components().insertView();
  100. db.components().insertComponent(ComponentTesting.newProjectCopy("a", view, privateProject));
  101. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(view.uuid()))
  102. .extracting(IdUuidPair::getUuid)
  103. .containsOnly(view.uuid());
  104. }
  105. @Test
  106. public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_module_with_specified_uuid() {
  107. ComponentDto privateProject = db.components().insertPrivateProject();
  108. ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(privateProject));
  109. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(module.uuid()))
  110. .isEmpty();
  111. }
  112. @Test
  113. public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_directory_with_specified_uuid() {
  114. ComponentDto privateProject = db.components().insertPrivateProject();
  115. ComponentDto directory = db.components().insertComponent(ComponentTesting.newDirectory(privateProject, "A/B"));
  116. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(directory.uuid()))
  117. .isEmpty();
  118. }
  119. @Test
  120. public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_file_with_specified_uuid() {
  121. ComponentDto privateProject = db.components().insertPrivateProject();
  122. ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(privateProject));
  123. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(file.uuid()))
  124. .isEmpty();
  125. }
  126. @Test
  127. public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_subview_with_specified_uuid() {
  128. ComponentDto view = db.components().insertView();
  129. ComponentDto subview = db.components().insertComponent(ComponentTesting.newSubView(view));
  130. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(subview.uuid()))
  131. .isEmpty();
  132. }
  133. @Test
  134. public void selectRootAndModulesOrSubviewsByProjectUuid_does_not_return_technicalCopy_with_specified_uuid() {
  135. ComponentDto privateProject = db.components().insertPrivateProject();
  136. ComponentDto view = db.components().insertView();
  137. ComponentDto technicalCopy = db.components().insertComponent(ComponentTesting.newProjectCopy("a", view, privateProject));
  138. assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(technicalCopy.uuid()))
  139. .isEmpty();
  140. }
  141. }