]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8675 drop unused param "q" in api/projects/search_my_projects
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 24 Jan 2017 09:49:35 +0000 (10:49 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 25 Jan 2017 10:41:56 +0000 (11:41 +0100)
server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsAction.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchMyProjectsDataLoader.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchMyProjectsActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchMyProjectsRequest.java

index 5a58a1cb9ddd4cac135da13d86de7188f52815bc..2c6825e8db6e59944e0d54bfb620957c901daf91 100644 (file)
@@ -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<ComponentDto, Project> {
index 648faeeb49908527cac7c4afadf0891ac593c1b8..6e41546905eca511542ac11e86ebd11c9f328c54 100644 (file)
@@ -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<ComponentDto> projects = searchResult.projects;
-      List<String> projectUuids = Lists.transform(projects, ComponentDto::projectUuid);
-      List<ComponentLinkDto> projectLinks = dbClient.componentLinkDao().selectByComponentUuids(dbSession, projectUuids);
-      List<SnapshotDto> 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<MeasureDto> qualityGates = dbClient.measureDao().selectByQuery(dbSession, measureQuery);
+  SearchMyProjectsData load(DbSession dbSession, SearchMyProjectsRequest request) {
+    SearchMyProjectsData.Builder data = builder();
+    ProjectsResult searchResult = searchProjects(dbSession, request);
+    List<ComponentDto> projects = searchResult.projects;
+    List<String> projectUuids = Lists.transform(projects, ComponentDto::projectUuid);
+    List<ComponentLinkDto> projectLinks = dbClient.componentLinkDao().selectByComponentUuids(dbSession, projectUuids);
+    List<SnapshotDto> 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<MeasureDto> 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<Long> componentIds = dbClient.roleDao().selectComponentIdsByPermissionAndUserId(dbSession, UserRole.ADMIN, userId);
     ComponentQuery dbQuery = ComponentQuery.builder()
       .setQualifiers(Qualifiers.PROJECT)
-      .setNameOrKeyQuery(request.getQuery())
       .setComponentIds(ImmutableSet.copyOf(componentIds))
       .build();
 
index d5f894dfd79afb8728ec0cd98e592234b84d6b79..82a03bd37b65038f6d2e840d0d2380a2a9343ddf 100644 (file)
@@ -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")
index e73c3a1a58a7dbc67ffa15be71f56ee3ac08d3bf..7d1a06ba153409418be28983deb05b8bab97e20d 100644 (file)
@@ -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;