]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7432 Functional exception when snapshot not found in WS api/qualitygates/projec...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 3 May 2016 17:14:58 +0000 (19:14 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 6 May 2016 09:14:12 +0000 (11:14 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ProjectStatusAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java

index 416fb42496bf8ceadab26be16a2744ea2698df92..0278ac0857c5eaa4a9f42ed9a1c38c006b5348d4 100644 (file)
@@ -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);
   }
 
index ca96b5c2c37ef8b395cd633e11469944d86fc388..8df4871a69239ccc8d440b1904aad98107d6e8ba 100644 (file)
@@ -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);
+    }
+  }
 }