diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-10-10 17:51:05 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-10-11 08:38:34 +0200 |
commit | d3651a139e3a66b4e9260c1ab1d334779804e3b1 (patch) | |
tree | 881adeb8bb557433a882be3953f0e5a013bb9049 /sonar-ws-client | |
parent | 2eaff3585e75d769a6314e640f3d5afe02d07643 (diff) | |
download | sonarqube-d3651a139e3a66b4e9260c1ab1d334779804e3b1.tar.gz sonarqube-d3651a139e3a66b4e9260c1ab1d334779804e3b1.zip |
SONAR-4711 Add WS client for project provisioning
Diffstat (limited to 'sonar-ws-client')
11 files changed, 390 insertions, 0 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java index 830dce49eae..04d4c1b7964 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java @@ -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; @@ -93,6 +95,13 @@ public class SonarClient { } /** + * New client to interact with web services related to projects + */ + public ProjectClient projectClient() { + return new DefaultProjectClient(requestFactory); + } + + /** * Create a builder of {@link SonarClient}s. */ public static Builder builder() { 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 index 00000000000..03cb5469599 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/NewProject.java @@ -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 index 00000000000..4020ebcb895 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/Project.java @@ -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 index 00000000000..3eb6c75b002 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/ProjectClient.java @@ -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 index 00000000000..f287c307ad2 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProject.java @@ -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 index 00000000000..2ee9c82aec5 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/DefaultProjectClient.java @@ -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 index 00000000000..a4b15c56715 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/internal/package-info.java @@ -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 index 00000000000..b20bfee4ec6 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/project/package-info.java @@ -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; diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java index eab25a50d34..cb1516a9db9 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java @@ -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 index 00000000000..444a7737c3f --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/project/NewProjectTest.java @@ -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 index 00000000000..e110737b9ec --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/project/internal/DefaultProjectClientTest.java @@ -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(); + + } +} |