]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 20 Mar 2014 10:30:15 +0000 (11:30 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 20 Mar 2014 10:30:20 +0000 (11:30 +0100)
sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java
sonar-batch/src/main/java/org/sonar/batch/qualitygate/ResolvedCondition.java
sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java
sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java
sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java

index e2e298f2fd1a9f6925b89ba8e62ac6a83813cb81..7294e4e668bf2ae27fa40c76f2384ae016ed7158 100644 (file)
@@ -36,12 +36,15 @@ import java.net.HttpURLConnection;
 
 public class QualityGateProvider extends ProviderAdapter {
 
+
   private static final Logger LOG = LoggerFactory.getLogger(QualityGateProvider.class);
 
   private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate";
 
   private static final String SHOW_URL = "/api/qualitygates/show";
 
+  private static final String ATTRIBUTE_CONDITIONS = "conditions";
+
   private QualityGate instance;
 
   public QualityGate provide(Settings settings, ServerClient client, MetricFinder metricFinder) {
@@ -93,8 +96,8 @@ public class QualityGateProvider extends ProviderAdapter {
 
     QualityGate configuredGate = new QualityGate(root.get("name").getAsString());
 
-    if (root.has("conditions")) {
-      for (JsonElement condition: root.get("conditions").getAsJsonArray()) {
+    if (root.has(ATTRIBUTE_CONDITIONS)) {
+      for (JsonElement condition: root.get(ATTRIBUTE_CONDITIONS).getAsJsonArray()) {
         JsonObject conditionObject = condition.getAsJsonObject();
         configuredGate.add(new ResolvedCondition(conditionObject, metricFinder.findByKey(conditionObject.get("metric").getAsString())));
       }
index 6f893419c3dcc86197832e0cba418ff0d56381c7..0e22e7285b3c756e3832652cd285459339b98597 100644 (file)
@@ -57,15 +57,18 @@ public class ResolvedCondition {
     return json.get("op").getAsString();
   }
 
-  public @CheckForNull String warningThreshold() {
+  @CheckForNull
+  public String warningThreshold() {
     return json.has(ATTRIBUTE_WARNING) ? json.get(ATTRIBUTE_WARNING).getAsString() : null;
   }
 
-  public @CheckForNull String errorThreshold() {
+  @CheckForNull
+  public String errorThreshold() {
     return json.has(ATTRIBUTE_ERROR) ? json.get(ATTRIBUTE_ERROR).getAsString() : null;
   }
 
-  public @CheckForNull Integer period() {
+  @CheckForNull
+  public Integer period() {
     return json.has(ATTRIBUTE_PERIOD) ? json.get(ATTRIBUTE_PERIOD).getAsInt() : null;
   }
 }
index 937a2ab4e53657e908ab5eee101efe4a0e87a8e9..0c7fee0c56c55613913019417f9c37135701a21a 100644 (file)
@@ -73,6 +73,8 @@ public class QualityGatesWs implements WebService {
 
     defineConditionActions(controller);
 
+    defineProjectAssociationActions(controller);
+
     controller.createAction("app")
       .setInternal(true)
       .setDescription("Get initialization items for the admin UI. For internal use.")
@@ -223,7 +225,9 @@ public class QualityGatesWs implements WebService {
     search.createParam(PARAM_QUERY).setDescription("Optionally, part of the name of the projects to search for.");
     search.createParam(PARAM_PAGE);
     search.createParam(PARAM_PAGE_SIZE);
+  }
 
+  private void defineProjectAssociationActions(NewController controller) {
     NewAction select = controller.createAction("select")
       .setPost(true)
       .setHandler(new RequestHandler() {
@@ -283,16 +287,10 @@ public class QualityGatesWs implements WebService {
   protected void show(Request request, Response response) {
     Long qGateId = request.paramAsLong(PARAM_ID);
     String qGateName = request.param(PARAM_NAME);
-    if (qGateId == null && qGateName == null) {
-      throw new BadRequestException("Either one of 'id' or 'name' is required.");
-    } else if (qGateId != null && qGateName != null) {
-      throw new BadRequestException("Only one of 'id' or 'name' must be provided.");
-    }
+    checkOneOfIdOrNamePresent(qGateId, qGateName);
 
     QualityGateDto qGate = qGateId == null ? qualityGates.get(qGateName) : qualityGates.get(qGateId);
-    if (qGateId == null) {
-      qGateId = qGate.getId();
-    }
+    qGateId = qGate.getId();
 
     JsonWriter writer = response.newJsonWriter().beginObject()
       .prop(PARAM_ID, qGate.getId())
@@ -308,6 +306,14 @@ public class QualityGatesWs implements WebService {
     writer.endObject().close();
   }
 
+  private void checkOneOfIdOrNamePresent(Long qGateId, String qGateName) {
+    if (qGateId == null && qGateName == null) {
+      throw new BadRequestException("Either one of 'id' or 'name' is required.");
+    } else if (qGateId != null && qGateName != null) {
+      throw new BadRequestException("Only one of 'id' or 'name' must be provided.");
+    }
+  }
+
   protected void createCondition(Request request, Response response) {
     writeQualityGateCondition(
       qualityGates.createCondition(
index b1897fed8139301deb46108e781c3fa2b8d6f25d..e29c5123a3758c4fb7efbdb86e0a660e1f8d1cd0 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.wsclient.qualitygate.internal;
 
+import org.json.simple.JSONArray;
+
 import org.sonar.wsclient.unmarshallers.JsonUtils;
 import org.sonar.wsclient.qualitygate.QualityGate;
 
@@ -37,9 +39,12 @@ public class DefaultQualityGates implements QualityGates {
   @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);
+    JSONArray gatesJson = JsonUtils.getArray(json, "qualitygates");
+    if (gatesJson != null) {
+      for (Object entry: gatesJson) {
+        QualityGate qGate = new DefaultQualityGate((Map<String, String>) entry);
+        qualityGates.put(qGate.id(), qGate);
+      }
     }
     defaultId = JsonUtils.getLong(json, "default");
   }
index ddf9c29e9d47c463ef448337e5d3d22ac4231a85..f6cb803a6a90945869d9ae48f2e8bc76d647dfcf 100644 (file)
@@ -60,7 +60,7 @@ public class DefaultQualityGateClientTest {
     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
 
     httpServer.stubResponseBody(
-        "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}]}");
+        "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}],\"default\":42}");
 
     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
     QualityGates qGates = client.list();
@@ -68,6 +68,22 @@ public class DefaultQualityGateClientTest {
     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list");
     assertThat(httpServer.requestParams()).isEmpty();
     assertThat(qGates.qualityGates()).hasSize(3);
+    assertThat(qGates.defaultGate().id()).isEqualTo(42L);
+  }
+
+  @Test
+  public void should_list_qualitygates_empty() {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+
+    httpServer.stubResponseBody(
+        "{}");
+
+    QualityGateClient client = new DefaultQualityGateClient(requestFactory);
+    QualityGates qGates = client.list();
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list");
+    assertThat(httpServer.requestParams()).isEmpty();
+    assertThat(qGates.qualityGates()).isEmpty();
     assertThat(qGates.defaultGate()).isNull();
   }