From 2fcad2e70a89b783aea8b81bd8eb59355be9285c Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 3 May 2016 19:14:58 +0200 Subject: [PATCH] SONAR-7432 Functional exception when snapshot not found in WS api/qualitygates/project_status --- .../qualitygate/ws/ProjectStatusAction.java | 2 - .../ws/ProjectStatusActionTest.java | 65 ++++++++++++------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ProjectStatusAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ProjectStatusAction.java index 416fb42496b..0278ac0857c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ProjectStatusAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ProjectStatusAction.java @@ -46,7 +46,6 @@ import org.sonar.server.ws.KeyExamples; import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse; import org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest; -import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Strings.isNullOrEmpty; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; @@ -142,7 +141,6 @@ public class ProjectStatusAction implements QGateWsAction { private ProjectAndSnapshot getProjectThenSnapshot(DbSession dbSession, ProjectStatusWsRequest request) { ComponentDto projectDto = componentFinder.getByUuidOrKey(dbSession, request.getProjectId(), request.getProjectKey(), ParamNames.PROJECT_ID_AND_KEY); SnapshotDto snapshotDto = dbClient.snapshotDao().selectLastSnapshotByComponentId(dbSession, projectDto.getId()); - checkState(snapshotDto != null, "Last analysis of project '%s' not found", projectDto.getKey()); return new ProjectAndSnapshot(projectDto, snapshotDto); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java index ca96b5c2c37..8df4871a692 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java @@ -31,6 +31,7 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.metric.MetricDto; @@ -57,7 +58,6 @@ import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_PROJECT_ID; import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_PROJECT_KEY; - public class ProjectStatusActionTest { private static final String ANALYSIS_ID = "task-uuid"; @Rule @@ -66,6 +66,7 @@ public class ProjectStatusActionTest { public UserSessionRule userSession = UserSessionRule.standalone(); @Rule public DbTester db = DbTester.create(System2.INSTANCE); + ComponentDbTester componentDb = new ComponentDbTester(db); WsActionTester ws; DbClient dbClient; @@ -83,8 +84,7 @@ public class ProjectStatusActionTest { public void json_example() throws IOException { userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN); - ComponentDto project = newProjectDto("project-uuid"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project) .setPeriodMode(1, "last_period") .setPeriodDate(1, 956789123456L) @@ -113,8 +113,7 @@ public class ProjectStatusActionTest { public void return_status_by_project_id() throws IOException { userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN); - ComponentDto project = newProjectDto("project-uuid"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project) .setPeriodMode(1, "last_period") .setPeriodDate(1, 956789123456L) @@ -143,9 +142,7 @@ public class ProjectStatusActionTest { public void return_status_by_project_key() throws IOException { userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN); - ComponentDto project = newProjectDto("project-uuid") - .setKey("project-key"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid").setKey("project-key")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project) .setPeriodMode(1, "last_period") .setPeriodDate(1, 956789123456L) @@ -174,12 +171,22 @@ public class ProjectStatusActionTest { public void return_undefined_status_if_measure_is_not_found() { userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN); - ComponentDto project = newProjectDto("project-uuid"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)); dbSession.commit(); - ProjectStatusWsResponse result = newRequest(snapshot.getId().toString()); + ProjectStatusWsResponse result = call(snapshot.getId().toString()); + + assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE); + assertThat(result.getProjectStatus().getConditionsCount()).isEqualTo(0); + } + + @Test + public void return_undefined_status_if_snapshot_is_not_found() { + userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN); + componentDb.insertComponent(newProjectDto("project-uuid")); + + ProjectStatusWsResponse result = callByProjectUuid("project-uuid"); assertThat(result.getProjectStatus().getStatus()).isEqualTo(Status.NONE); assertThat(result.getProjectStatus().getConditionsCount()).isEqualTo(0); @@ -189,36 +196,33 @@ public class ProjectStatusActionTest { public void not_fail_with_system_admin_permission() { userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN); - ComponentDto project = newProjectDto("project-uuid"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)); dbSession.commit(); - newRequest(snapshot.getId().toString()); + call(snapshot.getId().toString()); } @Test public void not_fail_with_global_scan_permission() { userSession.login("john").setGlobalPermissions(SCAN_EXECUTION); - ComponentDto project = newProjectDto("project-uuid"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)); dbSession.commit(); - newRequest(snapshot.getId().toString()); + call(snapshot.getId().toString()); } @Test public void not_fail_with_project_scan_permission() { - ComponentDto project = newProjectDto("project-uuid"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)); dbSession.commit(); userSession.login("john").addProjectUuidPermissions(SCAN_EXECUTION, project.uuid()); - newRequest(snapshot.getId().toString()); + call(snapshot.getId().toString()); } @Test @@ -228,20 +232,19 @@ public class ProjectStatusActionTest { expectedException.expect(NotFoundException.class); expectedException.expectMessage("Analysis with id 'task-uuid' is not found"); - newRequest(ANALYSIS_ID); + call(ANALYSIS_ID); } @Test public void fail_if_insufficient_privileges() { userSession.login("john").setGlobalPermissions(PROVISIONING); - ComponentDto project = newProjectDto("project-uuid"); - dbClient.componentDao().insert(dbSession, project); + ComponentDto project = componentDb.insertComponent(newProjectDto("project-uuid")); SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newSnapshotForProject(project)); dbSession.commit(); expectedException.expect(ForbiddenException.class); - newRequest(snapshot.getId().toString()); + call(snapshot.getId().toString()); } @Test @@ -263,7 +266,7 @@ public class ProjectStatusActionTest { ws.newRequest().execute().getInput(); } - private ProjectStatusWsResponse newRequest(String taskId) { + private ProjectStatusWsResponse call(String taskId) { try { return ProjectStatusWsResponse.parseFrom( ws.newRequest() @@ -274,4 +277,16 @@ public class ProjectStatusActionTest { throw Throwables.propagate(e); } } + + private ProjectStatusWsResponse callByProjectUuid(String projectUuid) { + try { + return ProjectStatusWsResponse.parseFrom( + ws.newRequest() + .setParam(PARAM_PROJECT_ID, projectUuid) + .setMediaType(MediaTypes.PROTOBUF) + .execute().getInputStream()); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } } -- 2.39.5