From 14b427c8460d6e33229d3ba80d1ed873143781e3 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 14 Sep 2016 14:44:00 +0200 Subject: [PATCH] SONAR-8078 remove pagination from api/rules/repositories --- .../server/rule/ws/RepositoriesAction.java | 81 +++++++------------ .../rule/ws/RepositoriesActionTest.java | 53 +++++------- .../repositories_limited.json | 14 ---- 3 files changed, 46 insertions(+), 102 deletions(-) delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_limited.json diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java index ac06c0aea8b..f1c8462d081 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java @@ -19,14 +19,9 @@ */ package org.sonar.server.rule.ws; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import com.google.common.io.Resources; import java.util.Collection; -import java.util.List; -import java.util.SortedMap; import java.util.regex.Pattern; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -34,8 +29,12 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.NewAction; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.text.JsonWriter; -import org.sonar.server.rule.RuleRepositories; -import org.sonar.server.rule.RuleRepositories.Repository; +import org.sonar.core.util.stream.Collectors; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.rule.RuleRepositoryDto; + +import static org.apache.commons.lang.StringUtils.isEmpty; /** * @since 5.1 @@ -44,10 +43,11 @@ public class RepositoriesAction implements RulesWsAction { private static final String LANGUAGE = "language"; private static final String MATCH_ALL = ".*"; - private final RuleRepositories repositories; - public RepositoriesAction(RuleRepositories repositories) { - this.repositories = repositories; + private final DbClient dbClient; + + public RepositoriesAction(DbClient dbClient) { + this.dbClient = dbClient; } @Override @@ -64,66 +64,39 @@ public class RepositoriesAction implements RulesWsAction { action.createParam(LANGUAGE) .setDescription("A language key; if provided, only repositories for the given language will be returned") .setExampleValue("java"); - action.createParam(Param.PAGE_SIZE) - .setDescription("The size of the list to return, 0 for all repositories") - .setExampleValue("25") - .setDefaultValue("0"); } @Override public void handle(Request request, Response response) throws Exception { String query = request.param(Param.TEXT_QUERY); String languageKey = request.param(LANGUAGE); - int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); JsonWriter json = response.newJsonWriter().beginObject().name("repositories").beginArray(); - for (Repo repo : listMatchingRepositories(query, languageKey, pageSize)) { - json.beginObject().prop("key", repo.key).prop("name", repo.name).prop(LANGUAGE, repo.language).endObject(); + for (RuleRepositoryDto repo : listMatchingRepositories(query, languageKey)) { + json + .beginObject() + .prop("key", repo.getKee()) + .prop("name", repo.getName()) + .prop(LANGUAGE, repo.getLanguage()) + .endObject(); } json.endArray().endObject().close(); } - private Collection listMatchingRepositories(@Nullable String query, @Nullable String languageKey, int pageSize) { + private Collection listMatchingRepositories(@Nullable String query, @Nullable String languageKey) { Pattern pattern = Pattern.compile(query == null ? MATCH_ALL : MATCH_ALL + query + MATCH_ALL, Pattern.CASE_INSENSITIVE); - SortedMap reposByName = Maps.newTreeMap(); - Collection repos = listRepositories(languageKey); - - for (Repo repo : repos) { - if (pattern.matcher(repo.key).matches() || pattern.matcher(repo.name).matches()) { - reposByName.put(repo.name + " -- " + repo.language, repo); - } - } - - List result = Lists.newArrayList(reposByName.values()); - if (pageSize > 0 && pageSize < result.size()) { - result = result.subList(0, pageSize); - } - return result; - } - - private Collection listRepositories(@CheckForNull String languageKey) { - List allRepos = Lists.newArrayList(); - Collection reposFromPlugins = languageKey == null ? repositories.repositories() : repositories.repositoriesForLang(languageKey); - for (Repository repo : reposFromPlugins) { - allRepos.add(new Repo(repo)); - } - return allRepos; + return selectFromDb(languageKey).stream() + .filter(r -> pattern.matcher(r.getKee()).matches() || pattern.matcher(r.getName()).matches()) + .collect(Collectors.toList()); } - private static final class Repo { - private String key; - private String name; - private String language; - - private Repo(String key, String name, String language) { - this.key = key; - this.name = name; - this.language = language; - } - - private Repo(Repository repo) { - this(repo.key(), repo.name(), repo.language()); + private Collection selectFromDb(@Nullable String language) { + try (DbSession dbSession = dbClient.openSession(false)) { + if (isEmpty(language)) { + return dbClient.ruleRepositoryDao().selectAll(dbSession); + } + return dbClient.ruleRepositoryDao().selectByLanguage(dbSession, language); } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java index 34d9fd65dd1..ef9152dd8be 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java @@ -19,54 +19,40 @@ */ package org.sonar.server.rule.ws; -import com.google.common.collect.ImmutableList; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.server.rule.RuleRepositories; +import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.rule.RuleRepositoryDto; import org.sonar.server.ws.WsTester; import org.sonar.server.ws.WsTester.TestRequest; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static java.util.Arrays.asList; -@RunWith(MockitoJUnitRunner.class) public class RepositoriesActionTest { - private WsTester tester; + private WsTester wsTester; - @Mock - private RuleRepositories repositories; + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); @Before public void setUp() { - tester = new WsTester(new RulesWs(new RepositoriesAction(repositories))); - - RuleRepositories.Repository repo1 = mock(RuleRepositories.Repository.class); - when(repo1.key()).thenReturn("xoo"); - when(repo1.name()).thenReturn("SonarQube"); - when(repo1.language()).thenReturn("xoo"); - - RuleRepositories.Repository repo2 = mock(RuleRepositories.Repository.class); - when(repo2.key()).thenReturn("squid"); - when(repo2.name()).thenReturn("SonarQube"); - when(repo2.language()).thenReturn("ws"); - - RuleRepositories.Repository repo3 = mock(RuleRepositories.Repository.class); - when(repo3.key()).thenReturn("common-ws"); - when(repo3.name()).thenReturn("SonarQube Common"); - when(repo3.language()).thenReturn("ws"); - - when(repositories.repositories()).thenReturn(ImmutableList.of(repo1, repo2, repo3)); - when(repositories.repositoriesForLang("xoo")).thenReturn(ImmutableList.of(repo1)); - when(repositories.repositoriesForLang("ws")).thenReturn(ImmutableList.of(repo2, repo3)); + wsTester = new WsTester(new RulesWs(new RepositoriesAction(dbTester.getDbClient()))); } @Test public void should_list_repositories() throws Exception { - tester = new WsTester(new RulesWs(new RepositoriesAction(repositories))); + DbSession dbSession = dbTester.getSession(); + RuleRepositoryDto repo1 = new RuleRepositoryDto("xoo", "xoo", "SonarQube"); + RuleRepositoryDto repo2 = new RuleRepositoryDto("squid", "ws", "SonarQube"); + RuleRepositoryDto repo3 = new RuleRepositoryDto("common-ws", "ws", "SonarQube Common"); + dbTester.getDbClient().ruleRepositoryDao().insert(dbSession, asList(repo1, repo2, repo3)); + dbSession.commit(); + + wsTester = new WsTester(new RulesWs(new RepositoriesAction(dbTester.getDbClient()))); newRequest().execute().assertJson(this.getClass(), "repositories.json"); newRequest().setParam("language", "xoo").execute().assertJson(this.getClass(), "repositories_xoo.json"); @@ -74,12 +60,11 @@ public class RepositoriesActionTest { newRequest().setParam("q", "common").execute().assertJson(this.getClass(), "repositories_common.json"); newRequest().setParam("q", "squid").execute().assertJson(this.getClass(), "repositories_squid.json"); newRequest().setParam("q", "sonar").execute().assertJson(this.getClass(), "repositories_sonar.json"); - newRequest().setParam("q", "sonar").setParam("ps", "2").execute().assertJson(this.getClass(), "repositories_limited.json"); newRequest().setParam("ps", "4").execute().assertJson(this.getClass(), "repositories.json"); newRequest().setParam("ps", "100").execute().assertJson(this.getClass(), "repositories.json"); } protected TestRequest newRequest() { - return tester.newGetRequest("api/rules", "repositories"); + return wsTester.newGetRequest("api/rules", "repositories"); } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_limited.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_limited.json deleted file mode 100644 index bb30a982d52..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_limited.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "repositories": [ - { - "key": "squid", - "name": "SonarQube", - "language": "ws" - }, - { - "key": "xoo", - "name": "SonarQube", - "language": "xoo" - } - ] -} \ No newline at end of file -- 2.39.5