]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3946 - API: add search engine to Java WS client
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 9 Nov 2012 09:09:22 +0000 (10:09 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 9 Nov 2012 09:09:22 +0000 (10:09 +0100)
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb
sonar-ws-client/src/main/java/org/sonar/wsclient/services/AbstractQuery.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchQuery.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchResult.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshaller.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java
sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceSearchQueryTest.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshallerTest.java [new file with mode: 0644]
sonar-ws-client/src/test/resources/resources/search.json [new file with mode: 0644]

index dadfe993559972362f944d5691ec66edaae136da..af39a4fbd7cba5920eacad4798d3604ac3f6152c 100644 (file)
@@ -65,7 +65,7 @@ class Api::ResourcesController < Api::ApiController
 
     resources=[]
     unless resource_ids.empty?
-      resources=Project.find(:all, :select => 'id,qualifier,name,long_name', :conditions => ['id in (?) and enabled=?', resource_ids, true])
+      resources=Project.find(:all, :select => 'id,qualifier,name,long_name,kee', :conditions => ['id in (?) and enabled=?', resource_ids, true])
     end
 
     if select2_format
@@ -83,7 +83,7 @@ class Api::ResourcesController < Api::ApiController
         }
       end
     else
-      json = {:total => total, :page => page, :page_size => page_size, :data => resources.map { |r| {:id => r.id, :nm => r.name(true), :q => r.qualifier} }}
+      json = {:total => total, :page => page, :page_size => page_size, :data => resources.map { |r| {:id => r.id, :key => r.key, :nm => r.name(true), :q => r.qualifier} }}
     end
 
     respond_to do |format|
index 3bf6f10b2f1c331990f67fc6fc043368233579b7..970b2a9094183066804ab3901685f021d976c433 100644 (file)
@@ -103,12 +103,19 @@ public abstract class AbstractQuery<M extends Model> {
     return WSUtils.getINSTANCE().encodeUrl(value);
   }
 
+  protected static void appendUrlParameter(StringBuilder url, String paramKey, int paramValue) {
+    url.append(paramKey)
+      .append('=')
+      .append(paramValue)
+      .append("&");
+  }
+
   protected static void appendUrlParameter(StringBuilder url, String paramKey, Object paramValue) {
     if (paramValue != null) {
       url.append(paramKey)
-          .append('=')
-          .append(encode(paramValue.toString()))
-          .append('&');
+        .append('=')
+        .append(encode(paramValue.toString()))
+        .append('&');
     }
   }
 
@@ -131,9 +138,9 @@ public abstract class AbstractQuery<M extends Model> {
     if (paramValue != null) {
       String format = (includeTime ? "yyyy-MM-dd'T'HH:mm:ssZ" : "yyyy-MM-dd");
       url.append(paramKey)
-          .append('=')
-          .append(encode(WSUtils.getINSTANCE().format(paramValue, format)))
-          .append('&');
+        .append('=')
+        .append(encode(WSUtils.getINSTANCE().format(paramValue, format)))
+        .append('&');
     }
   }
 }
\ No newline at end of file
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchQuery.java
new file mode 100644 (file)
index 0000000..aae35d9
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.wsclient.services;
+
+/**
+ * @since 3.4
+ */
+public class ResourceSearchQuery extends Query<ResourceSearchResult> {
+
+  private int page = -1;
+  private int pageSize = -1;
+  private String[] qualifiers = null;
+  private String text;
+
+  private ResourceSearchQuery() {
+  }
+
+  public static ResourceSearchQuery create(String text) {
+    return new ResourceSearchQuery().setText(text);
+  }
+
+  public int getPage() {
+    return page;
+  }
+
+  public ResourceSearchQuery setPage(int page) {
+    this.page = page;
+    return this;
+  }
+
+  public int getPageSize() {
+    return pageSize;
+  }
+
+  public ResourceSearchQuery setPageSize(int pageSize) {
+    this.pageSize = pageSize;
+    return this;
+  }
+
+  public String[] getQualifiers() {
+    return qualifiers;
+  }
+
+  public ResourceSearchQuery setQualifiers(String... qualifiers) {
+    this.qualifiers = qualifiers;
+    return this;
+  }
+
+  public String getText() {
+    return text;
+  }
+
+  public ResourceSearchQuery setText(String text) {
+    this.text = text;
+    return this;
+  }
+
+  @Override
+  public String getUrl() {
+    StringBuilder url = new StringBuilder();
+    url.append("/api/resources/search?s=");
+    url.append(text);
+    url.append("&");
+    if (page > 0) {
+      appendUrlParameter(url, "p", page);
+    }
+    if (pageSize > 0) {
+      appendUrlParameter(url, "ps", pageSize);
+    }
+    appendUrlParameter(url, "q", qualifiers);
+    return url.toString();
+  }
+
+  @Override
+  public Class<ResourceSearchResult> getModelClass() {
+    return ResourceSearchResult.class;
+  }
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchResult.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchResult.java
new file mode 100644 (file)
index 0000000..f95335d
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.wsclient.services;
+
+import java.util.List;
+
+public class ResourceSearchResult extends Model {
+
+  public static class Resource {
+    private String key, name, qualifier;
+
+    public String key() {
+      return key;
+    }
+
+    public String name() {
+      return name;
+    }
+
+    public String qualifier() {
+      return qualifier;
+    }
+
+    public void setKey(String key) {
+      this.key = key;
+    }
+
+    public void setName(String s) {
+      this.name = s;
+    }
+
+    public void setQualifier(String qualifier) {
+      this.qualifier = qualifier;
+    }
+  }
+
+
+  private int page, pageSize, total;
+  private List<ResourceSearchResult.Resource> resources;
+
+  public int getPage() {
+    return page;
+  }
+
+  public int getTotal() {
+    return total;
+  }
+
+  public List<ResourceSearchResult.Resource> getResources() {
+    return resources;
+  }
+
+  public void setPage(int page) {
+    this.page = page;
+  }
+
+  public void setTotal(int total) {
+    this.total = total;
+  }
+
+  public int getPageSize() {
+    return pageSize;
+  }
+
+  public void setPageSize(int pageSize) {
+    this.pageSize = pageSize;
+  }
+
+  public void setResources(List<Resource> resources) {
+    this.resources = resources;
+  }
+}
index 0170c24e6c536f7b6d414cdb795dbdc713d9ef0d..649ff4eacca77e0622a32fb83c85d22e5dda19cb 100644 (file)
@@ -31,11 +31,15 @@ public abstract class AbstractUnmarshaller<M extends Model> implements Unmarshal
     WSUtils utils = WSUtils.getINSTANCE();
     M result = null;
     Object array = utils.parse(json);
-    if (utils.getArraySize(array) >= 1) {
-      Object elt = utils.getArrayElement(array, 0);
-      if (elt != null) {
-        result = parse(elt);
+    if (array instanceof List) {
+      if (utils.getArraySize(array) >= 1) {
+        Object elt = utils.getArrayElement(array, 0);
+        if (elt != null) {
+          result = parse(elt);
+        }
       }
+    } else {
+      result = parse(array);
     }
     return result;
 
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshaller.java
new file mode 100644 (file)
index 0000000..7cef7c4
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.wsclient.unmarshallers;
+
+import org.json.simple.JSONObject;
+import org.sonar.wsclient.services.ResourceSearchResult;
+import org.sonar.wsclient.services.WSUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @since 3.4
+ */
+public class ResourceSearchUnmarshaller extends AbstractUnmarshaller<ResourceSearchResult> {
+
+
+  @Override
+  protected ResourceSearchResult parse(Object json) {
+    System.out.println("JSON: " + json);
+    WSUtils utils = WSUtils.getINSTANCE();
+    ResourceSearchResult result = new ResourceSearchResult();
+    result.setPage(utils.getInteger(json, "page"));
+    result.setPageSize(utils.getInteger(json, "page_size"));
+    result.setTotal(utils.getInteger(json, "total"));
+
+    List<ResourceSearchResult.Resource> resources = new ArrayList<ResourceSearchResult.Resource>();
+    for (Object jsonResource : JsonUtils.getArray((JSONObject) json, "data")) {
+      ResourceSearchResult.Resource resource = new ResourceSearchResult.Resource();
+      resource.setKey(JsonUtils.getString((JSONObject) jsonResource, "key"));
+      resource.setName(JsonUtils.getString((JSONObject) jsonResource, "nm"));
+      resource.setQualifier(JsonUtils.getString((JSONObject) jsonResource, "q"));
+      resources.add(resource);
+    }
+    result.setResources(resources);
+    return result;
+  }
+
+
+}
index d2bffdec7a58e4687d52b62a4b6e3bc1f32cc70f..0e5d299f2f35c99315ba7e1f5662d2fa29ff8517 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.wsclient.services.Plugin;
 import org.sonar.wsclient.services.Profile;
 import org.sonar.wsclient.services.Property;
 import org.sonar.wsclient.services.Resource;
+import org.sonar.wsclient.services.ResourceSearchResult;
 import org.sonar.wsclient.services.Review;
 import org.sonar.wsclient.services.Rule;
 import org.sonar.wsclient.services.Server;
@@ -68,6 +69,7 @@ public final class Unmarshallers {
     unmarshallers.put(Review.class, new ReviewUnmarshaller());
     unmarshallers.put(ManualMeasure.class, new ManualMeasureUnmarshaller());
     unmarshallers.put(Authentication.class, new AuthenticationUnmarshaller());
+    unmarshallers.put(ResourceSearchResult.class, new ResourceSearchUnmarshaller());
   }
 
   public static <M extends Model> Unmarshaller<M> forModel(Class<M> modelClass) {
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceSearchQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceSearchQueryTest.java
new file mode 100644 (file)
index 0000000..f789d36
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.wsclient.services;
+
+import org.junit.Test;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class ResourceSearchQueryTest extends QueryTestCase {
+
+  @Test
+  public void test_url() {
+    ResourceSearchQuery query = ResourceSearchQuery.create("commons");
+    assertThat(query.getUrl(), is("/api/resources/search?s=commons&"));
+    assertThat(query.getModelClass().getName(), is(ResourceSearchResult.class.getName()));
+  }
+
+  @Test
+  public void test_optional_parameters() {
+    ResourceSearchQuery query = ResourceSearchQuery.create("commons");
+    query.setPage(5);
+    query.setPageSize(20);
+    query.setQualifiers("TRK", "BRC");
+    assertThat(query.getUrl(), is("/api/resources/search?s=commons&p=5&ps=20&q=TRK,BRC&"));
+  }
+}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshallerTest.java
new file mode 100644 (file)
index 0000000..ab4650d
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.wsclient.unmarshallers;
+
+import org.junit.Test;
+import org.sonar.wsclient.services.ResourceSearchResult;
+import org.sonar.wsclient.services.Server;
+
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class ResourceSearchUnmarshallerTest extends UnmarshallerTestCase {
+
+  @Test
+  public void testToModel() {
+    ResourceSearchResult result = new ResourceSearchUnmarshaller().toModel(loadFile("/resources/search.json"));
+    assertThat(result.getPage(), is(1));
+    assertThat(result.getPageSize(), is(10));
+    assertThat(result.getTotal(), is(4));
+    assertThat(result.getResources().size(), is(4));
+
+  }
+
+}
diff --git a/sonar-ws-client/src/test/resources/resources/search.json b/sonar-ws-client/src/test/resources/resources/search.json
new file mode 100644 (file)
index 0000000..f0946f2
--- /dev/null
@@ -0,0 +1,26 @@
+{"total":4, "page":1, "page_size":10, "data":[
+  {
+    "id":8,
+    "key":"org.apache.commons:commons-i18n:org.apache.commons.i18n.JdbcMessageProvider",
+    "nm":"org.apache.commons.i18n.JdbcMessageProvider",
+    "q":"CLA"
+  },
+  {
+    "id":15,
+    "key":"org.apache.commons:commons-i18n:org.apache.commons.i18n.MessageProvider",
+    "nm":"org.apache.commons.i18n.MessageProvider",
+    "q":"CLA"
+  },
+  {
+    "id":16,
+    "key":"org.apache.commons:commons-i18n:org.apache.commons.i18n.ResourceBundleMessageProvider",
+    "nm":"org.apache.commons.i18n.ResourceBundleMessageProvider",
+    "q":"CLA"
+  },
+  {
+    "id":17,
+    "key":"org.apache.commons:commons-i18n:org.apache.commons.i18n.XMLMessageProvider",
+    "nm":"org.apache.commons.i18n.XMLMessageProvider",
+    "q":"CLA"
+  }
+]}
\ No newline at end of file