]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4711 Add WS client for project provisioning
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 10 Oct 2013 15:51:05 +0000 (17:51 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 11 Oct 2013 06:38:34 +0000 (08:38 +0200)
sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/project/NewProject.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/project/Project.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/project/ProjectClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProject.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProjectClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/package-info.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/project/package-info.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/project/NewProjectTest.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/project/internal/DefaultProjectClientTest.java [new file with mode: 0644]

index 830dce49eae9299922b6c65a77486efb7c2c82b8..04d4c1b79645a896adf9e5bf019a3bb409f7b64c 100644 (file)
@@ -26,6 +26,8 @@ import org.sonar.wsclient.issue.internal.DefaultActionPlanClient;
 import org.sonar.wsclient.issue.internal.DefaultIssueClient;
 import org.sonar.wsclient.permissions.PermissionClient;
 import org.sonar.wsclient.permissions.internal.DefaultPermissionClient;
+import org.sonar.wsclient.project.ProjectClient;
+import org.sonar.wsclient.project.internal.DefaultProjectClient;
 import org.sonar.wsclient.user.UserClient;
 import org.sonar.wsclient.user.internal.DefaultUserClient;
 
@@ -92,6 +94,13 @@ public class SonarClient {
     return new DefaultPermissionClient(requestFactory);
   }
 
+  /**
+   * New client to interact with web services related to projects
+   */
+  public ProjectClient projectClient() {
+    return new DefaultProjectClient(requestFactory);
+  }
+
   /**
    * Create a builder of {@link SonarClient}s.
    */
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/project/NewProject.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/NewProject.java
new file mode 100644 (file)
index 0000000..03cb546
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.project;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @since 4.0
+ */
+public class NewProject {
+
+  private final Map<String, Object> params;
+
+  private NewProject() {
+    params = new HashMap<String, Object>();
+  }
+
+  public static final NewProject create() {
+    return new NewProject();
+  }
+
+  public Map<String, Object> urlParams() {
+    return params;
+  }
+
+  public NewProject key(String key) {
+    params.put("key", key);
+    return this;
+  }
+
+  public NewProject name(String name) {
+    params.put("name", name);
+    return this;
+  }
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/project/Project.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/Project.java
new file mode 100644 (file)
index 0000000..4020ebc
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.project;
+
+/**
+ * @since 4.0
+ */
+public interface Project {
+
+  String id();
+
+  String key();
+
+  String name();
+
+  String scope();
+
+  String qualifier();
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/project/ProjectClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/ProjectClient.java
new file mode 100644 (file)
index 0000000..3eb6c75
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.project;
+
+/**
+ * @since 4.0
+ */
+public interface ProjectClient {
+
+  Project create(NewProject newProject);
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProject.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProject.java
new file mode 100644 (file)
index 0000000..f287c30
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.project.internal;
+
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+
+import org.sonar.wsclient.project.Project;
+
+import java.util.Map;
+
+/**
+ * @since 4.0
+ */
+public class DefaultProject implements Project {
+
+  private final Map<String, String> json;
+
+  public DefaultProject(Map<String, String> json) {
+    this.json = json;
+  }
+
+  @Override
+  public String id() {
+    return JsonUtils.getString(json, "id");
+  }
+
+  @Override
+  public String key() {
+    return JsonUtils.getString(json, "k");
+  }
+
+  @Override
+  public String name() {
+    return JsonUtils.getString(json, "nm");
+  }
+
+  @Override
+  public String scope() {
+    return JsonUtils.getString(json, "sc");
+  }
+
+  @Override
+  public String qualifier() {
+    return JsonUtils.getString(json, "qu");
+  }
+
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProjectClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProjectClient.java
new file mode 100644 (file)
index 0000000..2ee9c82
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.project.internal;
+
+import org.json.simple.JSONValue;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.project.NewProject;
+import org.sonar.wsclient.project.Project;
+import org.sonar.wsclient.project.ProjectClient;
+
+import java.util.Map;
+
+/**
+ * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#issueClient()}.
+ */
+public class DefaultProjectClient implements ProjectClient {
+
+  private static final String ROOT_URL = "/api/projects";
+  private static final String CREATE_URL = ROOT_URL + "/create";
+
+  private final HttpRequestFactory requestFactory;
+
+  public DefaultProjectClient(HttpRequestFactory requestFactory) {
+    this.requestFactory = requestFactory;
+  }
+
+  @Override
+  public Project create(NewProject newProject) {
+    String json = requestFactory.post(CREATE_URL, newProject.urlParams());
+    return jsonToProject(json);
+  }
+
+  @SuppressWarnings({"rawtypes", "unchecked"})
+  private Project jsonToProject(String json) {
+    Map jsonRoot = (Map) JSONValue.parse(json);
+    return new DefaultProject((Map) jsonRoot);
+  }
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/package-info.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/package-info.java
new file mode 100644 (file)
index 0000000..a4b15c5
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.
+ */
+
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.wsclient.project.internal;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/project/package-info.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/package-info.java
new file mode 100644 (file)
index 0000000..b20bfee
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.
+ */
+
+@javax.annotation.ParametersAreNonnullByDefault
+package org.sonar.wsclient.project;
index eab25a50d3422cc5edcfe3ec225e71eb02281c7f..cb1516a9db91b36c64338d08ee49d1e6b73d220c 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Test;
 import org.sonar.wsclient.issue.internal.DefaultActionPlanClient;
 import org.sonar.wsclient.issue.internal.DefaultIssueClient;
 import org.sonar.wsclient.permissions.internal.DefaultPermissionClient;
+import org.sonar.wsclient.project.internal.DefaultProjectClient;
 import org.sonar.wsclient.user.internal.DefaultUserClient;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -36,6 +37,7 @@ public class SonarClientTest {
     assertThat(client.actionPlanClient()).isNotNull().isInstanceOf(DefaultActionPlanClient.class);
     assertThat(client.userClient()).isNotNull().isInstanceOf(DefaultUserClient.class);
     assertThat(client.permissionClient()).isNotNull().isInstanceOf(DefaultPermissionClient.class);
+    assertThat(client.projectClient()).isNotNull().isInstanceOf(DefaultProjectClient.class);
   }
 
   @Test
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/project/NewProjectTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/project/NewProjectTest.java
new file mode 100644 (file)
index 0000000..444a773
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.project;
+
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class NewProjectTest {
+
+  @Test
+  public void test_empty() throws Exception {
+    assertThat(NewProject.create().urlParams()).isEmpty();
+  }
+
+  @Test
+  public void test_create() throws Exception {
+    String key = "key", name = "name";
+    Map<String, Object> params = NewProject.create().key(key).name(name).urlParams();
+    assertThat(params.get("key")).isEqualTo(key);
+    assertThat(params.get("name")).isEqualTo(name);
+  }
+}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/project/internal/DefaultProjectClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/project/internal/DefaultProjectClientTest.java
new file mode 100644 (file)
index 0000000..e110737
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.sonar.wsclient.project.internal;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.wsclient.MockHttpServerInterceptor;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.project.NewProject;
+import org.sonar.wsclient.project.Project;
+import org.sonar.wsclient.project.ProjectClient;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.MapAssert.entry;
+
+public class DefaultProjectClientTest {
+
+  @Rule
+  public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
+
+  @Test
+  public void should_create_project() {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+    httpServer.stubResponseBody("{\"id\":\"1\",\"k\":\"polop\",\"nm\":\"Polop\",\"sc\":\"PRJ\",\"qu\":\"TRK\"}");
+
+    ProjectClient client = new DefaultProjectClient(requestFactory);
+    Project result = client.create(NewProject.create().key("polop").name("Polop"));
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/projects/create");
+    assertThat(httpServer.requestParams()).includes(
+      entry("key", "polop"),
+      entry("name", "Polop")
+    );
+    assertThat(result).isNotNull();
+
+  }
+}