123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- /*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonarqube.ws.tester;
-
- import com.google.common.util.concurrent.Uninterruptibles;
- import com.google.gson.Gson;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.function.Consumer;
- import javax.annotation.CheckForNull;
- import javax.annotation.Nullable;
- import org.junit.rules.ExternalResource;
- import org.sonarqube.ws.Ce;
- import org.sonarqube.ws.Projects.CreateWsResponse.Project;
- import org.sonarqube.ws.client.PostRequest;
- import org.sonarqube.ws.client.ce.ActivityStatusRequest;
- import org.sonarqube.ws.client.projects.ProjectsService;
- import org.sonarqube.ws.client.projects.SearchRequest;
- import org.sonarqube.ws.client.views.CreateRequest;
- import org.sonarqube.ws.client.views.DeleteRequest;
- import org.sonarqube.ws.client.views.ProjectsRequest;
- import org.sonarqube.ws.client.views.RefreshRequest;
- import org.sonarqube.ws.client.views.ShowRequest;
- import org.sonarqube.ws.client.views.ViewsService;
-
- import static java.util.Arrays.asList;
- import static java.util.Arrays.stream;
- import static org.assertj.core.api.Assertions.assertThat;
-
- public class ViewTester extends ExternalResource {
-
- private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
-
- private final TesterSession session;
-
- ViewTester(TesterSession session) {
- this.session = session;
- }
-
- public ViewsService service() {
- return session.wsClient().views();
- }
-
- public void deleteAll() {
- ProjectsService service = session.wsClient().projects();
- service.search(new SearchRequest().setQualifiers(asList("VW"))).getComponentsList()
- .forEach(p -> {
- waitForCeQueueEmpty();
- session.wsClient().views().delete(new DeleteRequest().setKey(p.getKey()));
- });
- waitForCeQueueEmpty();
-
- org.sonarqube.ws.client.components.SearchRequest searchRequest = new org.sonarqube.ws.client.components.SearchRequest().setQualifiers(asList("VW", "SVW"));
- assertThat(session.wsClient().components().search(searchRequest).getComponentsList()).isEmpty();
-
- assertNotViewInDef();
- }
-
- @SafeVarargs
- public final CreateRequest generate(Consumer<CreateRequest>... populators) {
- String key = generateKey();
- CreateRequest request = new CreateRequest()
- .setKey(key)
- .setName("Name " + key)
- .setDescription("Description " + key);
- stream(populators).forEach(p -> p.accept(request));
- service().create(request);
- return request;
- }
-
- @SafeVarargs
- public final String createSubPortfolio(String parentKey, Consumer<CreateRequest>... populators) {
- String key = generateKey();
- CreateRequest request = new CreateRequest()
- .setParent(parentKey)
- .setKey(key)
- .setName("Sub view name " + key)
- .setDescription("Sub view description " + key);
- stream(populators).forEach(p -> p.accept(request));
- service().create(request);
- return request.getKey();
- }
-
- public void addProject(String viewKey, Project project) {
- addProject(viewKey, project.getKey());
- }
-
- public void addProject(String viewKey, String projectKey) {
- session.wsClient().wsConnector().call(
- new PostRequest("/api/views/add_project")
- .setParam("key", viewKey)
- .setParam("project", projectKey))
- .failIfNotSuccessful();
- }
-
- public void addProjectBranch(String viewKey, String projectKey, String branch) {
- session.wsClient().wsConnector().call(
- new PostRequest("/api/views/add_project_branch")
- .setParam("key", viewKey)
- .setParam("project", projectKey)
- .setParam("branch", branch))
- .failIfNotSuccessful();
- }
-
- public AddLocalReferenceResponse addPortfolio(String key, String portfolioRefKey) {
- return AddLocalReferenceResponse.parse(session.wsClient().wsConnector().call(
- new PostRequest("/api/views/add_portfolio")
- .setParam("portfolio", key)
- .setParam("reference", portfolioRefKey))
- .failIfNotSuccessful().content());
- }
-
- public MoveResponse move(String key, String destinationKey) {
- return MoveResponse.parse(session.wsClient().wsConnector().call(
- new PostRequest("/api/views/move")
- .setParam("key", key)
- .setParam("destination", destinationKey))
- .failIfNotSuccessful().content());
- }
-
- public String generateKey() {
- int id = ID_GENERATOR.getAndIncrement();
- return "viewKey" + id;
- }
-
- public void refresh() {
- service().refresh(new RefreshRequest());
- waitForCeQueueEmpty();
- }
-
- public ViewTester waitForCeQueueEmpty() {
- Ce.ActivityStatusWsResponse status;
- boolean empty;
- do {
- status = session.wsClient().ce().activityStatus(new ActivityStatusRequest());
- empty = status.getInProgress() + status.getPending() == 0;
- if (!empty) {
- Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
- }
-
- } while (!empty);
-
- return this;
- }
-
- public ListResponse list() {
- return ListResponse.parse(service().list());
- }
-
- public ShowResponse show(ShowRequest showRequest) {
- return ShowResponse.parse(service().show(showRequest));
- }
-
- public ProjectsResponse projects(ProjectsRequest request) {
- return ProjectsResponse.parse(service().projects(request));
- }
-
- public SearchResponse search(org.sonarqube.ws.client.views.SearchRequest searchRequest) {
- return SearchResponse.parse(service().search(searchRequest));
- }
-
- private void assertNotViewInDef() {
- assertThat(ListResponse.parse(service().list()).getViews()).isEmpty();
- }
-
- public static class ShowResponse {
- private final String key;
- private final String name;
- private final String desc;
- private final String qualifier;
- private final String selectionMode;
- private final String regexp;
- private final List<SelectedProject> selectedProjects;
- private final List<SubView> subViews;
- private final List<String> tags;
-
- public ShowResponse(String key, String name, String desc, String qualifier, String selectionMode, String regexp,
- List<SelectedProject> selectedProjects, @Nullable List<SubView> subViews, List<String> tags) {
- this.key = key;
- this.name = name;
- this.desc = desc;
- this.qualifier = qualifier;
- this.selectionMode = selectionMode;
- this.regexp = regexp;
- this.selectedProjects = selectedProjects;
- this.subViews = subViews;
- this.tags = tags;
- }
-
- public String getKey() {
- return key;
- }
-
- public String getName() {
- return name;
- }
-
- public String getDesc() {
- return desc;
- }
-
- public String getQualifier() {
- return qualifier;
- }
-
- public List<SelectedProject> getSelectedProjects() {
- return selectedProjects;
- }
-
- public String getSelectionMode() {
- return selectionMode;
- }
-
- public String getRegexp() {
- return regexp;
- }
-
- public List<String> getTags() {
- return tags;
- }
-
- @CheckForNull
- public List<SubView> getSubViews() {
- return subViews;
- }
-
- public static ShowResponse parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, ShowResponse.class);
- }
-
- public static class SelectedProject {
- private final String projectKey;
- private final List<String> selectedBranches;
-
- public SelectedProject(String projectKey, List<String> selectedBranches) {
- this.projectKey = projectKey;
- this.selectedBranches = selectedBranches;
- }
-
- public String getProjectKey() {
- return projectKey;
- }
-
- public List<String> getSelectedBranches() {
- return selectedBranches;
- }
- }
-
- public static class SubView {
- private final String key;
- private final String name;
- private final String desc;
- private final String selectionMode;
- private final String originalKey;
- private final String manual_measure_key;
- private final String manual_measure_value;
- private final List<SubView> subViews;
-
- public SubView(String key, String name, String desc, String selectionMode, String originalKey, String manual_measure_key, String manual_measure_value,
- List<SubView> subViews) {
- this.key = key;
- this.name = name;
- this.desc = desc;
- this.selectionMode = selectionMode;
- this.originalKey = originalKey;
- this.manual_measure_key = manual_measure_key;
- this.manual_measure_value = manual_measure_value;
- this.subViews = subViews;
- }
-
- public String getKey() {
- return key;
- }
-
- public String getName() {
- return name;
- }
-
- public String getDesc() {
- return desc;
- }
-
- public String getSelectionMode() {
- return selectionMode;
- }
-
- public String getOriginalKey() {
- return originalKey;
- }
-
- public String getManual_measure_key() {
- return manual_measure_key;
- }
-
- public String getManual_measure_value() {
- return manual_measure_value;
- }
-
- public List<SubView> getSubViews() {
- return subViews;
- }
- }
- }
-
- public static class ListResponse {
-
- private List<View> views;
-
- private ListResponse(List<View> views) {
- this.views = views;
- }
-
- public List<View> getViews() {
- return views;
- }
-
- public static ListResponse parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, ListResponse.class);
- }
-
- public static class View {
- private final String key;
- private final String name;
- private final String qualifier;
-
- private View(String key, String name, String qualifier) {
- this.key = key;
- this.name = name;
- this.qualifier = qualifier;
- }
-
- public String getKey() {
- return key;
- }
-
- public String getName() {
- return name;
- }
-
- public String getQualifier() {
- return qualifier;
- }
-
- public static View parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, View.class);
- }
- }
- }
-
- public static class AddLocalReferenceResponse {
- private final String key;
- private final String name;
-
- public AddLocalReferenceResponse(String key, String name) {
- this.key = key;
- this.name = name;
- }
-
- public static AddLocalReferenceResponse parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, AddLocalReferenceResponse.class);
- }
-
- public String getKey() {
- return key;
- }
-
- public String getName() {
- return name;
- }
- }
-
- public static class ProjectsResponse {
-
- private List<Project> results;
- private boolean more;
-
- public ProjectsResponse(List<Project> results, boolean more) {
- this.results = results;
- this.more = more;
- }
-
- public List<Project> getProjects() {
- return results;
- }
-
- public boolean isMore() {
- return more;
- }
-
- public static ProjectsResponse parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, ProjectsResponse.class);
- }
-
- public static class Project {
- private final String key;
- private final String name;
- private final boolean selected;
- private final boolean enabled;
-
- private Project(String key, String name, boolean selected, boolean enabled) {
- this.key = key;
- this.name = name;
- this.selected = selected;
- this.enabled = enabled;
- }
-
- public String getKey() {
- return key;
- }
-
- public String getName() {
- return name;
- }
-
- public boolean isSelected() {
- return selected;
- }
-
- public boolean isEnabled() {
- return enabled;
- }
- }
- }
-
- public static class SearchResponse {
-
- private List<Component> components;
-
- private SearchResponse(List<Component> components) {
- this.components = components;
- }
-
- public List<Component> getComponents() {
- return components;
- }
-
- public static SearchResponse parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, SearchResponse.class);
- }
-
- public static class Component {
- private final String key;
- private final String name;
- private final String qualifier;
-
- private Component(String key, String name, String qualifier) {
- this.key = key;
- this.name = name;
- this.qualifier = qualifier;
- }
-
- public String getKey() {
- return key;
- }
-
- public String getName() {
- return name;
- }
-
- public String getQualifier() {
- return qualifier;
- }
-
- public static Component parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, Component.class);
- }
- }
- }
-
- public static class MoveResponse {
- private final String key;
- private final String name;
-
- public MoveResponse(String key, String name) {
- this.key = key;
- this.name = name;
- }
-
- public static MoveResponse parse(String json) {
- Gson gson = new Gson();
- return gson.fromJson(json, MoveResponse.class);
- }
-
- public String getKey() {
- return key;
- }
-
- public String getName() {
- return name;
- }
- }
- }
|