From: Simon Brandhof Date: Fri, 9 Nov 2012 09:09:22 +0000 (+0100) Subject: SONAR-3946 - API: add search engine to Java WS client X-Git-Tag: 3.4~347 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9df7c3794fdc152f387c15519dd1772e33216a9c;p=sonarqube.git SONAR-3946 - API: add search engine to Java WS client --- diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb index dadfe993559..af39a4fbd7c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb @@ -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| diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/AbstractQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/AbstractQuery.java index 3bf6f10b2f1..970b2a90941 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/AbstractQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/AbstractQuery.java @@ -103,12 +103,19 @@ public abstract class AbstractQuery { 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 { 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 index 00000000000..aae35d9f2e0 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchQuery.java @@ -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 { + + 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 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 index 00000000000..f95335d4289 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceSearchResult.java @@ -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 resources; + + public int getPage() { + return page; + } + + public int getTotal() { + return total; + } + + public List 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 resources) { + this.resources = resources; + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java index 0170c24e6c5..649ff4eacca 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/AbstractUnmarshaller.java @@ -31,11 +31,15 @@ public abstract class AbstractUnmarshaller 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 index 00000000000..7cef7c4ac0c --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshaller.java @@ -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 { + + + @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 resources = new ArrayList(); + 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; + } + + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java index d2bffdec7a5..0e5d299f2f3 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java @@ -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 Unmarshaller forModel(Class 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 index 00000000000..f789d361819 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceSearchQueryTest.java @@ -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 index 00000000000..ab4650df9ad --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ResourceSearchUnmarshallerTest.java @@ -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 index 00000000000..f0946f2305f --- /dev/null +++ b/sonar-ws-client/src/test/resources/resources/search.json @@ -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