]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4764 Add Java WS Client to restore default profiles
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 25 Apr 2014 13:51:44 +0000 (15:51 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 25 Apr 2014 14:03:35 +0000 (16:03 +0200)
19 files changed:
sonar-core/src/main/resources/org/sonar/l10n/core.properties
sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java
sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java
sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelOperationsTest.java
sonar-server/src/test/java/org/sonar/server/util/BooleanTypeValidationTest.java
sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java
sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java
sonar-server/src/test/java/org/sonar/server/util/StringListTypeValidationTest.java
sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java
sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/QProfileResult.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileResult.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/qprofile/internal/DefaultQProfileClientTest.java [new file with mode: 0644]

index ffbf75fbfe3092da667971788250758f5cb2fafb..3fa124b3aaed9ab8a55b79cade96737919721dec 100644 (file)
@@ -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}".
index 73fda7d37448b14ec213a3e6615132d4a7c6e37e..2dfd845e75872293fd122742d8464c485ae8adb7 100644 (file)
@@ -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<Object> 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<Object> l10nParams() {
-    return l10nParams;
+  public Object[] l10nParams() {
+    if (l10nParams == null) {
+      return new Object[0];
+    } else {
+      return Arrays.copyOf(l10nParams, l10nParams.length);
+    }
   }
 }
index b053b7349dfd4d7e2fe88c47b1a72b54d7618bdf..d613f63e365431fc648951294a5a60fb7d73a534 100644 (file)
@@ -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);
index 114664637b1757eaeee762a63bde28597408512f..8a3d850d7ab0b71113d3eae1acf1f9419edff9f1 100644 (file)
@@ -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);
     }
   }
 
index f269509eafb3c646cf4dcaba674011cb955dc21c..cb2ad27e558babf90dc0b79fd8c11790504ef252 100644 (file)
@@ -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")
index 8d6bcd97f7a11153686aef32522d2f64befcce44..1a16244bcc8beb25a74d0ff79fc9a74e6babd5e1 100644 (file)
@@ -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<String> 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();
index 8b871dce3f29c8d275aab1cf79a657943fcfbdc9..81bc7b2ef84b5ed213d4598503d151d8b44c33a9 100644 (file)
@@ -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");
     }
   }
 
index eaaadfcc6d63e4f05785de576b079c184071d495..8c07873d21de6c9840079b6267548312f388476e 100644 (file)
@@ -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");
     }
   }
 
index be5de36100ec52468a5ed09e8afbf4bc69270db7..a1a11664b0ff0ade829c05ed66adcbf52ed14028 100644 (file)
@@ -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");
     }
   }
 
index 3bb530d6cccf1f05bb55b79c176d10e2fffa66f8..9db9ed73e23abf6ccfc86a009688f8470e81d0db 100644 (file)
@@ -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");
     }
   }
 
index 6ebfba436c34a3b5d1e04560a2431f396a49d75e..0c50e23d9b9f70f35e2cf5f42470c9ff4b352264 100644 (file)
@@ -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");
     }
   }
 
index f0d61af96c19dbd53570f16e2838514c227de66f..e65e6d0fc479ea5c2751c641dcbadbf9768b6234 100644 (file)
@@ -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<BadRequestException.Message> 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<BadRequestException.Message> 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<BadRequestException.Message> 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);
             }
index 03e1c2fca23550b029b04cd321763625f1d44e31..7e06483dcd548789e796998af962d88946ea3637 100644 (file)
@@ -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 (file)
index 0000000..e26cd14
--- /dev/null
@@ -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 (file)
index 0000000..9734e4c
--- /dev/null
@@ -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<String> infos();
+
+  List<String> 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 (file)
index 0000000..f0ce08c
--- /dev/null
@@ -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 (file)
index 0000000..ca9e623
--- /dev/null
@@ -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<String> infos() {
+    List<String> infos = new ArrayList<String>();
+    List<String> jsonInfos = (List<String>) json.get("infos");
+    if (jsonInfos != null) {
+      for (String info : jsonInfos) {
+        infos.add(info);
+      }
+    }
+    return infos;
+  }
+
+  @Override
+  public List<String> warnings() {
+    List<String> warnings = new ArrayList<String>();
+    List<String> jsonWarnings = (List<String>) json.get("warnings");
+    if (jsonWarnings != null) {
+      for (String warning : jsonWarnings) {
+        warnings.add(warning);
+      }
+    }
+    return warnings;
+  }
+
+}
index b6efcf414406abc00acf1af7f2da74aac870b9c5..1ca87ea7322b2ff8df4531153187e74559f69189 100644 (file)
@@ -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 (file)
index 0000000..cfd5bce
--- /dev/null
@@ -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");
+  }
+
+}