From 3469a119ac9d3f585b0f4858dead8c10d6b2d3eb Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 24 Jan 2017 10:49:35 +0100 Subject: [PATCH] SONAR-8675 drop unused param "q" in api/projects/search_my_projects --- .../project/ws/SearchMyProjectsAction.java | 17 ++---- .../ws/SearchMyProjectsDataLoader.java | 44 +++++++--------- .../ws/SearchMyProjectsActionTest.java | 52 ------------------- .../project/SearchMyProjectsRequest.java | 13 ----- 4 files changed, 23 insertions(+), 103 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsAction.java index 5a58a1cb9dd..2c6825e8db6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsAction.java @@ -38,7 +38,6 @@ import static com.google.common.base.Strings.emptyToNull; import static com.google.common.base.Strings.isNullOrEmpty; import static java.lang.String.format; import static org.sonar.core.util.Protobuf.setNullable; -import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonar.server.ws.WsUtils.writeProtobuf; public class SearchMyProjectsAction implements ProjectsWsAction { @@ -82,12 +81,10 @@ public class SearchMyProjectsAction implements ProjectsWsAction { private SearchMyProjectsWsResponse doHandle(SearchMyProjectsRequest request) { checkAuthenticated(); - DbSession dbSession = dbClient.openSession(false); - try { - SearchMyProjectsData data = dataLoader.load(request); + + try (DbSession dbSession = dbClient.openSession(false)) { + SearchMyProjectsData data = dataLoader.load(dbSession, request); return buildResponse(request, data); - } finally { - dbClient.closeSession(dbSession); } } @@ -113,16 +110,10 @@ public class SearchMyProjectsAction implements ProjectsWsAction { } private static SearchMyProjectsRequest toRequest(Request request) { - SearchMyProjectsRequest searchMyProjectsRequest = SearchMyProjectsRequest.builder() - .setQuery(request.param(Param.TEXT_QUERY)) + return SearchMyProjectsRequest.builder() .setPage(request.mandatoryParamAsInt(Param.PAGE)) .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)) .build(); - - String searchQuery = searchMyProjectsRequest.getQuery(); - checkRequest(searchQuery == null || searchQuery.length() >= QUERY_MINIMUM_LENGTH, - "The '%s' parameter must have at least %d characters", Param.TEXT_QUERY, QUERY_MINIMUM_LENGTH); - return searchMyProjectsRequest; } private static class ProjectDtoToWs implements Function { diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsDataLoader.java index 648faeeb499..6e41546905e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsDataLoader.java @@ -51,32 +51,27 @@ public class SearchMyProjectsDataLoader { this.dbClient = dbClient; } - SearchMyProjectsData load(SearchMyProjectsRequest request) { - DbSession dbSession = dbClient.openSession(false); - try { - SearchMyProjectsData.Builder data = builder(); - ProjectsResult searchResult = searchProjects(dbSession, request); - List projects = searchResult.projects; - List projectUuids = Lists.transform(projects, ComponentDto::projectUuid); - List projectLinks = dbClient.componentLinkDao().selectByComponentUuids(dbSession, projectUuids); - List snapshots = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, projectUuids); - MetricDto gateStatusMetric = dbClient.metricDao().selectOrFailByKey(dbSession, CoreMetrics.ALERT_STATUS_KEY); - MeasureQuery measureQuery = MeasureQuery.builder() - .setProjectUuids(projectUuids) - .setMetricId(gateStatusMetric.getId()) - .build(); - List qualityGates = dbClient.measureDao().selectByQuery(dbSession, measureQuery); + SearchMyProjectsData load(DbSession dbSession, SearchMyProjectsRequest request) { + SearchMyProjectsData.Builder data = builder(); + ProjectsResult searchResult = searchProjects(dbSession, request); + List projects = searchResult.projects; + List projectUuids = Lists.transform(projects, ComponentDto::projectUuid); + List projectLinks = dbClient.componentLinkDao().selectByComponentUuids(dbSession, projectUuids); + List snapshots = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, projectUuids); + MetricDto gateStatusMetric = dbClient.metricDao().selectOrFailByKey(dbSession, CoreMetrics.ALERT_STATUS_KEY); + MeasureQuery measureQuery = MeasureQuery.builder() + .setProjectUuids(projectUuids) + .setMetricId(gateStatusMetric.getId()) + .build(); + List qualityGates = dbClient.measureDao().selectByQuery(dbSession, measureQuery); - data.setProjects(projects) - .setProjectLinks(projectLinks) - .setSnapshots(snapshots) - .setQualityGates(qualityGates) - .setTotalNbOfProjects(searchResult.total); + data.setProjects(projects) + .setProjectLinks(projectLinks) + .setSnapshots(snapshots) + .setQualityGates(qualityGates) + .setTotalNbOfProjects(searchResult.total); - return data.build(); - } finally { - dbClient.closeSession(dbSession); - } + return data.build(); } @VisibleForTesting @@ -86,7 +81,6 @@ public class SearchMyProjectsDataLoader { List componentIds = dbClient.roleDao().selectComponentIdsByPermissionAndUserId(dbSession, UserRole.ADMIN, userId); ComponentQuery dbQuery = ComponentQuery.builder() .setQualifiers(Qualifiers.PROJECT) - .setNameOrKeyQuery(request.getQuery()) .setComponentIds(ImmutableSet.copyOf(componentIds)) .build(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchMyProjectsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchMyProjectsActionTest.java index d5f894dfd79..82a03bd37b6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchMyProjectsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchMyProjectsActionTest.java @@ -43,7 +43,6 @@ import org.sonar.db.metric.MetricDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; -import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; @@ -54,7 +53,6 @@ import org.sonarqube.ws.WsProjects.SearchMyProjectsWsResponse.Project; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; -import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; import static org.sonar.db.component.ComponentTesting.newDeveloper; import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.component.ComponentTesting.newView; @@ -237,48 +235,6 @@ public class SearchMyProjectsActionTest { assertThat(result.getProjectsList()).extracting(Project::getId).containsOnly(jdk7.uuid(), cLang.uuid(), sonarqube.uuid()); } - @Test - public void search_my_projects_by_name() { - OrganizationDto organizationDto = db.organizations().insert(); - ComponentDto sonarqube = db.components().insertComponent(newProjectDto(organizationDto).setName("ONE_PROJECT_NAME")); - ComponentDto jdk8 = db.components().insertComponent(newProjectDto(organizationDto).setName("TWO_PROJECT_NAME")); - ComponentDto ruby = db.components().insertComponent(newProjectDto(organizationDto).setName("ANOTHER_42")); - dbClient.snapshotDao().insert(dbSession, newAnalysis(sonarqube), newAnalysis(jdk8), newAnalysis(ruby)); - db.components().indexAllComponents(); - db.commit(); - - db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, sonarqube); - db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk8); - db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, ruby); - - SearchMyProjectsWsResponse result = call_ws(ws.newRequest().setParam(TEXT_QUERY, "_project_")); - - assertThat(result.getProjectsCount()).isEqualTo(2); - assertThat(result.getProjectsList()).extracting(Project::getId) - .containsOnlyOnce(sonarqube.uuid(), jdk8.uuid()) - .doesNotContain(ruby.uuid()); - } - - @Test - public void search_my_projects_by_exact_match_on_key() { - OrganizationDto organizationDto = db.organizations().insert(); - ComponentDto sonarqube = db.components().insertComponent(newProjectDto(organizationDto).setKey("MY_PROJECT_KEY")); - ComponentDto ruby = db.components().insertComponent(newProjectDto(organizationDto).setKey("MY_PROJECT_KEY_OR_ELSE")); - dbClient.snapshotDao().insert(dbSession, newAnalysis(sonarqube), newAnalysis(ruby)); - db.components().indexAllComponents(); - db.commit(); - - db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, sonarqube); - db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, ruby); - - SearchMyProjectsWsResponse result = call_ws(ws.newRequest().setParam(TEXT_QUERY, "MY_PROJECT_KEY")); - - assertThat(result.getProjectsCount()).isEqualTo(1); - assertThat(result.getProjectsList()).extracting(Project::getId) - .containsOnlyOnce(sonarqube.uuid()) - .doesNotContain(ruby.uuid()); - } - @Test public void empty_response() { String result = ws.newRequest().execute().getInput(); @@ -293,14 +249,6 @@ public class SearchMyProjectsActionTest { call_ws(); } - @Test - public void fail_if_query_length_is_less_than_3_characters() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("The 'q' parameter must have at least 3 characters"); - - call_ws(ws.newRequest().setParam(TEXT_QUERY, "ab")); - } - private ComponentDto insertClang(OrganizationDto organizationDto) { return db.components().insertComponent(newProjectDto(organizationDto, Uuids.UUID_EXAMPLE_01) .setName("Clang") diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchMyProjectsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchMyProjectsRequest.java index e73c3a1a58a..7d1a06ba153 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchMyProjectsRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchMyProjectsRequest.java @@ -26,21 +26,14 @@ import javax.annotation.concurrent.Immutable; @Immutable public class SearchMyProjectsRequest { - private final String query; private final Integer page; private final Integer pageSize; private SearchMyProjectsRequest(Builder builder) { - this.query = builder.query; this.page = builder.page; this.pageSize = builder.pageSize; } - @CheckForNull - public String getQuery() { - return query; - } - @CheckForNull public Integer getPage() { return page; @@ -56,7 +49,6 @@ public class SearchMyProjectsRequest { } public static class Builder { - private String query; private Integer page; private Integer pageSize; @@ -64,11 +56,6 @@ public class SearchMyProjectsRequest { // enforce method constructor } - public Builder setQuery(@Nullable String query) { - this.query = query; - return this; - } - public Builder setPage(@Nullable Integer page) { this.page = page; return this; -- 2.39.5