aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-client
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-02-19 18:46:04 +0100
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-02-25 08:43:24 +0100
commit156baa1485e4f01052e81bd00837df61cbf24129 (patch)
tree77bebf48464bd80ea371b73ff31de3cc0b5c76ab /sonar-ws-client
parent181b908e2cc754e05255059c3a581d48c173cf3c (diff)
downloadsonarqube-156baa1485e4f01052e81bd00837df61cbf24129.tar.gz
sonarqube-156baa1485e4f01052e81bd00837df61cbf24129.zip
SONAR-4366 Add WS client classes for quality gate CRUD services
Diffstat (limited to 'sonar-ws-client')
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java10
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java30
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java38
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java32
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java46
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java96
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java57
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java130
8 files changed, 439 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 99341392df5..6e78bcb974c 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
@@ -19,6 +19,9 @@
*/
package org.sonar.wsclient;
+import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient;
+
+import org.sonar.wsclient.qualitygate.QualityGateClient;
import org.sonar.wsclient.internal.HttpRequestFactory;
import org.sonar.wsclient.issue.ActionPlanClient;
import org.sonar.wsclient.issue.IssueClient;
@@ -121,6 +124,13 @@ public class SonarClient {
return new DefaultRuleClient(requestFactory);
}
+ /**
+ * New client to interact with web services related to quality gates
+ */
+ public QualityGateClient qualityGateClient() {
+ return new DefaultQualityGateClient(requestFactory);
+ }
+
public SystemClient systemClient() {
return new DefaultSystemClient(requestFactory);
}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java
new file mode 100644
index 00000000000..3a2279e7467
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java
@@ -0,0 +1,30 @@
+/*
+ * 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.qualitygate;
+
+/**
+ * @since 4.3
+ */
+public interface QualityGate {
+
+ long id();
+
+ String name();
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java
new file mode 100644
index 00000000000..17a1fe99410
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.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.qualitygate;
+
+/**
+ * @since 4.3
+ */
+public interface QualityGateClient {
+
+ QualityGates list();
+
+ QualityGate create(String qGateName);
+
+ QualityGate rename(long qGateId, String qGateName);
+
+ void destroy(long qGateId);
+
+ void setDefault(long qGateId);
+
+ void unsetDefault();
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java
new file mode 100644
index 00000000000..3a3947b9622
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java
@@ -0,0 +1,32 @@
+/*
+ * 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.qualitygate;
+
+import java.util.Collection;
+
+/**
+ * @since 4.3
+ */
+public interface QualityGates {
+
+ Collection<QualityGate> qualityGates();
+
+ QualityGate defaultGate();
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java
new file mode 100644
index 00000000000..0d86d899206
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java
@@ -0,0 +1,46 @@
+/*
+ * 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.qualitygate.internal;
+
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+
+import java.util.Map;
+
+import org.sonar.wsclient.qualitygate.QualityGate;
+
+public class DefaultQualityGate implements QualityGate {
+
+ private Map<String, String> json;
+
+ DefaultQualityGate(Map<String, String> json) {
+ this.json = json;
+ }
+
+ @Override
+ public long id() {
+ return JsonUtils.getLong(json, "id");
+ }
+
+ @Override
+ public String name() {
+ return JsonUtils.getString(json, "name");
+ }
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java
new file mode 100644
index 00000000000..ac920a88c5e
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java
@@ -0,0 +1,96 @@
+/*
+ * 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.qualitygate.internal;
+
+import org.json.simple.JSONValue;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.qualitygate.QualityGate;
+import org.sonar.wsclient.qualitygate.QualityGateClient;
+import org.sonar.wsclient.qualitygate.QualityGates;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DefaultQualityGateClient implements QualityGateClient {
+
+ private static final String ROOT_URL = "/api/qualitygates";
+ private static final String LIST_URL = ROOT_URL + "/list";
+ private static final String CREATE_URL = ROOT_URL + "/create";
+ private static final String RENAME_URL = ROOT_URL + "/rename";
+ private static final String DESTROY_URL = ROOT_URL + "/destroy";
+ private static final String SET_DEFAULT_URL = ROOT_URL + "/set_as_default";
+ private static final String UNSET_DEFAULT_URL = ROOT_URL + "/unset_default";
+
+ private final HttpRequestFactory requestFactory;
+
+ public DefaultQualityGateClient(HttpRequestFactory requestFactory) {
+ this.requestFactory = requestFactory;
+ }
+
+ @Override
+ public QualityGates list() {
+ String json = requestFactory.get(LIST_URL, Collections.<String, Object> emptyMap());
+ return jsonToQualityGates(json);
+ }
+
+ @Override
+ public QualityGate create(String qGateName) {
+ String json = requestFactory.post(CREATE_URL, Collections.singletonMap("name", (Object) qGateName));
+ return jsonToQualityGate(json);
+ }
+
+ @Override
+ public QualityGate rename(long qGateId, String qGateName) {
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("id", qGateId);
+ params.put("name", qGateName);
+ String json = requestFactory.post(RENAME_URL, params);
+ return jsonToQualityGate(json);
+ }
+
+ @Override
+ public void destroy(long qGateId) {
+ requestFactory.post(DESTROY_URL, Collections.singletonMap("id", (Object) qGateId));
+ }
+
+ @Override
+ public void setDefault(long qGateId) {
+ requestFactory.post(SET_DEFAULT_URL, Collections.singletonMap("id", (Object) qGateId));
+ }
+
+ @Override
+ public void unsetDefault() {
+ requestFactory.post(UNSET_DEFAULT_URL, Collections.<String, Object> emptyMap());
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private QualityGate jsonToQualityGate(String json) {
+ Map jsonRoot = (Map) JSONValue.parse(json);
+ return new DefaultQualityGate((Map) jsonRoot);
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private QualityGates jsonToQualityGates(String json) {
+ Map jsonRoot = (Map) JSONValue.parse(json);
+ return new DefaultQualityGates((Map) jsonRoot);
+ }
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java
new file mode 100644
index 00000000000..23f6a031f33
--- /dev/null
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java
@@ -0,0 +1,57 @@
+/*
+ * 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.qualitygate.internal;
+
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+import org.sonar.wsclient.qualitygate.QualityGate;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.sonar.wsclient.qualitygate.QualityGates;
+
+public class DefaultQualityGates implements QualityGates {
+
+ private Map<Long, QualityGate> qualityGates;
+
+ private Long defaultId;
+
+ @SuppressWarnings("unchecked")
+ public DefaultQualityGates(Map<String, Object> json) {
+ qualityGates = new LinkedHashMap<Long, QualityGate>();
+ for (Object entry: JsonUtils.getArray(json, "qualitygates")) {
+ QualityGate qGate = new DefaultQualityGate((Map<String, String>) entry);
+ qualityGates.put(qGate.id(), qGate);
+ }
+ defaultId = JsonUtils.getLong(json, "default");
+ }
+
+ @Override
+ public Collection<QualityGate> qualityGates() {
+ return qualityGates.values();
+ }
+
+ @Override
+ public QualityGate defaultGate() {
+ return qualityGates.get(defaultId);
+ }
+
+}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java
new file mode 100644
index 00000000000..45c1ddac63b
--- /dev/null
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.qualitygate.internal;
+
+import java.net.HttpURLConnection;
+
+import org.sonar.wsclient.qualitygate.QualityGates;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.wsclient.MockHttpServerInterceptor;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.qualitygate.QualityGate;
+import org.sonar.wsclient.qualitygate.QualityGateClient;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.MapAssert.entry;
+
+public class DefaultQualityGateClientTest {
+
+ @Rule
+ public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
+
+ @Test
+ public void should_create_qualitygate() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}");
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ QualityGate result = client.create("Ninth");
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create");
+ assertThat(httpServer.requestParams()).includes(
+ entry("name", "Ninth")
+ );
+ assertThat(result).isNotNull();
+ }
+
+ @Test
+ public void should_list_qualitygates() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubResponseBody(
+ "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}]}");
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ QualityGates qGates = client.list();
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list");
+ assertThat(httpServer.requestParams()).isEmpty();
+ assertThat(qGates.qualityGates()).hasSize(3);
+ assertThat(qGates.defaultGate()).isNull();
+ }
+
+ @Test
+ public void should_rename_qualitygate() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}");
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ QualityGate result = client.rename(666L, "Hell");
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/rename");
+ assertThat(httpServer.requestParams()).includes(
+ entry("id", "666"),
+ entry("name", "Hell")
+ );
+ assertThat(result).isNotNull();
+ }
+
+ @Test
+ public void should_destroy_qualitygate() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ client.destroy(666L);
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/destroy");
+ assertThat(httpServer.requestParams()).includes(
+ entry("id", "666")
+ );
+ }
+
+ @Test
+ public void should_set_default_qualitygate() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ client.setDefault(666L);
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/set_as_default");
+ assertThat(httpServer.requestParams()).includes(
+ entry("id", "666")
+ );
+ }
+
+ @Test
+ public void should_unset_default_qualitygate() {
+ HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+ httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
+
+ QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+ client.unsetDefault();
+
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/unset_default");
+ assertThat(httpServer.requestParams()).isEmpty();
+ }
+}