]> source.dussan.org Git - sonarqube.git/commitdiff
Add skeleton of new WS /api/rules/search
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 20 Jan 2014 13:55:16 +0000 (14:55 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 20 Jan 2014 13:56:49 +0000 (14:56 +0100)
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/rule/RuleWebService.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/rule/RuleWebServiceTest.java [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/rule/RuleWebServiceTest/search.json [new file with mode: 0644]

index a87fb802b12fa522a4c8bebfbc95be7df9ad795c..85eca5cf65ba7781ae495d2ca473a41bf5b9c585 100644 (file)
@@ -335,6 +335,7 @@ public final class Platform {
     servicesContainer.addSingleton(RuleTagLookup.class);
     servicesContainer.addSingleton(RubyRuleService.class);
     servicesContainer.addSingleton(RuleRepositories.class);
+    servicesContainer.addSingleton(RuleWebService.class);
 
     // technical debt
     servicesContainer.addSingleton(InternalRubyTechnicalDebtService.class);
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleWebService.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleWebService.java
new file mode 100644 (file)
index 0000000..e40c69a
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.server.rule;
+
+import org.sonar.api.web.ws.Request;
+import org.sonar.api.web.ws.RequestHandler;
+import org.sonar.api.web.ws.Response;
+import org.sonar.api.web.ws.WebService;
+
+public class RuleWebService implements WebService {
+
+  @Override
+  public void define(Context context) {
+    NewController controller = context.newController("api/rules")
+      .setDescription("Coding rules");
+
+    NewAction search = controller.newAction("search");
+    search
+      .setDescription("Search for coding rules")
+      .setSince("4.2")
+      .setHandler(new RequestHandler() {
+        @Override
+        public void handle(Request request, Response response) throws Exception {
+          search(request, response);
+        }
+      });
+
+    controller.done();
+  }
+
+  void search(Request request, Response response) {
+    response.newJsonWriter().beginObject()
+      // TODO
+      .prop("TODO", true)
+      .endObject()
+      .close();
+  }
+}
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleWebServiceTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleWebServiceTest.java
new file mode 100644 (file)
index 0000000..0700df7
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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.server.rule;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.skyscreamer.jsonassert.JSONAssert;
+import org.sonar.api.web.ws.RequestHandler;
+import org.sonar.api.web.ws.SimpleRequest;
+import org.sonar.api.web.ws.SimpleResponse;
+import org.sonar.api.web.ws.WebService;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RuleWebServiceTest {
+
+  WebService.Context context = new WebService.Context();
+  RuleWebService ws = new RuleWebService();
+
+  @Test
+  public void define_ws() throws Exception {
+    ws.define(context);
+
+    WebService.Controller controller = context.controller("api/rules");
+    assertThat(controller).isNotNull();
+    assertThat(controller.path()).isEqualTo("api/rules");
+    assertThat(controller.description()).isNotEmpty();
+
+    WebService.Action search = controller.action("search");
+    assertThat(search).isNotNull();
+    assertThat(search.key()).isEqualTo("search");
+    assertThat(search.handler()).isNotNull();
+    assertThat(search.since()).isEqualTo("4.2");
+    assertThat(search.isPost()).isFalse();
+  }
+
+  @Test
+  public void search_for_rules() throws Exception {
+    ws.define(context);
+    RequestHandler handler = context.controller("api/rules").action("search").handler();
+    SimpleRequest request = new SimpleRequest();
+    SimpleResponse response = new SimpleResponse();
+
+    handler.handle(request, response);
+
+    String json = response.outputAsString();
+    JSONAssert.assertEquals(
+      IOUtils.toString(getClass().getResource("/org/sonar/server/rule/RuleWebServiceTest/search.json")),
+      json, true
+    );
+  }
+}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/RuleWebServiceTest/search.json b/sonar-server/src/test/resources/org/sonar/server/rule/RuleWebServiceTest/search.json
new file mode 100644 (file)
index 0000000..a3a00e8
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "TODO": true
+}