From 9a37480a156494a1486b6fa13b7f0d6c6f71fa19 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Wed, 29 Jan 2014 10:56:35 +0100 Subject: [PATCH] SONAR-4326 Add WS client to manage tags on rules (add/remove) --- .../java/org/sonar/wsclient/SonarClient.java | 9 +++ .../org/sonar/wsclient/rule/RuleClient.java | 37 ++++++++++ .../rule/internal/DefaultRuleClient.java | 64 +++++++++++++++++ .../rule/internal/DefaultRuleClientTest.java | 70 +++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleClient.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleClient.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleClientTest.java 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 038c061d714..99341392df5 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 @@ -28,7 +28,9 @@ 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.rule.RuleClient; import org.sonar.wsclient.rule.RuleTagClient; +import org.sonar.wsclient.rule.internal.DefaultRuleClient; import org.sonar.wsclient.rule.internal.DefaultRuleTagClient; import org.sonar.wsclient.system.SystemClient; import org.sonar.wsclient.system.internal.DefaultSystemClient; @@ -112,6 +114,13 @@ public class SonarClient { return new DefaultRuleTagClient(requestFactory); } + /** + * New client to interact with web services related to rules + */ + public RuleClient ruleClient() { + return new DefaultRuleClient(requestFactory); + } + public SystemClient systemClient() { return new DefaultSystemClient(requestFactory); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleClient.java new file mode 100644 index 00000000000..9d5f82c5a9d --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleClient.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.rule; + +/** + * Allows management of rules + * @since 4.2 + */ +public interface RuleClient { + + /** + * Associate new tags to a rule + */ + void addTags(String key, String... tags); + + /** + * Remove tags from a rule + */ + void removeTags(String key, String... tags); +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleClient.java new file mode 100644 index 00000000000..04d0525010f --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleClient.java @@ -0,0 +1,64 @@ +/* + * 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.rule.internal; + +import org.sonar.wsclient.internal.HttpRequestFactory; +import org.sonar.wsclient.rule.RuleClient; + +import java.util.HashMap; +import java.util.Map; + +/** + * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#ruleClient()}. + */ +public class DefaultRuleClient implements RuleClient { + + private static final String ROOT_URL = "/api/rules"; + private static final String ADD_TAGS_URL = ROOT_URL + "/add_tags"; + private static final String REMOVE_TAGS_URL = ROOT_URL + "/remove_tags"; + + private final HttpRequestFactory requestFactory; + + public DefaultRuleClient(HttpRequestFactory requestFactory) { + this.requestFactory = requestFactory; + } + + @Override + public void addTags(String key, String... tags) { + requestFactory.post(ADD_TAGS_URL, buildQueryParams(key, tags)); + } + + @Override + public void removeTags(String key, String... tags) { + requestFactory.post(REMOVE_TAGS_URL, buildQueryParams(key, tags)); + } + + private Map buildQueryParams(String key, String... tags) { + Map params = new HashMap(); + params.put("key", key); + StringBuilder tagsBuilder = new StringBuilder(); + for (int i=0; i < tags.length - 1; i++) { + tagsBuilder.append(tags[i]).append(","); + } + tagsBuilder.append(tags[tags.length - 1]); + params.put("tags", tagsBuilder.toString()); + return params; + } +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleClientTest.java new file mode 100644 index 00000000000..9baf51c4e58 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleClientTest.java @@ -0,0 +1,70 @@ +/* + * 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.rule.internal; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.wsclient.MockHttpServerInterceptor; +import org.sonar.wsclient.internal.HttpRequestFactory; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.MapAssert.entry; + +public class DefaultRuleClientTest { + + @Rule + public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor(); + + private DefaultRuleClient client; + + @Before + public void initClient() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + this.client = new DefaultRuleClient(requestFactory); + } + + @Test + public void should_add_tags() { + httpServer.stubStatusCode(200); + + final String ruleKey = "repo:rule1"; + client.addTags(ruleKey, "tag1", "tag2", "tag3"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/rules/add_tags"); + assertThat(httpServer.requestParams()).includes( + entry("key", ruleKey), + entry("tags", "tag1,tag2,tag3")); + } + + @Test + public void should_remove_tags() { + httpServer.stubStatusCode(200); + + final String ruleKey = "repo:rule1"; + client.removeTags(ruleKey, "tag1", "tag2", "tag3"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/rules/remove_tags"); + assertThat(httpServer.requestParams()).includes( + entry("key", ruleKey), + entry("tags", "tag1,tag2,tag3")); + } + +} -- 2.39.5