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.

ApplicationTester.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 com.google.common.util.concurrent.Uninterruptibles;
  22. import com.google.gson.Gson;
  23. import java.util.List;
  24. import java.util.concurrent.TimeUnit;
  25. import java.util.concurrent.atomic.AtomicInteger;
  26. import java.util.function.Consumer;
  27. import javax.annotation.CheckForNull;
  28. import javax.annotation.Nullable;
  29. import org.junit.rules.ExternalResource;
  30. import org.sonarqube.ws.Ce;
  31. import org.sonarqube.ws.client.PostRequest;
  32. import org.sonarqube.ws.client.applications.ApplicationsService;
  33. import org.sonarqube.ws.client.applications.CreateRequest;
  34. import org.sonarqube.ws.client.applications.DeleteRequest;
  35. import org.sonarqube.ws.client.applications.SearchProjectsRequest;
  36. import org.sonarqube.ws.client.applications.ShowRequest;
  37. import org.sonarqube.ws.client.applications.UpdateRequest;
  38. import org.sonarqube.ws.client.ce.ActivityStatusRequest;
  39. import org.sonarqube.ws.client.projects.ProjectsService;
  40. import org.sonarqube.ws.client.projects.SearchRequest;
  41. import static java.util.Arrays.stream;
  42. import static java.util.Collections.singletonList;
  43. import static org.assertj.core.api.Assertions.assertThat;
  44. public class ApplicationTester extends ExternalResource {
  45. private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
  46. private final TesterSession session;
  47. ApplicationTester(TesterSession session) {
  48. this.session = session;
  49. }
  50. public ApplicationsService service() {
  51. return session.wsClient().applications();
  52. }
  53. public void deleteAll() {
  54. ProjectsService service = session.wsClient().projects();
  55. service.search(new SearchRequest().setQualifiers(singletonList("APP"))).getComponentsList()
  56. .forEach(p -> {
  57. waitForCeQueueEmpty();
  58. session.wsClient().applications().delete(new DeleteRequest().setApplication(p.getKey()));
  59. });
  60. waitForCeQueueEmpty();
  61. org.sonarqube.ws.client.components.SearchRequest searchRequest = new org.sonarqube.ws.client.components.SearchRequest().setQualifiers(singletonList("APP"));
  62. assertThat(session.wsClient().components().search(searchRequest).getComponentsList()).isEmpty();
  63. }
  64. public void updateName(String applicationKey, String name) {
  65. service().update(new UpdateRequest().setApplication(applicationKey).setName(name));
  66. }
  67. public ApplicationTester waitForCeQueueEmpty() {
  68. Ce.ActivityStatusWsResponse status;
  69. boolean empty;
  70. do {
  71. status = session.wsClient().ce().activityStatus(new ActivityStatusRequest());
  72. empty = status.getInProgress() + status.getPending() == 0;
  73. if (!empty) {
  74. Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
  75. }
  76. } while (!empty);
  77. return this;
  78. }
  79. @SafeVarargs
  80. public final Application generate(Consumer<CreateRequest>... populators) {
  81. int id = ID_GENERATOR.getAndIncrement();
  82. CreateRequest request = new CreateRequest()
  83. .setKey("applicationKey" + id)
  84. .setName("applicationName" + id)
  85. .setDescription("applicationDescription" + id);
  86. stream(populators).forEach(p -> p.accept(request));
  87. return CreateResponse.parse(session.wsClient().applications().create(request)).getApplication();
  88. }
  89. public ShowResponse show(ShowRequest showRequest) {
  90. return ShowResponse.parse(session.wsClient().applications().show(showRequest));
  91. }
  92. public void refresh() {
  93. session.wsClient().wsConnector().call(new PostRequest("/api/applications/refresh")).failIfNotSuccessful();
  94. waitForCeQueueEmpty();
  95. }
  96. public SearchProjectsResponse searchProjects(SearchProjectsRequest searchProjectsRequest) {
  97. return SearchProjectsResponse.parse(session.wsClient().applications().searchProjects(searchProjectsRequest));
  98. }
  99. public static class CreateResponse {
  100. private final Application application;
  101. public CreateResponse(Application application) {
  102. this.application = application;
  103. }
  104. public Application getApplication() {
  105. return application;
  106. }
  107. public static CreateResponse parse(String json) {
  108. Gson gson = new Gson();
  109. return gson.fromJson(json, CreateResponse.class);
  110. }
  111. }
  112. public static class ShowResponse {
  113. private final Application application;
  114. public ShowResponse(Application application) {
  115. this.application = application;
  116. }
  117. public Application getApplication() {
  118. return application;
  119. }
  120. public static ShowResponse parse(String json) {
  121. Gson gson = new Gson();
  122. return gson.fromJson(json, ShowResponse.class);
  123. }
  124. }
  125. public static class SearchProjectsResponse {
  126. private final Paging paging;
  127. private final List<Project> projects;
  128. public SearchProjectsResponse(Paging paging, List<Project> projects) {
  129. this.paging = paging;
  130. this.projects = projects;
  131. }
  132. public Paging getPaging() {
  133. return paging;
  134. }
  135. public List<Project> getProjects() {
  136. return projects;
  137. }
  138. public static SearchProjectsResponse parse(String json) {
  139. Gson gson = new Gson();
  140. return gson.fromJson(json, SearchProjectsResponse.class);
  141. }
  142. public static class Project {
  143. private final String key;
  144. private final String name;
  145. private final boolean enabled;
  146. private final boolean selected;
  147. public Project(String key, String name, boolean enabled, boolean selected) {
  148. this.key = key;
  149. this.name = name;
  150. this.enabled = enabled;
  151. this.selected = selected;
  152. }
  153. public String getKey() {
  154. return key;
  155. }
  156. public String getName() {
  157. return name;
  158. }
  159. public boolean isEnabled() {
  160. return enabled;
  161. }
  162. public boolean isSelected() {
  163. return selected;
  164. }
  165. }
  166. }
  167. public static class Paging {
  168. public final int pageIndex;
  169. public final int pageSize;
  170. public final int total;
  171. public Paging(int pageIndex, int pageSize, int total) {
  172. this.pageIndex = pageIndex;
  173. this.pageSize = pageSize;
  174. this.total = total;
  175. }
  176. public int getPageIndex() {
  177. return pageIndex;
  178. }
  179. public int getPageSize() {
  180. return pageSize;
  181. }
  182. public int getTotal() {
  183. return total;
  184. }
  185. }
  186. public static class Application {
  187. private final String key;
  188. private final String branch;
  189. private final boolean isMain;
  190. private final String name;
  191. private final String description;
  192. private final String visibility;
  193. private final List<Project> projects;
  194. private final List<Application.Branch> branches;
  195. private final List<String> tags;
  196. public Application(String key, String branch, boolean isMain, String name, String description, String visibility, List<Project> projects, List<Branch> branches,
  197. List<String> tags) {
  198. this.key = key;
  199. this.branch = branch;
  200. this.isMain = isMain;
  201. this.name = name;
  202. this.description = description;
  203. this.visibility = visibility;
  204. this.projects = projects;
  205. this.branches = branches;
  206. this.tags = tags;
  207. }
  208. public String getKey() {
  209. return key;
  210. }
  211. public String getBranch() {
  212. return branch;
  213. }
  214. public boolean isMain() {
  215. return isMain;
  216. }
  217. public String getName() {
  218. return name;
  219. }
  220. public String getDescription() {
  221. return description;
  222. }
  223. public String getVisibility() {
  224. return visibility;
  225. }
  226. public List<Application.Project> getProjects() {
  227. return projects;
  228. }
  229. public List<String> getTags() {
  230. return tags;
  231. }
  232. @CheckForNull
  233. public List<Application.Branch> getBranches() {
  234. return branches;
  235. }
  236. public static class Project {
  237. private final String key;
  238. private final String branch;
  239. private final Boolean isMain;
  240. private final String name;
  241. private final boolean enabled;
  242. private final Boolean selected;
  243. public Project(String key, String branch, @Nullable Boolean isMain, String name, boolean enabled, @Nullable Boolean selected) {
  244. this.key = key;
  245. this.branch = branch;
  246. this.isMain = isMain;
  247. this.name = name;
  248. this.enabled = enabled;
  249. this.selected = selected;
  250. }
  251. public String getKey() {
  252. return key;
  253. }
  254. @CheckForNull
  255. public String getBranch() {
  256. return branch;
  257. }
  258. @CheckForNull
  259. public Boolean isMain() {
  260. return isMain;
  261. }
  262. public String getName() {
  263. return name;
  264. }
  265. public boolean isEnabled() {
  266. return enabled;
  267. }
  268. @CheckForNull
  269. public Boolean isSelected() {
  270. return selected;
  271. }
  272. }
  273. public static class Branch {
  274. private final String name;
  275. private final boolean isMain;
  276. public Branch(String name, boolean isMain) {
  277. this.name = name;
  278. this.isMain = isMain;
  279. }
  280. public String getName() {
  281. return name;
  282. }
  283. public boolean isMain() {
  284. return isMain;
  285. }
  286. }
  287. }
  288. }