]> source.dussan.org Git - sonarqube.git/commitdiff
Add partial support of api/projects to sonar-ws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 14 Apr 2016 08:50:20 +0000 (10:50 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 14 Apr 2016 13:40:01 +0000 (15:40 +0200)
sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/project/DeleteRequest.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/project/package-info.java [new file with mode: 0644]
sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java [new file with mode: 0644]

index 1c0d8278ee97c708d2469a92e57c9790d2ed462c..110c8d0377ae690dfcbc6cc505a6d87ce69f98a0 100644 (file)
@@ -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;
+  }
 }
index fc79d58e69598f8c1adb02e33f442f20eee661c2..36369c45e5cb5c1fea631c73c784796560d7e5c5 100644 (file)
@@ -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 (file)
index 0000000..698d766
--- /dev/null
@@ -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 (file)
index 0000000..5a0df28
--- /dev/null
@@ -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 (file)
index 0000000..2eb5ae1
--- /dev/null
@@ -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 (file)
index 0000000..041d5d4
--- /dev/null
@@ -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 (file)
index 0000000..ccb720b
--- /dev/null
@@ -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<ProjectsService> 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"));
+  }
+}