]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5094 Add create/update/delete condition capabilities to WS client
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 10 Mar 2014 12:01:29 +0000 (13:01 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 10 Mar 2014 15:51:26 +0000 (16:51 +0100)
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/NewCondition.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/UpdateCondition.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java
sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java

diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/NewCondition.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/NewCondition.java
new file mode 100644 (file)
index 0000000..0d2b42d
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * 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 javax.annotation.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @since 4.3
+ */
+public class NewCondition {
+
+  private final Map<String, Object> params;
+
+  private NewCondition() {
+    params = new HashMap<String, Object>();
+  }
+
+  public static NewCondition create(long qGateId) {
+    NewCondition newCondition = new NewCondition();
+    newCondition.params.put("gateId", qGateId);
+    return newCondition;
+  }
+
+  public Map<String, Object> urlParams() {
+    return params;
+  }
+
+  public NewCondition metricKey(String metricKey) {
+    params.put("metric", metricKey);
+    return this;
+  }
+
+  public NewCondition operator(String operator) {
+    params.put("op", operator);
+    return this;
+  }
+
+  public NewCondition warningThreshold(@Nullable String warning) {
+    params.put("warning", warning);
+    return this;
+  }
+
+  public NewCondition errorThreshold(@Nullable String error) {
+    params.put("error", error);
+    return this;
+  }
+
+  public NewCondition period(@Nullable Integer period) {
+    params.put("period", period);
+    return this;
+  }
+}
+
index 073cae9a8681cb8c0af71e0bc8bd6177b38293d4..fd499276dd2fec4473b17b13a06813ffbdc771e1 100644 (file)
@@ -35,6 +35,12 @@ public interface QualityGateClient {
 
   QualityGateDetails show(String qGateName);
 
+  QualityGateCondition createCondition(NewCondition condition);
+
+  QualityGateCondition updateCondition(UpdateCondition condition);
+
+  void deleteCondition(long conditionId);
+
   void destroy(long qGateId);
 
   void setDefault(long qGateId);
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/UpdateCondition.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/UpdateCondition.java
new file mode 100644 (file)
index 0000000..c4832f2
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * 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 javax.annotation.Nullable;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @since 4.3
+ */
+public class UpdateCondition {
+
+  private final Map<String, Object> params;
+
+  private UpdateCondition() {
+    params = new HashMap<String, Object>();
+  }
+
+  public static UpdateCondition create(long id) {
+    UpdateCondition newCondition = new UpdateCondition();
+    newCondition.params.put("id", id);
+    return newCondition;
+  }
+
+  public Map<String, Object> urlParams() {
+    return params;
+  }
+
+  public UpdateCondition metricKey(String metricKey) {
+    params.put("metric", metricKey);
+    return this;
+  }
+
+  public UpdateCondition operator(String operator) {
+    params.put("op", operator);
+    return this;
+  }
+
+  public UpdateCondition warningThreshold(@Nullable String warning) {
+    params.put("warning", warning);
+    return this;
+  }
+
+  public UpdateCondition errorThreshold(@Nullable String error) {
+    params.put("error", error);
+    return this;
+  }
+
+  public UpdateCondition period(@Nullable Integer period) {
+    params.put("period", period);
+    return this;
+  }
+}
+
index ff24c155604eb295f72fd93a90ff62ebccb5da3c..032a3fbf5c073df37aee17ad592b995b4756004e 100644 (file)
@@ -31,6 +31,9 @@ public class DefaultQualityGateClient implements QualityGateClient {
   private static final String LIST_URL = ROOT_URL + "/list";
   private static final String SHOW_URL = ROOT_URL + "/show";
   private static final String CREATE_URL = ROOT_URL + "/create";
+  private static final String CREATE_CONDITION_URL = ROOT_URL + "/create_condition";
+  private static final String UPDATE_CONDITION_URL = ROOT_URL + "/update_condition";
+  private static final String DELETE_CONDITION_URL = ROOT_URL + "/delete_condition";
   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";
@@ -75,6 +78,23 @@ public class DefaultQualityGateClient implements QualityGateClient {
     return jsonToDetails(json);
   }
 
+  @Override
+  public QualityGateCondition createCondition(NewCondition condition) {
+    String json = requestFactory.post(CREATE_CONDITION_URL, condition.urlParams());
+    return jsonToCondition(json);
+  }
+
+  @Override
+  public QualityGateCondition updateCondition(UpdateCondition condition) {
+    String json = requestFactory.post(UPDATE_CONDITION_URL, condition.urlParams());
+    return jsonToCondition(json);
+  }
+
+  @Override
+  public void deleteCondition(long conditionId) {
+    requestFactory.post(DELETE_CONDITION_URL, Collections.singletonMap("id", (Object) conditionId));
+  }
+
   @Override
   public void destroy(long qGateId) {
     requestFactory.post(DESTROY_URL, Collections.singletonMap("id", (Object) qGateId));
@@ -121,5 +141,8 @@ public class DefaultQualityGateClient implements QualityGateClient {
     return conditions;
   }
 
-
+  @SuppressWarnings({"rawtypes", "unchecked"})
+  private DefaultQualityGateCondition jsonToCondition(String json) {
+    return new DefaultQualityGateCondition((Map) JSONValue.parse(json));
+  }
 }
index 7bb2f309a26abeda7ce284b5d18ec3e715877e9d..f5f7595976b2eb930db4db3bc8836b31f3ead91f 100644 (file)
@@ -227,4 +227,65 @@ public class DefaultQualityGateClientTest {
     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/unset_default");
     assertThat(httpServer.requestParams()).isEmpty();
   }
+
+  @Test
+  public void should_create_condition_on_qualitygate() {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+    httpServer.stubResponseBody("{\"id\":42,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"90\",\"error\":\"80\",\"period\":3}");
+
+    QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+    QualityGateCondition result = client.createCondition(NewCondition.create(12345L)
+      .metricKey("new_coverage").operator("LT").warningThreshold("90").errorThreshold("80").period(3));
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create_condition");
+    assertThat(httpServer.requestParams()).includes(
+        entry("gateId", "12345"),
+        entry("metric", "new_coverage"),
+        entry("op", "LT"),
+        entry("warning", "90"),
+        entry("error", "80"),
+        entry("period", "3")
+        );
+    assertThat(result).isNotNull();
+    assertThat(result.id()).isEqualTo(42L);
+  }
+
+  @Test
+  public void should_update_condition() {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+    httpServer.stubResponseBody("{\"id\":12345,\"metric\":\"ncloc\",\"op\":\"GT\",\"warning\":\"1000\",\"error\":\"2000\",\"period\":1}");
+
+    QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+    QualityGateCondition result = client.updateCondition(UpdateCondition.create(12345L)
+      .metricKey("ncloc").operator("GT").warningThreshold("1000").errorThreshold("2000").period(1));
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/update_condition");
+    assertThat(httpServer.requestParams()).includes(
+        entry("id", "12345"),
+        entry("metric", "ncloc"),
+        entry("op", "GT"),
+        entry("warning", "1000"),
+        entry("error", "2000"),
+        entry("period", "1")
+        );
+    assertThat(result).isNotNull();
+    assertThat(result.id()).isEqualTo(12345L);
+  }
+
+  @Test
+  public void should_delete_condition() {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+    httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
+
+    QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+    client.deleteCondition(666L);;
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/delete_condition");
+    assertThat(httpServer.requestParams()).includes(
+        entry("id", "666")
+        );
+  }
 }