]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8644 Do not fail with error 500 on unknown component
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 27 Jan 2017 10:18:59 +0000 (11:18 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 30 Jan 2017 12:26:35 +0000 (13:26 +0100)
server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java
server/sonar-server/src/test/java/org/sonar/server/test/ws/ListActionTest.java

index 832dc6fe41a977180b76a87708048b6ee03780a8..a9f1251e82df80d9ee64a5f8a7b38eac88f9c304 100644 (file)
@@ -227,9 +227,8 @@ public class ListAction implements TestsWsAction {
   }
 
   private SearchResult<TestDoc> searchTestsByTestFileKey(DbSession dbSession, String testFileKey, SearchOptions searchOptions) {
-    userSession.checkComponentPermission(UserRole.CODEVIEWER, testFileKey);
     ComponentDto testFile = componentFinder.getByKey(dbSession, testFileKey);
-
+    userSession.checkComponentUuidPermission(UserRole.CODEVIEWER, testFile.projectUuid());
     return testIndex.searchByTestFileUuid(testFile.uuid(), searchOptions);
   }
 
@@ -245,7 +244,7 @@ public class ListAction implements TestsWsAction {
   }
 
   private void checkComponentUuidPermission(DbSession dbSession, String componentUuid) {
-    ComponentDto component = dbClient.componentDao().selectOrFailByUuid(dbSession, componentUuid);
+    ComponentDto component = componentFinder.getByUuid(dbSession, componentUuid);
     userSession.checkComponentUuidPermission(UserRole.CODEVIEWER, component.projectUuid());
   }
 
index 066efff7b687e6b462b4fa6918b07543b2b6b0b4..ed629fdc5c0b1acd0182e33ce83dafd8d5c49818 100644 (file)
@@ -27,7 +27,6 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.config.MapSettings;
 import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
@@ -72,7 +71,7 @@ public class ListActionTest {
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create();
 
   private DbClient dbClient = db.getDbClient();
 
@@ -93,7 +92,7 @@ public class ListActionTest {
   }
 
   @Test
-  public void return_test() throws Exception {
+  public void list_tests() throws Exception {
     userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
     DbFileSources.Test test = newTest(mainFile, 10, 11, 12, 20, 21, 25).setStatus(OK).build();
     insertTests(testFile, test);
@@ -115,7 +114,7 @@ public class ListActionTest {
   }
 
   @Test
-  public void return_tests_based_on_test_uuid() throws Exception {
+  public void list_tests_by_test_uuid() throws Exception {
     userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
     DbFileSources.Test test1 = newTest(mainFile, 10).build();
     DbFileSources.Test test2 = newTest(mainFile, 11).build();
@@ -127,7 +126,7 @@ public class ListActionTest {
   }
 
   @Test
-  public void return_tests_based_on_test_file_uuid() throws Exception {
+  public void list_tests_by_test_file_uuid() throws Exception {
     userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
     ComponentDto anotherTestFile = db.components().insertComponent(newFileDto(project));
     DbFileSources.Test test1 = newTest(mainFile, 10).build();
@@ -142,8 +141,8 @@ public class ListActionTest {
   }
 
   @Test
-  public void return_tests_based_on_test_file_key() throws Exception {
-    userSessionRule.addComponentPermission(UserRole.CODEVIEWER, project.key(), testFile.key());
+  public void list_tests_by_test_file_key() throws Exception {
+    userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
     ComponentDto anotherTestFile = db.components().insertComponent(newFileDto(project));
     DbFileSources.Test test1 = newTest(mainFile, 10).build();
     DbFileSources.Test test2 = newTest(mainFile, 11).build();
@@ -157,7 +156,7 @@ public class ListActionTest {
   }
 
   @Test
-  public void return_tests_based_on_source_file_uuid_and_line_number() throws Exception {
+  public void list_tests_by_source_file_uuid_and_line_number() throws Exception {
     userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
     ComponentDto anotherMainFile = db.components().insertComponent(newFileDto(project));
     DbFileSources.Test test1 = newTest(mainFile, 10, 11, 12).build();
@@ -172,7 +171,7 @@ public class ListActionTest {
   }
 
   @Test
-  public void return_tests_based_on_source_file_key_and_line_number() throws Exception {
+  public void list_tests_by_source_file_key_and_line_number() throws Exception {
     userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
     ComponentDto anotherMainFile = db.components().insertComponent(newFileDto(project));
     DbFileSources.Test test1 = newTest(mainFile, 10, 11, 12).build();
@@ -187,7 +186,7 @@ public class ListActionTest {
   }
 
   @Test
-  public void return_pagination() throws Exception {
+  public void tests_are_paginated() throws Exception {
     userSessionRule.addProjectUuidPermissions(CODEVIEWER, project.uuid());
     insertTests(testFile, newTest(mainFile, 10).build(), newTest(mainFile, 11).build(), newTest(mainFile, 12).build());
 
@@ -253,13 +252,32 @@ public class ListActionTest {
 
   @Test
   public void fail_when_test_uuid_is_unknown() throws Exception {
-    userSessionRule.addProjectUuidPermissions(USER, project.uuid());
-    DbFileSources.Test test = newTest(mainFile, 10).build();
-    insertTests(testFile, test);
+    expectedException.expect(NotFoundException.class);
+    call(ws.newRequest().setParam(TEST_ID, "unknown"));
+  }
+
+  @Test
+  public void fail_when_test_file_id_is_unknown() throws Exception {
+    expectedException.expect(NotFoundException.class);
+    call(ws.newRequest().setParam(TEST_FILE_ID, "unknown"));
+  }
 
+  @Test
+  public void fail_when_test_file_key_is_unknown() throws Exception {
+    expectedException.expect(NotFoundException.class);
+    call(ws.newRequest().setParam(TEST_FILE_ID, "unknown"));
+  }
+
+  @Test
+  public void fail_when_source_file_id_is_unknown() throws Exception {
+    expectedException.expect(NotFoundException.class);
+    call(ws.newRequest().setParam(SOURCE_FILE_ID, "unknown").setParam(SOURCE_FILE_LINE_NUMBER, "10"));
+  }
+
+  @Test
+  public void fail_when_source_file_key_is_unknown() throws Exception {
     expectedException.expect(NotFoundException.class);
-    expectedException.expectMessage("Test with id 'unknown-test-uuid' is not found");
-    call(ws.newRequest().setParam(TEST_ID, "unknown-test-uuid"));
+    call(ws.newRequest().setParam(SOURCE_FILE_KEY, "unknown").setParam(SOURCE_FILE_LINE_NUMBER, "10"));
   }
 
   private void insertTests(ComponentDto testFile, DbFileSources.Test... tests) {