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.

ViewTester.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.Projects.CreateWsResponse.Project;
  32. import org.sonarqube.ws.client.PostRequest;
  33. import org.sonarqube.ws.client.ce.ActivityStatusRequest;
  34. import org.sonarqube.ws.client.projects.ProjectsService;
  35. import org.sonarqube.ws.client.projects.SearchRequest;
  36. import org.sonarqube.ws.client.views.CreateRequest;
  37. import org.sonarqube.ws.client.views.DeleteRequest;
  38. import org.sonarqube.ws.client.views.ProjectsRequest;
  39. import org.sonarqube.ws.client.views.RefreshRequest;
  40. import org.sonarqube.ws.client.views.ShowRequest;
  41. import org.sonarqube.ws.client.views.ViewsService;
  42. import static java.util.Arrays.asList;
  43. import static java.util.Arrays.stream;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. public class ViewTester extends ExternalResource {
  46. private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
  47. private final TesterSession session;
  48. ViewTester(TesterSession session) {
  49. this.session = session;
  50. }
  51. public ViewsService service() {
  52. return session.wsClient().views();
  53. }
  54. public void deleteAll() {
  55. ProjectsService service = session.wsClient().projects();
  56. service.search(new SearchRequest().setQualifiers(asList("VW"))).getComponentsList()
  57. .forEach(p -> {
  58. waitForCeQueueEmpty();
  59. session.wsClient().views().delete(new DeleteRequest().setKey(p.getKey()));
  60. });
  61. waitForCeQueueEmpty();
  62. org.sonarqube.ws.client.components.SearchRequest searchRequest = new org.sonarqube.ws.client.components.SearchRequest().setQualifiers(asList("VW", "SVW"));
  63. assertThat(session.wsClient().components().search(searchRequest).getComponentsList()).isEmpty();
  64. assertNotViewInDef();
  65. }
  66. @SafeVarargs
  67. public final CreateRequest generate(Consumer<CreateRequest>... populators) {
  68. String key = generateKey();
  69. CreateRequest request = new CreateRequest()
  70. .setKey(key)
  71. .setName("Name " + key)
  72. .setDescription("Description " + key);
  73. stream(populators).forEach(p -> p.accept(request));
  74. service().create(request);
  75. return request;
  76. }
  77. @SafeVarargs
  78. public final String createSubPortfolio(String parentKey, Consumer<CreateRequest>... populators) {
  79. String key = generateKey();
  80. CreateRequest request = new CreateRequest()
  81. .setParent(parentKey)
  82. .setKey(key)
  83. .setName("Sub view name " + key)
  84. .setDescription("Sub view description " + key);
  85. stream(populators).forEach(p -> p.accept(request));
  86. service().create(request);
  87. return request.getKey();
  88. }
  89. public void addProject(String viewKey, Project project) {
  90. addProject(viewKey, project.getKey());
  91. }
  92. public void addProject(String viewKey, String projectKey) {
  93. session.wsClient().wsConnector().call(
  94. new PostRequest("/api/views/add_project")
  95. .setParam("key", viewKey)
  96. .setParam("project", projectKey))
  97. .failIfNotSuccessful();
  98. }
  99. public void addProjectBranch(String viewKey, String projectKey, String branch) {
  100. session.wsClient().wsConnector().call(
  101. new PostRequest("/api/views/add_project_branch")
  102. .setParam("key", viewKey)
  103. .setParam("project", projectKey)
  104. .setParam("branch", branch))
  105. .failIfNotSuccessful();
  106. }
  107. public AddLocalReferenceResponse addPortfolio(String key, String portfolioRefKey) {
  108. return AddLocalReferenceResponse.parse(session.wsClient().wsConnector().call(
  109. new PostRequest("/api/views/add_portfolio")
  110. .setParam("portfolio", key)
  111. .setParam("reference", portfolioRefKey))
  112. .failIfNotSuccessful().content());
  113. }
  114. public MoveResponse move(String key, String destinationKey) {
  115. return MoveResponse.parse(session.wsClient().wsConnector().call(
  116. new PostRequest("/api/views/move")
  117. .setParam("key", key)
  118. .setParam("destination", destinationKey))
  119. .failIfNotSuccessful().content());
  120. }
  121. public String generateKey() {
  122. int id = ID_GENERATOR.getAndIncrement();
  123. return "viewKey" + id;
  124. }
  125. public void refresh() {
  126. service().refresh(new RefreshRequest());
  127. waitForCeQueueEmpty();
  128. }
  129. public ViewTester waitForCeQueueEmpty() {
  130. Ce.ActivityStatusWsResponse status;
  131. boolean empty;
  132. do {
  133. status = session.wsClient().ce().activityStatus(new ActivityStatusRequest());
  134. empty = status.getInProgress() + status.getPending() == 0;
  135. if (!empty) {
  136. Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
  137. }
  138. } while (!empty);
  139. return this;
  140. }
  141. public ListResponse list() {
  142. return ListResponse.parse(service().list());
  143. }
  144. public ShowResponse show(ShowRequest showRequest) {
  145. return ShowResponse.parse(service().show(showRequest));
  146. }
  147. public ProjectsResponse projects(ProjectsRequest request) {
  148. return ProjectsResponse.parse(service().projects(request));
  149. }
  150. public SearchResponse search(org.sonarqube.ws.client.views.SearchRequest searchRequest) {
  151. return SearchResponse.parse(service().search(searchRequest));
  152. }
  153. private void assertNotViewInDef() {
  154. assertThat(ListResponse.parse(service().list()).getViews()).isEmpty();
  155. }
  156. public static class ShowResponse {
  157. private final String key;
  158. private final String name;
  159. private final String desc;
  160. private final String qualifier;
  161. private final String selectionMode;
  162. private final String regexp;
  163. private final List<SelectedProject> selectedProjects;
  164. private final List<SubView> subViews;
  165. private final List<String> tags;
  166. public ShowResponse(String key, String name, String desc, String qualifier, String selectionMode, String regexp,
  167. List<SelectedProject> selectedProjects, @Nullable List<SubView> subViews, List<String> tags) {
  168. this.key = key;
  169. this.name = name;
  170. this.desc = desc;
  171. this.qualifier = qualifier;
  172. this.selectionMode = selectionMode;
  173. this.regexp = regexp;
  174. this.selectedProjects = selectedProjects;
  175. this.subViews = subViews;
  176. this.tags = tags;
  177. }
  178. public String getKey() {
  179. return key;
  180. }
  181. public String getName() {
  182. return name;
  183. }
  184. public String getDesc() {
  185. return desc;
  186. }
  187. public String getQualifier() {
  188. return qualifier;
  189. }
  190. public List<SelectedProject> getSelectedProjects() {
  191. return selectedProjects;
  192. }
  193. public String getSelectionMode() {
  194. return selectionMode;
  195. }
  196. public String getRegexp() {
  197. return regexp;
  198. }
  199. public List<String> getTags() {
  200. return tags;
  201. }
  202. @CheckForNull
  203. public List<SubView> getSubViews() {
  204. return subViews;
  205. }
  206. public static ShowResponse parse(String json) {
  207. Gson gson = new Gson();
  208. return gson.fromJson(json, ShowResponse.class);
  209. }
  210. public static class SelectedProject {
  211. private final String projectKey;
  212. private final List<String> selectedBranches;
  213. public SelectedProject(String projectKey, List<String> selectedBranches) {
  214. this.projectKey = projectKey;
  215. this.selectedBranches = selectedBranches;
  216. }
  217. public String getProjectKey() {
  218. return projectKey;
  219. }
  220. public List<String> getSelectedBranches() {
  221. return selectedBranches;
  222. }
  223. }
  224. public static class SubView {
  225. private final String key;
  226. private final String name;
  227. private final String desc;
  228. private final String selectionMode;
  229. private final String originalKey;
  230. private final String manual_measure_key;
  231. private final String manual_measure_value;
  232. private final List<SubView> subViews;
  233. public SubView(String key, String name, String desc, String selectionMode, String originalKey, String manual_measure_key, String manual_measure_value,
  234. List<SubView> subViews) {
  235. this.key = key;
  236. this.name = name;
  237. this.desc = desc;
  238. this.selectionMode = selectionMode;
  239. this.originalKey = originalKey;
  240. this.manual_measure_key = manual_measure_key;
  241. this.manual_measure_value = manual_measure_value;
  242. this.subViews = subViews;
  243. }
  244. public String getKey() {
  245. return key;
  246. }
  247. public String getName() {
  248. return name;
  249. }
  250. public String getDesc() {
  251. return desc;
  252. }
  253. public String getSelectionMode() {
  254. return selectionMode;
  255. }
  256. public String getOriginalKey() {
  257. return originalKey;
  258. }
  259. public String getManual_measure_key() {
  260. return manual_measure_key;
  261. }
  262. public String getManual_measure_value() {
  263. return manual_measure_value;
  264. }
  265. public List<SubView> getSubViews() {
  266. return subViews;
  267. }
  268. }
  269. }
  270. public static class ListResponse {
  271. private List<View> views;
  272. private ListResponse(List<View> views) {
  273. this.views = views;
  274. }
  275. public List<View> getViews() {
  276. return views;
  277. }
  278. public static ListResponse parse(String json) {
  279. Gson gson = new Gson();
  280. return gson.fromJson(json, ListResponse.class);
  281. }
  282. public static class View {
  283. private final String key;
  284. private final String name;
  285. private final String qualifier;
  286. private View(String key, String name, String qualifier) {
  287. this.key = key;
  288. this.name = name;
  289. this.qualifier = qualifier;
  290. }
  291. public String getKey() {
  292. return key;
  293. }
  294. public String getName() {
  295. return name;
  296. }
  297. public String getQualifier() {
  298. return qualifier;
  299. }
  300. public static View parse(String json) {
  301. Gson gson = new Gson();
  302. return gson.fromJson(json, View.class);
  303. }
  304. }
  305. }
  306. public static class AddLocalReferenceResponse {
  307. private final String key;
  308. private final String name;
  309. public AddLocalReferenceResponse(String key, String name) {
  310. this.key = key;
  311. this.name = name;
  312. }
  313. public static AddLocalReferenceResponse parse(String json) {
  314. Gson gson = new Gson();
  315. return gson.fromJson(json, AddLocalReferenceResponse.class);
  316. }
  317. public String getKey() {
  318. return key;
  319. }
  320. public String getName() {
  321. return name;
  322. }
  323. }
  324. public static class ProjectsResponse {
  325. private List<Project> results;
  326. private boolean more;
  327. public ProjectsResponse(List<Project> results, boolean more) {
  328. this.results = results;
  329. this.more = more;
  330. }
  331. public List<Project> getProjects() {
  332. return results;
  333. }
  334. public boolean isMore() {
  335. return more;
  336. }
  337. public static ProjectsResponse parse(String json) {
  338. Gson gson = new Gson();
  339. return gson.fromJson(json, ProjectsResponse.class);
  340. }
  341. public static class Project {
  342. private final String key;
  343. private final String name;
  344. private final boolean selected;
  345. private final boolean enabled;
  346. private Project(String key, String name, boolean selected, boolean enabled) {
  347. this.key = key;
  348. this.name = name;
  349. this.selected = selected;
  350. this.enabled = enabled;
  351. }
  352. public String getKey() {
  353. return key;
  354. }
  355. public String getName() {
  356. return name;
  357. }
  358. public boolean isSelected() {
  359. return selected;
  360. }
  361. public boolean isEnabled() {
  362. return enabled;
  363. }
  364. }
  365. }
  366. public static class SearchResponse {
  367. private List<Component> components;
  368. private SearchResponse(List<Component> components) {
  369. this.components = components;
  370. }
  371. public List<Component> getComponents() {
  372. return components;
  373. }
  374. public static SearchResponse parse(String json) {
  375. Gson gson = new Gson();
  376. return gson.fromJson(json, SearchResponse.class);
  377. }
  378. public static class Component {
  379. private final String key;
  380. private final String name;
  381. private final String qualifier;
  382. private Component(String key, String name, String qualifier) {
  383. this.key = key;
  384. this.name = name;
  385. this.qualifier = qualifier;
  386. }
  387. public String getKey() {
  388. return key;
  389. }
  390. public String getName() {
  391. return name;
  392. }
  393. public String getQualifier() {
  394. return qualifier;
  395. }
  396. public static Component parse(String json) {
  397. Gson gson = new Gson();
  398. return gson.fromJson(json, Component.class);
  399. }
  400. }
  401. }
  402. public static class MoveResponse {
  403. private final String key;
  404. private final String name;
  405. public MoveResponse(String key, String name) {
  406. this.key = key;
  407. this.name = name;
  408. }
  409. public static MoveResponse parse(String json) {
  410. Gson gson = new Gson();
  411. return gson.fromJson(json, MoveResponse.class);
  412. }
  413. public String getKey() {
  414. return key;
  415. }
  416. public String getName() {
  417. return name;
  418. }
  419. }
  420. }