package org.sonar.server.test.index;
import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.es.SearchResult;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILES;
import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILE_LINES;
import static org.sonar.server.test.index.TestIndexDefinition.FIELD_COVERED_FILE_UUID;
}
public TestDoc searchByTestUuid(String testUuid) {
+ Optional<TestDoc> testDoc = getNullableByTestUuid(testUuid);
+ if (testDoc.isPresent()) {
+ return testDoc.get();
+ }
+
+ throw new IllegalStateException(String.format("Test id '%s' not found", testUuid));
+ }
+
+ public Optional<TestDoc> getNullableByTestUuid(String testUuid) {
for (SearchHit hit : getClient().prepareSearch(TestIndexDefinition.INDEX)
.setTypes(TestIndexDefinition.TYPE)
.setSize(1)
.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter(FIELD_TEST_UUID, testUuid)))
.get().getHits().getHits()) {
- return new TestDoc(hit.sourceAsMap());
+ return Optional.of(new TestDoc(hit.sourceAsMap()));
}
- throw new IllegalStateException(String.format("Test id '%s' not found", testUuid));
+ return Optional.absent();
}
public SearchResult<TestDoc> searchByTestUuid(String testUuid, SearchOptions searchOptions) {
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.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.WsUtils;
import org.sonarqube.ws.WsTests;
+import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
public class CoveredFilesAction implements TestsWsAction {
public static final String TEST_ID = "testId";
@Override
public void handle(Request request, Response response) throws Exception {
String testId = request.mandatoryParam(TEST_ID);
- userSession.checkComponentUuidPermission(UserRole.CODEVIEWER, index.searchByTestUuid(testId).fileUuid());
+ TestDoc testDoc = checkFoundWithOptional(index.getNullableByTestUuid(testId), "Test with id '%s' is not found", testId);
+ userSession.checkComponentUuidPermission(UserRole.CODEVIEWER, testDoc.fileUuid());
List<CoveredFileDoc> coveredFiles = index.coveredFiles(testId);
Map<String, ComponentDto> componentsByUuid = buildComponentsByUuid(coveredFiles);
responseBuilder.addFiles(fileBuilder);
}
}
- WsUtils.writeProtobuf(responseBuilder.build(), request, response);
+ writeProtobuf(responseBuilder.build(), request, response);
}
private Map<String, ComponentDto> buildComponentsByUuid(List<CoveredFileDoc> coveredFiles) {
try {
components = dbClient.componentDao().selectByUuids(dbSession, sourceFileUuids);
} finally {
- MyBatis.closeQuietly(dbSession);
+ dbClient.closeSession(dbSession);
}
return Maps.uniqueIndex(components, ComponentDtoFunctions.toUuid());
}
*/
package org.sonar.server.test.ws;
+import com.google.common.base.Optional;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+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;
import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsTester;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyList;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.server.test.ws.CoveredFilesAction.TEST_ID;
+import static org.sonar.test.JsonAssert.assertJson;
public class CoveredFilesActionTest {
@Rule
public UserSessionRule userSessionRule = UserSessionRule.standalone();
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
- WsTester ws;
- private DbClient dbClient;
- private TestIndex testIndex;
+ WsActionTester ws;
+ DbClient dbClient;
+ TestIndex testIndex;
@Before
public void setUp() {
dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS);
testIndex = mock(TestIndex.class, RETURNS_DEEP_STUBS);
- ws = new WsTester(new TestsWs(new CoveredFilesAction(dbClient, testIndex, userSessionRule)));
+
+ ws = new WsActionTester(new CoveredFilesAction(dbClient, testIndex, userSessionRule));
+ }
+
+ @Test
+ public void covered_files() {
+ userSessionRule.addComponentUuidPermission(UserRole.CODEVIEWER, "SonarQube", "test-file-uuid");
+
+ when(testIndex.getNullableByTestUuid(anyString())).thenReturn(Optional.of(new TestDoc().setFileUuid("test-file-uuid")));
+ when(testIndex.coveredFiles("test-uuid")).thenReturn(Arrays.asList(
+ new CoveredFileDoc().setFileUuid(FILE_1_ID).setCoveredLines(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
+ new CoveredFileDoc().setFileUuid(FILE_2_ID).setCoveredLines(Arrays.asList(1, 2, 3))));
+ when(dbClient.componentDao().selectByUuids(any(DbSession.class), anyList())).thenReturn(
+ Arrays.asList(
+ newFileDto(newProjectDto(), FILE_1_ID).setKey("org.foo.Bar.java").setLongName("src/main/java/org/foo/Bar.java"),
+ newFileDto(newProjectDto(), FILE_2_ID).setKey("org.foo.File.java").setLongName("src/main/java/org/foo/File.java")));
+
+ TestRequest request = ws.newRequest().setParam(TEST_ID, "test-uuid");
+
+ assertJson(request.execute().getInput()).isSimilarTo(getClass().getResource("CoveredFilesActionTest/tests-covered-files.json"));
}
@Test
- public void covered_files() throws Exception {
+ public void fail_when_test_uuid_is_unknown() {
userSessionRule.addComponentUuidPermission(UserRole.CODEVIEWER, "SonarQube", "test-file-uuid");
- when(testIndex.searchByTestUuid(anyString()).fileUuid()).thenReturn("test-file-uuid");
+ when(testIndex.getNullableByTestUuid(anyString())).thenReturn(Optional.<TestDoc>absent());
when(testIndex.coveredFiles("test-uuid")).thenReturn(Arrays.asList(
new CoveredFileDoc().setFileUuid(FILE_1_ID).setCoveredLines(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
- new CoveredFileDoc().setFileUuid(FILE_2_ID).setCoveredLines(Arrays.asList(1, 2, 3))
- ));
+ new CoveredFileDoc().setFileUuid(FILE_2_ID).setCoveredLines(Arrays.asList(1, 2, 3))));
when(dbClient.componentDao().selectByUuids(any(DbSession.class), anyList())).thenReturn(
Arrays.asList(
newFileDto(newProjectDto(), FILE_1_ID).setKey("org.foo.Bar.java").setLongName("src/main/java/org/foo/Bar.java"),
newFileDto(newProjectDto(), FILE_2_ID).setKey("org.foo.File.java").setLongName("src/main/java/org/foo/File.java")));
- WsTester.TestRequest request = ws.newGetRequest("api/tests", "covered_files").setParam(TEST_ID, "test-uuid");
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage("Test with id 'test-uuid' is not found");
- request.execute().assertJson(getClass(), "tests-covered-files.json");
+ ws.newRequest().setParam(TEST_ID, "test-uuid").execute();
}
}