From ee1c192ab4f9bb53f8089abf277cc0a81c24361b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 11 Aug 2017 16:01:46 +0200 Subject: [PATCH] SONAR-9708 Return incremental field in api/ce/component --- .../server/ce/ws/ComponentActionTest.java | 118 ++++++++++++------ sonar-ws/src/main/protobuf/ws-ce.proto | 2 +- 2 files changed, 82 insertions(+), 38 deletions(-) diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java index 1da27f81fb3..d153fa0f9d4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.ce.ws; +import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -29,6 +30,7 @@ import org.sonar.db.ce.CeActivityDto; 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; @@ -39,7 +41,7 @@ import org.sonarqube.ws.MediaTypes; 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; @@ -50,19 +52,19 @@ public class ComponentActionTest { @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); @@ -71,17 +73,19 @@ public class ComponentActionTest { @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"); @@ -89,36 +93,40 @@ public class ComponentActionTest { // 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 @@ -126,6 +134,38 @@ public class ComponentActionTest { 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); @@ -137,7 +177,7 @@ public class ComponentActionTest { @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); @@ -151,7 +191,7 @@ public class ComponentActionTest { @Test public void fail_when_no_component_parameter() { expectedException.expect(IllegalArgumentException.class); - logInWithBrowsePermission(dbTester.components().insertPrivateProject()); + logInWithBrowsePermission(db.components().insertPrivateProject()); ws.newRequest().execute(); } @@ -160,28 +200,32 @@ public class ComponentActionTest { 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; } } diff --git a/sonar-ws/src/main/protobuf/ws-ce.proto b/sonar-ws/src/main/protobuf/ws-ce.proto index e6f69926046..dd869eaa1c3 100644 --- a/sonar-ws/src/main/protobuf/ws-ce.proto +++ b/sonar-ws/src/main/protobuf/ws-ce.proto @@ -50,7 +50,7 @@ message ActivityStatusWsResponse { optional int32 failing = 2; } -// GET api/ce/project +// GET api/ce/component message ProjectResponse { repeated Task queue = 1; optional Task current = 2; -- 2.39.5