]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5287 Improve "/api/sources/show"
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 12 May 2014 15:26:05 +0000 (17:26 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 12 May 2014 15:26:05 +0000 (17:26 +0200)
sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java
sonar-server/src/main/resources/org/sonar/server/source/ws/example-show.json [deleted file]
sonar-server/src/main/resources/org/sonar/server/source/ws/scm-example-show.json [new file with mode: 0644]
sonar-server/src/main/resources/org/sonar/server/source/ws/show-example-show.json [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java
sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java
sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source.json
sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_grouped_scm_commits.json [deleted file]
sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_params_from_and_to.json
sonar-server/src/test/resources/org/sonar/server/source/ws/ShowActionTest/show_source_with_scm_commits.json [deleted file]

index d3a3ad31d3402f4d9d1fe8a69f29a60b8b3d5767..7b54a4cbc8b27cd073ab663f73736981e0cc1f1d 100644 (file)
@@ -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 (file)
index f421822..0000000
+++ /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 (file)
index 0000000..54e9187
--- /dev/null
@@ -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 (file)
index 0000000..74ef52f
--- /dev/null
@@ -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>"]
+  ]
+}
index 38fec0dc63fe38526f31b4bc9a424951b8d8740d..3418dba94efa2e34b241ece089a12d2ad84784dc 100644 (file)
@@ -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
@@ -68,13 +62,6 @@ public class ShowActionTest {
     request.execute().assertJson(getClass(), "show_source.json");
   }
 
-  @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";
@@ -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);
-    }
-  }
 }
index 32c78261dfdf266fa28398d862cf44733be5f1a1..0af6b0cc185a9c85e5e190e9ac6073e5db3869e5 100644 (file)
@@ -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);
   }
 }
index f31010c347c650dd1b315302363403ca2f368b5e..97081692ba080159ea25bbd7f64e40a0e23718e0 100644 (file)
@@ -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 (file)
index ae51626..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-{
-  "source": {
-    "1": "public class <span class=\"sym-31 sym\">HelloWorld</span> {}"
-  },
-  "scm": "done,group=true"
-}
-
-
-
-
-
-
-
-
index f345655217c270e6ceab6510bcd576fb63d155dd..50606fa19a6bca173b51c3d3b3b373247b3529af 100644 (file)
@@ -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 (file)
index 99ff3d2..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-{
-  "source": {
-    "1": "public class <span class=\"sym-31 sym\">HelloWorld</span> {}"
-  },
-  "scm": "done,group=false"
-}
-
-
-
-
-
-
-
-