import com.google.common.io.Resources;
import java.util.List;
import java.util.Map;
+import javax.annotation.Nonnull;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.server.test.index.CoveredFileDoc;
import org.sonar.server.test.index.TestIndex;
import org.sonar.server.user.UserSession;
+import org.sonar.server.ws.WsUtils;
+import org.sonarqube.ws.WsTests;
public class CoveredFilesAction implements TestsWsAction {
}
@Override
- public void handle(Request request, Response response) {
+ public void handle(Request request, Response response) throws Exception {
String testUuid = request.mandatoryParam(TEST_UUID);
userSession.checkComponentUuidPermission(UserRole.CODEVIEWER, index.searchByTestUuid(testUuid).fileUuid());
List<CoveredFileDoc> coveredFiles = index.coveredFiles(testUuid);
Map<String, ComponentDto> componentsByUuid = buildComponentsByUuid(coveredFiles);
- JsonWriter json = response.newJsonWriter().beginObject();
+
+ WsTests.CoveredFilesResponse.Builder responseBuilder = WsTests.CoveredFilesResponse.newBuilder();
if (!coveredFiles.isEmpty()) {
- writeTests(coveredFiles, componentsByUuid, json);
- }
- json.endObject().close();
- }
+ for (CoveredFileDoc doc : coveredFiles) {
+ WsTests.CoveredFilesResponse.CoveredFile.Builder fileBuilder = WsTests.CoveredFilesResponse.CoveredFile.newBuilder();
+ fileBuilder.setId(doc.fileUuid());
+ fileBuilder.setCoveredLines(doc.coveredLines().size());
+ ComponentDto component = componentsByUuid.get(doc.fileUuid());
+ if (component != null) {
+ fileBuilder.setKey(component.key());
+ fileBuilder.setLongName(component.longName());
+ }
- private static void writeTests(List<CoveredFileDoc> coveredFiles, Map<String, ComponentDto> componentsByUuid, JsonWriter json) {
- json.name("files").beginArray();
- for (CoveredFileDoc coveredFile : coveredFiles) {
- json.beginObject();
- json.prop("key", componentsByUuid.get(coveredFile.fileUuid()).key());
- json.prop("longName", componentsByUuid.get(coveredFile.fileUuid()).longName());
- json.prop("coveredLines", coveredFile.coveredLines().size());
- json.endObject();
+ responseBuilder.addFiles(fileBuilder);
+ }
}
- json.endArray();
+ WsUtils.writeProtobuf(responseBuilder.build(), request, response);
}
private Map<String, ComponentDto> buildComponentsByUuid(List<CoveredFileDoc> coveredFiles) {
private static class CoveredFileToFileUuidFunction implements Function<CoveredFileDoc, String> {
@Override
- public String apply(CoveredFileDoc coveredFile) {
+ public String apply(@Nonnull CoveredFileDoc coveredFile) {
return coveredFile.fileUuid();
}
}
when(testIndex.searchByTestUuid(anyString()).fileUuid()).thenReturn("test-file-uuid");
when(testIndex.coveredFiles("test-uuid")).thenReturn(Arrays.asList(
- new CoveredFileDoc().setFileUuid("bar-uuid").setCoveredLines(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
- new CoveredFileDoc().setFileUuid("file-uuid").setCoveredLines(Arrays.asList(1, 2, 3))
+ new CoveredFileDoc().setFileUuid("FILE1").setCoveredLines(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
+ new CoveredFileDoc().setFileUuid("FILE2").setCoveredLines(Arrays.asList(1, 2, 3))
));
when(dbClient.componentDao().selectByUuids(any(DbSession.class), anyList())).thenReturn(
Arrays.asList(
- newFileDto(newProjectDto(), "bar-uuid").setKey("org.foo.Bar.java").setLongName("src/main/java/org/foo/Bar.java"),
- newFileDto(newProjectDto(), "file-uuid").setKey("org.foo.File.java").setLongName("src/main/java/org/foo/File.java")));
+ newFileDto(newProjectDto(), "FILE1").setKey("org.foo.Bar.java").setLongName("src/main/java/org/foo/Bar.java"),
+ newFileDto(newProjectDto(), "FILE2").setKey("org.foo.File.java").setLongName("src/main/java/org/foo/File.java")));
WsTester.TestRequest request = ws.newGetRequest("api/tests", "covered_files").setParam(TEST_UUID, "test-uuid");