*/
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;
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;
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);
}
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();
.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();
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();
}