]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4366 Add WS client classes for quality gate CRUD services
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 19 Feb 2014 17:46:04 +0000 (18:46 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 25 Feb 2014 07:43:24 +0000 (08:43 +0100)
sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java [new file with mode: 0644]

index 80ad46b1f028fd3e29de2c9bad59f1fdf7797e0d..d59782c77cf506905e91a496bd8633ffb7f67476 100644 (file)
  */
 package org.sonar.server.qualitygate;
 
-import org.apache.commons.lang.StringUtils;
-import org.sonar.core.properties.PropertyDto;
-import org.sonar.core.properties.PropertiesDao;
-import org.sonar.server.exceptions.NotFoundException;
 import com.google.common.base.Strings;
+import org.apache.commons.lang.StringUtils;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
 import org.sonar.core.qualitygate.db.QualityGateDao;
 import org.sonar.core.qualitygate.db.QualityGateDto;
 import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.util.Validation;
 
@@ -87,6 +87,7 @@ public class QualityGates {
   }
 
   public void setDefault(@Nullable Long idToUseAsDefault) {
+    checkPermission(UserSession.get());
     if (idToUseAsDefault == null) {
       propertiesDao.deleteGlobalProperty(SONAR_QUALITYGATE_PROPERTY);
     } else {
@@ -121,7 +122,7 @@ public class QualityGates {
   private QualityGateDto getNonNull(long id) {
     QualityGateDto qGate = dao.selectById(id);
     if (qGate == null) {
-      throw new NotFoundException();
+      throw new NotFoundException("There is no quality gate with id=" + id);
     }
     return qGate;
   }
index 99341392df54653ba463045477257550ecdb3b34..6e78bcb974c99031508758f24c3169a922aa2cdd 100644 (file)
@@ -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 (file)
index 0000000..3a2279e
--- /dev/null
@@ -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 (file)
index 0000000..17a1fe9
--- /dev/null
@@ -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 (file)
index 0000000..3a3947b
--- /dev/null
@@ -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 (file)
index 0000000..0d86d89
--- /dev/null
@@ -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 (file)
index 0000000..ac920a8
--- /dev/null
@@ -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 (file)
index 0000000..23f6a03
--- /dev/null
@@ -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 (file)
index 0000000..45c1dda
--- /dev/null
@@ -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();
+  }
+}