From: Julien Lancelot Date: Fri, 25 Apr 2014 13:51:44 +0000 (+0200) Subject: SONAR-4764 Add Java WS Client to restore default profiles X-Git-Tag: 4.4-RC1~1381^2~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=25a221af79dabe8cdff5dc07080f69eb1c580c0e;p=sonarqube.git SONAR-4764 Add Java WS Client to restore default profiles --- diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index ffbf75fbfe3..3fa124b3aae 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -1581,6 +1581,7 @@ quality_profiles.create_x_language_profile=Create {0} Profile quality_profiles.are_you_sure_want_x_profile_as_default=Are you sure that you want to set the profile "{0}" as default? quality_profiles.profile_x_created=Profile "{0}" created. Set it as default or link it to a project to use it for next measures. quality_profiles.already_exists=This profile already exists. +quality_profiles.profile_x_already_exists=Profile "{0}" already exists. quality_profiles.please_type_profile_name=Please type a profile name. quality_profiles.profile_x_deleted=Profile "{0}" is deleted. quality_profiles.default_profile_is_x=Default profile is "{0}". diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java index 73fda7d3744..2dfd845e758 100644 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java +++ b/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java @@ -22,16 +22,13 @@ package org.sonar.server.exceptions; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.Collection; -import java.util.Collections; - -import static com.google.common.collect.Lists.newArrayList; +import java.util.Arrays; public class ServerException extends RuntimeException { private final int httpCode; private final String l10nKey; - private final Collection l10nParams; + private final Object[] l10nParams; public ServerException(int httpCode) { this.httpCode = httpCode; @@ -50,7 +47,7 @@ public class ServerException extends RuntimeException { super(message); this.httpCode = httpCode; this.l10nKey = l10nKey; - this.l10nParams = l10nParams == null ? Collections.emptyList() : Collections.unmodifiableCollection(newArrayList(l10nParams)); + this.l10nParams = l10nParams; } public int httpCode() { @@ -63,7 +60,11 @@ public class ServerException extends RuntimeException { } @CheckForNull - public Collection l10nParams() { - return l10nParams; + public Object[] l10nParams() { + if (l10nParams == null) { + return new Object[0]; + } else { + return Arrays.copyOf(l10nParams, l10nParams.length); + } } } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index b053b7349df..d613f63e365 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -104,6 +104,8 @@ import org.sonar.server.qualitygate.RegisterQualityGates; import org.sonar.server.qualitygate.ws.QgateAppHandler; import org.sonar.server.qualitygate.ws.QualityGatesWs; import org.sonar.server.qualityprofile.*; +import org.sonar.server.qualityprofile.ws.QProfileBackupWsHandler; +import org.sonar.server.qualityprofile.ws.QProfilesWs; import org.sonar.server.rule.*; import org.sonar.server.rule.ws.*; import org.sonar.server.source.CodeColorizers; @@ -251,6 +253,8 @@ class ServerComponents { pico.addSingleton(QProfileBackup.class); pico.addSingleton(QProfileRepositoryExporter.class); pico.addSingleton(ESActiveRule.class); + pico.addSingleton(QProfileBackupWsHandler.class); + pico.addSingleton(QProfilesWs.class); // rule pico.addSingleton(AnnotationRuleParser.class); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java index 114664637b1..8a3d850d7ab 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java @@ -238,7 +238,7 @@ public class QProfileOperations implements ServerComponent { private void checkNotAlreadyExists(String name, String language, SqlSession session) { if (dao.selectByNameAndLanguage(name, language, session) != null) { - throw BadRequestException.ofL10n("quality_profiles.already_exists"); + throw BadRequestException.ofL10n("quality_profiles.profile_x_already_exists", name); } } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java index f269509eafb..cb2ad27e558 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java @@ -33,7 +33,7 @@ public class QProfilesWs implements WebService { @Override public void define(Context context) { NewController controller = context.createController("api/qprofiles") - .setDescription("Quality profiles"); + .setDescription("Quality profiles management"); controller.createAction("restore_default") .setDescription("Restore default profiles") diff --git a/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java b/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java index 8d6bcd97f7a..1a16244bcc8 100644 --- a/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java +++ b/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java @@ -19,7 +19,6 @@ */ package org.sonar.server.ws; -import org.apache.commons.lang.StringUtils; import org.elasticsearch.common.collect.Lists; import org.picocontainer.Startable; import org.slf4j.LoggerFactory; @@ -33,6 +32,8 @@ import org.sonar.server.exceptions.BadRequestException.Message; import org.sonar.server.exceptions.ServerException; import org.sonar.server.plugins.MimeTypes; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import javax.servlet.http.HttpServletResponse; import java.io.OutputStreamWriter; @@ -87,11 +88,7 @@ public class WebServiceEngine implements ServerComponent, Startable { // TODO replace by BadRequestException in Request#mandatoryParam() sendError(400, e.getMessage(), response); } catch (BadRequestException e) { - if (StringUtils.isBlank(e.getMessage())) { - sendError(e, response); - } else { - sendError(e.httpCode(), e.getMessage(), response); - } + sendError(e, response); } catch (ServerException e) { // TODO support ServerException l10n messages sendError(e.httpCode(), e.getMessage(), response); @@ -124,12 +121,24 @@ public class WebServiceEngine implements ServerComponent, Startable { private void sendError(BadRequestException e, ServletResponse response) { Collection messages = Lists.newArrayList(); - for (Message message: e.errors()) { - messages.add(i18n.message(Locale.getDefault(), message.l10nKey(), message.text(), message.l10nParams())); + if (e.getMessage()!= null || e.l10nKey() != null) { + messages.add(message(e.getMessage(), e.l10nKey(), e.l10nParams())); + } + for (Message message : e.errors()) { + messages.add(message(message.text(), message.l10nKey(), message.l10nParams())); } sendErrors(response, e.httpCode(), messages.toArray(new String[0])); } + @CheckForNull + private String message(@Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams){ + if (l10nKey != null) { + return i18n.message(Locale.getDefault(), l10nKey, message, l10nParams); + } else { + return message; + } + } + private void sendError(int status, String message, ServletResponse response) { sendErrors(response, status, message); } @@ -144,7 +153,7 @@ public class WebServiceEngine implements ServerComponent, Startable { try { json.beginObject(); json.name("errors").beginArray(); - for (String message: errors) { + for (String message : errors) { json.beginObject().prop("msg", message).endObject(); } json.endArray(); diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelOperationsTest.java index 8b871dce3f2..81bc7b2ef84 100644 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelOperationsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelOperationsTest.java @@ -170,7 +170,7 @@ public class DebtModelOperationsTest { fail(); } catch (BadRequestException e) { assertThat(e.l10nKey()).isEqualTo("errors.is_already_used"); - assertThat(e.l10nParams().iterator().next()).isEqualTo("Compilation"); + assertThat(e.l10nParams()[0]).isEqualTo("Compilation"); } } diff --git a/sonar-server/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java b/sonar-server/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java index eaaadfcc6d6..8c07873d21d 100644 --- a/sonar-server/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java +++ b/sonar-server/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java @@ -57,7 +57,7 @@ public class BooleanTypeValidationTest { } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); BadRequestException badRequestException = (BadRequestException) e; - assertThat(badRequestException.l10nParams().toArray()[0]).isEqualTo("abc"); + assertThat(badRequestException.l10nParams()[0]).isEqualTo("abc"); } } diff --git a/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java b/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java index be5de36100e..a1a11664b0f 100644 --- a/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java +++ b/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java @@ -55,7 +55,7 @@ public class FloatTypeValidationTest { } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); BadRequestException badRequestException = (BadRequestException) e; - assertThat(badRequestException.l10nParams().toArray()[0]).isEqualTo("abc"); + assertThat(badRequestException.l10nParams()[0]).isEqualTo("abc"); } } diff --git a/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java b/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java index 3bb530d6ccc..9db9ed73e23 100644 --- a/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java +++ b/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java @@ -54,7 +54,7 @@ public class IntegerTypeValidationTest { } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); BadRequestException badRequestException = (BadRequestException) e; - assertThat(badRequestException.l10nParams().toArray()[0]).isEqualTo("abc"); + assertThat(badRequestException.l10nParams()[0]).isEqualTo("abc"); } } diff --git a/sonar-server/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java b/sonar-server/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java index 6ebfba436c3..0c50e23d9b9 100644 --- a/sonar-server/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java +++ b/sonar-server/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java @@ -55,8 +55,8 @@ public class StringListTypeValidationTest { } catch (Exception e) { assertThat(e).isInstanceOf(BadRequestException.class); BadRequestException badRequestException = (BadRequestException) e; - assertThat(badRequestException.l10nParams().toArray()[0]).isEqualTo("abc"); - assertThat(badRequestException.l10nParams().toArray()[1]).isEqualTo("a, b, c"); + assertThat(badRequestException.l10nParams()[0]).isEqualTo("abc"); + assertThat(badRequestException.l10nParams()[1]).isEqualTo("a, b, c"); } } diff --git a/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java b/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java index f0d61af96c1..e65e6d0fc47 100644 --- a/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java +++ b/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java @@ -42,6 +42,8 @@ import java.util.Locale; import java.util.Map; import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -194,15 +196,46 @@ public class WebServiceEngineTest { assertThat(response.stream().mediaType()).isEqualTo(MimeTypes.JSON); } + @Test + public void bad_request_with_i18n_message() throws Exception { + InternalRequest request = new SimpleRequest().setParam("count", "3"); + ServletResponse response = new ServletResponse(); + when(i18n.message(eq(Locale.getDefault()), eq("bad.request.reason"), anyString(), eq(0))).thenReturn("Bad request reason #0"); + + engine.execute(request, response, "api/system", "fail_with_i18n_message"); + + assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[" + + "{\"msg\":\"Bad request reason #0\"}" + + "]}"); + assertThat(response.stream().httpStatus()).isEqualTo(400); + assertThat(response.stream().mediaType()).isEqualTo(MimeTypes.JSON); + } + @Test public void bad_request_with_multiple_messages() throws Exception { InternalRequest request = new SimpleRequest().setParam("count", "3"); ServletResponse response = new ServletResponse(); + + engine.execute(request, response, "api/system", "fail_with_multiple_messages"); + + assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[" + + "{\"msg\":\"Bad request reason #0\"}," + + "{\"msg\":\"Bad request reason #1\"}," + + "{\"msg\":\"Bad request reason #2\"}" + + "]}"); + assertThat(response.stream().httpStatus()).isEqualTo(400); + assertThat(response.stream().mediaType()).isEqualTo(MimeTypes.JSON); + } + + @Test + public void bad_request_with_multiple_i18n_messages() throws Exception { + InternalRequest request = new SimpleRequest().setParam("count", "3"); + ServletResponse response = new ServletResponse(); when(i18n.message(Locale.getDefault(), "bad.request.reason", null, 0)).thenReturn("Bad request reason #0"); when(i18n.message(Locale.getDefault(), "bad.request.reason", null, 1)).thenReturn("Bad request reason #1"); when(i18n.message(Locale.getDefault(), "bad.request.reason", null, 2)).thenReturn("Bad request reason #2"); - engine.execute(request, response, "api/system", "fail"); + engine.execute(request, response, "api/system", "fail_with_multiple_i18n_messages"); assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[" + "{\"msg\":\"Bad request reason #0\"}," @@ -244,23 +277,45 @@ public class WebServiceEngineTest { .setHandler(new RequestHandler() { @Override public void handle(Request request, Response response) { - if (request.param("count") != null) { - List errors = Lists.newArrayList(); - for (int count=0; count < Integer.valueOf(request.param("count")); count ++) { - errors.add(Message.ofL10n("bad.request.reason", count)); - } - throw BadRequestException.of(errors); - } throw new IllegalStateException("Unexpected"); } }); + newController.createAction("fail_with_i18n_message") + .setHandler(new RequestHandler() { + @Override + public void handle(Request request, Response response) { + throw BadRequestException.ofL10n("bad.request.reason", 0); + } + }); + newController.createAction("fail_with_multiple_messages") + .setHandler(new RequestHandler() { + @Override + public void handle(Request request, Response response) { + List errors = Lists.newArrayList(); + for (int count = 0; count < Integer.valueOf(request.param("count")); count++) { + errors.add(Message.of("Bad request reason #" + count)); + } + throw BadRequestException.of(errors); + } + }); + newController.createAction("fail_with_multiple_i18n_messages") + .setHandler(new RequestHandler() { + @Override + public void handle(Request request, Response response) { + List errors = Lists.newArrayList(); + for (int count = 0; count < Integer.valueOf(request.param("count")); count++) { + errors.add(Message.ofL10n("bad.request.reason", count)); + } + throw BadRequestException.of(errors); + } + }); newController.createAction("alive") - .setHandler(new RequestHandler() { - @Override - public void handle(Request request, Response response) { - response.noContent(); - } - }); + .setHandler(new RequestHandler() { + @Override + public void handle(Request request, Response response) { + response.noContent(); + } + }); // parameter "message" is required but not "author" newController.createAction("print") @@ -271,7 +326,7 @@ public class WebServiceEngineTest { public void handle(Request request, Response response) { try { IOUtils.write( - request.mandatoryParam("message") + " by " + request.param("author", "-"), response.stream().output()); + request.mandatoryParam("message") + " by " + request.param("author", "-"), response.stream().output()); } catch (IOException e) { throw new IllegalStateException(e); } 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 03e1c2fca23..7e06483dcd5 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,9 +19,6 @@ */ 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; @@ -31,6 +28,10 @@ import org.sonar.wsclient.permissions.PermissionClient; import org.sonar.wsclient.permissions.internal.DefaultPermissionClient; import org.sonar.wsclient.project.ProjectClient; import org.sonar.wsclient.project.internal.DefaultProjectClient; +import org.sonar.wsclient.qprofile.QProfileClient; +import org.sonar.wsclient.qprofile.internal.DefaultQProfileClient; +import org.sonar.wsclient.qualitygate.QualityGateClient; +import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient; import org.sonar.wsclient.rule.RuleClient; import org.sonar.wsclient.rule.RuleTagClient; import org.sonar.wsclient.rule.internal.DefaultRuleClient; @@ -131,6 +132,14 @@ public class SonarClient { return new DefaultQualityGateClient(requestFactory); } + /** + * New client to interact with web services related to quality profilesgates + */ + public QProfileClient qProfileClient() { + return new DefaultQProfileClient(requestFactory); + } + + public SystemClient systemClient() { return new DefaultSystemClient(requestFactory); } diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileClient.java new file mode 100644 index 00000000000..e26cd1449d7 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileClient.java @@ -0,0 +1,31 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.qprofile; + + +/** + * @since 4.4 + */ +public interface QProfileClient { + + QProfileResult restoreDefault(String language); + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileResult.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileResult.java new file mode 100644 index 00000000000..9734e4ce16c --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileResult.java @@ -0,0 +1,30 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.qprofile; + +import java.util.List; + +public interface QProfileResult { + + List infos(); + + List warnings(); +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClient.java new file mode 100644 index 00000000000..f0ce08cd502 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClient.java @@ -0,0 +1,46 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.qprofile.internal; + +import org.json.simple.JSONValue; +import org.sonar.wsclient.internal.HttpRequestFactory; +import org.sonar.wsclient.qprofile.QProfileClient; +import org.sonar.wsclient.qprofile.QProfileResult; + +import java.util.Collections; +import java.util.Map; + +public class DefaultQProfileClient implements QProfileClient { + + private final HttpRequestFactory requestFactory; + + public DefaultQProfileClient(HttpRequestFactory requestFactory) { + this.requestFactory = requestFactory; + } + + @Override + public QProfileResult restoreDefault(String language) { + String json = requestFactory.post("/api/qprofiles/restore_default", Collections.singletonMap("language", (Object) language)); + Map jsonRoot = (Map) JSONValue.parse(json); + return new DefaultQProfileResult(jsonRoot); + } + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileResult.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileResult.java new file mode 100644 index 00000000000..ca9e6237e06 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileResult.java @@ -0,0 +1,61 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.qprofile.internal; + +import org.sonar.wsclient.qprofile.QProfileResult; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class DefaultQProfileResult implements QProfileResult { + + private Map json; + + DefaultQProfileResult(Map json) { + this.json = json; + } + + @Override + public List infos() { + List infos = new ArrayList(); + List jsonInfos = (List) json.get("infos"); + if (jsonInfos != null) { + for (String info : jsonInfos) { + infos.add(info); + } + } + return infos; + } + + @Override + public List warnings() { + List warnings = new ArrayList(); + List jsonWarnings = (List) json.get("warnings"); + if (jsonWarnings != null) { + for (String warning : jsonWarnings) { + warnings.add(warning); + } + } + return warnings; + } + +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java index b6efcf41440..1ca87ea7322 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java @@ -24,6 +24,10 @@ import org.sonar.wsclient.issue.internal.DefaultActionPlanClient; import org.sonar.wsclient.issue.internal.DefaultIssueClient; import org.sonar.wsclient.permissions.internal.DefaultPermissionClient; import org.sonar.wsclient.project.internal.DefaultProjectClient; +import org.sonar.wsclient.qprofile.internal.DefaultQProfileClient; +import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient; +import org.sonar.wsclient.rule.internal.DefaultRuleClient; +import org.sonar.wsclient.rule.internal.DefaultRuleTagClient; import org.sonar.wsclient.system.internal.DefaultSystemClient; import org.sonar.wsclient.user.internal.DefaultUserClient; @@ -39,6 +43,10 @@ public class SonarClientTest { assertThat(client.userClient()).isNotNull().isInstanceOf(DefaultUserClient.class); assertThat(client.permissionClient()).isNotNull().isInstanceOf(DefaultPermissionClient.class); assertThat(client.projectClient()).isNotNull().isInstanceOf(DefaultProjectClient.class); + assertThat(client.ruleTagClient()).isNotNull().isInstanceOf(DefaultRuleTagClient.class); + assertThat(client.ruleClient()).isNotNull().isInstanceOf(DefaultRuleClient.class); + assertThat(client.qualityGateClient()).isNotNull().isInstanceOf(DefaultQualityGateClient.class); + assertThat(client.qProfileClient()).isNotNull().isInstanceOf(DefaultQProfileClient.class); assertThat(client.systemClient()).isNotNull().isInstanceOf(DefaultSystemClient.class); } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClientTest.java new file mode 100644 index 00000000000..cfd5bce80de --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClientTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.qprofile.internal; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.wsclient.MockHttpServerInterceptor; +import org.sonar.wsclient.internal.HttpRequestFactory; +import org.sonar.wsclient.qprofile.QProfileResult; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.MapAssert.entry; + +public class DefaultQProfileClientTest { + + @Rule + public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor(); + + @Test + public void restore_default_profiles() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + + httpServer.stubResponseBody("{\"infos\":[\"Some info\"],\"warnings\":[\"Some warnings\"]}"); + + DefaultQProfileClient client = new DefaultQProfileClient(requestFactory); + QProfileResult result = client.restoreDefault("java"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/qprofiles/restore_default"); + assertThat(httpServer.requestParams()).includes( + entry("language", "java") + ); + assertThat(result).isNotNull(); + assertThat(result.infos()).containsOnly("Some info"); + assertThat(result.warnings()).containsOnly("Some warnings"); + } + +}