From: Jean-Baptiste Lievremont Date: Thu, 20 Mar 2014 10:30:15 +0000 (+0100) Subject: Fix quality flaws X-Git-Tag: 4.3~344 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dd2e1401e5c74ab50a7432964995de87e100d95f;p=sonarqube.git Fix quality flaws --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java b/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java index e2e298f2fd1..7294e4e668b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java @@ -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()))); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/qualitygate/ResolvedCondition.java b/sonar-batch/src/main/java/org/sonar/batch/qualitygate/ResolvedCondition.java index 6f893419c3d..0e22e7285b3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/qualitygate/ResolvedCondition.java +++ b/sonar-batch/src/main/java/org/sonar/batch/qualitygate/ResolvedCondition.java @@ -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; } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java b/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java index 937a2ab4e53..0c7fee0c56c 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java +++ b/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java @@ -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( 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 index b1897fed813..e29c5123a37 100644 --- 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 @@ -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 json) { qualityGates = new LinkedHashMap(); - for (Object entry: JsonUtils.getArray(json, "qualitygates")) { - QualityGate qGate = new DefaultQualityGate((Map) entry); - qualityGates.put(qGate.id(), qGate); + JSONArray gatesJson = JsonUtils.getArray(json, "qualitygates"); + if (gatesJson != null) { + for (Object entry: gatesJson) { + QualityGate qGate = new DefaultQualityGate((Map) entry); + qualityGates.put(qGate.id(), qGate); + } } defaultId = JsonUtils.getLong(json, "default"); } 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 index ddf9c29e9d4..f6cb803a6a9 100644 --- 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 @@ -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(); }