pico.addSingleton(SourceService.class);
pico.addSingleton(SourcesWs.class);
pico.addSingleton(ShowAction.class);
- pico.addSingleton(IndexAction.class);
+ pico.addSingleton(LinesAction.class);
pico.addSingleton(ScmWriter.class);
pico.addSingleton(RawAction.class);
pico.addSingleton(ScmAction.class);
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.source.ws;
-
-import com.google.common.io.Resources;
-import org.apache.commons.lang.ObjectUtils;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.RequestHandler;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.source.index.SourceLineDoc;
-import org.sonar.server.source.index.SourceLineIndex;
-
-import java.util.Date;
-import java.util.List;
-
-public class IndexAction implements RequestHandler {
-
- private final SourceLineIndex sourceLineIndex;
-
- public IndexAction(SourceLineIndex sourceLineIndex) {
- this.sourceLineIndex = sourceLineIndex;
- }
-
- void define(WebService.NewController controller) {
- WebService.NewAction action = controller.createAction("index")
- .setDescription("Show source code with line oriented info. Require Browse permission on file's project<br/>" +
- "Each element of the result array is an object which contains:" +
- "<ol>" +
- "<li>Line number</li>" +
- "<li>Content of the line</li>" +
- "<li>Author of the line (from SCM information)</li>" +
- "<li>Revision of the line (from SCM information)</li>" +
- "<li>Last commit date of the line (from SCM information)</li>" +
- "</ol>")
- .setSince("5.0")
- .setInternal(true)
- .setResponseExample(Resources.getResource(getClass(), "example-index.json"))
- .setHandler(this);
-
- action
- .createParam("uuid")
- .setRequired(true)
- .setDescription("File uuid")
- .setExampleValue("f333aab4-7e3a-4d70-87e1-f4c491f05e5c");
-
- action
- .createParam("from")
- .setDescription("First line to return. Starts at 1")
- .setExampleValue("10")
- .setDefaultValue("1");
-
- action
- .createParam("to")
- .setDescription("Last line to return (inclusive)")
- .setExampleValue("20");
- }
-
- @Override
- public void handle(Request request, Response response) {
- String fileUuid = request.mandatoryParam("uuid");
- int from = Math.max(request.mandatoryParamAsInt("from"), 1);
- int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
-
- List<SourceLineDoc> sourceLines = sourceLineIndex.getLines(fileUuid, from, to);
- if (sourceLines.isEmpty()) {
- throw new NotFoundException("File '" + fileUuid + "' has no sources");
- }
-
- JsonWriter json = response.newJsonWriter().beginObject();
- writeSource(sourceLines, from, json);
-
- json.endObject().close();
- }
-
- private void writeSource(List<SourceLineDoc> lines, int from, JsonWriter json) {
- json.name("sources").beginArray();
- for (SourceLineDoc line: lines) {
- json.beginObject()
- .prop("line", line.line())
- .prop("code", line.source())
- .prop("scmAuthor", line.scmAuthor())
- .prop("scmRevision", line.scmRevision());
- Date scmDate = line.scmDate();
- json.prop("scmDate", scmDate == null ? null : DateUtils.formatDateTime(scmDate))
- .endObject();
- }
- json.endArray();
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.source.ws;
+
+import com.google.common.io.Resources;
+import org.apache.commons.lang.ObjectUtils;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.source.index.SourceLineDoc;
+import org.sonar.server.source.index.SourceLineIndex;
+
+import java.util.Date;
+import java.util.List;
+
+public class LinesAction implements RequestHandler {
+
+ private final SourceLineIndex sourceLineIndex;
+
+ public LinesAction(SourceLineIndex sourceLineIndex) {
+ this.sourceLineIndex = sourceLineIndex;
+ }
+
+ void define(WebService.NewController controller) {
+ WebService.NewAction action = controller.createAction("lines")
+ .setDescription("Show source code with line oriented info. Require Browse permission on file's project<br/>" +
+ "Each element of the result array is an object which contains:" +
+ "<ol>" +
+ "<li>Line number</li>" +
+ "<li>Content of the line</li>" +
+ "<li>Author of the line (from SCM information)</li>" +
+ "<li>Revision of the line (from SCM information)</li>" +
+ "<li>Last commit date of the line (from SCM information)</li>" +
+ "</ol>")
+ .setSince("5.0")
+ .setInternal(true)
+ .setResponseExample(Resources.getResource(getClass(), "example-lines.json"))
+ .setHandler(this);
+
+ action
+ .createParam("uuid")
+ .setRequired(true)
+ .setDescription("File uuid")
+ .setExampleValue("f333aab4-7e3a-4d70-87e1-f4c491f05e5c");
+
+ action
+ .createParam("from")
+ .setDescription("First line to return. Starts at 1")
+ .setExampleValue("10")
+ .setDefaultValue("1");
+
+ action
+ .createParam("to")
+ .setDescription("Last line to return (inclusive)")
+ .setExampleValue("20");
+ }
+
+ @Override
+ public void handle(Request request, Response response) {
+ String fileUuid = request.mandatoryParam("uuid");
+ int from = Math.max(request.mandatoryParamAsInt("from"), 1);
+ int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
+
+ List<SourceLineDoc> sourceLines = sourceLineIndex.getLines(fileUuid, from, to);
+ if (sourceLines.isEmpty()) {
+ throw new NotFoundException("File '" + fileUuid + "' has no sources");
+ }
+
+ JsonWriter json = response.newJsonWriter().beginObject();
+ writeSource(sourceLines, from, json);
+
+ json.endObject().close();
+ }
+
+ private void writeSource(List<SourceLineDoc> lines, int from, JsonWriter json) {
+ json.name("sources").beginArray();
+ for (SourceLineDoc line: lines) {
+ json.beginObject()
+ .prop("line", line.line())
+ .prop("code", line.source())
+ .prop("scmAuthor", line.scmAuthor())
+ .prop("scmRevision", line.scmRevision());
+ Date scmDate = line.scmDate();
+ json.prop("scmDate", scmDate == null ? null : DateUtils.formatDateTime(scmDate))
+ .endObject();
+ }
+ json.endArray();
+ }
+}
public class SourcesWs implements WebService {
private final ShowAction showAction;
- private final IndexAction show2Action;
+ private final LinesAction show2Action;
private final RawAction rawAction;
private final ScmAction scmAction;
- public SourcesWs(ShowAction showAction, RawAction rawAction, ScmAction scmAction, IndexAction show2Action) {
+ public SourcesWs(ShowAction showAction, RawAction rawAction, ScmAction scmAction, LinesAction show2Action) {
this.showAction = showAction;
this.show2Action = show2Action;
this.rawAction = rawAction;
+++ /dev/null
-{
- "sources": [
- {"line": 20, "code": "<span class=\"k\">package </span>org.sonar.check;", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
- {"line": 21, "code":"", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
- {"line": 22, "code":"<span class=\"k\">public </span><span class=\"k\">enum </span><span class=\"sym-922 sym\">Priority</span> {", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
- {"line": 23, "code":" <span class=\"cppd\">/**</span>", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
- {"line": 24, "code":"<span class=\"cppd\"> * WARNING : DO NOT CHANGE THE ENUMERATION ORDER</span>", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
- {"line": 25, "code":"<span class=\"cppd\"> * the enum ordinal is used for db persistence</span>", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"]
- ]
-}
--- /dev/null
+{
+ "sources": [
+ {"line": 20, "code": "<span class=\"k\">package </span>org.sonar.check;", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
+ {"line": 21, "code":"", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
+ {"line": 22, "code":"<span class=\"k\">public </span><span class=\"k\">enum </span><span class=\"sym-922 sym\">Priority</span> {", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
+ {"line": 23, "code":" <span class=\"cppd\">/**</span>", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
+ {"line": 24, "code":"<span class=\"cppd\"> * WARNING : DO NOT CHANGE THE ENUMERATION ORDER</span>", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"],
+ {"line": 25, "code":"<span class=\"cppd\"> * the enum ordinal is used for db persistence</span>", "scmAuthor": "simon.brandhof@sonarsource.com", "scmDate": "2011-01-02T12:34:56+0100", "scmRevision":"a1e2b3e5d6f5"]
+ ]
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.source.ws;
-
-import com.google.common.collect.ImmutableMap;
-import org.elasticsearch.common.collect.Lists;
-import org.elasticsearch.common.collect.Maps;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.utils.DateUtils;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.search.BaseNormalizer;
-import org.sonar.server.source.index.SourceLineDoc;
-import org.sonar.server.source.index.SourceLineIndex;
-import org.sonar.server.source.index.SourceLineIndexDefinition;
-import org.sonar.server.ws.WsTester;
-
-import java.util.Date;
-import java.util.Map;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.fest.assertions.Assertions.assertThat;
-import static org.fest.assertions.Fail.fail;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public class IndexActionTest {
-
- @Mock
- SourceLineIndex sourceLineIndex;
-
- WsTester tester;
-
- @Before
- public void setUp() throws Exception {
- tester = new WsTester(
- new SourcesWs(
- mock(ShowAction.class),
- mock(RawAction.class),
- mock(ScmAction.class),
- new IndexAction(sourceLineIndex)
- )
- );
- }
-
- @Test
- public void show_source() throws Exception {
- String componentUuid = "efgh";
- Date updatedAt = new Date();
- Date scmDate = DateUtils.parseDateTime("2014-01-01T12:34:56+0100");
- SourceLineDoc line1 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
- .put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd")
- .put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh")
- .put(SourceLineIndexDefinition.FIELD_LINE, 1)
- .put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe")
- .put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate)
- .put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop")
- .put(SourceLineIndexDefinition.FIELD_SOURCE, "class Polop {")
- .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
- .build());
- SourceLineDoc line2 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
- .put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd")
- .put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh")
- .put(SourceLineIndexDefinition.FIELD_LINE, 2)
- .put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe")
- .put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate)
- .put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop")
- .put(SourceLineIndexDefinition.FIELD_SOURCE, " // Empty")
- .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
- .build());
- SourceLineDoc line3 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
- .put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd")
- .put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh")
- .put(SourceLineIndexDefinition.FIELD_LINE, 3)
- .put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe")
- .put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate)
- .put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop")
- .put(SourceLineIndexDefinition.FIELD_SOURCE, "}")
- .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
- .build());
- when(sourceLineIndex.getLines(eq(componentUuid), anyInt(), anyInt())).thenReturn(newArrayList(
- line1,
- line2,
- line3
- ));
-
- WsTester.TestRequest request = tester.newGetRequest("api/sources", "index").setParam("uuid", componentUuid);
- // Using non-strict match b/c of dates
- request.execute().assertJson(getClass(), "show_source.json", false);
- }
-
- @Test
- public void fail_to_show_source_if_no_source_found() throws Exception {
- String componentKey = "src/Foo.java";
- when(sourceLineIndex.getLines(anyString(), anyInt(), anyInt())).thenReturn(Lists.<SourceLineDoc>newArrayList());
-
- try {
- WsTester.TestRequest request = tester.newGetRequest("api/sources", "index").setParam("uuid", componentKey);
- request.execute();
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(NotFoundException.class);
- }
- }
-
- @Test
- public void show_source_with_from_and_to_params() throws Exception {
- String fileKey = "src/Foo.java";
- Map<String, Object> fieldMap = Maps.newHashMap();
- fieldMap.put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd");
- fieldMap.put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh");
- fieldMap.put(SourceLineIndexDefinition.FIELD_LINE, 3);
- fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe");
- fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_DATE, null);
- fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop");
- fieldMap.put(SourceLineIndexDefinition.FIELD_SOURCE, "}");
- fieldMap.put(BaseNormalizer.UPDATED_AT_FIELD, new Date());
- when(sourceLineIndex.getLines(fileKey, 3, 3)).thenReturn(newArrayList(
- new SourceLineDoc(fieldMap)
- ));
- WsTester.TestRequest request = tester
- .newGetRequest("api/sources", "index")
- .setParam("uuid", fileKey)
- .setParam("from", "3")
- .setParam("to", "3");
- request.execute().assertJson(getClass(), "show_source_with_params_from_and_to.json");
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.source.ws;
+
+import com.google.common.collect.ImmutableMap;
+import org.elasticsearch.common.collect.Lists;
+import org.elasticsearch.common.collect.Maps;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.search.BaseNormalizer;
+import org.sonar.server.source.index.SourceLineDoc;
+import org.sonar.server.source.index.SourceLineIndex;
+import org.sonar.server.source.index.SourceLineIndexDefinition;
+import org.sonar.server.ws.WsTester;
+
+import java.util.Date;
+import java.util.Map;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class LinesActionTest {
+
+ @Mock
+ SourceLineIndex sourceLineIndex;
+
+ WsTester tester;
+
+ @Before
+ public void setUp() throws Exception {
+ tester = new WsTester(
+ new SourcesWs(
+ mock(ShowAction.class),
+ mock(RawAction.class),
+ mock(ScmAction.class),
+ new LinesAction(sourceLineIndex)
+ )
+ );
+ }
+
+ @Test
+ public void show_source() throws Exception {
+ String componentUuid = "efgh";
+ Date updatedAt = new Date();
+ Date scmDate = DateUtils.parseDateTime("2014-01-01T12:34:56+0100");
+ SourceLineDoc line1 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
+ .put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd")
+ .put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh")
+ .put(SourceLineIndexDefinition.FIELD_LINE, 1)
+ .put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe")
+ .put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate)
+ .put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop")
+ .put(SourceLineIndexDefinition.FIELD_SOURCE, "class Polop {")
+ .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
+ .build());
+ SourceLineDoc line2 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
+ .put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd")
+ .put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh")
+ .put(SourceLineIndexDefinition.FIELD_LINE, 2)
+ .put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe")
+ .put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate)
+ .put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop")
+ .put(SourceLineIndexDefinition.FIELD_SOURCE, " // Empty")
+ .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
+ .build());
+ SourceLineDoc line3 = new SourceLineDoc(ImmutableMap.<String, Object>builder()
+ .put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd")
+ .put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh")
+ .put(SourceLineIndexDefinition.FIELD_LINE, 3)
+ .put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe")
+ .put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate)
+ .put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop")
+ .put(SourceLineIndexDefinition.FIELD_SOURCE, "}")
+ .put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt)
+ .build());
+ when(sourceLineIndex.getLines(eq(componentUuid), anyInt(), anyInt())).thenReturn(newArrayList(
+ line1,
+ line2,
+ line3
+ ));
+
+ WsTester.TestRequest request = tester.newGetRequest("api/sources", "lines").setParam("uuid", componentUuid);
+ // Using non-strict match b/c of dates
+ request.execute().assertJson(getClass(), "show_source.json", false);
+ }
+
+ @Test
+ public void fail_to_show_source_if_no_source_found() throws Exception {
+ String componentKey = "src/Foo.java";
+ when(sourceLineIndex.getLines(anyString(), anyInt(), anyInt())).thenReturn(Lists.<SourceLineDoc>newArrayList());
+
+ try {
+ WsTester.TestRequest request = tester.newGetRequest("api/sources", "lines").setParam("uuid", componentKey);
+ request.execute();
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(NotFoundException.class);
+ }
+ }
+
+ @Test
+ public void show_source_with_from_and_to_params() throws Exception {
+ String fileKey = "src/Foo.java";
+ Map<String, Object> fieldMap = Maps.newHashMap();
+ fieldMap.put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd");
+ fieldMap.put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh");
+ fieldMap.put(SourceLineIndexDefinition.FIELD_LINE, 3);
+ fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe");
+ fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_DATE, null);
+ fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop");
+ fieldMap.put(SourceLineIndexDefinition.FIELD_SOURCE, "}");
+ fieldMap.put(BaseNormalizer.UPDATED_AT_FIELD, new Date());
+ when(sourceLineIndex.getLines(fileKey, 3, 3)).thenReturn(newArrayList(
+ new SourceLineDoc(fieldMap)
+ ));
+ WsTester.TestRequest request = tester
+ .newGetRequest("api/sources", "lines")
+ .setParam("uuid", fileKey)
+ .setParam("from", "3")
+ .setParam("to", "3");
+ request.execute().assertJson(getClass(), "show_source_with_params_from_and_to.json");
+ }
+}
public void setUp() throws Exception {
when(dbClient.componentDao()).thenReturn(componentDao);
when(dbClient.openSession(false)).thenReturn(session);
- tester = new WsTester(new SourcesWs(mock(ShowAction.class), new RawAction(dbClient, sourceService), mock(ScmAction.class), mock(IndexAction.class)));
+ tester = new WsTester(new SourcesWs(mock(ShowAction.class), new RawAction(dbClient, sourceService), mock(ScmAction.class), mock(LinesAction.class)));
}
@Test
SourceService sourceService = mock(SourceService.class);
ScmWriter scmWriter = mock(ScmWriter.class);
- WsTester tester = new WsTester(new SourcesWs(mock(ShowAction.class), mock(RawAction.class), new ScmAction(sourceService, scmWriter), mock(IndexAction.class)));
+ WsTester tester = new WsTester(new SourcesWs(mock(ShowAction.class), mock(RawAction.class), new ScmAction(sourceService, scmWriter), mock(LinesAction.class)));
@Test
public void get_scm() throws Exception {
@Before
public void setUp() throws Exception {
- tester = new WsTester(new SourcesWs(new ShowAction(sourceService), mock(RawAction.class), new ScmAction(sourceService, mock(ScmWriter.class)), mock(IndexAction.class)));
+ tester = new WsTester(new SourcesWs(new ShowAction(sourceService), mock(RawAction.class), new ScmAction(sourceService, mock(ScmWriter.class)), mock(LinesAction.class)));
}
@Test
ShowAction showAction = new ShowAction(mock(SourceService.class));
RawAction rawAction = new RawAction(mock(DbClient.class), mock(SourceService.class));
ScmAction scmAction = new ScmAction(mock(SourceService.class), new ScmWriter());
- IndexAction indexAction = new IndexAction(mock(SourceLineIndex.class));
- WsTester tester = new WsTester(new SourcesWs(showAction, rawAction, scmAction, indexAction));
+ LinesAction linesAction = new LinesAction(mock(SourceLineIndex.class));
+ WsTester tester = new WsTester(new SourcesWs(showAction, rawAction, scmAction, linesAction));
@Test
public void define_ws() throws Exception {
assertThat(scm.responseExampleAsString()).isNotEmpty();
assertThat(scm.params()).hasSize(4);
- WebService.Action index = controller.action("index");
+ WebService.Action index = controller.action("lines");
assertThat(index).isNotNull();
- assertThat(index.handler()).isSameAs(indexAction);
+ assertThat(index.handler()).isSameAs(linesAction);
assertThat(index.since()).isEqualTo("5.0");
assertThat(index.isInternal()).isTrue();
assertThat(index.responseExampleAsString()).isNotEmpty();
+++ /dev/null
-{
- "sources": [
- {
- "code": "class Polop {",
- "line": 1,
- "scmAuthor": "polop",
- "scmRevision": "cafebabe"
- },
- {
- "code": " // Empty",
- "line": 2,
- "scmAuthor": "polop",
- "scmRevision": "cafebabe"
- },
- {
- "code": "}",
- "line": 3,
- "scmAuthor": "polop",
- "scmRevision": "cafebabe"
- }
- ]
-}
-
-
-
-
-
-
-
-
+++ /dev/null
-{
- "sources": [
- {
- "code": "}",
- "line": 3,
- "scmAuthor": "polop",
- "scmRevision": "cafebabe"
- }
- ]
-}
--- /dev/null
+{
+ "sources": [
+ {
+ "code": "class Polop {",
+ "line": 1,
+ "scmAuthor": "polop",
+ "scmRevision": "cafebabe"
+ },
+ {
+ "code": " // Empty",
+ "line": 2,
+ "scmAuthor": "polop",
+ "scmRevision": "cafebabe"
+ },
+ {
+ "code": "}",
+ "line": 3,
+ "scmAuthor": "polop",
+ "scmRevision": "cafebabe"
+ }
+ ]
+}
+
+
+
+
+
+
+
+
--- /dev/null
+{
+ "sources": [
+ {
+ "code": "}",
+ "line": 3,
+ "scmAuthor": "polop",
+ "scmRevision": "cafebabe"
+ }
+ ]
+}