From 156baa1485e4f01052e81bd00837df61cbf24129 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Wed, 19 Feb 2014 18:46:04 +0100 Subject: [PATCH] SONAR-4366 Add WS client classes for quality gate CRUD services --- .../server/qualitygate/QualityGates.java | 11 +- .../java/org/sonar/wsclient/SonarClient.java | 10 ++ .../wsclient/qualitygate/QualityGate.java | 30 ++++ .../qualitygate/QualityGateClient.java | 38 +++++ .../wsclient/qualitygate/QualityGates.java | 32 +++++ .../internal/DefaultQualityGate.java | 46 +++++++ .../internal/DefaultQualityGateClient.java | 96 +++++++++++++ .../internal/DefaultQualityGates.java | 57 ++++++++ .../DefaultQualityGateClientTest.java | 130 ++++++++++++++++++ 9 files changed, 445 insertions(+), 5 deletions(-) create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java diff --git a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java index 80ad46b1f02..d59782c77cf 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java +++ b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java @@ -19,15 +19,15 @@ */ package org.sonar.server.qualitygate; -import org.apache.commons.lang.StringUtils; -import org.sonar.core.properties.PropertyDto; -import org.sonar.core.properties.PropertiesDao; -import org.sonar.server.exceptions.NotFoundException; import com.google.common.base.Strings; +import org.apache.commons.lang.StringUtils; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; import org.sonar.core.qualitygate.db.QualityGateDao; import org.sonar.core.qualitygate.db.QualityGateDto; import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; import org.sonar.server.util.Validation; @@ -87,6 +87,7 @@ public class QualityGates { } public void setDefault(@Nullable Long idToUseAsDefault) { + checkPermission(UserSession.get()); if (idToUseAsDefault == null) { propertiesDao.deleteGlobalProperty(SONAR_QUALITYGATE_PROPERTY); } else { @@ -121,7 +122,7 @@ public class QualityGates { private QualityGateDto getNonNull(long id) { QualityGateDto qGate = dao.selectById(id); if (qGate == null) { - throw new NotFoundException(); + throw new NotFoundException("There is no quality gate with id=" + id); } return qGate; } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java index 99341392df5..6e78bcb974c 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java @@ -19,6 +19,9 @@ */ package org.sonar.wsclient; +import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient; + +import org.sonar.wsclient.qualitygate.QualityGateClient; import org.sonar.wsclient.internal.HttpRequestFactory; import org.sonar.wsclient.issue.ActionPlanClient; import org.sonar.wsclient.issue.IssueClient; @@ -121,6 +124,13 @@ public class SonarClient { return new DefaultRuleClient(requestFactory); } + /** + * New client to interact with web services related to quality gates + */ + public QualityGateClient qualityGateClient() { + return new DefaultQualityGateClient(requestFactory); + } + public SystemClient systemClient() { return new DefaultSystemClient(requestFactory); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java new file mode 100644 index 00000000000..3a2279e7467 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGate.java @@ -0,0 +1,30 @@ +/* + * 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 QualityGate { + + long id(); + + String name(); +} 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 new file mode 100644 index 00000000000..17a1fe99410 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGateClient.java @@ -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 QualityGateClient { + + QualityGates list(); + + QualityGate create(String qGateName); + + QualityGate rename(long qGateId, String qGateName); + + void destroy(long qGateId); + + void setDefault(long qGateId); + + void unsetDefault(); +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java new file mode 100644 index 00000000000..3a3947b9622 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/QualityGates.java @@ -0,0 +1,32 @@ +/* + * 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 java.util.Collection; + +/** + * @since 4.3 + */ +public interface QualityGates { + + Collection qualityGates(); + + QualityGate defaultGate(); +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java new file mode 100644 index 00000000000..0d86d899206 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGate.java @@ -0,0 +1,46 @@ +/* + * 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.unmarshallers.JsonUtils; + +import java.util.Map; + +import org.sonar.wsclient.qualitygate.QualityGate; + +public class DefaultQualityGate implements QualityGate { + + private Map json; + + DefaultQualityGate(Map json) { + this.json = json; + } + + @Override + public long id() { + return JsonUtils.getLong(json, "id"); + } + + @Override + public String name() { + return JsonUtils.getString(json, "name"); + } + +} 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 new file mode 100644 index 00000000000..ac920a88c5e --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClient.java @@ -0,0 +1,96 @@ +/* + * 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.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.QualityGates; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +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 CREATE_URL = ROOT_URL + "/create"; + 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"; + private static final String UNSET_DEFAULT_URL = ROOT_URL + "/unset_default"; + + private final HttpRequestFactory requestFactory; + + public DefaultQualityGateClient(HttpRequestFactory requestFactory) { + this.requestFactory = requestFactory; + } + + @Override + public QualityGates list() { + String json = requestFactory.get(LIST_URL, Collections. emptyMap()); + return jsonToQualityGates(json); + } + + @Override + public QualityGate create(String qGateName) { + String json = requestFactory.post(CREATE_URL, Collections.singletonMap("name", (Object) qGateName)); + return jsonToQualityGate(json); + } + + @Override + public QualityGate rename(long qGateId, String qGateName) { + Map params = new HashMap(); + params.put("id", qGateId); + params.put("name", qGateName); + String json = requestFactory.post(RENAME_URL, params); + return jsonToQualityGate(json); + } + + @Override + public void destroy(long qGateId) { + requestFactory.post(DESTROY_URL, Collections.singletonMap("id", (Object) qGateId)); + } + + @Override + public void setDefault(long qGateId) { + requestFactory.post(SET_DEFAULT_URL, Collections.singletonMap("id", (Object) qGateId)); + } + + @Override + public void unsetDefault() { + requestFactory.post(UNSET_DEFAULT_URL, Collections. emptyMap()); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + private QualityGate jsonToQualityGate(String json) { + Map jsonRoot = (Map) JSONValue.parse(json); + return new DefaultQualityGate((Map) jsonRoot); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + private QualityGates jsonToQualityGates(String json) { + Map jsonRoot = (Map) JSONValue.parse(json); + return new DefaultQualityGates((Map) jsonRoot); + } + +} 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 new file mode 100644 index 00000000000..23f6a031f33 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGates.java @@ -0,0 +1,57 @@ +/* + * 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.unmarshallers.JsonUtils; +import org.sonar.wsclient.qualitygate.QualityGate; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.sonar.wsclient.qualitygate.QualityGates; + +public class DefaultQualityGates implements QualityGates { + + private Map qualityGates; + + private Long defaultId; + + @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); + } + defaultId = JsonUtils.getLong(json, "default"); + } + + @Override + public Collection qualityGates() { + return qualityGates.values(); + } + + @Override + public QualityGate defaultGate() { + return qualityGates.get(defaultId); + } + +} 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 new file mode 100644 index 00000000000..45c1ddac63b --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/qualitygate/internal/DefaultQualityGateClientTest.java @@ -0,0 +1,130 @@ +/* + * 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 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 static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.MapAssert.entry; + +public class DefaultQualityGateClientTest { + + @Rule + public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor(); + + @Test + public void should_create_qualitygate() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}"); + + QualityGateClient client = new DefaultQualityGateClient(requestFactory); + QualityGate result = client.create("Ninth"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create"); + assertThat(httpServer.requestParams()).includes( + entry("name", "Ninth") + ); + assertThat(result).isNotNull(); + } + + @Test + public void should_list_qualitygates() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubResponseBody( + "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}]}"); + + QualityGateClient client = new DefaultQualityGateClient(requestFactory); + QualityGates qGates = client.list(); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list"); + assertThat(httpServer.requestParams()).isEmpty(); + assertThat(qGates.qualityGates()).hasSize(3); + assertThat(qGates.defaultGate()).isNull(); + } + + @Test + public void should_rename_qualitygate() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}"); + + QualityGateClient client = new DefaultQualityGateClient(requestFactory); + QualityGate result = client.rename(666L, "Hell"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/rename"); + assertThat(httpServer.requestParams()).includes( + entry("id", "666"), + entry("name", "Hell") + ); + assertThat(result).isNotNull(); + } + + @Test + public void should_destroy_qualitygate() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT); + + QualityGateClient client = new DefaultQualityGateClient(requestFactory); + client.destroy(666L); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/destroy"); + assertThat(httpServer.requestParams()).includes( + entry("id", "666") + ); + } + + @Test + public void should_set_default_qualitygate() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT); + + QualityGateClient client = new DefaultQualityGateClient(requestFactory); + client.setDefault(666L); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/set_as_default"); + assertThat(httpServer.requestParams()).includes( + entry("id", "666") + ); + } + + @Test + public void should_unset_default_qualitygate() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT); + + QualityGateClient client = new DefaultQualityGateClient(requestFactory); + client.unsetDefault(); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/unset_default"); + assertThat(httpServer.requestParams()).isEmpty(); + } +} -- 2.39.5