diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-12 17:26:05 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-12 17:26:05 +0200 |
commit | 11388c97824d22fd5eb85681253170b893a56aac (patch) | |
tree | 9bae8371bb8350081f26446842c438b792937898 | |
parent | 7f9853abd57f2527229b2e8855f7e13e4e3ed132 (diff) | |
download | sonarqube-11388c97824d22fd5eb85681253170b893a56aac.tar.gz sonarqube-11388c97824d22fd5eb85681253170b893a56aac.zip |
SONAR-5287 Improve "/api/sources/show"
10 files changed, 49 insertions, 147 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java b/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java index d3a3ad31d34..7b54a4cbc8b 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java +++ b/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java @@ -34,19 +34,16 @@ import java.util.List; public class ShowAction implements RequestHandler { private final SourceService sourceService; - private final ScmWriter scmWriter; - public ShowAction(SourceService sourceService, ScmWriter scmWriter) { + public ShowAction(SourceService sourceService) { this.sourceService = sourceService; - this.scmWriter = scmWriter; } void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction("show") .setDescription("Get source code. Parameter 'output' with value 'raw' is missing before being marked as a public WS") - .setSince("4.2") - .setInternal(true) - .setResponseExample(Resources.getResource(getClass(), "example-show.json")) + .setSince("4.4") + .setResponseExample(Resources.getResource(getClass(), "show-example-show.json")) .setHandler(this); action @@ -65,18 +62,6 @@ public class ShowAction implements RequestHandler { .createParam("to") .setDescription("Last line to return (inclusive)") .setExampleValue("20"); - - action - .createParam("scm") - .setDescription("Enable loading of SCM information per line") - .setBooleanPossibleValues() - .setDefaultValue("false"); - - action - .createParam("group_commits") - .setDescription("Group lines by SCM commit. Used only if 'scm' is 'true'") - .setBooleanPossibleValues() - .setDefaultValue("true"); } @Override @@ -93,21 +78,18 @@ public class ShowAction implements RequestHandler { JsonWriter json = response.newJsonWriter().beginObject(); writeSource(sourceHtml, from, json); - if (request.mandatoryParamAsBoolean("scm")) { - String scmAuthorData = sourceService.getScmAuthorData(fileKey); - String scmDataData = sourceService.getScmDateData(fileKey); - scmWriter.write(scmAuthorData, scmDataData, from, to, request.mandatoryParamAsBoolean("group_commits"), json); - } - json.endObject().close(); } private void writeSource(List<String> lines, int from, JsonWriter json) { - json.name("source").beginObject(); + json.name("sources").beginArray(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); - json.prop(Integer.toString(i + from), line); + json.beginArray(); + json.value(i + from); + json.value(line); + json.endArray(); } - json.endObject(); + json.endArray(); } } diff --git a/sonar-server/src/main/resources/org/sonar/server/source/ws/example-show.json b/sonar-server/src/main/resources/org/sonar/server/source/ws/example-show.json deleted file mode 100644 index f4218223887..00000000000 --- a/sonar-server/src/main/resources/org/sonar/server/source/ws/example-show.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "source": { - "20": "<span class=\"k\">package </span>org.sonar.check;", - "21": "", - "22": "<span class=\"k\">public </span><span class=\"k\">enum </span><span class=\"sym-922 sym\">Priority</span> {", - "23": " <span class=\"cppd\">/**</span>", - "24": "<span class=\"cppd\"> * WARNING : DO NOT CHANGE THE ENUMERATION ORDER</span>", - "25": "<span class=\"cppd\"> * the enum ordinal is used for db persistence</span>" - }, - "scm": { - "20": ["simon.brandhof@gmail.com", "2010-09-06"] - } -} diff --git a/sonar-server/src/main/resources/org/sonar/server/source/ws/scm-example-show.json b/sonar-server/src/main/resources/org/sonar/server/source/ws/scm-example-show.json new file mode 100644 index 00000000000..54e9187c2cc --- /dev/null +++ b/sonar-server/src/main/resources/org/sonar/server/source/ws/scm-example-show.json @@ -0,0 +1,7 @@ +{ + "scm": { + "1": ["julien", "2013-03-13"], + "2": ["julien", "2013-03-13"], + "3": ["simon", "2014-01-01"] + } +} diff --git a/sonar-server/src/main/resources/org/sonar/server/source/ws/show-example-show.json b/sonar-server/src/main/resources/org/sonar/server/source/ws/show-example-show.json new file mode 100644 index 00000000000..74ef52f4510 --- /dev/null +++ b/sonar-server/src/main/resources/org/sonar/server/source/ws/show-example-show.json @@ -0,0 +1,10 @@ +{ + "sources": [ + [20, "<span class=\"k\">package </span>org.sonar.check;"], + [21, ""], + [22, "<span class=\"k\">public </span><span class=\"k\">enum </span><span class=\"sym-922 sym\">Priority</span> {"], + [23, " <span class=\"cppd\">/**</span>"], + [24, "<span class=\"cppd\"> * WARNING : DO NOT CHANGE THE ENUMERATION ORDER</span>"], + [25, "<span class=\"cppd\"> * the enum ordinal is used for db persistence</span>"] + ] +} diff --git a/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java b/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java index 38fec0dc63f..3418dba94ef 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java @@ -21,13 +21,10 @@ package org.sonar.server.source.ws; import org.junit.Before; import org.junit.Test; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.SourceService; import org.sonar.server.ws.WsTester; -import javax.annotation.Nullable; import java.util.Collections; import static com.google.common.collect.Lists.newArrayList; @@ -36,20 +33,17 @@ 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.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class ShowActionTest { SourceService sourceService = mock(SourceService.class); - ScmWriter scmWriter = new FakeScmWriter(); WsTester tester; @Before public void setUp() throws Exception { - tester = new WsTester(new SourcesWs(new ShowAction(sourceService, scmWriter), new ScmAction(sourceService, scmWriter))); + tester = new WsTester(new SourcesWs(new ShowAction(sourceService), new ScmAction(sourceService, mock(ScmWriter.class)))); } @Test @@ -69,13 +63,6 @@ public class ShowActionTest { } @Test - public void response_example_exists() throws Exception { - WebService.Action show = tester.controller("api/sources").action("show"); - assertThat(show.responseExampleFormat()).isEqualTo("json"); - assertThat(show.responseExampleAsString()).isNotEmpty(); - } - - @Test public void fail_to_show_source_if_no_source_found() throws Exception { String componentKey = "src/Foo.java"; when(sourceService.getLinesAsHtml(anyString(), anyInt(), anyInt())).thenReturn(Collections.<String>emptyList()); @@ -122,45 +109,4 @@ public class ShowActionTest { verify(sourceService).getLinesAsHtml(fileKey, 1, 5); } - @Test - public void show_source_with_grouped_scm_commits() throws Exception { - String fileKey = "src/Foo.java"; - when(sourceService.getLinesAsHtml(eq(fileKey), anyInt(), anyInt())).thenReturn(newArrayList( - "public class <span class=\"sym-31 sym\">HelloWorld</span> {}" - )); - - when(sourceService.getScmAuthorData(fileKey)).thenReturn("1=julien;"); - when(sourceService.getScmDateData(fileKey)).thenReturn("1=2013-03-13T16:22:31+0100;"); - - WsTester.TestRequest request = tester - .newGetRequest("api/sources", "show") - .setParam("key", fileKey) - .setParam("scm", "true"); - request.execute().assertJson(getClass(), "show_source_with_grouped_scm_commits.json"); - } - - @Test - public void show_source_with_scm_commits() throws Exception { - String fileKey = "src/Foo.java"; - when(sourceService.getLinesAsHtml(eq(fileKey), anyInt(), anyInt())).thenReturn(newArrayList( - "public class <span class=\"sym-31 sym\">HelloWorld</span> {}" - )); - - when(sourceService.getScmAuthorData(fileKey)).thenReturn("1=julien;"); - when(sourceService.getScmDateData(fileKey)).thenReturn("1=2013-03-13T16:22:31+0100;"); - - WsTester.TestRequest request = tester - .newGetRequest("api/sources", "show") - .setParam("key", fileKey) - .setParam("scm", "true") - .setParam("group_commits", "false"); - request.execute().assertJson(getClass(), "show_source_with_scm_commits.json"); - } - - class FakeScmWriter extends ScmWriter { - @Override - void write(@Nullable String authorsData, @Nullable String datesDate, int from, int to, boolean group, JsonWriter json) { - json.prop("scm", "done,group=" + group); - } - } } diff --git a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java index 32c78261dfd..0af6b0cc185 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java @@ -30,7 +30,7 @@ import static org.mockito.Mockito.mock; public class SourcesWsTest { - ShowAction showAction = new ShowAction(mock(SourceService.class), new ScmWriter()); + ShowAction showAction = new ShowAction(mock(SourceService.class)); ScmAction scmAction = new ScmAction(mock(SourceService.class), new ScmWriter()); WsTester tester = new WsTester(new SourcesWs(showAction, scmAction)); @@ -44,11 +44,17 @@ public class SourcesWsTest { WebService.Action show = controller.action("show"); assertThat(show).isNotNull(); assertThat(show.handler()).isSameAs(showAction); - assertThat(show.params()).hasSize(5); + assertThat(show.since()).isEqualTo("4.4"); + assertThat(show.isInternal()).isFalse(); + assertThat(show.responseExampleAsString()).isNotEmpty(); + assertThat(show.params()).hasSize(3); WebService.Action scm = controller.action("scm"); assertThat(scm).isNotNull(); assertThat(scm.handler()).isSameAs(scmAction); + assertThat(show.since()).isEqualTo("4.4"); + assertThat(show.isInternal()).isFalse(); + assertThat(show.responseExampleAsString()).isNotEmpty(); assertThat(scm.params()).hasSize(4); } } diff --git a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source.json b/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source.json index f31010c347c..97081692ba0 100644 --- a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source.json +++ b/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source.json @@ -1,12 +1,12 @@ { - "source": { - "1": "/*", - "2": " * Header", - "3": " */", - "4": "", - "5": "public class <span class=\"sym-31 sym\">HelloWorld</span> {", - "6": "}" - } + "sources": [ + [1, "/*"], + [2, " * Header"], + [3, " */"], + [4, ""], + [5, "public class <span class=\"sym-31 sym\">HelloWorld</span> {"], + [6, "}"] + ] } diff --git a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_grouped_scm_commits.json b/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_grouped_scm_commits.json deleted file mode 100644 index ae51626916f..00000000000 --- a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_grouped_scm_commits.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "source": { - "1": "public class <span class=\"sym-31 sym\">HelloWorld</span> {}" - }, - "scm": "done,group=true" -} - - - - - - - - diff --git a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_params_from_and_to.json b/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_params_from_and_to.json index f345655217c..50606fa19a6 100644 --- a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_params_from_and_to.json +++ b/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_params_from_and_to.json @@ -1,15 +1,7 @@ { - "source": { - "3": " */", - "4": "", - "5": "public class <span class=\"sym-31 sym\">HelloWorld</span> {" - } + "sources": [ + [3, " */"], + [4, ""], + [5, "public class <span class=\"sym-31 sym\">HelloWorld</span> {"] + ] } - - - - - - - - diff --git a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_scm_commits.json b/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_scm_commits.json deleted file mode 100644 index 99ff3d224c2..00000000000 --- a/sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_scm_commits.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "source": { - "1": "public class <span class=\"sym-31 sym\">HelloWorld</span> {}" - }, - "scm": "done,group=false" -} - - - - - - - - |