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.

ProjectTester.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.ws.tester;
  21. import java.util.concurrent.atomic.AtomicInteger;
  22. import java.util.function.Consumer;
  23. import javax.annotation.Nullable;
  24. import org.sonarqube.ws.Components;
  25. import org.sonarqube.ws.Projects;
  26. import org.sonarqube.ws.client.HttpException;
  27. import org.sonarqube.ws.client.WsResponse;
  28. import org.sonarqube.ws.client.components.ShowRequest;
  29. import org.sonarqube.ws.client.projects.CreateRequest;
  30. import org.sonarqube.ws.client.projects.DeleteRequest;
  31. import org.sonarqube.ws.client.projects.ExportFindingsRequest;
  32. import org.sonarqube.ws.client.projects.ProjectsService;
  33. import org.sonarqube.ws.client.projects.SearchRequest;
  34. import static java.util.Arrays.stream;
  35. import static java.util.Collections.singletonList;
  36. public class ProjectTester {
  37. private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
  38. private final TesterSession session;
  39. ProjectTester(TesterSession session) {
  40. this.session = session;
  41. }
  42. void deleteAll() {
  43. ProjectsService service = session.wsClient().projects();
  44. service.search(new SearchRequest().setQualifiers(singletonList("TRK"))).getComponentsList().forEach(p -> service.delete(new DeleteRequest().setProject(p.getKey())));
  45. }
  46. public ProjectsService service() {
  47. return session.wsClient().projects();
  48. }
  49. public WsResponse exportFindings(String projectKey, @Nullable String branchKey) {
  50. ProjectsService service = session.wsClient().projects();
  51. return service.exportFindings(new ExportFindingsRequest(projectKey, branchKey));
  52. }
  53. @SafeVarargs
  54. public final Projects.CreateWsResponse.Project provision(Consumer<CreateRequest>... populators) {
  55. String key = generateKey();
  56. CreateRequest request = new CreateRequest()
  57. .setProject(key)
  58. .setName("Name " + key);
  59. stream(populators).forEach(p -> p.accept(request));
  60. return session.wsClient().projects().create(request).getProject();
  61. }
  62. public Components.Component getComponent(String componentKey) {
  63. try {
  64. return session.wsClient().components().show(new ShowRequest().setComponent((componentKey))).getComponent();
  65. } catch (org.sonarqube.ws.client.HttpException e) {
  66. if (e.code() == 404) {
  67. return null;
  68. }
  69. throw new IllegalStateException(e);
  70. }
  71. }
  72. public boolean exists(String projectKey) {
  73. try {
  74. Components.ShowWsResponse response = session.wsClient().components().show(new ShowRequest().setComponent(projectKey));
  75. return response.getComponent() != null;
  76. } catch (HttpException e) {
  77. if (e.code() == 404) {
  78. return false;
  79. }
  80. throw new IllegalStateException(e);
  81. }
  82. }
  83. public String generateKey() {
  84. int id = ID_GENERATOR.getAndIncrement();
  85. return "key" + id;
  86. }
  87. }