From 0d5c741914e8a4e1df63d231f55b472750e1098b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 22 May 2014 15:23:39 +0200 Subject: [PATCH] Add Java WS client for /api/coverage/show and /api/duplications/show --- .../server/test/ws/CoverageShowAction.java | 1 + .../java/org/sonar/wsclient/SonarClient.java | 26 ++++ .../org/sonar/wsclient/duplication/Block.java | 39 ++++++ .../wsclient/duplication/Duplication.java | 32 +++++ .../duplication/DuplicationClient.java | 32 +++++ .../wsclient/duplication/Duplications.java | 39 ++++++ .../org/sonar/wsclient/duplication/File.java | 39 ++++++ .../internal/DefaultDuplicationClient.java | 123 ++++++++++++++++++ .../internal/DefaultDuplications.java | 62 +++++++++ .../duplication/internal/package-info.java | 24 ++++ .../wsclient/duplication/package-info.java | 24 ++++ .../org/sonar/wsclient/test/Coverage.java | 45 +++++++ .../sonar/wsclient/test/CoverageClient.java | 34 +++++ .../wsclient/test/CoverageShowQuery.java | 80 ++++++++++++ .../test/internal/DefaultCoverage.java | 64 +++++++++ .../test/internal/DefaultCoverageClient.java | 58 +++++++++ .../wsclient/test/internal/package-info.java | 24 ++++ .../org/sonar/wsclient/test/package-info.java | 24 ++++ .../org/sonar/wsclient/SonarClientTest.java | 6 + .../DefaultDuplicationClientTest.java | 71 ++++++++++ .../internal/DefaultCoverageClientTest.java | 60 +++++++++ .../show_duplications.json | 30 +++++ .../show_coverage.json | 7 + 23 files changed, 944 insertions(+) create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Block.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplication.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/DuplicationClient.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplications.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/File.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClient.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplications.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/package-info.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/package-info.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/test/Coverage.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageClient.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageShowQuery.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverage.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverageClient.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/package-info.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/test/package-info.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/test/internal/DefaultCoverageClientTest.java create mode 100644 sonar-ws-client/src/test/resources/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest/show_duplications.json create mode 100644 sonar-ws-client/src/test/resources/org/sonar/wsclient/test/internal/DefaultCoverageClientTest/show_coverage.json diff --git a/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java b/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java index 41ce23f4b5d..fd1dafc609b 100644 --- a/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java +++ b/sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java @@ -51,6 +51,7 @@ public class CoverageShowAction implements RequestHandler { "
    " + "
  1. Line number
  2. " + "
  3. Is the line covered?
  4. " + + "
  5. Number of tests covering this line
  6. " + "
  7. Number of branches
  8. " + "
  9. Number of branches covered
  10. " + "
") diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java index beabf3d0735..51b594321dd 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/SonarClient.java @@ -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 index 00000000000..ef2e7250329 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Block.java @@ -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 index 00000000000..9008afe835d --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplication.java @@ -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 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 index 00000000000..27f624b2ff4 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/DuplicationClient.java @@ -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 index 00000000000..c7e927873af --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/Duplications.java @@ -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 duplications(); + + List 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 index 00000000000..976f9a7ab90 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/File.java @@ -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 index 00000000000..b9dfe736c4e --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClient.java @@ -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 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 jsonDuplications = (List) jRoot.get("duplications"); + if (jsonDuplications != null) { + for (Map jsonDuplication : jsonDuplications) { + final List blockList = new ArrayList(); + + List blocks = (List) 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 blocks() { + return blockList; + } + }; + + duplications.addDuplication(duplication); + } + } + } + + private void parseFiles(DefaultDuplications duplications, Map jRoot) { + Map jsonFiles = (Map) jRoot.get("files"); + if (jsonFiles != null) { + for (Map.Entry 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 index 00000000000..fefc8596825 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/DefaultDuplications.java @@ -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 duplications = new ArrayList(); + private final Map files = new HashMap(); + + @Override + public List duplications() { + return duplications; + } + + @Override + public List files() { + return new ArrayList(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 index 00000000000..9488f801b80 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/internal/package-info.java @@ -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 index 00000000000..d703cacae42 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/duplication/package-info.java @@ -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 index 00000000000..b18c7edcd76 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/Coverage.java @@ -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 index 00000000000..97a28d7eb16 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageClient.java @@ -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 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 index 00000000000..092cfd51d92 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/CoverageShowQuery.java @@ -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 index 00000000000..4c9e73d7a42 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverage.java @@ -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 index 00000000000..d5666045f21 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/DefaultCoverageClient.java @@ -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 show(CoverageShowQuery query) { + Map params = EncodingUtils.toMap("key", query.key(), "from", query.from(), "to", query.to(), "type", query.type()); + String jsonResult = requestFactory.get("/api/coverage/show", params); + + List coverages = new ArrayList(); + Map jRoot = (Map) JSONValue.parse(jsonResult); + 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 index 00000000000..e7ae755dea6 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/internal/package-info.java @@ -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 index 00000000000..578e47ce73b --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/test/package-info.java @@ -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; diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java index 3f3c6e53050..4a461b9ba7d 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/SonarClientTest.java @@ -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 index 00000000000..69128926ed1 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest.java @@ -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 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 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 index 00000000000..3ddd1119b5d --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/test/internal/DefaultCoverageClientTest.java @@ -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 = 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 index 00000000000..83b36f842ef --- /dev/null +++ b/sonar-ws-client/src/test/resources/org/sonar/wsclient/duplication/internal/DefaultDuplicationClientTest/show_duplications.json @@ -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 index 00000000000..8e05f85da30 --- /dev/null +++ b/sonar-ws-client/src/test/resources/org/sonar/wsclient/test/internal/DefaultCoverageClientTest/show_coverage.json @@ -0,0 +1,7 @@ +{ + "coverage": [ + [49, true, 14, 3, 2], + [52, false, null, null, null], + [72, true, 4, 2, 1] + ] +} -- 2.39.5