*/
package org.sonar.server.ce.ws;
+import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.SnapshotDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonarqube.ws.WsCe;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
+import static org.sonar.db.ce.CeActivityDto.Status.SUCCESS;
import static org.sonar.server.ce.ws.ComponentAction.PARAM_COMPONENT_ID;
import static org.sonar.server.ce.ws.ComponentAction.PARAM_COMPONENT_KEY;
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ public DbTester db = DbTester.create(System2.INSTANCE);
- private TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), System2.INSTANCE);
- private ComponentAction underTest = new ComponentAction(userSession, dbTester.getDbClient(), formatter, TestComponentFinder.from(dbTester));
+ private TaskFormatter formatter = new TaskFormatter(db.getDbClient(), System2.INSTANCE);
+ private ComponentAction underTest = new ComponentAction(userSession, db.getDbClient(), formatter, TestComponentFinder.from(db));
private WsActionTester ws = new WsActionTester(underTest);
@Test
public void empty_queue_and_empty_activity() {
- ComponentDto project = dbTester.components().insertComponent(newPrivateProjectDto(dbTester.organizations().insert(), "PROJECT_1"));
+ ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
WsCe.ProjectResponse response = ws.newRequest()
- .setParam("componentId", "PROJECT_1")
+ .setParam("componentId", project.uuid())
.executeProtobuf(WsCe.ProjectResponse.class);
assertThat(response.getQueueCount()).isEqualTo(0);
@Test
public void project_tasks() {
- OrganizationDto organizationDto = dbTester.organizations().insert();
- ComponentDto project = dbTester.components().insertComponent(newPrivateProjectDto(organizationDto, "PROJECT_1"));
- userSession.addProjectPermission(UserRole.USER, project);
- insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
- insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
- insertActivity("T3", "PROJECT_1", CeActivityDto.Status.FAILED);
- insertQueue("T4", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS);
- insertQueue("T5", "PROJECT_1", CeQueueDto.Status.PENDING);
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project1 = db.components().insertPrivateProject(organization);
+ SnapshotDto analysisProject1 = db.components().insertSnapshot(project1);
+ ComponentDto project2 = db.components().insertPrivateProject(organization);
+ userSession.addProjectPermission(UserRole.USER, project1);
+ insertActivity("T1", project1,CeActivityDto.Status.SUCCESS, analysisProject1);
+ insertActivity("T2", project2, CeActivityDto.Status.FAILED, null);
+ insertActivity("T3", project1,CeActivityDto.Status.FAILED, null);
+ insertQueue("T4", project1,CeQueueDto.Status.IN_PROGRESS);
+ insertQueue("T5", project1,CeQueueDto.Status.PENDING);
WsCe.ProjectResponse response = ws.newRequest()
- .setParam("componentId", "PROJECT_1")
+ .setParam("componentId", project1.uuid())
.executeProtobuf(WsCe.ProjectResponse.class);
assertThat(response.getQueueCount()).isEqualTo(2);
assertThat(response.getQueue(0).getId()).isEqualTo("T4");
// T3 is the latest task executed on PROJECT_1
assertThat(response.hasCurrent()).isTrue();
assertThat(response.getCurrent().getId()).isEqualTo("T3");
+ assertThat(response.getCurrent().hasAnalysisId()).isFalse();
assertThat(response.getQueueList())
.extracting(WsCe.Task::getOrganization)
- .containsOnly(organizationDto.getKey());
- assertThat(response.getCurrent().getOrganization()).isEqualTo(organizationDto.getKey());
+ .containsOnly(organization.getKey());
+ assertThat(response.getCurrent().getOrganization()).isEqualTo(organization.getKey());
}
@Test
public void search_tasks_by_component_key() {
- ComponentDto project = dbTester.components().insertPrivateProject();
+ ComponentDto project = db.components().insertPrivateProject();
logInWithBrowsePermission(project);
- insertActivity("T1", project.uuid(), CeActivityDto.Status.SUCCESS);
+ SnapshotDto analysis = db.components().insertSnapshot(project);
+ insertActivity("T1", project, CeActivityDto.Status.SUCCESS, analysis);
WsCe.ProjectResponse response = ws.newRequest()
.setParam(PARAM_COMPONENT_KEY, project.getDbKey())
.executeProtobuf(WsCe.ProjectResponse.class);
assertThat(response.hasCurrent()).isTrue();
+ assertThat(response.getCurrent().getId()).isEqualTo("T1");
+ assertThat(response.getCurrent().getAnalysisId()).isEqualTo(analysis.getUuid());
}
@Test
public void canceled_tasks_must_not_be_picked_as_current_analysis() {
- ComponentDto project = dbTester.components().insertComponent(newPrivateProjectDto(dbTester.getDefaultOrganization(), "PROJECT_1"));
+ ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
- insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
- insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
- insertActivity("T3", "PROJECT_1", CeActivityDto.Status.SUCCESS);
- insertActivity("T4", "PROJECT_1", CeActivityDto.Status.CANCELED);
- insertActivity("T5", "PROJECT_1", CeActivityDto.Status.CANCELED);
+ insertActivity("T1", project, CeActivityDto.Status.SUCCESS);
+ insertActivity("T2", project, CeActivityDto.Status.FAILED);
+ insertActivity("T3", project, CeActivityDto.Status.SUCCESS);
+ insertActivity("T4", project, CeActivityDto.Status.CANCELED);
+ insertActivity("T5", project, CeActivityDto.Status.CANCELED);
WsCe.ProjectResponse response = ws.newRequest()
- .setParam("componentId", "PROJECT_1")
+ .setParam("componentId", project.uuid())
.executeProtobuf(WsCe.ProjectResponse.class);
assertThat(response.getQueueCount()).isEqualTo(0);
// T3 is the latest task executed on PROJECT_1 ignoring Canceled ones
assertThat(response.getCurrent().getId()).isEqualTo("T3");
}
+ @Test
+ public void incremental_analysis_by_component_id() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ SnapshotDto incrementalAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(true));
+ insertActivity("T1", project, SUCCESS, incrementalAnalysis);
+
+ WsCe.ProjectResponse response = ws.newRequest()
+ .setParam("componentId", project.uuid())
+ .executeProtobuf(WsCe.ProjectResponse.class);
+
+ assertThat(response.getCurrent())
+ .extracting(WsCe.Task::getId, WsCe.Task::getIncremental)
+ .containsExactlyInAnyOrder("T1", true);
+ }
+
+ @Test
+ public void incremental_analysis_by_component_key() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ SnapshotDto incrementalAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(true));
+ insertActivity("T1", project, SUCCESS, incrementalAnalysis);
+
+ WsCe.ProjectResponse response = ws.newRequest()
+ .setParam("componentKey", project.getKey())
+ .executeProtobuf(WsCe.ProjectResponse.class);
+
+ assertThat(response.getCurrent())
+ .extracting(WsCe.Task::getId, WsCe.Task::getIncremental)
+ .containsExactlyInAnyOrder("T1", true);
+ }
+
@Test
public void fail_with_404_when_component_does_not_exist() throws Exception {
expectedException.expect(NotFoundException.class);
@Test
public void throw_ForbiddenException_if_user_cant_access_project() {
- ComponentDto project = dbTester.components().insertPrivateProject();
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn();
expectedException.expect(ForbiddenException.class);
@Test
public void fail_when_no_component_parameter() {
expectedException.expect(IllegalArgumentException.class);
- logInWithBrowsePermission(dbTester.components().insertPrivateProject());
+ logInWithBrowsePermission(db.components().insertPrivateProject());
ws.newRequest().execute();
}
userSession.logIn().addProjectPermission(UserRole.USER, project);
}
- private CeQueueDto insertQueue(String taskUuid, String componentUuid, CeQueueDto.Status status) {
+ private CeQueueDto insertQueue(String taskUuid, ComponentDto component, CeQueueDto.Status status) {
CeQueueDto queueDto = new CeQueueDto();
queueDto.setTaskType(CeTaskTypes.REPORT);
- queueDto.setComponentUuid(componentUuid);
+ queueDto.setComponentUuid(component.uuid());
queueDto.setUuid(taskUuid);
queueDto.setStatus(status);
- dbTester.getDbClient().ceQueueDao().insert(dbTester.getSession(), queueDto);
- dbTester.getSession().commit();
+ db.getDbClient().ceQueueDao().insert(db.getSession(), queueDto);
+ db.getSession().commit();
return queueDto;
}
- private CeActivityDto insertActivity(String taskUuid, String componentUuid, CeActivityDto.Status status) {
+ private CeActivityDto insertActivity(String taskUuid, ComponentDto component, CeActivityDto.Status status) {
+ return insertActivity(taskUuid, component, status, db.components().insertSnapshot(component));
+ }
+
+ private CeActivityDto insertActivity(String taskUuid, ComponentDto component, CeActivityDto.Status status, @Nullable SnapshotDto analysis) {
CeQueueDto queueDto = new CeQueueDto();
queueDto.setTaskType(CeTaskTypes.REPORT);
- queueDto.setComponentUuid(componentUuid);
+ queueDto.setComponentUuid(component.uuid());
queueDto.setUuid(taskUuid);
CeActivityDto activityDto = new CeActivityDto(queueDto);
activityDto.setStatus(status);
activityDto.setExecutionTimeMs(500L);
- activityDto.setAnalysisUuid("U1");
- dbTester.getDbClient().ceActivityDao().insert(dbTester.getSession(), activityDto);
- dbTester.getSession().commit();
+ activityDto.setAnalysisUuid(analysis == null ? null : analysis.getUuid());
+ db.getDbClient().ceActivityDao().insert(db.getSession(), activityDto);
+ db.getSession().commit();
return activityDto;
}
}