diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-05-10 16:30:24 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-05-11 11:08:30 +0200 |
commit | 45fe2d05b66ad79545b10fdd89a31fdc9ebb632a (patch) | |
tree | 6aa16b9021e1d33aa6eeda6f1349b56c00eeed5e /server | |
parent | cb0937144b627e3167a363270db86b5950e1c2fc (diff) | |
download | sonarqube-45fe2d05b66ad79545b10fdd89a31fdc9ebb632a.tar.gz sonarqube-45fe2d05b66ad79545b10fdd89a31fdc9ebb632a.zip |
SONAR-7607 Log flooding in WS api/tests/list when testId is unknown
Diffstat (limited to 'server')
3 files changed, 52 insertions, 9 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 922934c0020..832dc6fe41a 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 @@ -35,7 +35,6 @@ import org.sonar.api.web.UserRole; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.db.MyBatis; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDtoFunctions; import org.sonar.server.component.ComponentFinder; @@ -51,6 +50,7 @@ import org.sonarqube.ws.Common; import org.sonarqube.ws.WsTests; import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static org.sonar.server.ws.WsUtils.checkFoundWithOptional; public class ListAction implements TestsWsAction { public static final String TEST_ID = "testId"; @@ -86,7 +86,8 @@ public class ListAction implements TestsWsAction { "<li>%s - get the tests in a test file</li>" + "<li>%s and %6$s - get the tests that cover a specific line of code</li>" + "<li>%s and %6$s - get the tests that cover a specific line of code</li>" + - "</ul>", TEST_ID, TEST_FILE_ID, TEST_FILE_KEY, SOURCE_FILE_ID, SOURCE_FILE_KEY, SOURCE_FILE_LINE_NUMBER)) + "</ul>", + TEST_ID, TEST_FILE_ID, TEST_FILE_KEY, SOURCE_FILE_ID, SOURCE_FILE_KEY, SOURCE_FILE_LINE_NUMBER)) .setSince("5.2") .setResponseExample(Resources.getResource(getClass(), "tests-example-list.json")) .setDeprecatedSince("5.6") @@ -144,7 +145,7 @@ public class ListAction implements TestsWsAction { tests = searchTests(dbSession, testUuid, testFileUuid, testFileKey, sourceFileUuid, sourceFileKey, sourceFileLineNumber, searchOptions); componentsByTestFileUuid = buildComponentsByTestFileUuid(dbSession, tests.getDocs()); } finally { - MyBatis.closeQuietly(dbSession); + dbClient.closeSession(dbSession); } WsTests.ListResponse.Builder responseBuilder = WsTests.ListResponse.newBuilder(); @@ -238,7 +239,8 @@ public class ListAction implements TestsWsAction { } private SearchResult<TestDoc> searchTestsByTestUuid(DbSession dbSession, String testUuid, SearchOptions searchOptions) { - checkComponentUuidPermission(dbSession, testIndex.getByTestUuid(testUuid).fileUuid()); + TestDoc testDoc = checkFoundWithOptional(testIndex.getNullableByTestUuid(testUuid), "Test with id '%s' is not found", testUuid); + checkComponentUuidPermission(dbSession, testDoc.fileUuid()); return testIndex.searchByTestUuid(testUuid, searchOptions); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexTest.java index aebb5d98cda..5dae3fda48f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/test/index/TestIndexTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.test.index; +import com.google.common.base.Optional; import java.util.Arrays; import java.util.List; import org.junit.Before; @@ -29,6 +30,7 @@ import org.sonar.server.es.EsTester; import org.sonar.server.es.SearchOptions; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.guava.api.Assertions.assertThat; public class TestIndexTest { @ClassRule @@ -99,6 +101,33 @@ public class TestIndexTest { } @Test + public void getNullableByTestUuid() throws Exception { + es.putDocuments(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, + newTestDoc("1", "TESTFILE1", newCoverageBlock("3"), newCoverageBlock("4"), newCoverageBlock("5")), + newTestDoc("2", "TESTFILE1", newCoverageBlock("5"), newCoverageBlock("6"), newCoverageBlock("7"))); + + Optional<TestDoc> result = underTest.getNullableByTestUuid("1"); + + assertThat(result).isPresent(); + TestDoc test = result.get(); + assertThat(test.testUuid()).isEqualTo("1"); + assertThat(test.fileUuid()).isEqualTo("TESTFILE1"); + assertThat(test.name()).isEqualTo("name-1"); + assertThat(test.durationInMs()).isEqualTo(1L); + assertThat(test.status()).isEqualTo("status-1"); + assertThat(test.message()).isEqualTo("message-1"); + assertThat(test.coveredFiles()).hasSize(3); + assertThat(test.coveredFiles()).extractingResultOf("fileUuid").containsOnly("main-uuid-3", "main-uuid-4", "main-uuid-5"); + } + + @Test + public void getNullableByTestUuid_with_absent_value() { + Optional<TestDoc> result = underTest.getNullableByTestUuid("unknown-uuid"); + + assertThat(result).isAbsent(); + } + + @Test public void searchByTestUuid_with_SearchOptions() throws Exception { es.putDocuments(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, newTestDoc("1", "TESTFILE1", newCoverageBlock("3"), newCoverageBlock("4"), newCoverageBlock("5")), 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 18589727abd..f015a432a6f 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 @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.config.Settings; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; @@ -34,6 +35,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.test.index.CoveredFileDoc; import org.sonar.server.test.index.TestDoc; import org.sonar.server.test.index.TestIndex; @@ -44,18 +46,18 @@ import org.sonar.server.ws.WsTester; public class ListActionTest { - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - @ClassRule public static EsTester es = new EsTester().addDefinitions(new TestIndexDefinition(new Settings())); @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); - + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); DbClient dbClient = db.getDbClient(); - TestIndex testIndex; + TestIndex testIndex; WsTester ws; @Before @@ -201,6 +203,16 @@ public class ListActionTest { .execute(); } + @Test + public void fail_when_test_uuid_is_unknown() throws Exception { + expectedException.expect(NotFoundException.class); + expectedException.expectMessage("Test with id 'unknown-test-uuid' is not found"); + + ws.newGetRequest("api/tests", "list") + .setParam(ListAction.TEST_ID, "unknown-test-uuid") + .execute(); + } + private static final class TestFile1 { public static final String UUID = "TEST-UUID-1"; public static final String FILE_UUID = "ABCD"; |