diff options
author | Julien Lancelot <julien.lancelot@gmail.com> | 2013-05-13 18:55:51 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@gmail.com> | 2013-05-13 18:55:51 +0200 |
commit | 2d4b079141608ec417e284ff714bfe0b7d6dd040 (patch) | |
tree | e2c0ea11a50493c5099a7f30f0c433af00d663e4 | |
parent | 3920d6ae7a4ece63563995c9505dca8f2544eceb (diff) | |
download | sonarqube-2d4b079141608ec417e284ff714bfe0b7d6dd040.tar.gz sonarqube-2d4b079141608ec417e284ff714bfe0b7d6dd040.zip |
SONAR-4282 Create Action Plan WS to get all action plans
9 files changed, 426 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 b8ee9cdd5f1..12b71f21aff 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 @@ -20,6 +20,8 @@ package org.sonar.wsclient; import org.sonar.wsclient.internal.HttpRequestFactory; +import org.sonar.wsclient.issue.ActionPlanClient; +import org.sonar.wsclient.issue.DefaultActionPlanClient; import org.sonar.wsclient.issue.DefaultIssueClient; import org.sonar.wsclient.issue.IssueClient; import org.sonar.wsclient.user.DefaultUserClient; @@ -40,6 +42,10 @@ public class SonarClient { return new DefaultIssueClient(requestFactory); } + public ActionPlanClient actionPlanClient() { + return new DefaultActionPlanClient(requestFactory); + } + public UserClient userClient() { return new DefaultUserClient(requestFactory); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlan.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlan.java new file mode 100644 index 00000000000..5d524e5c5d4 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlan.java @@ -0,0 +1,93 @@ +/* + * 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.issue; + +import org.sonar.wsclient.unmarshallers.JsonUtils; + +import javax.annotation.CheckForNull; + +import java.util.Date; +import java.util.Map; + +/** + * @since 3.6 + */ +public class ActionPlan { + + private final Map json; + + ActionPlan(Map json) { + this.json = json; + } + + /** + * Unique key + */ + public String key() { + return JsonUtils.getString(json, "key"); + } + + public String projectKey() { + return JsonUtils.getString(json, "project"); + } + + public String name() { + return JsonUtils.getString(json, "name"); + } + + @CheckForNull + public String description() { + return JsonUtils.getString(json, "desc"); + } + + public String status() { + return JsonUtils.getString(json, "status"); + } + + /** + * Login of the user who created the action plan. + */ + public String userLogin() { + return JsonUtils.getString(json, "userLogin"); + } + + @CheckForNull + public Date deadLine() { + return JsonUtils.getDateTime(json, "deadLine"); + } + + public Date createdAt() { + return JsonUtils.getDateTime(json, "createdAt"); + } + + public Date updatedAt() { + return JsonUtils.getDateTime(json, "updatedAt"); + } + + public int totalIssues() { + return JsonUtils.getInteger(json, "totalIssues"); + } + + public int openIssues() { + return JsonUtils.getInteger(json, "openIssues"); + } + + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanClient.java new file mode 100644 index 00000000000..cdeab14c359 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanClient.java @@ -0,0 +1,31 @@ +/* + * 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.issue; + +import java.util.List; + +/** + * @since 3.6 + */ +public interface ActionPlanClient { + + List<ActionPlan> find(); + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanParser.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanParser.java new file mode 100644 index 00000000000..2ab624142b0 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanParser.java @@ -0,0 +1,83 @@ +/* + * 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.issue; + +import org.json.simple.JSONValue; +import org.sonar.wsclient.component.Component; +import org.sonar.wsclient.rule.Rule; +import org.sonar.wsclient.unmarshallers.JsonUtils; +import org.sonar.wsclient.user.User; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @since 3.6 + */ +class ActionPlanParser { + + Issues parseIssues(String json) { + Issues result = new Issues(); + Map jsonRoot = (Map) JSONValue.parse(json); + List<Map> jsonIssues = (List) jsonRoot.get("issues"); + if (jsonIssues != null) { + for (Map jsonIssue : jsonIssues) { + result.add(new Issue(jsonIssue)); + } + } + + List<Map> jsonRules = (List) jsonRoot.get("rules"); + if (jsonRules != null) { + for (Map jsonRule : jsonRules) { + result.add(new Rule(jsonRule)); + } + } + + List<Map> jsonUsers = (List) jsonRoot.get("users"); + if (jsonUsers != null) { + for (Map jsonUser : jsonUsers) { + result.add(new User(jsonUser)); + } + } + + List<Map> jsonComponents = (List) jsonRoot.get("components"); + if (jsonComponents != null) { + for (Map jsonComponent : jsonComponents) { + result.add(new Component(jsonComponent)); + } + } + + Map paging = (Map) jsonRoot.get("paging"); + result.setPaging(new Paging(paging)); + result.setSecurityExclusions(JsonUtils.getBoolean(jsonRoot, "securityExclusions")); + return result; + } + + List<String> parseTransitions(String json) { + List<String> transitions = new ArrayList<String>(); + Map jRoot = (Map) JSONValue.parse(json); + List<String> jTransitions = (List) jRoot.get("transitions"); + for (String jTransition : jTransitions) { + transitions.add(jTransition); + } + return transitions; + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanQuery.java new file mode 100644 index 00000000000..1ae43e56914 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/ActionPlanQuery.java @@ -0,0 +1,43 @@ +/* + * 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.issue; + +import java.util.HashMap; +import java.util.Map; + +/** + * @since 3.6 + */ +public class ActionPlanQuery { + static final String BASE_URL = "/api/action_plans/search"; + + private final Map<String, Object> params = new HashMap<String, Object>(); + + private ActionPlanQuery() { + } + + public static ActionPlanQuery create() { + return new ActionPlanQuery(); + } + + Map<String, Object> urlParams() { + return params; + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultActionPlanClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultActionPlanClient.java new file mode 100644 index 00000000000..7f4ad8750f6 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultActionPlanClient.java @@ -0,0 +1,62 @@ +/* + * 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.issue; + +import com.github.kevinsawicki.http.HttpRequest; +import org.json.simple.JSONValue; +import org.sonar.wsclient.internal.HttpRequestFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#actionPlanClient()}. + */ +public class DefaultActionPlanClient implements ActionPlanClient { + + private final HttpRequestFactory requestFactory; + + /** + * For internal use. Use {@link org.sonar.wsclient.SonarClient} to get an instance. + */ + public DefaultActionPlanClient(HttpRequestFactory requestFactory) { + this.requestFactory = requestFactory; + } + + @Override + public List<ActionPlan> find() { + HttpRequest request = requestFactory.get(ActionPlanQuery.BASE_URL, null); + if (!request.ok()) { + throw new IllegalStateException("Fail to search for users. Bad HTTP response status: " + request.code()); + } + List<ActionPlan> result = new ArrayList<ActionPlan>(); + String json = request.body("UTF-8"); + Map jsonRoot = (Map) JSONValue.parse(json); + List<Map> jsonActionPlans = (List) jsonRoot.get("actionPlans"); + if (jsonActionPlans != null) { + for (Map jsonActionPlan : jsonActionPlans) { + result.add(new ActionPlan(jsonActionPlan)); + } + } + return result; + } + +} 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 03ae927b15d..9c9acbbbd55 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 @@ -20,6 +20,7 @@ package org.sonar.wsclient; import org.junit.Test; +import org.sonar.wsclient.issue.DefaultActionPlanClient; import org.sonar.wsclient.issue.DefaultIssueClient; import org.sonar.wsclient.user.DefaultUserClient; @@ -31,6 +32,7 @@ public class SonarClientTest { public void should_build_clients() { SonarClient client = SonarClient.builder().url("http://localhost:9000").build(); assertThat(client.issueClient()).isNotNull().isInstanceOf(DefaultIssueClient.class); + assertThat(client.actionPlanClient()).isNotNull().isInstanceOf(DefaultActionPlanClient.class); assertThat(client.userClient()).isNotNull().isInstanceOf(DefaultUserClient.class); } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/ActionPlanQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/ActionPlanQueryTest.java new file mode 100644 index 00000000000..b446736f86c --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/ActionPlanQueryTest.java @@ -0,0 +1,38 @@ +/* + * 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.issue; + +import org.junit.Test; + +import java.util.Map; + +import static org.fest.assertions.Assertions.assertThat; + +public class ActionPlanQueryTest { + + @Test + public void test_empty_params() throws Exception { + ActionPlanQuery query = ActionPlanQuery.create(); + Map<String, Object> params = query.urlParams(); + + assertThat(params).isEmpty(); + } +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java new file mode 100644 index 00000000000..3ce0afb9f59 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/DefaultActionPlanClientTest.java @@ -0,0 +1,68 @@ +/* + * 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.issue; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.wsclient.MockHttpServerInterceptor; +import org.sonar.wsclient.internal.HttpRequestFactory; + +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; + +public class DefaultActionPlanClientTest { + + @Rule + public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor(); + + @Test + public void should_find_action_plans() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url(), null, null); + httpServer.doReturnBody("{\"actionPlans\": [{\"key\": \"382f6f2e-ad9d-424a-b973-9b065e04348a\",\n" + + "\"name\": \"Long term\",\n" + + "\"status\": \"CLOSED\",\n" + + "\"project\": \"com.sonarsource.it.samples:simple-sample\",\n" + + "\"userLogin\": \"admin\",\n" + + "\"deadLine\": \"2013-05-30T00:00:00+0200\",\n" + + "\"totalIssues\": 0,\n" + + "\"openIssues\": 0,\n" + + "\"createdAt\": \"2013-05-13T12:50:29+0200\",\n" + + "\"updatedAt\": \"2013-05-13T12:50:44+0200\"}]}"); + + ActionPlanClient client = new DefaultActionPlanClient(requestFactory); + List<ActionPlan> actionPlans = client.find(); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/search"); + assertThat(actionPlans).hasSize(1); + ActionPlan actionPlan = actionPlans.get(0); + assertThat(actionPlan.key()).isEqualTo("382f6f2e-ad9d-424a-b973-9b065e04348a"); + assertThat(actionPlan.name()).isEqualTo("Long term"); + assertThat(actionPlan.status()).isEqualTo("CLOSED"); + assertThat(actionPlan.userLogin()).isEqualTo("admin"); + assertThat(actionPlan.deadLine()).isNotNull(); + assertThat(actionPlan.totalIssues()).isEqualTo(0); + assertThat(actionPlan.openIssues()).isEqualTo(0); + assertThat(actionPlan.createdAt()).isNotNull(); + assertThat(actionPlan.updatedAt()).isNotNull(); + } + +} |