]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5287 SONAR-5293 Create Java WS client for "/api/sources/show" and "/api/sources...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 13 May 2014 08:26:07 +0000 (10:26 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 13 May 2014 08:26:26 +0000 (10:26 +0200)
sonar-ws-client/pom.xml
sonar-ws-client/src/main/java/org/sonar/wsclient/source/Scm.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/source/Source.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultScm.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSource.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSourceClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/JsonUtils.java
sonar-ws-client/src/test/java/org/sonar/wsclient/source/internal/DefaultSourceClientTest.java [new file with mode: 0644]
sonar-ws-client/src/test/resources/org/sonar/wsclient/source/internal/DefaultSourceClientTest/return_scm.json [new file with mode: 0644]
sonar-ws-client/src/test/resources/org/sonar/wsclient/source/internal/DefaultSourceClientTest/show_source.json [new file with mode: 0644]

index 1a9a8ad3ff49da5f597c868a8b00566db7f4753e..1a1206dd6f036e5edf08613526531257dfbc8462 100644 (file)
       <artifactId>fest-assert</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/Scm.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/Scm.java
new file mode 100644 (file)
index 0000000..cf42082
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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.wsclient.source;
+
+import javax.annotation.CheckForNull;
+
+import java.util.Date;
+
+public interface Scm {
+
+  long index();
+
+  @CheckForNull
+  String author();
+
+  @CheckForNull
+  Date date();
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/Source.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/Source.java
new file mode 100644 (file)
index 0000000..f1d1cf7
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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.wsclient.source;
+
+/**
+ * @since 4.4
+ */
+public interface Source {
+
+  long index();
+
+  String line();
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/SourceClient.java
new file mode 100644 (file)
index 0000000..36b6525
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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.wsclient.source;
+
+import javax.annotation.Nullable;
+
+import java.util.List;
+
+/**
+ * Display sources information
+ * @since 4.4
+ */
+public interface SourceClient {
+
+  List<Source> show(String key, @Nullable String from, @Nullable String to);
+
+  List<Scm> scm(String key, @Nullable String from, @Nullable String to, @Nullable Boolean groupCommits);
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultScm.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultScm.java
new file mode 100644 (file)
index 0000000..b563f5e
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.wsclient.source.internal;
+
+import org.sonar.wsclient.source.Scm;
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+
+import javax.annotation.CheckForNull;
+
+import java.util.Date;
+import java.util.List;
+
+public class DefaultScm implements Scm {
+
+  private final List json;
+
+  public DefaultScm(List json) {
+    this.json = json;
+  }
+
+  @Override
+  public long index() {
+    return (Long) json.get(0);
+  }
+
+  @Override
+  @CheckForNull
+  public String author() {
+    return (String) json.get(1);
+  }
+
+  @Override
+  @CheckForNull
+  public Date date() {
+    return JsonUtils.parseDate((String) json.get(2));
+  }
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSource.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSource.java
new file mode 100644 (file)
index 0000000..8794a5f
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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.wsclient.source.internal;
+
+import org.sonar.wsclient.source.Source;
+
+import java.util.List;
+
+public class DefaultSource implements Source {
+
+  private final List json;
+
+  public DefaultSource(List json) {
+    this.json = json;
+  }
+
+  @Override
+  public long index() {
+    return (Long) json.get(0);
+  }
+
+  @Override
+  public String line() {
+    return (String) json.get(1);
+  }
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSourceClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/source/internal/DefaultSourceClient.java
new file mode 100644 (file)
index 0000000..d5e0acf
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * 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.wsclient.source.internal;
+
+import org.json.simple.JSONValue;
+import org.sonar.wsclient.internal.EncodingUtils;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.source.Scm;
+import org.sonar.wsclient.source.Source;
+import org.sonar.wsclient.source.SourceClient;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class DefaultSourceClient implements SourceClient {
+
+  private final HttpRequestFactory requestFactory;
+
+  public DefaultSourceClient(HttpRequestFactory requestFactory) {
+    this.requestFactory = requestFactory;
+  }
+
+  @Override
+  public List<Source> show(String key, @Nullable String from, @Nullable String to) {
+    Map<String, Object> params = EncodingUtils.toMap("key", key, "from", from, "to", to);
+    String json = requestFactory.get("/api/sources/show", params);
+
+    List<Source> sources = new ArrayList<Source>();
+    Map jRoot = (Map) JSONValue.parse(json);
+    List<List> jsonSources = (List) jRoot.get("sources");
+    if (jsonSources != null) {
+      for (List jSource : jsonSources) {
+        sources.add(new DefaultSource(jSource));
+      }
+    }
+    return sources;
+  }
+
+  @Override
+  public List<Scm> scm(String key, @Nullable String from, @Nullable String to, @Nullable Boolean groupCommits) {
+    Map<String, Object> params = EncodingUtils.toMap("key", key, "from", from, "to", to);
+    params.put("group_commits", groupCommits);
+    String json = requestFactory.get("/api/sources/scm", params);
+
+    List<Scm> result = new ArrayList<Scm>();
+    Map jRoot = (Map) JSONValue.parse(json);
+    List<List> jsonResult = (List) jRoot.get("scm");
+    if (jsonResult != null) {
+      for (List line : jsonResult) {
+        result.add(new DefaultScm(line));
+      }
+    }
+    return result;
+  }
+
+}
index cd24c301dcfea716a736ddec965d2074e2f088c8..6843ec8c9b3728f381798fb42c84c7f21c2d02a4 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.wsclient.unmarshallers;
 import org.json.simple.JSONArray;
 
 import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -94,24 +95,28 @@ public final class JsonUtils {
 
   @CheckForNull
   public static Date getDateTime(Map obj, String field) {
-    return parseDate(obj, field, "yyyy-MM-dd'T'HH:mm:ssZ");
+    return parseDate(getString(obj, field), "yyyy-MM-dd'T'HH:mm:ssZ");
   }
 
   @CheckForNull
   public static Date getDate(Map obj, String field) {
-    return parseDate(obj, field, "yyyy-MM-dd");
+    return parseDate(getString(obj, field));
   }
 
   @CheckForNull
-  private static Date parseDate(Map obj, String field, String format) {
-    String value = getString(obj, field);
+  public static Date parseDate(@Nullable String value) {
+    return parseDate(value, "yyyy-MM-dd");
+  }
+
+  @CheckForNull
+  private static Date parseDate(@Nullable String value, String format) {
     if (value != null) {
       try {
         SimpleDateFormat dateFormat = new SimpleDateFormat(format);
         return dateFormat.parse(value);
 
       } catch (ParseException e) {
-        throw new IllegalArgumentException("Fail to parse date property '" + field + "': " + format, e);
+        throw new IllegalArgumentException("Fail to parse date '" + value + "': " + format, e);
       }
     }
     return null;
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/source/internal/DefaultSourceClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/source/internal/DefaultSourceClientTest.java
new file mode 100644 (file)
index 0000000..4abb642
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * 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.wsclient.source.internal;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.wsclient.MockHttpServerInterceptor;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.source.Scm;
+import org.sonar.wsclient.source.Source;
+import org.sonar.wsclient.source.SourceClient;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultSourceClientTest {
+
+  @Rule
+  public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
+
+  @Test
+  public void show_source() throws IOException {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+    httpServer.stubResponseBody(Resources.toString(Resources.getResource(this.getClass(), "DefaultSourceClientTest/show_source.json"), Charsets.UTF_8));
+
+    SourceClient client = new DefaultSourceClient(requestFactory);
+    List<Source> sources = client.show("MyFile", "20", "25");
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/sources/show?key=MyFile&from=20&to=25");
+    assertThat(sources).hasSize(6);
+    assertThat(sources.get(0).index()).isEqualTo(20);
+    assertThat(sources.get(0).line()).isEqualTo("<span class=\"k\">package </span>org.sonar.check;");
+  }
+
+  @Test
+  public void return_scm() throws IOException {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+    httpServer.stubResponseBody(Resources.toString(Resources.getResource(this.getClass(), "DefaultSourceClientTest/return_scm.json"), Charsets.UTF_8));
+
+    SourceClient client = new DefaultSourceClient(requestFactory);
+    List<Scm> result = client.scm("MyFile", "1", "3", true);
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/sources/scm?key=MyFile&from=1&to=3&group_commits=true");
+    assertThat(result).hasSize(3);
+    assertThat(result.get(0).index()).isEqualTo(1);
+    assertThat(result.get(0).author()).isEqualTo("julien");
+    assertThat(result.get(0).date().getTime()).isEqualTo(1363129200000L);
+  }
+
+}
diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/source/internal/DefaultSourceClientTest/return_scm.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/source/internal/DefaultSourceClientTest/return_scm.json
new file mode 100644 (file)
index 0000000..513f3e4
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "scm": [
+    [1, "julien", "2013-03-13"],
+    [2, "julien", "2013-03-14"],
+    [3, "simon", "2014-01-01"]
+  ]
+}
diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/source/internal/DefaultSourceClientTest/show_source.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/source/internal/DefaultSourceClientTest/show_source.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>"]
+  ]
+}