]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5743 Move new source WS to api/sources/lines
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 24 Nov 2014 13:52:22 +0000 (14:52 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 24 Nov 2014 17:00:53 +0000 (18:00 +0100)
16 files changed:
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/source/ws/SourcesWs.java
server/sonar-server/src/main/resources/org/sonar/server/source/ws/example-index.json [deleted file]
server/sonar-server/src/main/resources/org/sonar/server/source/ws/example-lines.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/ScmActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java
server/sonar-server/src/test/resources/org/sonar/server/source/ws/IndexActionTest/show_source.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/source/ws/IndexActionTest/show_source_with_params_from_and_to.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source_with_params_from_and_to.json [new file with mode: 0644]

index ec5e1f47fbf0feb9fa7ffc072297cb6bb9c66548..7ef804907eeeec69f827b98d7451cd196f23c659 100644 (file)
@@ -545,7 +545,7 @@ class ServerComponents {
     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);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java
deleted file mode 100644 (file)
index 33afb3a..0000000
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * 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();
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java
new file mode 100644 (file)
index 0000000..4a075f5
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * 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();
+  }
+}
index a2d7624c6a14e636401c71ebe839fa4529748de1..b571b17aa531b508dd09570e35c56a44f245fb5e 100644 (file)
@@ -25,11 +25,11 @@ import org.sonar.api.server.ws.WebService;
 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;
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/source/ws/example-index.json b/server/sonar-server/src/main/resources/org/sonar/server/source/ws/example-index.json
deleted file mode 100644 (file)
index 3cdfdd6..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-{
-  "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"]
-  ]
-}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/source/ws/example-lines.json b/server/sonar-server/src/main/resources/org/sonar/server/source/ws/example-lines.json
new file mode 100644 (file)
index 0000000..3cdfdd6
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "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"]
+  ]
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java
deleted file mode 100644 (file)
index 07e9622..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * 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");
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java
new file mode 100644 (file)
index 0000000..57d9319
--- /dev/null
@@ -0,0 +1,152 @@
+/*
+ * 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");
+  }
+}
index 312efb4a2c2937b5bbc992fbcac467ae4786d4b6..e848850b000003d237a8976486597a7f4b150c5b 100644 (file)
@@ -64,7 +64,7 @@ public class RawActionTest {
   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
index bd5ee2e012e1ffd5720f3c44fdd54c30e8bb36bc..f57ec7389f64be0f0007492bd04ba93988d3f219 100644 (file)
@@ -34,7 +34,7 @@ public class ScmActionTest {
 
   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 {
index 53888aa4e3318eab23a4707e6fdda5bc795c1cc7..8ceaae65b4798b6a52d63e1948f56b344c814574 100644 (file)
@@ -43,7 +43,7 @@ public class ShowActionTest {
 
   @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
index dc96bc36b93ebec6e2d3f919c1b68ec96ad62bf2..9d4334eb6d5f270be5ba7d38997e066112640b7f 100644 (file)
@@ -35,8 +35,8 @@ public class SourcesWsTest {
   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 {
@@ -70,9 +70,9 @@ public class SourcesWsTest {
     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();
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/ws/IndexActionTest/show_source.json b/server/sonar-server/src/test/resources/org/sonar/server/source/ws/IndexActionTest/show_source.json
deleted file mode 100644 (file)
index 27d3618..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-{
-  "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"
-    }
-  ]
-}
-
-
-
-
-
-
-
-
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/ws/IndexActionTest/show_source_with_params_from_and_to.json b/server/sonar-server/src/test/resources/org/sonar/server/source/ws/IndexActionTest/show_source_with_params_from_and_to.json
deleted file mode 100644 (file)
index ba2a4c6..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-{
-  "sources": [
-    {
-      "code": "}",
-      "line": 3,
-      "scmAuthor": "polop",
-      "scmRevision": "cafebabe"
-    }
-  ]
-}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json b/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json
new file mode 100644 (file)
index 0000000..27d3618
--- /dev/null
@@ -0,0 +1,30 @@
+{
+  "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"
+    }
+  ]
+}
+
+
+
+
+
+
+
+
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source_with_params_from_and_to.json b/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source_with_params_from_and_to.json
new file mode 100644 (file)
index 0000000..ba2a4c6
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "sources": [
+    {
+      "code": "}",
+      "line": 3,
+      "scmAuthor": "polop",
+      "scmRevision": "cafebabe"
+    }
+  ]
+}