]> source.dussan.org Git - sonarqube.git/commitdiff
Add Java WS client for /api/coverage/show and /api/duplications/show
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 22 May 2014 13:23:39 +0000 (15:23 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 22 May 2014 13:23:46 +0000 (15:23 +0200)
23 files changed:
sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java
sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Block.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplication.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/DuplicationClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplications.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/File.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplications.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/package-info.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/package-info.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/test/Coverage.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageShowQuery.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverage.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverageClient.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/package-info.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/test/package-info.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest.java [new file with mode: 0644]
sonar-ws-client/src/test/java/org/sonar/wsclient/test/internal/DefaultCoverageClientTest.java [new file with mode: 0644]
sonar-ws-client/src/test/resources/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest/show_duplications.json [new file with mode: 0644]
sonar-ws-client/src/test/resources/org/sonar/wsclient/test/internal/DefaultCoverageClientTest/show_coverage.json [new file with mode: 0644]

index 41ce23f4b5dae94898f941636b7f50e8ab810923..fd1dafc609b5d163b6c63e288837e4b6be222b73 100644 (file)
@@ -51,6 +51,7 @@ public class CoverageShowAction implements RequestHandler {
         "<ol>" +
         "<li>Line number</li>" +
         "<li>Is the line covered?</li>" +
+        "<li>Number of tests covering this line</li>" +
         "<li>Number of branches</li>" +
         "<li>Number of branches covered</li>" +
         "</ol>")
index beabf3d073590886876ca5c53f69f35bdbef16ca..51b594321ddeb2b61e898ca3ef65d8d6ce8eacb8 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.wsclient;
 
+import org.sonar.wsclient.duplication.DuplicationClient;
+import org.sonar.wsclient.duplication.internal.DefaultDuplicationClient;
 import org.sonar.wsclient.internal.HttpRequestFactory;
 import org.sonar.wsclient.issue.ActionPlanClient;
 import org.sonar.wsclient.issue.IssueClient;
@@ -34,8 +36,12 @@ import org.sonar.wsclient.qualitygate.QualityGateClient;
 import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient;
 import org.sonar.wsclient.rule.RuleClient;
 import org.sonar.wsclient.rule.internal.DefaultRuleClient;
+import org.sonar.wsclient.source.SourceClient;
+import org.sonar.wsclient.source.internal.DefaultSourceClient;
 import org.sonar.wsclient.system.SystemClient;
 import org.sonar.wsclient.system.internal.DefaultSystemClient;
+import org.sonar.wsclient.test.CoverageClient;
+import org.sonar.wsclient.test.internal.DefaultCoverageClient;
 import org.sonar.wsclient.user.UserClient;
 import org.sonar.wsclient.user.internal.DefaultUserClient;
 
@@ -130,6 +136,26 @@ public class SonarClient {
     return new DefaultQProfileClient(requestFactory);
   }
 
+  /**
+   * New client to interact with web services related to source
+   */
+  public SourceClient sourceClient() {
+    return new DefaultSourceClient(requestFactory);
+  }
+
+  /**
+   * New client to interact with web services related to coverage
+   */
+  public CoverageClient coverageClient() {
+    return new DefaultCoverageClient(requestFactory);
+  }
+
+  /**
+   * New client to interact with web services related to duplication
+   */
+  public DuplicationClient duplicationClient() {
+    return new DefaultDuplicationClient(requestFactory);
+  }
 
   public SystemClient systemClient() {
     return new DefaultSystemClient(requestFactory);
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Block.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Block.java
new file mode 100644 (file)
index 0000000..ef2e725
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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.duplication;
+
+import javax.annotation.CheckForNull;
+
+/**
+ * @since 4.4
+ */
+public interface Block {
+
+  @CheckForNull
+  String fileRef();
+
+  @CheckForNull
+  Integer from();
+
+  @CheckForNull
+  Integer size();
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplication.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplication.java
new file mode 100644 (file)
index 0000000..9008afe
--- /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.duplication;
+
+import java.util.List;
+
+/**
+ * @since 4.4
+ */
+public interface Duplication {
+
+  List<Block> blocks();
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/DuplicationClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/DuplicationClient.java
new file mode 100644 (file)
index 0000000..27f624b
--- /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.duplication;
+
+/**
+ * Display duplication information
+ *
+ * @since 4.4
+ */
+public interface DuplicationClient {
+
+  Duplications show(String componentKey);
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplications.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplications.java
new file mode 100644 (file)
index 0000000..c7e9278
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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.duplication;
+
+import javax.annotation.CheckForNull;
+
+import java.util.List;
+
+/**
+ * @since 4.4
+ */
+public interface Duplications {
+
+  List<Duplication> duplications();
+
+  List<File> files();
+
+  @CheckForNull
+  File fileByRef(String ref);
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/File.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/File.java
new file mode 100644 (file)
index 0000000..976f9a7
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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.duplication;
+
+import javax.annotation.CheckForNull;
+
+/**
+ * @since 4.4
+ */
+public interface File {
+
+  @CheckForNull
+  String key();
+
+  @CheckForNull
+  String name();
+
+  @CheckForNull
+  String projectName();
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClient.java
new file mode 100644 (file)
index 0000000..b9dfe73
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * 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.duplication.internal;
+
+import org.json.simple.JSONValue;
+import org.sonar.wsclient.duplication.*;
+import org.sonar.wsclient.internal.EncodingUtils;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.unmarshallers.JsonUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class DefaultDuplicationClient implements DuplicationClient {
+
+  private final HttpRequestFactory requestFactory;
+
+  public DefaultDuplicationClient(HttpRequestFactory requestFactory) {
+    this.requestFactory = requestFactory;
+  }
+
+  @Override
+  public Duplications show(String componentKey) {
+    Map<String, Object> params = EncodingUtils.toMap("key", componentKey);
+    String jsonResult = requestFactory.get("/api/duplications/show", params);
+
+    DefaultDuplications duplications = new DefaultDuplications();
+    Map jRoot = (Map) JSONValue.parse(jsonResult);
+    parseDuplications(duplications, jRoot);
+    parseFiles(duplications, jRoot);
+    return duplications;
+  }
+
+  private void parseDuplications(DefaultDuplications duplications, Map jRoot) {
+    List<Map> jsonDuplications = (List<Map>) jRoot.get("duplications");
+    if (jsonDuplications != null) {
+      for (Map jsonDuplication : jsonDuplications) {
+        final List<Block> blockList = new ArrayList<Block>();
+
+        List<Map> blocks = (List<Map>) jsonDuplication.get("blocks");
+        if (blocks != null) {
+          for (final Map block : blocks) {
+
+            blockList.add((new Block() {
+                @Override
+                public String fileRef() {
+                return JsonUtils.getString(block, "_ref");
+              }
+
+                @Override
+                public Integer from() {
+                return JsonUtils.getInteger(block, "from");
+              }
+
+                @Override
+                public Integer size() {
+                return JsonUtils.getInteger(block, "size");
+              }
+            }));
+          }
+        }
+
+        Duplication duplication = new Duplication() {
+          @Override
+          public List<Block> blocks() {
+            return blockList;
+          }
+        };
+
+        duplications.addDuplication(duplication);
+      }
+    }
+  }
+
+  private void parseFiles(DefaultDuplications duplications, Map jRoot) {
+    Map<String, Map> jsonFiles = (Map) jRoot.get("files");
+    if (jsonFiles != null) {
+      for (Map.Entry<String, Map> entry : jsonFiles.entrySet()) {
+        String ref = entry.getKey();
+        final Map file = entry.getValue();
+        if (ref != null && file != null) {
+
+          duplications.addFile(ref, new File() {
+            @Override
+            public String key () {
+              return JsonUtils.getString(file, "key");
+            }
+
+            @Override
+            public String name () {
+              return JsonUtils.getString(file, "name");
+            }
+
+            @Override
+            public String projectName () {
+              return JsonUtils.getString(file, "projectName");
+            }
+          });
+
+        }
+      }
+    }
+  }
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplications.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplications.java
new file mode 100644 (file)
index 0000000..fefc859
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.duplication.internal;
+
+import org.sonar.wsclient.duplication.Duplication;
+import org.sonar.wsclient.duplication.Duplications;
+import org.sonar.wsclient.duplication.File;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class DefaultDuplications implements Duplications {
+
+  private final List<Duplication> duplications = new ArrayList<Duplication>();
+  private final Map<String, File> files = new HashMap<String, File>();
+
+  @Override
+  public List<Duplication> duplications() {
+    return duplications;
+  }
+
+  @Override
+  public List<File> files() {
+    return new ArrayList<File>(files.values());
+  }
+
+  @Override
+  public File fileByRef(String ref) {
+    return files.get(ref);
+  }
+
+  DefaultDuplications addDuplication(Duplication duplication) {
+    duplications.add(duplication);
+    return this;
+  }
+
+  DefaultDuplications addFile(String ref, File file) {
+    files.put(ref, file);
+    return this;
+  }
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/package-info.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/package-info.java
new file mode 100644 (file)
index 0000000..9488f80
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.wsclient.duplication.internal;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/package-info.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/package-info.java
new file mode 100644 (file)
index 0000000..d703cac
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.wsclient.duplication;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/test/Coverage.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/Coverage.java
new file mode 100644 (file)
index 0000000..b18c7ed
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * 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.test;
+
+import javax.annotation.CheckForNull;
+
+/**
+ * @since 4.4
+ */
+public interface Coverage {
+
+  @CheckForNull
+  Integer lineIndex();
+
+  @CheckForNull
+  Boolean isCovered();
+
+  @CheckForNull
+  Integer tests();
+
+  @CheckForNull
+  Integer branches();
+
+  @CheckForNull
+  Integer coveredBranches();
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageClient.java
new file mode 100644 (file)
index 0000000..97a28d7
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * 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.test;
+
+import java.util.List;
+
+/**
+ * Display coverage information
+ *
+ * @since 4.4
+ */
+public interface CoverageClient {
+
+  List<Coverage> show(CoverageShowQuery query);
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageShowQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageShowQuery.java
new file mode 100644 (file)
index 0000000..092cfd5
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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.test;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class CoverageShowQuery {
+
+  private String key;
+  private String from;
+  private String to;
+  private String type;
+
+  private CoverageShowQuery() {
+    // Nothing here
+  }
+
+  public String key() {
+    return key;
+  }
+
+  public CoverageShowQuery setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  @CheckForNull
+  public String from() {
+    return from;
+  }
+
+  public CoverageShowQuery setFrom(@Nullable String from) {
+    this.from = from;
+    return this;
+  }
+
+  @CheckForNull
+  public String to() {
+    return to;
+  }
+
+  public CoverageShowQuery setTo(@Nullable String to) {
+    this.to = to;
+    return this;
+  }
+
+  @CheckForNull
+  public String type() {
+    return type;
+  }
+
+  public CoverageShowQuery setType(@Nullable String type) {
+    this.type = type;
+    return this;
+  }
+
+  public static CoverageShowQuery create(){
+    return new CoverageShowQuery();
+  }
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverage.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverage.java
new file mode 100644 (file)
index 0000000..4c9e73d
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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.test.internal;
+
+import org.sonar.wsclient.test.Coverage;
+
+import java.util.List;
+
+public class DefaultCoverage implements Coverage {
+
+  private final List json;
+
+  public DefaultCoverage(List json) {
+    this.json = json;
+  }
+
+  @Override
+  public Integer lineIndex() {
+    Object value = json.get(0);
+    return value != null ? ((Long) value).intValue() : null;
+  }
+
+  @Override
+  public Boolean isCovered() {
+    Object value = json.get(1);
+    return value != null ? (Boolean) value : null;
+  }
+
+  @Override
+  public Integer tests() {
+    Object value = json.get(2);
+    return value != null ? ((Long) value).intValue() : null;
+  }
+
+  @Override
+  public Integer branches() {
+    Object value = json.get(3);
+    return value != null ? ((Long) value).intValue() : null;
+  }
+
+  @Override
+  public Integer coveredBranches() {
+    Object value = json.get(4);
+    return value != null ? ((Long) value).intValue() : null;
+  }
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverageClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverageClient.java
new file mode 100644 (file)
index 0000000..d566604
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.test.internal;
+
+import org.json.simple.JSONValue;
+import org.sonar.wsclient.internal.EncodingUtils;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+import org.sonar.wsclient.test.Coverage;
+import org.sonar.wsclient.test.CoverageClient;
+import org.sonar.wsclient.test.CoverageShowQuery;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class DefaultCoverageClient implements CoverageClient {
+
+  private final HttpRequestFactory requestFactory;
+
+  public DefaultCoverageClient(HttpRequestFactory requestFactory) {
+    this.requestFactory = requestFactory;
+  }
+
+  @Override
+  public List<Coverage> show(CoverageShowQuery query) {
+    Map<String, Object> params = EncodingUtils.toMap("key", query.key(), "from", query.from(), "to", query.to(), "type", query.type());
+    String jsonResult = requestFactory.get("/api/coverage/show", params);
+
+    List<Coverage> coverages = new ArrayList<Coverage>();
+    Map jRoot = (Map) JSONValue.parse(jsonResult);
+    List<List> jsonList = (List) jRoot.get("coverage");
+    if (jsonList != null) {
+      for (List json : jsonList) {
+        coverages.add(new DefaultCoverage(json));
+      }
+    }
+    return coverages;
+  }
+
+}
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/package-info.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/package-info.java
new file mode 100644 (file)
index 0000000..e7ae755
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.wsclient.test.internal;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/test/package-info.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/package-info.java
new file mode 100644 (file)
index 0000000..578e47c
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.wsclient.test;
+
+import javax.annotation.ParametersAreNonnullByDefault;
index 3f3c6e53050908dbaf4565082ed5b72f8f390e11..4a461b9ba7d2f0b5c32a86920e57181cd2c8e0e7 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.wsclient;
 
 import org.junit.Test;
+import org.sonar.wsclient.duplication.internal.DefaultDuplicationClient;
 import org.sonar.wsclient.issue.internal.DefaultActionPlanClient;
 import org.sonar.wsclient.issue.internal.DefaultIssueClient;
 import org.sonar.wsclient.permissions.internal.DefaultPermissionClient;
@@ -27,7 +28,9 @@ import org.sonar.wsclient.project.internal.DefaultProjectClient;
 import org.sonar.wsclient.qprofile.internal.DefaultQProfileClient;
 import org.sonar.wsclient.qualitygate.internal.DefaultQualityGateClient;
 import org.sonar.wsclient.rule.internal.DefaultRuleClient;
+import org.sonar.wsclient.source.internal.DefaultSourceClient;
 import org.sonar.wsclient.system.internal.DefaultSystemClient;
+import org.sonar.wsclient.test.internal.DefaultCoverageClient;
 import org.sonar.wsclient.user.internal.DefaultUserClient;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -45,6 +48,9 @@ public class SonarClientTest {
     assertThat(client.ruleClient()).isNotNull().isInstanceOf(DefaultRuleClient.class);
     assertThat(client.qualityGateClient()).isNotNull().isInstanceOf(DefaultQualityGateClient.class);
     assertThat(client.qProfileClient()).isNotNull().isInstanceOf(DefaultQProfileClient.class);
+    assertThat(client.sourceClient()).isNotNull().isInstanceOf(DefaultSourceClient.class);
+    assertThat(client.coverageClient()).isNotNull().isInstanceOf(DefaultCoverageClient.class);
+    assertThat(client.duplicationClient()).isNotNull().isInstanceOf(DefaultDuplicationClient.class);
     assertThat(client.systemClient()).isNotNull().isInstanceOf(DefaultSystemClient.class);
   }
 
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest.java
new file mode 100644 (file)
index 0000000..6912892
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * 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.duplication.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.duplication.*;
+import org.sonar.wsclient.internal.HttpRequestFactory;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultDuplicationClientTest {
+
+  @Rule
+  public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
+
+  @Test
+  public void show_duplications() throws IOException {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+    httpServer.stubResponseBody(Resources.toString(Resources.getResource(this.getClass(), "DefaultDuplicationClientTest/show_duplications.json"), Charsets.UTF_8));
+
+    DuplicationClient client = new DefaultDuplicationClient(requestFactory);
+    Duplications result = client.show("MyFile");
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/duplications/show?key=MyFile");
+
+    List<Duplication> duplications = result.duplications();
+    assertThat(duplications).hasSize(1);
+
+    Duplication duplication = duplications.get(0);
+    assertThat(duplication.blocks()).hasSize(2);
+
+    Block block  = duplication.blocks().get(0);
+    assertThat(block.fileRef()).isEqualTo("1");
+    assertThat(block.from()).isEqualTo(94);
+    assertThat(block.size()).isEqualTo(101);
+
+    List<File> files = result.files();
+    assertThat(files).hasSize(2);
+
+    File file = result.fileByRef("1");
+    assertThat(file.key()).isEqualTo("org.codehaus.sonar:sonar-plugin-api:src/main/java/org/sonar/api/utils/command/CommandExecutor.java");
+    assertThat(file.name()).isEqualTo("CommandExecutor");
+    assertThat(file.projectName()).isEqualTo("SonarQube");
+  }
+
+}
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/test/internal/DefaultCoverageClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/test/internal/DefaultCoverageClientTest.java
new file mode 100644 (file)
index 0000000..3ddd111
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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.test.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.test.Coverage;
+import org.sonar.wsclient.test.CoverageClient;
+import org.sonar.wsclient.test.CoverageShowQuery;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class DefaultCoverageClientTest {
+
+  @Rule
+  public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
+
+  @Test
+  public void show_coverage() throws IOException {
+    HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
+    httpServer.stubResponseBody(Resources.toString(Resources.getResource(this.getClass(), "DefaultCoverageClientTest/show_coverage.json"), Charsets.UTF_8));
+
+    CoverageClient client = new DefaultCoverageClient(requestFactory);
+    List<Coverage> coverage = client.show(CoverageShowQuery.create().setKey("MyFile").setFrom("20").setTo("25").setType("UT"));
+
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/coverage/show?key=MyFile&from=20&to=25&type=UT");
+    assertThat(coverage).hasSize(3);
+    assertThat(coverage.get(0).lineIndex()).isEqualTo(49);
+    assertThat(coverage.get(0).isCovered()).isTrue();
+    assertThat(coverage.get(0).tests()).isEqualTo(14);
+    assertThat(coverage.get(0).branches()).isEqualTo(3);
+    assertThat(coverage.get(0).coveredBranches()).isEqualTo(2);
+  }
+
+}
diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest/show_duplications.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest/show_duplications.json
new file mode 100644 (file)
index 0000000..83b36f8
--- /dev/null
@@ -0,0 +1,30 @@
+{
+  "duplications": [
+    {
+      "blocks": [
+        {
+          "from": 94,
+          "size": 101,
+          "_ref": "1"
+        },
+        {
+          "from": 83,
+          "size": 101,
+          "_ref": "2"
+        }
+      ]
+    }
+  ],
+  "files": {
+    "1": {
+      "key": "org.codehaus.sonar:sonar-plugin-api:src/main/java/org/sonar/api/utils/command/CommandExecutor.java",
+      "name": "CommandExecutor",
+      "projectName": "SonarQube"
+    },
+    "2": {
+      "key": "com.sonarsource.orchestrator:sonar-orchestrator:src/main/java/com/sonar/orchestrator/util/CommandExecutor.java",
+      "name": "CommandExecutor",
+      "projectName": "SonarSource :: Orchestrator"
+    }
+  }
+}
diff --git a/sonar-ws-client/src/test/resources/org/sonar/wsclient/test/internal/DefaultCoverageClientTest/show_coverage.json b/sonar-ws-client/src/test/resources/org/sonar/wsclient/test/internal/DefaultCoverageClientTest/show_coverage.json
new file mode 100644 (file)
index 0000000..8e05f85
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "coverage": [
+    [49, true, 14, 3, 2],
+    [52, false, null, null, null],
+    [72, true, 4, 2, 1]
+  ]
+}