aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-client/src
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-01-29 10:20:39 +0100
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-01-29 15:52:27 +0100
commitff7d411f8307aca5c62e8422edece454e40c02fb (patch)
treefb9c14193eb9382a719cf7af64a4b9eef4c9901e /sonar-ws-client/src
parenta4da5c01aa43606948d69fe92a98a2cb1c438f0f (diff)
downloadsonarqube-ff7d411f8307aca5c62e8422edece454e40c02fb.tar.gz
sonarqube-ff7d411f8307aca5c62e8422edece454e40c02fb.zip
SONAR-4326 Create WS client for rule tag referential management
Diffstat (limited to 'sonar-ws-client/src')
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java9
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleTagClient.java39
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClient.java59
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClientTest.java67
4 files changed, 174 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 aefd3e382b1..038c061d714 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,6 +28,8 @@ 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.RuleTagClient;
+import org.sonar.wsclient.rule.internal.DefaultRuleTagClient;
import org.sonar.wsclient.system.SystemClient;
import org.sonar.wsclient.system.internal.DefaultSystemClient;
import org.sonar.wsclient.user.UserClient;
@@ -103,6 +105,13 @@ public class SonarClient {
return new DefaultProjectClient(requestFactory);
}
+ /**
+ * New client to interact with web services related to rule tags
+ */
+ public RuleTagClient ruleTagClient() {
+ return new DefaultRuleTagClient(requestFactory);
+ }
+
public SystemClient systemClient() {
return new DefaultSystemClient(requestFactory);
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleTagClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleTagClient.java
new file mode 100644
index 00000000000..50abfe2a15d
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/RuleTagClient.java
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+import java.util.Collection;
+
+/**
+ * Allows management of rule tags
+ * @since 4.2
+ */
+public interface RuleTagClient {
+
+ /**
+ * List all tags
+ */
+ Collection<String> list();
+
+ /**
+ * Create a new tag
+ */
+ void create(String tag);
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClient.java
new file mode 100644
index 00000000000..43f9101a9b2
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClient.java
@@ -0,0 +1,59 @@
+/*
+ * 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.json.simple.JSONValue;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.rule.RuleTagClient;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#ruleTagClient()}.
+ */
+public class DefaultRuleTagClient implements RuleTagClient {
+
+ private static final String ROOT_URL = "/api/rule_tags";
+ private static final String LIST_URL = ROOT_URL + "/list";
+ private static final String CREATE_URL = ROOT_URL + "/create";
+
+ private final HttpRequestFactory requestFactory;
+
+ public DefaultRuleTagClient(HttpRequestFactory requestFactory) {
+ this.requestFactory = requestFactory;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Collection<String> list() {
+ String json = requestFactory.get(LIST_URL, null);
+ final List<String> tagList = (List<String>) JSONValue.parse(json);
+ return tagList;
+ }
+
+ @Override
+ public void create(String tag) {
+ final HashMap<String, Object> params = new HashMap<String, Object>();
+ params.put("tag", tag);
+ requestFactory.post(CREATE_URL, params);
+ }
+}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClientTest.java
new file mode 100644
index 00000000000..ebc9a595786
--- /dev/null
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/rule/internal/DefaultRuleTagClientTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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 java.util.Collection;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.MapAssert.entry;
+
+public class DefaultRuleTagClientTest {
+
+ @Rule
+ public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
+
+ private DefaultRuleTagClient client;
+
+ @Before
+ public void initClient() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+ this.client = new DefaultRuleTagClient(requestFactory);
+ }
+
+ @Test
+ public void should_list_tags() {
+ httpServer.stubResponseBody("[\"tag1\",\"tag2\",\"tag3\"]");
+
+ Collection<String> result = client.list();
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/rule_tags/list");
+ assertThat(result).containsOnly("tag1", "tag2", "tag3");
+ }
+
+ @Test
+ public void should_create_rule_tag() {
+ httpServer.stubStatusCode(200);
+
+ final String newTag = "polop";
+ client.create(newTag);
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/rule_tags/create");
+ assertThat(httpServer.requestParams()).includes(entry("tag", newTag));
+ }
+
+}