Skip to content

Commit

Permalink
WS api/favorites/search use a SearchRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Dec 21, 2016
1 parent 028b8c4 commit d24f6e9
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 19 deletions.
7 changes: 4 additions & 3 deletions it/it-tests/src/test/java/it/analysis/FavoriteTest.java
Expand Up @@ -32,6 +32,7 @@
import org.sonarqube.ws.Favorites.Favorite; import org.sonarqube.ws.Favorites.Favorite;
import org.sonarqube.ws.WsPermissions; import org.sonarqube.ws.WsPermissions;
import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.favorite.SearchRequest;
import org.sonarqube.ws.client.permission.AddProjectCreatorToTemplateWsRequest; import org.sonarqube.ws.client.permission.AddProjectCreatorToTemplateWsRequest;
import org.sonarqube.ws.client.permission.RemoveProjectCreatorFromTemplateWsRequest; import org.sonarqube.ws.client.permission.RemoveProjectCreatorFromTemplateWsRequest;
import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest; import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
Expand Down Expand Up @@ -71,7 +72,7 @@ public void project_as_favorite_when_authenticated_and_first_analysis_and_a_proj


orchestrator.executeBuild(sampleProject); orchestrator.executeBuild(sampleProject);


Favorites.SearchResponse response = adminWsClient.favorites().search(null, null); Favorites.SearchResponse response = adminWsClient.favorites().search(new SearchRequest());
assertThat(response.getFavoritesList()).extracting(Favorite::getKey).contains(PROJECT_KEY); assertThat(response.getFavoritesList()).extracting(Favorite::getKey).contains(PROJECT_KEY);
} }


Expand All @@ -81,7 +82,7 @@ public void no_project_as_favorite_when_no_project_creator_permission() {


orchestrator.executeBuild(sampleProject); orchestrator.executeBuild(sampleProject);


Favorites.SearchResponse response = adminWsClient.favorites().search(null, null); Favorites.SearchResponse response = adminWsClient.favorites().search(new SearchRequest());
assertThat(response.getFavoritesList()).extracting(Favorite::getKey).doesNotContain(PROJECT_KEY); assertThat(response.getFavoritesList()).extracting(Favorite::getKey).doesNotContain(PROJECT_KEY);
} }


Expand All @@ -94,7 +95,7 @@ public void no_project_as_favorite_when_second_analysis() {


orchestrator.executeBuild(sampleProject); orchestrator.executeBuild(sampleProject);


Favorites.SearchResponse response = adminWsClient.favorites().search(null, null); Favorites.SearchResponse response = adminWsClient.favorites().search(new SearchRequest());
assertThat(response.getFavoritesList()).extracting(Favorite::getKey).doesNotContain(PROJECT_KEY); assertThat(response.getFavoritesList()).extracting(Favorite::getKey).doesNotContain(PROJECT_KEY);
} }


Expand Down
7 changes: 4 additions & 3 deletions it/it-tests/src/test/java/it/user/FavoritesWsTest.java
Expand Up @@ -28,6 +28,7 @@
import org.junit.Test; import org.junit.Test;
import org.sonarqube.ws.Favorites.Favorite; import org.sonarqube.ws.Favorites.Favorite;
import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.favorite.SearchRequest;


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.newAdminWsClient; import static util.ItUtils.newAdminWsClient;
Expand All @@ -52,20 +53,20 @@ public void inspectProject() {
@Test @Test
public void favorites_web_service() { public void favorites_web_service() {
// GET (nothing) // GET (nothing)
List<Favorite> favorites = adminClient.favorites().search(null, null).getFavoritesList(); List<Favorite> favorites = adminClient.favorites().search(new SearchRequest()).getFavoritesList();
assertThat(favorites).isEmpty(); assertThat(favorites).isEmpty();


// POST (create favorites) // POST (create favorites)
adminClient.favorites().add("sample"); adminClient.favorites().add("sample");
adminClient.favorites().add("sample:src/main/xoo/sample/Sample.xoo"); adminClient.favorites().add("sample:src/main/xoo/sample/Sample.xoo");


// GET (created favorites) // GET (created favorites)
favorites = adminClient.favorites().search(null, null).getFavoritesList(); favorites = adminClient.favorites().search(new SearchRequest()).getFavoritesList();
assertThat(favorites.stream().map(Favorite::getKey)).containsOnly("sample", "sample:src/main/xoo/sample/Sample.xoo"); assertThat(favorites.stream().map(Favorite::getKey)).containsOnly("sample", "sample:src/main/xoo/sample/Sample.xoo");


// DELETE (a favorite) // DELETE (a favorite)
adminClient.favorites().remove("sample"); adminClient.favorites().remove("sample");
favorites = adminClient.favorites().search(null, null).getFavoritesList(); favorites = adminClient.favorites().search(new SearchRequest()).getFavoritesList();
assertThat(favorites.stream().map(Favorite::getKey)).containsOnly("sample:src/main/xoo/sample/Sample.xoo"); assertThat(favorites.stream().map(Favorite::getKey)).containsOnly("sample:src/main/xoo/sample/Sample.xoo");
} }


Expand Down
Expand Up @@ -42,11 +42,13 @@
import org.sonarqube.ws.Common; import org.sonarqube.ws.Common;
import org.sonarqube.ws.Favorites.Favorite; import org.sonarqube.ws.Favorites.Favorite;
import org.sonarqube.ws.Favorites.SearchResponse; import org.sonarqube.ws.Favorites.SearchResponse;
import org.sonarqube.ws.client.favorite.SearchRequest;


import static org.sonar.core.util.Protobuf.setNullable; import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.core.util.stream.Collectors.toOneElement; import static org.sonar.core.util.stream.Collectors.toOneElement;
import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.favorite.FavoritesWsParameters.ACTION_SEARCH; import static org.sonarqube.ws.client.favorite.FavoritesWsParameters.ACTION_SEARCH;
import static org.sonarqube.ws.client.favorite.SearchRequest.MAX_PAGE_SIZE;


public class SearchAction implements FavoritesWsAction { public class SearchAction implements FavoritesWsAction {
private final FavoriteFinder favoriteFinder; private final FavoriteFinder favoriteFinder;
Expand All @@ -68,19 +70,26 @@ public void define(WebService.NewController context) {
.setResponseExample(getClass().getResource("search-example.json")) .setResponseExample(getClass().getResource("search-example.json"))
.setHandler(this); .setHandler(this);


action.addPagingParams(100, 500); action.addPagingParams(100, MAX_PAGE_SIZE);
} }


@Override @Override
public void handle(Request request, Response response) throws Exception { public void handle(Request request, Response response) throws Exception {
SearchResponse wsResponse = Stream.of(request) SearchResponse wsResponse = Stream.of(request)
.map(toWsRequest())
.map(search()) .map(search())
.map(new ResponseBuilder()) .map(new ResponseBuilder())
.collect(Collectors.toOneElement()); .collect(Collectors.toOneElement());
writeProtobuf(wsResponse, request, response); writeProtobuf(wsResponse, request, response);
} }


private Function<Request, SearchResults> search() { private static Function<Request, SearchRequest> toWsRequest() {
return request -> new SearchRequest()
.setPage(request.mandatoryParamAsInt(Param.PAGE))
.setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE));
}

private Function<SearchRequest, SearchResults> search() {
return request -> { return request -> {
try (DbSession dbSession = dbClient.openSession(false)) { try (DbSession dbSession = dbClient.openSession(false)) {
return Stream.of(request) return Stream.of(request)
Expand All @@ -103,7 +112,7 @@ private Consumer<SearchResults.Builder> addAuthorizedProjectUuids() {
.copyOf(dbClient.authorizationDao().selectAuthorizedRootProjectsUuids(results.dbSession, userSession.getUserId(), UserRole.USER)); .copyOf(dbClient.authorizationDao().selectAuthorizedRootProjectsUuids(results.dbSession, userSession.getUserId(), UserRole.USER));
} }


private static Consumer<Request> checkAuthentication(UserSession userSession) { private static Consumer<SearchRequest> checkAuthentication(UserSession userSession) {
return r -> userSession.checkLoggedIn(); return r -> userSession.checkLoggedIn();
} }


Expand All @@ -122,7 +131,7 @@ private SearchResults(Builder builder) {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }


static Function<Request, Builder> builder(DbSession dbSession) { static Function<SearchRequest, Builder> builder(DbSession dbSession) {
return request -> new Builder(dbSession, request); return request -> new Builder(dbSession, request);
} }


Expand All @@ -133,10 +142,10 @@ private static class Builder {
private Set<String> authorizedProjectUuids; private Set<String> authorizedProjectUuids;
private List<ComponentDto> allFavorites; private List<ComponentDto> allFavorites;


private Builder(DbSession dbSession, Request request) { private Builder(DbSession dbSession, SearchRequest request) {
this.dbSession = dbSession; this.dbSession = dbSession;
this.page = request.mandatoryParamAsInt(Param.PAGE); this.page = request.getPage();
this.pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); this.pageSize = request.getPageSize();
} }


public SearchResults build() { public SearchResults build() {
Expand Down
Expand Up @@ -20,7 +20,6 @@


package org.sonarqube.ws.client.favorite; package org.sonarqube.ws.client.favorite;


import javax.annotation.Nullable;
import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.server.ws.WebService.Param;
import org.sonarqube.ws.Favorites.SearchResponse; import org.sonarqube.ws.Favorites.SearchResponse;
import org.sonarqube.ws.client.BaseService; import org.sonarqube.ws.client.BaseService;
Expand Down Expand Up @@ -51,13 +50,13 @@ public void remove(String component) {
call(post); call(post);
} }


public SearchResponse search(@Nullable Integer page, @Nullable Integer pageSize) { public SearchResponse search(SearchRequest request) {
GetRequest get = new GetRequest(path(ACTION_SEARCH)); GetRequest get = new GetRequest(path(ACTION_SEARCH));
if (page != null) { if (request.getPage() != null) {
get.setParam(Param.PAGE, page); get.setParam(Param.PAGE, request.getPage());
} }
if (pageSize != null) { if (request.getPageSize() != null) {
get.setParam(Param.PAGE_SIZE, pageSize); get.setParam(Param.PAGE_SIZE, request.getPageSize());
} }


return call(get, SearchResponse.parser()); return call(get, SearchResponse.parser());
Expand Down
@@ -0,0 +1,54 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program 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.
*
* This program 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.sonarqube.ws.client.favorite;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

public class SearchRequest {
public static final int MAX_PAGE_SIZE = 500;

private Integer page;
private Integer pageSize;

@CheckForNull
public Integer getPage() {
return page;
}

public SearchRequest setPage(@Nullable Integer page) {
this.page = page;
return this;
}

@CheckForNull
public Integer getPageSize() {
return pageSize;
}

public SearchRequest setPageSize(@Nullable Integer pageSize) {
if (pageSize != null && pageSize > MAX_PAGE_SIZE) {
throw new IllegalArgumentException("Page size must be lower than or equals to " + MAX_PAGE_SIZE);
}
this.pageSize = pageSize;
return this;
}
}
Expand Up @@ -22,6 +22,7 @@


import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.sonar.api.server.ws.WebService.Param;
import org.sonarqube.ws.client.ServiceTester; import org.sonarqube.ws.client.ServiceTester;
import org.sonarqube.ws.client.WsConnector; import org.sonarqube.ws.client.WsConnector;


Expand Down Expand Up @@ -53,4 +54,15 @@ public void remove() {
.hasParam(PARAM_COMPONENT, "my_project") .hasParam(PARAM_COMPONENT, "my_project")
.andNoOtherParam(); .andNoOtherParam();
} }

@Test
public void search() {
underTest.search(new SearchRequest().setPage(42).setPageSize(255));

serviceTester.assertThat(serviceTester.getGetRequest())
.hasPath("search")
.hasParam(Param.PAGE, 42)
.hasParam(Param.PAGE_SIZE, 255)
.andNoOtherParam();
}
} }
@@ -0,0 +1,40 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program 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.
*
* This program 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.sonarqube.ws.client.favorite;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class SearchRequestTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();

private SearchRequest underTest = new SearchRequest();

@Test
public void fail_if_page_size_greater_than_500() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Page size must be lower than or equals to 500");

underTest.setPageSize(501);
}
}

0 comments on commit d24f6e9

Please sign in to comment.