From ddfdce994fda7cbdc9d71b5f8f0e0e68e75f5352 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 2 Feb 2016 17:03:12 +0100 Subject: [PATCH] SONAR-6987 WS api/tests/list search tests by source file key --- .../org/sonar/server/test/ws/ListAction.java | 41 +++++++++----- .../server/test/index/TestIndexerTest.java | 4 -- .../sonar/server/test/ws/ListActionTest.java | 55 ++++++++++++++++++- .../org/sonar/server/test/ws/TestsWsTest.java | 4 +- 4 files changed, 83 insertions(+), 21 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java index 9981948ba5f..0087e4c0f5b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/test/ws/ListAction.java @@ -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 { "
  • " + TEST_FILE_ID + "
  • " + "
  • " + TEST_ID + "
  • " + "
  • " + SOURCE_FILE_ID + " and " + SOURCE_FILE_LINE_NUMBER + "
  • " + + "
  • " + SOURCE_FILE_KEY + "and" + SOURCE_FILE_LINE_NUMBER + "
  • " + "") .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 tests; Map 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 { - @Override - public String apply(@Nonnull TestDoc testDoc) { - return testDoc.fileUuid(); - } - } - private SearchResult 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 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 { + @Override + public String apply(@Nonnull TestDoc testDoc) { + return testDoc.fileUuid(); + } + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java index d8585b2f726..1f7c272b882 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexerTest.java @@ -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 hits = getDocuments(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/test/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/test/ws/ListActionTest.java index 84330f2bad3..67ff39371fa 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/test/ws/ListActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/test/ws/ListActionTest.java @@ -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(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java index 098f85818ec..f5fcb86ff13 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java @@ -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 -- 2.39.5