]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5094 Add method in WS client to get conditions for a QG
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 5 Mar 2014 13:24:58 +0000 (14:24 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 5 Mar 2014 13:25:05 +0000 (14:25 +0100)
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateCondition.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateCondition.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java

index 17a1fe99410286b4a30a5db2bd155ee22f6ec7a4..9f5563f1cd429f4fb060c0c33ac0e2316182c696 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.wsclient.qualitygate;
 
+import java.util.Collection;
+
 /**
  * @since 4.3
  */
@@ -30,6 +32,8 @@ public interface QualityGateClient {
 
   QualityGate rename(long qGateId, String qGateName);
 
+  Collection<QualityGateCondition> conditions(long qGateId);
+
   void destroy(long qGateId);
 
   void setDefault(long qGateId);
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateCondition.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateCondition.java
new file mode 100644 (file)
index 0000000..1443ba5
--- /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 QualityGateCondition {
+
+  Long id();
+
+  String metricKey();
+
+  String operator();
+
+  String warningThreshold();
+
+  String errorThreshold();
+
+  Integer period();
+}
index ac920a88c5e31067f82cc06d781af86c136b78ee..1b95997bb7526bb2b841e22ef1262ef64be4430d 100644 (file)
@@ -23,16 +23,16 @@ 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.QualityGateCondition;
 import org.sonar.wsclient.qualitygate.QualityGates;
 
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 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 SHOW_URL = ROOT_URL + "/show";
   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";
@@ -66,6 +66,12 @@ public class DefaultQualityGateClient implements QualityGateClient {
     return jsonToQualityGate(json);
   }
 
+  @Override
+  public Collection<QualityGateCondition> conditions(long qGateId) {
+    String json = requestFactory.get(SHOW_URL, Collections.singletonMap("id", (Object) qGateId));
+    return jsonToConditions(json);
+  }
+
   @Override
   public void destroy(long qGateId) {
     requestFactory.post(DESTROY_URL, Collections.singletonMap("id", (Object) qGateId));
@@ -93,4 +99,14 @@ public class DefaultQualityGateClient implements QualityGateClient {
     return new DefaultQualityGates((Map) jsonRoot);
   }
 
+  @SuppressWarnings({"rawtypes", "unchecked"})
+  private Collection<QualityGateCondition> jsonToConditions(String json) {
+    Map jsonRoot = (Map) JSONValue.parse(json);
+    Collection<Map> conditionArray = (Collection<Map>) jsonRoot.get("conditions");
+    Collection<QualityGateCondition> conditions = new ArrayList<QualityGateCondition>();
+    for (Map conditionJson: conditionArray) {
+      conditions.add(new DefaultQualityGateCondition(conditionJson));
+    }
+    return conditions;
+  }
 }
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateCondition.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateCondition.java
new file mode 100644 (file)
index 0000000..d6604c4
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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.qualitygate.QualityGateCondition;
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+
+import java.util.Map;
+
+public class DefaultQualityGateCondition implements QualityGateCondition {
+
+  private Map<String, String> json;
+
+  public DefaultQualityGateCondition(Map<String, String> json) {
+    this.json = json;
+  }
+
+  @Override
+  public Long id() {
+    return JsonUtils.getLong(json, "id");
+  }
+
+  @Override
+  public String metricKey() {
+    return JsonUtils.getString(json, "metric");
+  }
+
+  @Override
+  public String operator() {
+    return JsonUtils.getString(json, "op");
+  }
+
+  @Override
+  public String warningThreshold() {
+    return JsonUtils.getString(json, "warning");
+  }
+
+  @Override
+  public String errorThreshold() {
+    return JsonUtils.getString(json, "error");
+  }
+
+  @Override
+  public Integer period() {
+    return JsonUtils.getInteger(json, "period");
+  }
+
+}
index 45c1ddac63b1dd1008f6966328727568560ae85f..24fd6d507c74620e5503cca7798da19c91728ee5 100644 (file)
  */
 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 org.sonar.wsclient.qualitygate.QualityGateCondition;
+import org.sonar.wsclient.qualitygate.QualityGates;
+
+import java.net.HttpURLConnection;
+import java.util.Collection;
+import java.util.Iterator;
+
 import static org.fest.assertions.Assertions.assertThat;
 import static org.fest.assertions.MapAssert.entry;
 
@@ -50,6 +54,8 @@ public class DefaultQualityGateClientTest {
         entry("name", "Ninth")
         );
     assertThat(result).isNotNull();
+    assertThat(result.id()).isEqualTo(666L);
+    assertThat(result.name()).isEqualTo("Ninth");
   }
 
   @Test
@@ -85,6 +91,44 @@ public class DefaultQualityGateClientTest {
     assertThat(result).isNotNull();
   }
 
+  @Test
+  public void should_show_qualitygate_conditions() {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+    httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\",\"conditions\":["
+      + "{\"id\":6,\"metric\":\"blocker_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+      + "{\"id\":7,\"metric\":\"critical_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+      + "{\"id\":10,\"metric\":\"test_errors\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+      + "{\"id\":11,\"metric\":\"test_failures\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
+      + "{\"id\":12,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"\",\"error\":\"80%\",\"period\":3},"
+      + "{\"id\":13,\"metric\":\"open_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
+      + "{\"id\":14,\"metric\":\"reopened_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
+      + "{\"id\":15,\"metric\":\"skipped_tests\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"}"
+      + "]}");
+
+    QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+
+    Collection<QualityGateCondition> conditions = client.conditions(5L);
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?id=5");
+
+    assertThat(conditions).hasSize(8);
+    Iterator<QualityGateCondition> condIterator = conditions.iterator();
+    QualityGateCondition first = condIterator.next();
+    assertThat(first.id()).isEqualTo(6L);
+    QualityGateCondition second = condIterator.next();
+    assertThat(second.period()).isNull();
+    QualityGateCondition third = condIterator.next();
+    assertThat(third.metricKey()).isEqualTo("test_errors");
+    QualityGateCondition fourth = condIterator.next();
+    assertThat(fourth.operator()).isEqualTo("GT");
+    QualityGateCondition fifth = condIterator.next();
+    assertThat(fifth.errorThreshold()).isEqualTo("80%");
+    assertThat(fifth.period()).isEqualTo(3);
+    QualityGateCondition sixth = condIterator.next();
+    assertThat(sixth.warningThreshold()).isEqualTo("0");
+  }
+
   @Test
   public void should_destroy_qualitygate() {
     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());