diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-03-10 13:01:29 +0100 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2014-03-10 16:51:26 +0100 |
commit | a79bee53e7309fece4a5e4952e11ee74e1d45f96 (patch) | |
tree | 42fcf90aaac54dd0a3440c49eeeaaaa7ce7d5e32 /sonar-ws-client | |
parent | 534ae0bdc1a96568732fab46f358ba1c299fc8be (diff) | |
download | sonarqube-a79bee53e7309fece4a5e4952e11ee74e1d45f96.tar.gz sonarqube-a79bee53e7309fece4a5e4952e11ee74e1d45f96.zip |
SONAR-5094 Add create/update/delete condition capabilities to WS client
Diffstat (limited to 'sonar-ws-client')
5 files changed, 237 insertions, 1 deletions
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 index 00000000000..0d2b42d1f38 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/NewCondition.java @@ -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; + } +} + diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java index 073cae9a868..fd499276dd2 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java @@ -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 index 00000000000..c4832f2158a --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/UpdateCondition.java @@ -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; + } +} + 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 ff24c155604..032a3fbf5c0 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 @@ -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"; @@ -76,6 +79,23 @@ public class DefaultQualityGateClient implements QualityGateClient { } @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)); + } } 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 7bb2f309a26..f5f7595976b 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 @@ -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") + ); + } } |