From: Jean-Baptiste Lievremont Date: Fri, 7 Mar 2014 15:38:53 +0000 (+0100) Subject: SONAR-4366 Fix NPE when quality gate is empty (no condition) X-Git-Tag: 4.3~503 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f715a2cc8f6702b5f4fc226bd95162df8c9bf58a;p=sonarqube.git SONAR-4366 Fix NPE when quality gate is empty (no condition) --- 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 index 601acbaf2eb..ff24c155604 100644 --- 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 @@ -113,8 +113,10 @@ public class DefaultQualityGateClient implements QualityGateClient { Map jsonRoot = (Map) JSONValue.parse(json); Collection conditionArray = (Collection) jsonRoot.get("conditions"); Collection conditions = new ArrayList(); - for (Map conditionJson: conditionArray) { - conditions.add(new DefaultQualityGateCondition(conditionJson)); + if (conditionArray != null) { + for (Map conditionJson: conditionArray) { + conditions.add(new DefaultQualityGateCondition(conditionJson)); + } } return conditions; } 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 a2628931f74..7bb2f309a26 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 @@ -168,6 +168,22 @@ public class DefaultQualityGateClientTest { assertThat(sixth.warningThreshold()).isEqualTo("0"); } + @Test + public void should_show_empty_qualitygate_by_id() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\"}"); + + QualityGateClient client = new DefaultQualityGateClient(requestFactory); + + QualityGateDetails qGate = client.show(5L); + assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?id=5"); + assertThat(qGate.id()).isEqualTo(5L); + assertThat(qGate.name()).isEqualTo("Sonar way"); + + Collection conditions = qGate.conditions(); + assertThat(conditions).isEmpty(); + } @Test public void should_destroy_qualitygate() {