]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9551 WS api/project_analyses/search allows application
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 25 Jul 2017 08:55:43 +0000 (10:55 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 31 Jul 2017 09:27:51 +0000 (11:27 +0200)
server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/SearchAction.java
server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/SearchActionTest.java

index 1acead3883eb62d350836e43b43b24efe8d54ef9..3eddef80ffac802e74119ae8a173d52fe08d1732 100644 (file)
  */
 package org.sonar.server.projectanalysis.ws;
 
+import com.google.common.collect.ImmutableSet;
 import java.util.EnumSet;
 import java.util.List;
+import java.util.Set;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.server.ws.Request;
@@ -56,6 +58,8 @@ import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameter
 import static org.sonarqube.ws.client.projectanalysis.SearchRequest.DEFAULT_PAGE_SIZE;
 
 public class SearchAction implements ProjectAnalysesWsAction {
+  private static final Set<String> ALLOWED_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.APP);
+
   private final DbClient dbClient;
   private final ComponentFinder componentFinder;
   private final UserSession userSession;
@@ -149,7 +153,7 @@ public class SearchAction implements ProjectAnalysesWsAction {
 
   private void addProject(SearchData.Builder data) {
     ComponentDto project = componentFinder.getByKey(data.getDbSession(), data.getRequest().getProject());
-    checkArgument(Scopes.PROJECT.equals(project.scope()) && Qualifiers.PROJECT.equals(project.qualifier()), "A project is required");
+    checkArgument(Scopes.PROJECT.equals(project.scope()) && ALLOWED_QUALIFIERS.contains(project.qualifier()), "A project or application is required");
     data.setProject(project);
   }
 
index b40b4209ba4d489758acf3d10d529a6903846e81..9765c2f6b183f4e3dad324bc59ce97d7910113e7 100644 (file)
@@ -159,6 +159,21 @@ public class SearchActionTest {
       tuple(e2.getUuid(), e2.getCategory(), e2.getName(), e2.getDescription()));
   }
 
+  @Test
+  public void return_analyses_of_application() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto application = db.components().insertApplication(organization);
+    userSession.registerComponents(application);
+    SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
+    SnapshotDto secondAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(2_000_000L));
+    SnapshotDto thirdAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(3_000_000L));
+
+    List<Analysis> result = call(application.key()).getAnalysesList();
+
+    assertThat(result).hasSize(3);
+    assertThat(result).extracting(Analysis::getKey).containsExactly(thirdAnalysis.getUuid(), secondAnalysis.getUuid(), firstAnalysis.getUuid());
+  }
+
   @Test
   public void paginate_analyses() {
     ComponentDto project = db.components().insertPrivateProject();
@@ -305,6 +320,18 @@ public class SearchActionTest {
       .doesNotContain(a1.getUuid(), a4.getUuid());
   }
 
+  @Test
+  public void empty_response() {
+    ComponentDto project = db.components().insertPrivateProject();
+    userSession.addProjectPermission(UserRole.USER, project);
+
+    SearchResponse result = call(project.key());
+
+    assertThat(result.hasPaging()).isTrue();
+    assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 0);
+    assertThat(result.getAnalysesCount()).isEqualTo(0);
+  }
+
   @Test
   public void definition() {
     WebService.Action definition = ws.getDef();
@@ -339,6 +366,18 @@ public class SearchActionTest {
     call("P1");
   }
 
+  @Test
+  public void fail_if_not_a_project_or_application() {
+    ComponentDto view = db.components().insertView();
+    db.components().insertSnapshot(newAnalysis(view));
+    userSession.registerComponents(view);
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("A project or application is required");
+
+    call(view.key());
+  }
+
   private static Function<Event, String> wsToDbCategory() {
     return e -> e == null ? null : EventCategory.valueOf(e.getCategory()).getLabel();
   }