]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6987 WS api/tests/list search tests by source file key
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 2 Feb 2016 16:03:12 +0000 (17:03 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 3 Feb 2016 15:09:52 +0000 (16:09 +0100)
server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java
server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java
server/sonar-server/src/test/java/org/sonar/server/test/ws/ListActionTest.java
server/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java

index 9981948ba5f9f25b2186946339e8429a856b85bf..0087e4c0f5b42bc5c1809b2bf36db16f2ff88f99 100644 (file)
@@ -45,6 +45,7 @@ import org.sonar.server.test.index.CoveredFileDoc;
 import org.sonar.server.test.index.TestDoc;
 import org.sonar.server.test.index.TestIndex;
 import org.sonar.server.user.UserSession;
+import org.sonar.server.ws.KeyExamples;
 import org.sonar.server.ws.WsUtils;
 import org.sonarqube.ws.Common;
 import org.sonarqube.ws.WsTests;
@@ -56,6 +57,7 @@ public class ListAction implements TestsWsAction {
   public static final String TEST_FILE_ID = "testFileId";
   public static final String TEST_FILE_KEY = "testFileKey";
   public static final String SOURCE_FILE_ID = "sourceFileId";
+  public static final String SOURCE_FILE_KEY = "sourceFileKey";
   public static final String SOURCE_FILE_LINE_NUMBER = "sourceFileLineNumber";
 
   private final DbClient dbClient;
@@ -82,6 +84,7 @@ public class ListAction implements TestsWsAction {
           "<li>" + TEST_FILE_ID + "</li>" +
           "<li>" + TEST_ID + "</li>" +
           "<li>" + SOURCE_FILE_ID + " and " + SOURCE_FILE_LINE_NUMBER + "</li>" +
+          "<li>" + SOURCE_FILE_KEY + "and" + SOURCE_FILE_LINE_NUMBER + "</li>" +
           "</ul>")
       .setSince("5.2")
       .setResponseExample(Resources.getResource(getClass(), "tests-example-list.json"))
@@ -105,12 +108,18 @@ public class ListAction implements TestsWsAction {
 
     action
       .createParam(SOURCE_FILE_ID)
-      .setDescription("IF of source file. Must be provided with the source file line number.")
+      .setDescription("ID of source file. Must be provided with the source file line number.")
       .setExampleValue(Uuids.UUID_EXAMPLE_03);
 
+    action
+      .createParam(SOURCE_FILE_KEY)
+      .setSince("5.4")
+      .setDescription("Key of source file. Must be provided with the source file line number.")
+      .setExampleValue(KeyExamples.KEY_FILE_EXAMPLE_001);
+
     action
       .createParam(SOURCE_FILE_LINE_NUMBER)
-      .setDescription("Source file line number. Must be provided with the source file ID.")
+      .setDescription("Source file line number. Must be provided with the source file ID or key.")
       .setExampleValue("10");
   }
 
@@ -120,17 +129,17 @@ public class ListAction implements TestsWsAction {
     String testFileUuid = request.param(TEST_FILE_ID);
     String testFileKey = request.param(TEST_FILE_KEY);
     String sourceFileUuid = request.param(SOURCE_FILE_ID);
+    String sourceFileKey = request.param(SOURCE_FILE_KEY);
     Integer sourceFileLineNumber = request.paramAsInt(SOURCE_FILE_LINE_NUMBER);
     SearchOptions searchOptions = new SearchOptions().setPage(
       request.mandatoryParamAsInt(WebService.Param.PAGE),
-      request.mandatoryParamAsInt(WebService.Param.PAGE_SIZE)
-      );
+      request.mandatoryParamAsInt(WebService.Param.PAGE_SIZE));
 
     DbSession dbSession = dbClient.openSession(false);
     SearchResult<TestDoc> tests;
     Map<String, ComponentDto> componentsByTestFileUuid;
     try {
-      tests = searchTests(dbSession, testUuid, testFileUuid, testFileKey, sourceFileUuid, sourceFileLineNumber, searchOptions);
+      tests = searchTests(dbSession, testUuid, testFileUuid, testFileKey, sourceFileUuid, sourceFileKey, sourceFileLineNumber, searchOptions);
       componentsByTestFileUuid = buildComponentsByTestFileUuid(dbSession, tests.getDocs());
     } finally {
       MyBatis.closeQuietly(dbSession);
@@ -185,15 +194,8 @@ public class ListAction implements TestsWsAction {
     return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid());
   }
 
-  private static class TestToFileUuidFunction implements Function<TestDoc, String> {
-    @Override
-    public String apply(@Nonnull TestDoc testDoc) {
-      return testDoc.fileUuid();
-    }
-  }
-
   private SearchResult<TestDoc> searchTests(DbSession dbSession, @Nullable String testUuid, @Nullable String testFileUuid, @Nullable String testFileKey,
-    @Nullable String sourceFileUuid, @Nullable Integer sourceFileLineNumber, SearchOptions searchOptions) {
+    @Nullable String sourceFileUuid, @Nullable String sourceFileKey, @Nullable Integer sourceFileLineNumber, SearchOptions searchOptions) {
     if (testUuid != null) {
       return searchTestsByTestUuid(dbSession, testUuid, searchOptions);
     }
@@ -206,10 +208,14 @@ public class ListAction implements TestsWsAction {
     if (sourceFileUuid != null && sourceFileLineNumber != null) {
       return searchTestsBySourceFileUuidAndLineNumber(dbSession, sourceFileUuid, sourceFileLineNumber, searchOptions);
     }
+    if (sourceFileKey != null && sourceFileLineNumber != null) {
+      ComponentDto component = componentFinder.getByKey(dbSession, sourceFileKey);
+      return searchTestsBySourceFileUuidAndLineNumber(dbSession, component.uuid(), sourceFileLineNumber, searchOptions);
+    }
 
     throw new IllegalArgumentException(
       "One (and only one) of the following combination of parameters must be provided: 1) test UUID. 2) test file UUID. " +
-        "3) test file key. 4) source file UUID and source file line number.");
+        "3) test file key. 4) source file ID or key with a source file line number.");
   }
 
   private SearchResult<TestDoc> searchTestsBySourceFileUuidAndLineNumber(DbSession dbSession, String sourceFileUuid, Integer sourceFileLineNumber, SearchOptions searchOptions) {
@@ -238,4 +244,11 @@ public class ListAction implements TestsWsAction {
     ComponentDto component = dbClient.componentDao().selectOrFailByUuid(dbSession, componentUuid);
     userSession.checkComponentUuidPermission(UserRole.CODEVIEWER, component.projectUuid());
   }
+
+  private static class TestToFileUuidFunction implements Function<TestDoc, String> {
+    @Override
+    public String apply(@Nonnull TestDoc testDoc) {
+      return testDoc.fileUuid();
+    }
+  }
 }
index d8585b2f72658cee94a4edbef4bcc09c425f245e..1f7c272b8828cd13eed5f61fe2eba2aa7792fe7c 100644 (file)
@@ -169,10 +169,6 @@ public class TestIndexerTest {
     indexTest("P1", "F2", "T1", "U121");
     indexTest("P2", "F3", "T1", "U231");
 
-    for (SearchHit hit : getDocuments()) {
-      System.out.println("BEFORE " + hit.getSourceAsString());
-    }
-
     underTest.deleteByProject("P1");
 
     List<SearchHit> hits = getDocuments();
index 84330f2bad34ddd861fb95acf7542c36efe4c11f..67ff39371fa31f31c4986ba1740b0dd75729c400 100644 (file)
@@ -142,7 +142,7 @@ public class ListActionTest {
   }
 
   @Test
-  public void list_based_on_main_file_and_line_number() throws Exception {
+  public void list_based_on_source_file_uuid_and_line_number() throws Exception {
     String mainFileUuid = "MAIN-FILE-UUID";
     userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, TestFile1.PROJECT_UUID);
     dbClient.componentDao().insert(db.getSession(),
@@ -192,6 +192,59 @@ public class ListActionTest {
     request.execute().assertJson(getClass(), "list-main-file.json");
   }
 
+  @Test
+  public void list_based_on_source_file_key_and_line_number() throws Exception {
+    String sourceFileUuid = "MAIN-FILE-UUID";
+    String sourceFileKey = "MAIN-FILE-KEY";
+    userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, TestFile1.PROJECT_UUID);
+    dbClient.componentDao().insert(db.getSession(),
+      new ComponentDto()
+        .setUuid(TestFile1.FILE_UUID)
+        .setLongName(TestFile1.LONG_NAME)
+        .setKey(TestFile1.KEY)
+        .setProjectUuid(TestFile1.PROJECT_UUID),
+      new ComponentDto()
+        .setUuid(TestFile2.FILE_UUID)
+        .setLongName(TestFile2.LONG_NAME)
+        .setProjectUuid(TestFile2.PROJECT_UUID)
+        .setKey(TestFile2.KEY),
+      new ComponentDto()
+        .setUuid(sourceFileUuid)
+        .setKey(sourceFileKey)
+        .setProjectUuid(TestFile1.PROJECT_UUID)
+    );
+    db.getSession().commit();
+
+    es.putDocuments(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE,
+      new TestDoc()
+        .setUuid(TestFile1.UUID)
+        .setProjectUuid(TestFile1.PROJECT_UUID)
+        .setName(TestFile1.NAME)
+        .setFileUuid(TestFile1.FILE_UUID)
+        .setDurationInMs(TestFile1.DURATION_IN_MS)
+        .setStatus(TestFile1.STATUS)
+        .setMessage(TestFile1.MESSAGE)
+        .setCoveredFiles(TestFile1.COVERED_FILES)
+        .setStackTrace(TestFile1.STACKTRACE),
+      new TestDoc()
+        .setUuid(TestFile2.UUID)
+        .setProjectUuid(TestFile2.PROJECT_UUID)
+        .setName(TestFile2.NAME)
+        .setFileUuid(TestFile2.FILE_UUID)
+        .setDurationInMs(TestFile2.DURATION_IN_MS)
+        .setStatus(TestFile2.STATUS)
+        .setStackTrace(TestFile2.STATUS)
+        .setMessage(TestFile2.MESSAGE)
+        .setCoveredFiles(TestFile2.COVERED_FILES)
+        .setStackTrace(TestFile2.STACKTRACE));
+
+    WsTester.TestRequest request = ws.newGetRequest("api/tests", "list")
+      .setParam(ListAction.SOURCE_FILE_KEY, sourceFileKey)
+      .setParam(ListAction.SOURCE_FILE_LINE_NUMBER, "10");
+
+    request.execute().assertJson(getClass(), "list-main-file.json");
+  }
+
   @Test(expected = IllegalArgumentException.class)
   public void fail_when_no_argument() throws Exception {
     ws.newGetRequest("api/tests", "list").execute();
index 098f85818ec6112e8139950c7b64a81f578101d6..f5fcb86ff13da383d9365a592c53d9078910186c 100644 (file)
@@ -23,8 +23,8 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
 import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.db.DbClient;
 import org.sonar.server.test.index.TestIndex;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
@@ -62,7 +62,7 @@ public class TestsWsTest {
     assertThat(action.isPost()).isFalse();
     assertThat(action.handler()).isNotNull();
     assertThat(action.responseExampleAsString()).isNotEmpty();
-    assertThat(action.params()).hasSize(7);
+    assertThat(action.params()).hasSize(8);
   }
 
   @Test