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.

ProjectDeletionTest.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.sonarqube.tests.project;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import com.sonar.orchestrator.build.SonarScanner;
  23. import java.util.Arrays;
  24. import java.util.Collection;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. import org.junit.ClassRule;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.DisableOnDebug;
  31. import org.junit.rules.TestRule;
  32. import org.junit.rules.Timeout;
  33. import org.sonarqube.qa.util.Tester;
  34. import org.sonarqube.ws.Components;
  35. import org.sonarqube.ws.Organizations;
  36. import org.sonarqube.ws.Projects;
  37. import org.sonarqube.ws.Projects.CreateWsResponse.Project;
  38. import org.sonarqube.ws.Users;
  39. import org.sonarqube.ws.client.GetRequest;
  40. import org.sonarqube.ws.client.WsClient;
  41. import org.sonarqube.ws.client.WsResponse;
  42. import org.sonarqube.ws.client.components.SearchProjectsRequest;
  43. import org.sonarqube.ws.client.projects.BulkDeleteRequest;
  44. import org.sonarqube.ws.client.projects.CreateRequest;
  45. import org.sonarqube.ws.client.projects.DeleteRequest;
  46. import org.sonarqube.ws.client.projects.SearchRequest;
  47. import util.ItUtils;
  48. import static java.util.Collections.singletonList;
  49. import static org.assertj.core.api.Assertions.assertThat;
  50. import static util.ItUtils.getComponent;
  51. import static util.ItUtils.projectDir;
  52. public class ProjectDeletionTest {
  53. @ClassRule
  54. public static final Orchestrator orchestrator = ProjectSuite.ORCHESTRATOR;
  55. @Rule
  56. public TestRule safeguard = new DisableOnDebug(Timeout.seconds(300));
  57. @Rule
  58. public Tester tester = new Tester(orchestrator).setElasticsearchHttpPort(ProjectSuite.SEARCH_HTTP_PORT);
  59. @Test
  60. public void delete_project_by_web_service() {
  61. String projectKey = "sample";
  62. String fileKey = "sample:src/main/xoo/sample/Sample.xoo";
  63. analyzeXooSample();
  64. assertThat(getComponent(orchestrator, projectKey)).isNotNull();
  65. assertThat(getComponent(orchestrator, fileKey)).isNotNull();
  66. // fail to delete a file
  67. ItUtils.expectBadRequestError(() -> executeDeleteRequest(tester.wsClient(), fileKey));
  68. // fail if anonymous
  69. ItUtils.expectUnauthorizedError(() -> executeDeleteRequest(tester.asAnonymous().wsClient(), projectKey));
  70. // fail if insufficient privilege
  71. Users.CreateWsResponse.User user = tester.users().generate();
  72. ItUtils.expectForbiddenError(() -> executeDeleteRequest(tester.as(user.getLogin()).wsClient(), projectKey));
  73. // succeed to delete if administrator
  74. executeDeleteRequest(tester.wsClient(), projectKey);
  75. assertThat(getComponent(orchestrator, "sample")).isNull();
  76. assertThat(getComponent(orchestrator, "sample:src/main/xoo/sample/Sample.xoo")).isNull();
  77. }
  78. @Test
  79. public void deletion_removes_project_from_search_engines() {
  80. Organizations.Organization organization = tester.organizations().generate();
  81. Project project1 = createProject(organization, "one", "Foo");
  82. Project project2 = createProject(organization, "two", "Bar");
  83. assertThatProjectIsSearchable(organization, "Foo");
  84. assertThatProjectIsSearchable(organization, "Bar");
  85. deleteProject(project1);
  86. assertThatProjectIsNotSearchable(organization, project1.getName());
  87. assertThatProjectIsSearchable(organization, project2.getName());
  88. deleteProject(project2);
  89. assertThatProjectIsNotSearchable(organization, project1.getName());
  90. assertThatProjectIsNotSearchable(organization, project2.getName());
  91. }
  92. @Test
  93. public void indexing_errors_are_recovered_asynchronously_when_deleting_project() throws Exception {
  94. Organizations.Organization organization = tester.organizations().generate();
  95. Project project = createProject(organization, "one", "Foo");
  96. tester.elasticsearch().lockWrites("components");
  97. tester.elasticsearch().lockWrites("projectmeasures");
  98. deleteProject(project);
  99. // WS reloads from database the results returned by Elasticsearch. That's
  100. // why the project does not appear in search engine.
  101. // However this test is still useful to verify that WS do not
  102. // fail during this Elasticsearch inconsistency.
  103. assertThatProjectIsNotSearchable(organization, project.getName());
  104. tester.elasticsearch().unlockWrites("components");
  105. tester.elasticsearch().unlockWrites("projectmeasures");
  106. // TODO verify that recovery daemon successfully updated indices
  107. }
  108. @Test
  109. public void bulk_deletion_removes_projects_from_search_engines() {
  110. Organizations.Organization organization = tester.organizations().generate();
  111. Project project1 = createProject(organization, "one", "Foo");
  112. Project project2 = createProject(organization, "two", "Bar");
  113. Project project3 = createProject(organization, "three", "Baz");
  114. bulkDeleteProjects(organization, project1, project3);
  115. assertThatProjectIsNotSearchable(organization, project1.getName());
  116. assertThatProjectIsSearchable(organization, project2.getName());
  117. assertThatProjectIsNotSearchable(organization, project3.getName());
  118. }
  119. @Test
  120. public void indexing_errors_are_recovered_asynchronously_when_bulk_deleting_projects() throws Exception {
  121. Organizations.Organization organization = tester.organizations().generate();
  122. Project project1 = createProject(organization, "one", "Foo");
  123. Project project2 = createProject(organization, "two", "Bar");
  124. Project project3 = createProject(organization, "three", "Baz");
  125. tester.elasticsearch().lockWrites("components");
  126. tester.elasticsearch().lockWrites("projectmeasures");
  127. bulkDeleteProjects(organization, project1, project3);
  128. // WS reloads from database the results returned by Elasticsearch. That's
  129. // why the project does not appear in search engine.
  130. // However this test is still useful to verify that WS do not
  131. // fail during this Elasticsearch inconsistency.
  132. assertThatProjectIsNotSearchable(organization, project1.getName());
  133. assertThatProjectIsSearchable(organization, project2.getName());
  134. assertThatProjectIsNotSearchable(organization, project3.getName());
  135. tester.elasticsearch().unlockWrites("components");
  136. tester.elasticsearch().unlockWrites("projectmeasures");
  137. // TODO verify that recovery daemon successfully updated indices
  138. }
  139. private void deleteProject(Project project) {
  140. tester.wsClient().projects().delete(new DeleteRequest().setProject(project.getKey()));
  141. }
  142. private void bulkDeleteProjects(Organizations.Organization organization, Project... projects) {
  143. BulkDeleteRequest request = new BulkDeleteRequest()
  144. .setOrganization(organization.getKey())
  145. .setProjects(Arrays.stream(projects).map(Project::getKey).collect(Collectors.toList()));
  146. tester.wsClient().projects().bulkDelete(request);
  147. }
  148. private Project createProject(Organizations.Organization organization, String key, String name) {
  149. CreateRequest createRequest = new CreateRequest().setProject(key).setName(name).setOrganization(organization.getKey());
  150. return tester.wsClient().projects().create(createRequest).getProject();
  151. }
  152. private void assertThatProjectIsSearchable(Organizations.Organization organization, String name) {
  153. assertThat(isInProjectsSearch(organization, name)).isTrue();
  154. assertThat(isInComponentSearchProjects(name)).isTrue();
  155. assertThat(isInComponentSuggestions(name)).isTrue();
  156. }
  157. private void assertThatProjectIsNotSearchable(Organizations.Organization organization, String name) {
  158. assertThat(isInProjectsSearch(organization, name)).isFalse();
  159. assertThat(isInComponentSearchProjects(name)).isFalse();
  160. assertThat(isInComponentSuggestions(name)).isFalse();
  161. }
  162. /**
  163. * Projects administration page - uses database
  164. */
  165. private boolean isInProjectsSearch(Organizations.Organization organization, String name) {
  166. Projects.SearchWsResponse response = tester.wsClient().projects().search(
  167. new SearchRequest().setOrganization(organization.getKey()).setQ(name).setQualifiers(singletonList("TRK")));
  168. return response.getComponentsCount() > 0;
  169. }
  170. /**
  171. * Projects page - api/components/search_projects - uses ES + DB
  172. */
  173. private boolean isInComponentSearchProjects(String name) {
  174. Components.SearchProjectsWsResponse response = tester.wsClient().components().searchProjects(
  175. new SearchProjectsRequest().setFilter("query=\"" + name + "\""));
  176. return response.getComponentsCount() > 0;
  177. }
  178. /**
  179. * Top-right search engine - api/components/suggestions - uses ES + DB
  180. */
  181. private boolean isInComponentSuggestions(String name) {
  182. GetRequest request = new GetRequest("api/components/suggestions").setParam("s", name);
  183. WsResponse response = tester.wsClient().wsConnector().call(request);
  184. Map<String, Object> json = ItUtils.jsonToMap(response.content());
  185. Collection<Map<String, Object>> results = (Collection<Map<String, Object>>) json.get("results");
  186. Collection items = results.stream()
  187. .filter(map -> "TRK".equals(map.get("q")))
  188. .map(map -> (Collection) map.get("items"))
  189. .findFirst()
  190. .orElseThrow(() -> new IllegalStateException("missing field results/[q=TRK]"));
  191. return !items.isEmpty();
  192. }
  193. private void analyzeXooSample() {
  194. SonarScanner build = SonarScanner.create(projectDir("shared/xoo-sample"));
  195. orchestrator.executeBuild(build);
  196. }
  197. private static void executeDeleteRequest(WsClient wsClient, String key) {
  198. wsClient.projects().delete(new DeleteRequest().setProject(key));
  199. }
  200. }