From e0da0f210c26738ef7c18809a507d46d6eb09610 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 14 Apr 2016 10:50:20 +0200 Subject: [PATCH] Add partial support of api/projects to sonar-ws --- .../sonarqube/ws/client/DefaultWsClient.java | 8 ++ .../org/sonarqube/ws/client/WsClient.java | 6 ++ .../ws/client/project/CreateRequest.java | 84 +++++++++++++++++++ .../ws/client/project/DeleteRequest.java | 73 ++++++++++++++++ .../ws/client/project/ProjectsService.java | 58 +++++++++++++ .../ws/client/project/package-info.java | 25 ++++++ .../client/project/ProjectsServiceTest.java | 81 ++++++++++++++++++ 7 files changed, 335 insertions(+) create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/project/DeleteRequest.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/project/package-info.java create mode 100644 sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java index 1c0d8278ee9..110c8d0377a 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java @@ -24,6 +24,7 @@ import org.sonarqube.ws.client.component.ComponentsService; import org.sonarqube.ws.client.issue.IssuesService; import org.sonarqube.ws.client.measure.MeasuresService; import org.sonarqube.ws.client.permission.PermissionsService; +import org.sonarqube.ws.client.project.ProjectsService; import org.sonarqube.ws.client.qualitygate.QualityGatesService; import org.sonarqube.ws.client.qualityprofile.QualityProfilesService; import org.sonarqube.ws.client.rule.RulesService; @@ -49,6 +50,7 @@ class DefaultWsClient implements WsClient { private final SystemService systemService; private final CeService ceService; private final RulesService rulesService; + private final ProjectsService projectsService; DefaultWsClient(WsConnector wsConnector) { this.wsConnector = wsConnector; @@ -62,6 +64,7 @@ class DefaultWsClient implements WsClient { this.systemService = new SystemService(wsConnector); this.ceService = new CeService(wsConnector); this.rulesService = new RulesService(wsConnector); + this.projectsService = new ProjectsService(wsConnector); } @Override @@ -118,4 +121,9 @@ class DefaultWsClient implements WsClient { public RulesService rules() { return rulesService; } + + @Override + public ProjectsService projects() { + return projectsService; + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java index fc79d58e695..36369c45e5c 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java @@ -24,6 +24,7 @@ import org.sonarqube.ws.client.component.ComponentsService; import org.sonarqube.ws.client.issue.IssuesService; import org.sonarqube.ws.client.measure.MeasuresService; import org.sonarqube.ws.client.permission.PermissionsService; +import org.sonarqube.ws.client.project.ProjectsService; import org.sonarqube.ws.client.qualitygate.QualityGatesService; import org.sonarqube.ws.client.qualityprofile.QualityProfilesService; import org.sonarqube.ws.client.rule.RulesService; @@ -70,4 +71,9 @@ public interface WsClient { RulesService rules(); WsConnector wsConnector(); + + /** + * @since 5.5 + */ + ProjectsService projects(); } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java new file mode 100644 index 00000000000..698d7666904 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.client.project; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +/** + * @since 5.5 + */ +public class CreateRequest { + + private final String key; + private final String name; + private final String branch; + + private CreateRequest(Builder builder) { + this.key = builder.key; + this.name = builder.name; + this.branch = builder.branch; + } + + public String getKey() { + return key; + } + + public String getName() { + return name; + } + + @CheckForNull + public String getBranch() { + return branch; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String key; + private String name; + private String branch; + + private Builder() { + } + + public Builder setKey(String key) { + this.key = key; + return this; + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setBranch(@Nullable String branch) { + this.branch = branch; + return this; + } + + public CreateRequest build() { + return new CreateRequest(this); + } + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/DeleteRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/DeleteRequest.java new file mode 100644 index 00000000000..5a0df28686a --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/DeleteRequest.java @@ -0,0 +1,73 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.client.project; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +/** + * @since 5.5 + */ +public class DeleteRequest { + + private final String id; + private final String key; + + private DeleteRequest(Builder builder) { + this.id = builder.id; + this.key = builder.key; + } + + @CheckForNull + public String getKey() { + return key; + } + + @CheckForNull + public String getId() { + return id; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String id; + private String key; + + private Builder() { + } + + public Builder setId(@Nullable String id) { + this.id = id; + return this; + } + + public Builder setKey(@Nullable String key) { + this.key = key; + return this; + } + + public DeleteRequest build() { + return new DeleteRequest(this); + } + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java new file mode 100644 index 00000000000..2eb5ae1af0b --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.client.project; + +import org.sonarqube.ws.client.BaseService; +import org.sonarqube.ws.client.PostRequest; +import org.sonarqube.ws.client.WsConnector; + +/** + * Maps web service {@code api/projects}. + * @since 5.5 + */ +public class ProjectsService extends BaseService { + + public ProjectsService(WsConnector wsConnector) { + super(wsConnector, "api/projects"); + } + + /** + * Provisions a new project. + * + * @throws org.sonarqube.ws.client.HttpException if HTTP status code is not 2xx. + */ + public void create(CreateRequest project) { + PostRequest request = new PostRequest(path("create")) + .setParam("key", project.getKey()) + .setParam("name", project.getName()) + .setParam("branch", project.getBranch()); + call(request); + } + + /** + * @throws org.sonarqube.ws.client.HttpException if HTTP status code is not 2xx. + */ + public void delete(DeleteRequest request) { + call(new PostRequest(path("delete")) + .setParam("id", request.getId()) + .setParam("key", request.getKey())); + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/package-info.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/package-info.java new file mode 100644 index 00000000000..041d5d4da2f --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/package-info.java @@ -0,0 +1,25 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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. + */ + +@ParametersAreNonnullByDefault +package org.sonarqube.ws.client.project; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java new file mode 100644 index 00000000000..ccb720b00fd --- /dev/null +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java @@ -0,0 +1,81 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.client.project; + +import org.junit.Rule; +import org.junit.Test; +import org.sonarqube.ws.client.ServiceTester; +import org.sonarqube.ws.client.WsConnector; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.MapEntry.entry; +import static org.mockito.Mockito.mock; + +public class ProjectsServiceTest { + + @Rule + public ServiceTester serviceTester = new ServiceTester<>(new ProjectsService(mock(WsConnector.class))); + + private ProjectsService underTest = serviceTester.getInstanceUnderTest(); + + @Test + public void creates_project() { + underTest.create(CreateRequest.builder() + .setKey("project_key") + .setName("Project Name") + .build()); + + assertThat(serviceTester.getPostRequest().getPath()).isEqualTo("api/projects/create"); + assertThat(serviceTester.getPostRequest().getParams()).containsOnly( + entry("key", "project_key"), + entry("name", "Project Name")); + } + + @Test + public void creates_project_on_branch() { + underTest.create(CreateRequest.builder() + .setKey("project_key") + .setName("Project Name") + .setBranch("the_branch") + .build()); + + assertThat(serviceTester.getPostRequest().getPath()).isEqualTo("api/projects/create"); + assertThat(serviceTester.getPostRequest().getParams()).containsOnly( + entry("key", "project_key"), + entry("name", "Project Name"), + entry("branch", "the_branch")); + } + + @Test + public void deletes_project_by_id() { + underTest.delete(DeleteRequest.builder().setId("abc").build()); + + assertThat(serviceTester.getPostRequest().getPath()).isEqualTo("api/projects/delete"); + assertThat(serviceTester.getPostRequest().getParams()).containsOnly(entry("id", "abc")); + } + + @Test + public void deletes_project_by_key() { + underTest.delete(DeleteRequest.builder().setKey("project_key").build()); + + assertThat(serviceTester.getPostRequest().getPath()).isEqualTo("api/projects/delete"); + assertThat(serviceTester.getPostRequest().getParams()).containsOnly(entry("key", "project_key")); + } +} -- 2.39.5