diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-01-17 12:19:23 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-01-18 15:23:40 +0100 |
commit | 5690b112fccf1b3498ed457535cff629609269a2 (patch) | |
tree | 227b350c6c042ed72df1183f680237527760e6a8 | |
parent | 013a9aa964e25e0c70bec60eaa4484d5c7e8683f (diff) | |
download | sonarqube-5690b112fccf1b3498ed457535cff629609269a2.tar.gz sonarqube-5690b112fccf1b3498ed457535cff629609269a2.zip |
SONAR-8574 Replace usage of api/resources/index by api/measures/component in ITs
41 files changed, 805 insertions, 844 deletions
diff --git a/it/it-tests/src/test/java/it/analysis/IssuesModeTest.java b/it/it-tests/src/test/java/it/analysis/IssuesModeTest.java index 905ceb7abb8..15115e6e42f 100644 --- a/it/it-tests/src/test/java/it/analysis/IssuesModeTest.java +++ b/it/it-tests/src/test/java/it/analysis/IssuesModeTest.java @@ -56,13 +56,13 @@ import org.sonar.wsclient.issue.Issue; import org.sonar.wsclient.issue.IssueClient; import org.sonar.wsclient.issue.IssueQuery; import org.sonar.wsclient.issue.Issues; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import org.sonar.wsclient.user.UserParameters; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; +import static util.ItUtils.getComponent; +import static util.ItUtils.getMeasureAsDouble; public class IssuesModeTest { @@ -206,7 +206,7 @@ public class IssuesModeTest { assertThat(result.getLogs()).doesNotContain("'One Issue Per Line' skipped because there is no related file in current project"); ItUtils.assertIssuesInJsonReport(result, 3, 0, 17); } - + // SONAR-8518 @Test public void shoud_support_sonar_profile_prop() throws IOException { @@ -214,8 +214,8 @@ public class IssuesModeTest { restoreProfile("empty.xml"); orchestrator.getServer().provisionProject("sample", "xoo-sample"); orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "empty"); - - SonarScanner runner = configureRunner("shared/xoo-sample", + + SonarScanner runner = configureRunner("shared/xoo-sample", "sonar.verbose", "true", "sonar.analysis.mode", "issues", "sonar.profile", "one-issue-per-line"); @@ -231,14 +231,13 @@ public class IssuesModeTest { orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "with-many-rules"); SonarScanner runner = configureRunner("analysis/xoo-sample-with-spaces/v2"); - BuildResult result = orchestrator.executeBuild(runner); - assertThat(getResource("sample:my sources/main/xoo/sample/My Sample.xoo")).isNotNull(); + orchestrator.executeBuild(runner); + assertThat(getComponent(orchestrator, "sample:my sources/main/xoo/sample/My Sample.xoo")).isNotNull(); runner = configureRunnerIssues("analysis/xoo-sample-with-spaces/v2", null); - result = orchestrator.executeBuild(runner); + BuildResult result = orchestrator.executeBuild(runner); // Analysis is not persisted in database - Resource project = getResource("com.sonarsource.it.samples:simple-sample"); - assertThat(project).isNull(); + assertThat(getComponent(orchestrator, "com.sonarsource.it.samples:simple-sample")).isNull(); assertThat(result.getLogs()).contains("Issues"); assertThat(result.getLogs()).contains("ANALYSIS SUCCESSFUL"); } @@ -251,19 +250,19 @@ public class IssuesModeTest { // First real scan with source SonarScanner runner = configureRunner("shared/xoo-history-v2"); - BuildResult result = orchestrator.executeBuild(runner); - assertThat(getResource("sample:src/main/xoo/sample/ClassAdded.xoo")).isNotNull(); + orchestrator.executeBuild(runner); + assertThat(getComponent(orchestrator, "sample:src/main/xoo/sample/ClassAdded.xoo")).isNotNull(); // Second scan should remove ClassAdded.xoo runner = configureRunner("shared/xoo-history-v1"); - result = orchestrator.executeBuild(runner); - assertThat(getResource("sample:src/main/xoo/sample/ClassAdded.xoo")).isNull(); + orchestrator.executeBuild(runner); + assertThat(getMeasureAsDouble(orchestrator, "sample:src/main/xoo/sample/ClassAdded.xoo", "ncloc")).isNull(); // Re-add ClassAdded.xoo in local workspace runner = configureRunnerIssues("shared/xoo-history-v2", null); - result = orchestrator.executeBuild(runner); + BuildResult result = orchestrator.executeBuild(runner); - assertThat(getResource("sample:src/main/xoo/sample/ClassAdded.xoo")).isNull(); + assertThat(getMeasureAsDouble(orchestrator, "sample:src/main/xoo/sample/ClassAdded.xoo", "ncloc")).isNull(); assertThat(result.getLogs()).contains("Issues"); assertThat(result.getLogs()).contains("ANALYSIS SUCCESSFUL"); } @@ -433,10 +432,6 @@ public class IssuesModeTest { orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/analysis/IssuesModeTest/" + fileName)); } - private Resource getResource(String key) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(key, "lines")); - } - private SonarScanner configureRunnerIssues(String projectDir, @Nullable File homeDir, String... props) throws IOException { SonarScanner scanner = SonarScanner.create(ItUtils.projectDir(projectDir), "sonar.working.directory", temp.newFolder().getAbsolutePath(), diff --git a/it/it-tests/src/test/java/it/analysis/MultiLanguageTest.java b/it/it-tests/src/test/java/it/analysis/MultiLanguageTest.java index 9442fc8ab69..c9b1b6ff929 100644 --- a/it/it-tests/src/test/java/it/analysis/MultiLanguageTest.java +++ b/it/it-tests/src/test/java/it/analysis/MultiLanguageTest.java @@ -24,14 +24,15 @@ import com.sonar.orchestrator.build.BuildResult; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; import it.Category3Suite; +import java.util.Map; import org.junit.After; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasureAsDouble; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; public class MultiLanguageTest { @@ -55,7 +56,7 @@ public class MultiLanguageTest { orchestrator.getServer().provisionProject("multi-language-sample", "multi-language-sample"); orchestrator.getServer().associateProjectToQualityProfile("multi-language-sample", "xoo", "one-issue-per-line"); - orchestrator.getServer().associateProjectToQualityProfile("multi-language-sample","xoo2", "one-issue-per-line-xoo2"); + orchestrator.getServer().associateProjectToQualityProfile("multi-language-sample", "xoo2", "one-issue-per-line-xoo2"); SonarScanner build = SonarScanner.create().setProjectDir(ItUtils.projectDir("analysis/xoo-multi-languages")); BuildResult result = orchestrator.executeBuild(build); @@ -65,18 +66,11 @@ public class MultiLanguageTest { assertThat(result.getLogs()).contains("Quality profile for xoo2: one-issue-per-line-xoo2"); // modules - Resource project = getResource("multi-language-sample", "files", "violations"); - assertThat(project.getMeasureIntValue("files")).isEqualTo(2); - assertThat(project.getMeasureIntValue("violations")).isEqualTo(26); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "multi-language-sample", "files", "violations"); + assertThat(measures.get("files")).isEqualTo(2); + assertThat(measures.get("violations")).isEqualTo(26); - Resource xooFile = getResource("multi-language-sample:src/sample/Sample.xoo", "violations"); - assertThat(xooFile.getMeasureIntValue("violations")).isEqualTo(13); - - Resource xoo2File = getResource("multi-language-sample:src/sample/Sample.xoo2", "violations"); - assertThat(xoo2File.getMeasureIntValue("violations")).isEqualTo(13); - } - - private Resource getResource(String resourceKey, String... metricKeys) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(resourceKey, metricKeys)); + assertThat(getMeasureAsDouble(orchestrator, "multi-language-sample:src/sample/Sample.xoo", "violations")).isEqualTo(13); + assertThat(getMeasureAsDouble(orchestrator, "multi-language-sample:src/sample/Sample.xoo2", "violations")).isEqualTo(13); } } diff --git a/it/it-tests/src/test/java/it/analysis/ProjectBuilderTest.java b/it/it-tests/src/test/java/it/analysis/ProjectBuilderTest.java index 8236c5fed52..f1d59cf9e1c 100644 --- a/it/it-tests/src/test/java/it/analysis/ProjectBuilderTest.java +++ b/it/it-tests/src/test/java/it/analysis/ProjectBuilderTest.java @@ -22,13 +22,14 @@ package it.analysis; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.MavenBuild; import it.Category3Suite; +import java.util.Map; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getComponent; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; /** * Test the extension point org.sonar.api.batch.bootstrap.ProjectBuilder @@ -56,34 +57,30 @@ public class ProjectBuilderTest { checkSubProject("project-builder-module-b"); checkFile("project-builder-module-a", "src/HelloA.java"); checkFile("project-builder-module-b", "src/HelloB.java"); - assertThat(getResource("com.sonarsource.it.projects.batch:project-builder-module-b:src/IgnoredFile.java")).isNull(); + assertThat(getComponent(orchestrator, "com.sonarsource.it.projects.batch:project-builder-module-b:src/IgnoredFile.java")).isNull(); } private void checkProject() { - Resource project = getResource("com.sonarsource.it.projects.batch:project-builder"); - // name has been changed by plugin - assertThat(project.getName()).isEqualTo("Name changed by plugin"); + assertThat(getComponent(orchestrator, "com.sonarsource.it.projects.batch:project-builder").getName()).isEqualTo("Name changed by plugin"); - assertThat(project).isNotNull(); - assertThat(project.getMeasureIntValue("files")).isEqualTo(2); - assertThat(project.getMeasureIntValue("lines")).isGreaterThan(10); + Map<String, Double> measures = getMeasures("com.sonarsource.it.projects.batch:project-builder"); + assertThat(measures.get("files")).isEqualTo(2); + assertThat(measures.get("lines")).isGreaterThan(10); } private void checkSubProject(String subProjectKey) { - Resource subProject = getResource("com.sonarsource.it.projects.batch:" + subProjectKey); - assertThat(subProject).isNotNull(); - assertThat(subProject.getMeasureIntValue("files")).isEqualTo(1); - assertThat(subProject.getMeasureIntValue("lines")).isGreaterThan(5); + Map<String, Double> measures = getMeasures("com.sonarsource.it.projects.batch:" + subProjectKey); + assertThat(measures.get("files")).isEqualTo(1); + assertThat(measures.get("lines")).isGreaterThan(5); } private void checkFile(String subProjectKey, String fileKey) { - Resource file = getResource("com.sonarsource.it.projects.batch:" + subProjectKey + ":" + fileKey); - assertThat(file).isNotNull(); - assertThat(file.getMeasureIntValue("lines")).isGreaterThan(5); + Map<String, Double> measures = getMeasures("com.sonarsource.it.projects.batch:" + subProjectKey + ":" + fileKey); + assertThat(measures.get("lines")).isGreaterThan(5); } - private Resource getResource(String key) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(key, "lines", "files")); + private Map<String, Double> getMeasures(String key) { + return getMeasuresAsDoubleByMetricKey(orchestrator, key, "lines", "files"); } } diff --git a/it/it-tests/src/test/java/it/analysis/ScannerTest.java b/it/it-tests/src/test/java/it/analysis/ScannerTest.java index da7ff0ad2e5..20030ca3982 100644 --- a/it/it-tests/src/test/java/it/analysis/ScannerTest.java +++ b/it/it-tests/src/test/java/it/analysis/ScannerTest.java @@ -26,7 +26,7 @@ import com.sonar.orchestrator.locator.FileLocation; import it.Category3Suite; import java.io.File; import java.io.IOException; -import java.util.Date; +import java.util.Map; import org.apache.commons.io.FileUtils; import org.junit.Assume; import org.junit.Before; @@ -36,12 +36,16 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.sonar.wsclient.Sonar; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.client.component.SearchWsRequest; import util.ItUtils; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getComponent; +import static util.ItUtils.getComponentNavigation; +import static util.ItUtils.getMeasureAsDouble; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; +import static util.ItUtils.newAdminWsClient; import static util.ItUtils.resetSettings; import static util.ItUtils.setServerProperty; @@ -70,14 +74,10 @@ public class ScannerTest { scan("shared/xoo-multi-modules-sample"); scan("shared/xoo-multi-modules-sample", "sonar.branch", "branch/0.x"); - Sonar sonar = orchestrator.getServer().getWsClient(); - assertThat(sonar.findAll(new ResourceQuery().setQualifiers("TRK"))).hasSize(2); - - Resource master = sonar.find(new ResourceQuery("com.sonarsource.it.samples:multi-modules-sample")); - assertThat(master.getName()).isEqualTo("Sonar :: Integration Tests :: Multi-modules Sample"); - - Resource branch = sonar.find(new ResourceQuery("com.sonarsource.it.samples:multi-modules-sample:branch/0.x")); - assertThat(branch.getName()).isEqualTo("Sonar :: Integration Tests :: Multi-modules Sample branch/0.x"); + assertThat(newAdminWsClient(orchestrator).components().search(new SearchWsRequest().setQualifiers(singletonList("TRK"))).getComponentsList()).hasSize(2); + assertThat(getComponent(orchestrator, "com.sonarsource.it.samples:multi-modules-sample").getName()).isEqualTo("Sonar :: Integration Tests :: Multi-modules Sample"); + assertThat(getComponent(orchestrator, "com.sonarsource.it.samples:multi-modules-sample:branch/0.x").getName()) + .isEqualTo("Sonar :: Integration Tests :: Multi-modules Sample branch/0.x"); } @Test @@ -85,8 +85,7 @@ public class ScannerTest { scan("shared/xoo-multi-modules-sample", "sonar.profile", "one-issue-per-line", "sonar.verbose", "true"); - Resource r = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("com.sonarsource.it.samples:multi-modules-sample", "violations")); - assertThat(r.getMeasureIntValue("violations")).isEqualTo(61); + assertThat(getMeasureAsDouble(orchestrator, "com.sonarsource.it.samples:multi-modules-sample", "violations")).isEqualTo(61); } // SONAR-4680 @@ -149,11 +148,10 @@ public class ScannerTest { orchestrator.getServer().associateProjectToQualityProfile("com.sonarsource.it.projects.batch:duplicate-source", "xoo", "one-issue-per-line"); scan("analysis/duplicate-source"); - Sonar sonar = orchestrator.getServer().getAdminWsClient(); - Resource project = sonar.find(new ResourceQuery("com.sonarsource.it.projects.batch:duplicate-source").setMetrics("files", "directories")); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "com.sonarsource.it.projects.batch:duplicate-source", "files", "directories"); // 2 main files and 1 test file all with same deprecated key - assertThat(project.getMeasureIntValue("files")).isEqualTo(2); - assertThat(project.getMeasureIntValue("directories")).isEqualTo(2); + assertThat(measures.get("files")).isEqualTo(2); + assertThat(measures.get("directories")).isEqualTo(2); } /** @@ -224,11 +222,8 @@ public class ScannerTest { } private void assertNameAndVersion(String projectKey, String expectedProjectName, String expectedProjectVersion) { - // new WS Client with api/components doesn't return the project version, so use the old one - Resource resource = orchestrator.getServer().getAdminWsClient().find(new ResourceQuery(projectKey)); - assertThat(resource.getName()).isEqualTo(expectedProjectName); - assertThat(resource.getVersion()).isEqualTo(expectedProjectVersion); - + assertThat(getComponent(orchestrator, projectKey).getName()).isEqualTo(expectedProjectName); + assertThat(getComponentNavigation(orchestrator, projectKey).getVersion()).isEqualTo(expectedProjectVersion); } @Test @@ -366,9 +361,9 @@ public class ScannerTest { orchestrator.getServer().associateProjectToQualityProfile("case-sensitive-file-extensions", "xoo", "one-issue-per-line"); scan("analysis/case-sensitive-file-extensions"); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("case-sensitive-file-extensions", "files", "ncloc")); - assertThat(project.getMeasureIntValue("files")).isEqualTo(2); - assertThat(project.getMeasureIntValue("ncloc")).isEqualTo(5 + 2); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "case-sensitive-file-extensions", "files", "ncloc"); + assertThat(measures.get("files")).isEqualTo(2); + assertThat(measures.get("ncloc")).isEqualTo(5 + 2); } /** @@ -379,8 +374,8 @@ public class ScannerTest { orchestrator.getServer().provisionProject("com.sonarsource.it.samples:multi-modules-sample", "Sonar :: Integration Tests :: Multi-modules Sample"); orchestrator.getServer().associateProjectToQualityProfile("com.sonarsource.it.samples:multi-modules-sample", "xoo", "one-issue-per-line"); scan("analysis/custom-module-key"); - assertThat(getResource("com.sonarsource.it.samples:moduleA")).isNotNull(); - assertThat(getResource("com.sonarsource.it.samples:moduleB")).isNotNull(); + assertThat(getComponent(orchestrator, "com.sonarsource.it.samples:moduleA")).isNotNull(); + assertThat(getComponent(orchestrator, "com.sonarsource.it.samples:moduleB")).isNotNull(); } /** @@ -392,8 +387,8 @@ public class ScannerTest { orchestrator.getServer().provisionProject("projectAB", "project AB"); orchestrator.getServer().associateProjectToQualityProfile("projectAB", "xoo", "one-issue-per-line"); scan("analysis/prevent-common-module/projectAB"); - assertThat(getResource("com.sonarsource.it.samples:moduleA")).isNotNull(); - assertThat(getResource("com.sonarsource.it.samples:moduleB")).isNotNull(); + assertThat(getComponent(orchestrator, "com.sonarsource.it.samples:moduleA")).isNotNull(); + assertThat(getComponent(orchestrator, "com.sonarsource.it.samples:moduleB")).isNotNull(); orchestrator.getServer().provisionProject("projectAC", "project AC"); orchestrator.getServer().associateProjectToQualityProfile("projectAC", "xoo", "one-issue-per-line"); @@ -404,20 +399,6 @@ public class ScannerTest { } /** - * SONAR-4235 - */ - @Test - public void test_project_creation_date() { - long before = new Date().getTime() - 2000l; - orchestrator.getServer().provisionProject("sample", "xoo-sample"); - orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line"); - orchestrator.executeBuild(SonarScanner.create(ItUtils.projectDir("shared/xoo-sample"))); - long after = new Date().getTime() + 2000l; - Resource xooSample = orchestrator.getServer().getWsClient().find(new ResourceQuery().setResourceKeyOrId("sample")); - assertThat(xooSample.getCreationDate().getTime()).isGreaterThan(before).isLessThan(after); - } - - /** * SONAR-4334 */ @Test @@ -438,10 +419,6 @@ public class ScannerTest { assertThat(result.getLogs()).contains("This property may only be used to rebuild the past in a chronological order."); } - private Resource getResource(String key) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(key, "lines")); - } - private BuildResult scan(String projectPath, String... props) { SonarScanner scanner = configureScanner(projectPath, props); return orchestrator.executeBuild(scanner); diff --git a/it/it-tests/src/test/java/it/analysisExclusion/FileExclusionsTest.java b/it/it-tests/src/test/java/it/analysisExclusion/FileExclusionsTest.java index 4612bad12a2..651e27d8beb 100644 --- a/it/it-tests/src/test/java/it/analysisExclusion/FileExclusionsTest.java +++ b/it/it-tests/src/test/java/it/analysisExclusion/FileExclusionsTest.java @@ -23,14 +23,18 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import it.Category4Suite; import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsComponents.Component; +import org.sonarqube.ws.client.component.TreeWsRequest; import util.ItUtils; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; +import static util.ItUtils.newWsClient; public class FileExclusionsTest { static final String PROJECT = "exclusions"; @@ -50,11 +54,10 @@ public class FileExclusionsTest { "sonar.exclusions", "**/*Exclude*.xoo,src/main/xoo/org/sonar/tests/packageToExclude/**", "sonar.test.exclusions", "**/ClassTwoTest.xoo"); - Resource project = projectWithMetrics("ncloc", "files", "directories"); - - assertThat(project.getMeasureIntValue("files")).isEqualTo(4); - assertThat(project.getMeasureIntValue("ncloc")).isEqualTo(60); - assertThat(project.getMeasureIntValue("directories")).isEqualTo(2); + Map<String, Double> measures = getMeasuresAsDouble("ncloc", "files", "directories"); + assertThat(measures.get("files").intValue()).isEqualTo(4); + assertThat(measures.get("ncloc").intValue()).isEqualTo(60); + assertThat(measures.get("directories").intValue()).isEqualTo(2); } /** @@ -67,11 +70,9 @@ public class FileExclusionsTest { "sonar.exclusions", "**/*Exclude*.xoo,org/sonar/tests/packageToExclude/**", "sonar.test.exclusions", "**/ClassTwoTest.xoo"); - List<Resource> testFiles = orchestrator.getServer().getWsClient() - .findAll(new ResourceQuery(PROJECT).setQualifiers("UTS").setDepth(-1)); - + List<Component> testFiles = getComponents("UTS"); assertThat(testFiles).hasSize(2); - assertThat(testFiles).extracting("name").doesNotContain("ClassTwoTest.xoo"); + assertThat(testFiles).extracting(Component::getName).doesNotContain("ClassTwoTest.xoo"); } /** @@ -83,14 +84,11 @@ public class FileExclusionsTest { "sonar.dynamicAnalysis", "false", "sonar.inclusions", "**/*One.xoo,**/*Two.xoo"); - Resource project = projectWithMetrics("files"); - assertThat(project.getMeasureIntValue("files")).isEqualTo(2); - - List<Resource> sourceFiles = orchestrator.getServer().getWsClient() - .findAll(new ResourceQuery(PROJECT).setQualifiers("FIL").setDepth(-1)); + assertThat(getMeasuresAsDouble("files").get("files")).isEqualTo(2); + List<Component> sourceFiles = getComponents("FIL"); assertThat(sourceFiles).hasSize(2); - assertThat(sourceFiles).extracting("name").containsOnly("ClassOne.xoo", "ClassTwo.xoo"); + assertThat(sourceFiles).extracting(Component::getName).containsOnly("ClassOne.xoo", "ClassTwo.xoo"); } /** @@ -100,14 +98,11 @@ public class FileExclusionsTest { public void include_test_files() { scan("sonar.test.inclusions", "src/test/xoo/**/*One*.xoo,src/test/xoo/**/*Two*.xoo"); - Resource project = projectWithMetrics("tests"); - assertThat(project.getMeasureIntValue("tests")).isEqualTo(2); - - List<Resource> testFiles = orchestrator.getServer().getWsClient() - .findAll(new ResourceQuery(PROJECT).setQualifiers("UTS").setDepth(-1)); + assertThat(getMeasuresAsDouble("tests").get("tests")).isEqualTo(2); + List<Component> testFiles = getComponents("UTS"); assertThat(testFiles).hasSize(2); - assertThat(testFiles).extracting("name").containsOnly("ClassOneTest.xoo", "ClassTwoTest.xoo"); + assertThat(testFiles).extracting(Component::getName).containsOnly("ClassOneTest.xoo", "ClassTwoTest.xoo"); } /** @@ -122,15 +117,13 @@ public class FileExclusionsTest { // exclude ClassThree and ClassToExclude "sonar.exclusions", "file:**/src/main/xoo/org/**/packageToExclude/*.xoo,file:**/src/main/xoo/org/**/*Exclude.xoo"); - List<Resource> sourceFiles = orchestrator.getServer().getWsClient() - .findAll(new ResourceQuery(PROJECT).setQualifiers("FIL").setDepth(-1)); - + List<Component> sourceFiles = getComponents("FIL"); assertThat(sourceFiles).hasSize(4); - assertThat(sourceFiles).extracting("name").containsOnly("ClassOne.xoo", "ClassToIgnoreGlobally.xoo", "ClassTwo.xoo", "NoSonarComment.xoo"); + assertThat(sourceFiles).extracting(Component::getName).containsOnly("ClassOne.xoo", "ClassToIgnoreGlobally.xoo", "ClassTwo.xoo", "NoSonarComment.xoo"); } - static Resource projectWithMetrics(String... metricKeys) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT, metricKeys)); + static Map<String, Double> getMeasuresAsDouble(String... metricKeys) { + return getMeasuresAsDoubleByMetricKey(orchestrator, PROJECT, metricKeys); } private void scan(String... properties) { @@ -139,4 +132,8 @@ public class FileExclusionsTest { .setProperties(properties); orchestrator.executeBuild(build); } + + public static List<Component> getComponents(String qualifier) { + return newWsClient(orchestrator).components().tree(new TreeWsRequest().setBaseComponentKey(PROJECT).setQualifiers(singletonList(qualifier))).getComponentsList(); + } } diff --git a/it/it-tests/src/test/java/it/analysisExclusion/IssueExclusionsTest.java b/it/it-tests/src/test/java/it/analysisExclusion/IssueExclusionsTest.java index 2103ed3f7a4..5e0057207fd 100644 --- a/it/it-tests/src/test/java/it/analysisExclusion/IssueExclusionsTest.java +++ b/it/it-tests/src/test/java/it/analysisExclusion/IssueExclusionsTest.java @@ -24,14 +24,14 @@ import com.sonar.orchestrator.build.BuildResult; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; import it.Category4Suite; +import java.util.Map; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; public class IssueExclusionsTest { @@ -71,7 +71,7 @@ public class IssueExclusionsTest { "sonar.issue.enforce.multicriteria.1.ruleKey", "*"); checkIssueCountBySeverity( - 1 /* tag */+ 18 /* lines in HelloA1.xoo */+ 1 /* file */, + 1 /* tag */ + 18 /* lines in HelloA1.xoo */ + 1 /* file */, 0 + 1, 0 + 18, 0 + 1, @@ -89,7 +89,7 @@ public class IssueExclusionsTest { "sonar.issue.enforce.multicriteria.2.ruleKey", "*"); checkIssueCountBySeverity( - 2 /* tags */+ 18 /* lines in HelloA1.xoo */+ 15 /* lines in HelloA2.xoo */+ 2 /* files */, + 2 /* tags */ + 18 /* lines in HelloA1.xoo */ + 15 /* lines in HelloA2.xoo */ + 2 /* files */, 0 + 2, 0 + 18 + 15, 0 + 2, @@ -107,7 +107,7 @@ public class IssueExclusionsTest { "sonar.issue.enforce.multicriteria.2.ruleKey", "xoo:HasTag"); checkIssueCountBySeverity( - 1 /* tag in HelloA2 */+ 18 /* lines in HelloA1.xoo */+ 4 /* files */+ 4 /* modules */, + 1 /* tag in HelloA2 */ + 18 /* lines in HelloA1.xoo */ + 4 /* files */ + 4 /* modules */, 0 + 1, 0 + 18, 4, @@ -122,7 +122,7 @@ public class IssueExclusionsTest { "sonar.issue.ignore.allfile.1.fileRegexp", "EXTERMINATE-ALL-ISSUES"); checkIssueCountBySeverity( - 67 - 1 /* tag */- 18 /* lines in HelloA1.xoo */- 1 /* file */, + 67 - 1 /* tag */ - 18 /* lines in HelloA1.xoo */ - 1 /* file */, 2 - 1, 57 - 18, 4 - 1, @@ -138,7 +138,7 @@ public class IssueExclusionsTest { "sonar.issue.ignore.block.1.endBlockRegexp", "UNMUTE-SONAR"); checkIssueCountBySeverity( - 67 - 1 /* tag */- 5 /* lines in HelloA2.xoo */, + 67 - 1 /* tag */ - 5 /* lines in HelloA2.xoo */, 2 - 1, 57 - 5, 4, @@ -154,7 +154,7 @@ public class IssueExclusionsTest { "sonar.issue.ignore.block.1.endBlockRegexp", ""); checkIssueCountBySeverity( - 67 - 1 /* tag */- 7 /* remaining lines in HelloA2.xoo */, + 67 - 1 /* tag */ - 7 /* remaining lines in HelloA2.xoo */, 2 - 1, 57 - 7, 4, @@ -191,8 +191,8 @@ public class IssueExclusionsTest { "sonar.issue.ignore.multicriteria.1.ruleKey", "xoo:OneIssuePerLine"); checkIssueCountBySeverity( - 67 - 1 /* tag in HelloA1.xoo */- 1 /* tag in HelloA2.xoo */ - - 18 /* lines in HelloA1.xoo */- 5 /* lines in HelloA2.xoo */- 12 /* lines in HelloB1.xoo */ + 67 - 1 /* tag in HelloA1.xoo */ - 1 /* tag in HelloA2.xoo */ + - 18 /* lines in HelloA1.xoo */ - 5 /* lines in HelloA2.xoo */ - 12 /* lines in HelloB1.xoo */ - 1 /* HelloA1.xoo file */, 0, 57 - 18 - 5 - 12, @@ -246,15 +246,14 @@ public class IssueExclusionsTest { } private void checkIssueCountBySeverity(int total, int taggedXoo, int perLine, int perFile, int blocker, int perModule) { - Resource project = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(PROJECT_KEY, "violations", "info_violations", "minor_violations", "major_violations", - "blocker_violations", "critical_violations")); - assertThat(project.getMeasureIntValue("violations")).isEqualTo(total); - assertThat(project.getMeasureIntValue("info_violations")).isEqualTo(taggedXoo); // Has tag 'xoo' - assertThat(project.getMeasureIntValue("minor_violations")).isEqualTo(perLine); // One per line - assertThat(project.getMeasureIntValue("major_violations")).isEqualTo(perFile); // One per file - assertThat(project.getMeasureIntValue("blocker_violations")).isEqualTo(blocker); - assertThat(project.getMeasureIntValue("critical_violations")).isEqualTo(perModule); // One per module + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, PROJECT_KEY, "violations", "info_violations", "minor_violations", "major_violations", + "blocker_violations", "critical_violations"); + assertThat(measures.get("violations").intValue()).isEqualTo(total); + assertThat(measures.get("info_violations").intValue()).isEqualTo(taggedXoo); // Has tag 'xoo' + assertThat(measures.get("minor_violations").intValue()).isEqualTo(perLine); // One per line + assertThat(measures.get("major_violations").intValue()).isEqualTo(perFile); // One per file + assertThat(measures.get("blocker_violations").intValue()).isEqualTo(blocker); + assertThat(measures.get("critical_violations").intValue()).isEqualTo(perModule); // One per module } private void checkAnalysisFails(String... properties) { diff --git a/it/it-tests/src/test/java/it/customMeasure/CustomMeasuresTest.java b/it/it-tests/src/test/java/it/customMeasure/CustomMeasuresTest.java index b2ade28a916..c3ba2dc6c5c 100644 --- a/it/it-tests/src/test/java/it/customMeasure/CustomMeasuresTest.java +++ b/it/it-tests/src/test/java/it/customMeasure/CustomMeasuresTest.java @@ -27,9 +27,7 @@ import java.util.regex.Pattern; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; import static util.ItUtils.projectDir; @@ -52,13 +50,13 @@ public class CustomMeasuresTest { setBurnedBudget(1200.3); setTeamSize(4); - assertThat(getMeasure("team_size")).isNull(); - assertThat(getMeasure("burned_budget")).isNull(); + assertThat(getMeasureAsDouble("team_size")).isNull(); + assertThat(getMeasureAsDouble("burned_budget")).isNull(); analyzeProject(); - assertThat(getMeasure("burned_budget").getValue()).isEqualTo(1200.3); - assertThat(getMeasure("team_size").getIntValue()).isEqualTo(4); + assertThat(getMeasureAsDouble("burned_budget")).isEqualTo(1200.3); + assertThat(getMeasureAsDouble("team_size")).isEqualTo(4d); } @Test @@ -67,9 +65,9 @@ public class CustomMeasuresTest { setTeamSize(4); analyzeProject(); updateTeamSize(15); - assertThat(getMeasure("team_size").getIntValue()).isEqualTo(4); + assertThat(getMeasureAsDouble("team_size")).isEqualTo(4d); analyzeProject();// the value is available when the project is analyzed again - assertThat(getMeasure("team_size").getIntValue()).isEqualTo(15); + assertThat(getMeasureAsDouble("team_size")).isEqualTo(15d); } @Test @@ -78,10 +76,11 @@ public class CustomMeasuresTest { setTeamSize(4); analyzeProject(); deleteCustomMeasure("team_size"); - assertThat(getMeasure("team_size").getIntValue()).isEqualTo(4);// the value is still available. It will be removed during next analyzed + assertThat(getMeasureAsDouble("team_size")).isEqualTo(4d);// the value is still available. It will be removed during next + // analyzed analyzeProject(); - assertThat(getMeasure("team_size")).isNull(); + assertThat(getMeasureAsDouble("team_size")).isNull(); } private void analyzeProject() { @@ -112,8 +111,7 @@ public class CustomMeasuresTest { orchestrator.getServer().adminWsClient().post("api/custom_measures/delete", "id", customMeasureId); } - private Measure getMeasure(String metricKey) { - Resource resource = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, metricKey)); - return resource != null ? resource.getMeasure(metricKey) : null; + private Double getMeasureAsDouble(String metricKey) { + return ItUtils.getMeasureAsDouble(orchestrator, PROJECT_KEY, metricKey); } } diff --git a/it/it-tests/src/test/java/it/duplication/CrossModuleDuplicationsTest.java b/it/it-tests/src/test/java/it/duplication/CrossModuleDuplicationsTest.java index 371a4917249..6ffa169c9d7 100644 --- a/it/it-tests/src/test/java/it/duplication/CrossModuleDuplicationsTest.java +++ b/it/it-tests/src/test/java/it/duplication/CrossModuleDuplicationsTest.java @@ -26,6 +26,7 @@ import com.sonar.orchestrator.locator.FileLocation; import it.Category4Suite; import java.io.File; import java.io.IOException; +import java.util.Map; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.BeforeClass; @@ -33,11 +34,10 @@ import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; public class CrossModuleDuplicationsTest { private static final String PROJECT_KEY = "cross-module"; @@ -59,7 +59,7 @@ public class CrossModuleDuplicationsTest { public void setUpProject() throws IOException { orchestrator.resetData(); orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/duplication/xoo-duplication-profile.xml")); - + FileUtils.copyDirectory(ItUtils.projectDir(PROJECT_DIR), temp.getRoot()); projectDir = temp.getRoot(); } @@ -135,17 +135,10 @@ public class CrossModuleDuplicationsTest { } private static void verifyDuplicationMeasures(String componentKey, int duplicatedBlocks, int duplicatedLines, int duplicatedFiles, double duplicatedLinesDensity) { - Resource file = getComponent(componentKey); - assertThat(file.getMeasureValue("duplicated_blocks").intValue()).isEqualTo(duplicatedBlocks); - assertThat(file.getMeasureValue("duplicated_lines").intValue()).isEqualTo(duplicatedLines); - assertThat(file.getMeasureValue("duplicated_files").intValue()).isEqualTo(duplicatedFiles); - assertThat(file.getMeasureValue("duplicated_lines_density")).isEqualTo(duplicatedLinesDensity); - } - - private static Resource getComponent(String key) { - Resource component = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(key, "duplicated_lines", "duplicated_blocks", "duplicated_files", "duplicated_lines_density")); - assertThat(component).isNotNull(); - return component; + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, componentKey, "duplicated_lines", "duplicated_blocks", "duplicated_files", "duplicated_lines_density"); + assertThat(measures.get("duplicated_blocks").intValue()).isEqualTo(duplicatedBlocks); + assertThat(measures.get("duplicated_lines").intValue()).isEqualTo(duplicatedLines); + assertThat(measures.get("duplicated_files").intValue()).isEqualTo(duplicatedFiles); + assertThat(measures.get("duplicated_lines_density")).isEqualTo(duplicatedLinesDensity); } } diff --git a/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsOnRemoveFileTest.java b/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsOnRemoveFileTest.java index 3c063cb4a37..8134aad5057 100644 --- a/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsOnRemoveFileTest.java +++ b/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsOnRemoveFileTest.java @@ -26,10 +26,10 @@ import org.apache.commons.io.IOUtils; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.ResourceQuery; import static org.assertj.core.api.Assertions.assertThat; import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; +import static util.ItUtils.getComponent; import static util.ItUtils.runProjectAnalysis; import static util.selenium.Selenese.runSelenese; @@ -52,7 +52,7 @@ public class CrossProjectDuplicationsOnRemoveFileTest { // Remove origin project orchestrator.getServer().adminWsClient().post("api/projects/bulk_delete", "keys", ORIGIN_PROJECT); - assertThat(orchestrator.getServer().getAdminWsClient().find(ResourceQuery.create(ORIGIN_PROJECT))).isNull(); + assertThat(getComponent(orchestrator, ORIGIN_PROJECT)).isNull(); } @Test diff --git a/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsTest.java b/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsTest.java index 88702941d78..109ac4ddacd 100644 --- a/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsTest.java +++ b/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsTest.java @@ -23,19 +23,22 @@ import com.google.common.collect.ObjectArrays; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.locator.FileLocation; import it.Category4Suite; -import java.util.List; +import java.util.Map; import org.apache.commons.io.IOUtils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.issue.Issue; -import org.sonar.wsclient.issue.IssueQuery; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.issue.SearchWsRequest; +import util.issue.IssueRule; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; +import static util.ItUtils.getMeasureAsDouble; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; +import static util.ItUtils.newAdminWsClient; import static util.ItUtils.runProjectAnalysis; import static util.ItUtils.setServerProperty; import static util.selenium.Selenese.runSelenese; @@ -56,6 +59,9 @@ public class CrossProjectDuplicationsTest { @ClassRule public static Orchestrator orchestrator = Category4Suite.ORCHESTRATOR; + @ClassRule + public static final IssueRule issueRule = IssueRule.from(orchestrator); + @BeforeClass public static void analyzeProjects() { orchestrator.resetData(); @@ -83,20 +89,17 @@ public class CrossProjectDuplicationsTest { @Test public void duplicate_project_has_duplication_as_it_has_been_analyzed_twice() throws Exception { - assertThat(getMeasure(DUPLICATE_PROJECT, "duplicated_lines")).isEqualTo(27); - assertThat(getMeasure(DUPLICATE_PROJECT, "duplicated_blocks")).isEqualTo(1); - assertThat(getMeasure(DUPLICATE_PROJECT, "duplicated_files")).isEqualTo(1); - assertThat(getComponent(DUPLICATE_PROJECT, "duplicated_lines_density").getMeasureValue("duplicated_lines_density")).isEqualTo(45d); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, DUPLICATE_PROJECT, "duplicated_lines", "duplicated_blocks", "duplicated_files", "duplicated_lines_density"); + assertThat(measures.get("duplicated_lines").intValue()).isEqualTo(27); + assertThat(measures.get("duplicated_blocks").intValue()).isEqualTo(1); + assertThat(measures.get("duplicated_files").intValue()).isEqualTo(1); + assertThat(measures.get("duplicated_lines_density")).isEqualTo(45d); } @Test public void issue_on_duplicated_blocks_is_generated_on_file() throws Exception { - List<Issue> issues = orchestrator.getServer().wsClient().issueClient() - .find(IssueQuery.create() - .components(DUPLICATE_FILE) - .rules("common-xoo:DuplicatedBlocks")) - .list(); - assertThat(issues).hasSize(1); + assertThat(issueRule.search(new SearchWsRequest().setComponentKeys(singletonList(DUPLICATE_FILE)).setRules(singletonList("common-xoo:DuplicatedBlocks"))).getIssuesList()) + .hasSize(1); } @Test @@ -155,22 +158,12 @@ public class CrossProjectDuplicationsTest { additionalProperties, String.class)); } - private static int getMeasure(String projectKey, String metricKey) { - Integer intMeasure = getComponent(projectKey, metricKey).getMeasureIntValue(metricKey); - assertThat(intMeasure).isNotNull(); - return intMeasure; - } - - private static Resource getComponent(String projectKey, String metricKey) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(projectKey, metricKey)); - } - private static void assertProjectHasNoDuplication(String projectKey) { - assertThat(getMeasure(projectKey, "duplicated_lines")).isZero(); + assertThat(getMeasureAsDouble(orchestrator, projectKey, "duplicated_lines")).isZero(); } private static void verifyWsResultOnDuplicateFile(String ws, String expectedFilePath) throws Exception { - String duplication = orchestrator.getServer().adminWsClient().get(ws, "key", DUPLICATE_FILE); + String duplication = newAdminWsClient(orchestrator).wsConnector().call(new GetRequest(ws).setParam("key", DUPLICATE_FILE)).content(); assertEquals(IOUtils.toString(CrossProjectDuplicationsTest.class.getResourceAsStream("/duplication/CrossProjectDuplicationsTest/" + expectedFilePath), "UTF-8"), duplication, false); } diff --git a/it/it-tests/src/test/java/it/duplication/DuplicationsTest.java b/it/it-tests/src/test/java/it/duplication/DuplicationsTest.java index 18e2d20d498..79e645af3d0 100644 --- a/it/it-tests/src/test/java/it/duplication/DuplicationsTest.java +++ b/it/it-tests/src/test/java/it/duplication/DuplicationsTest.java @@ -23,19 +23,19 @@ import com.google.common.collect.ObjectArrays; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.locator.FileLocation; import it.Category4Suite; -import java.util.List; +import java.util.Map; import org.apache.commons.io.IOUtils; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.issue.Issue; -import org.sonar.wsclient.issue.IssueQuery; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.client.issue.SearchWsRequest; +import util.issue.IssueRule; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; import static util.ItUtils.runProjectAnalysis; import static util.ItUtils.setServerProperty; @@ -48,6 +48,9 @@ public class DuplicationsTest { @ClassRule public static Orchestrator orchestrator = Category4Suite.ORCHESTRATOR; + @ClassRule + public static final IssueRule issueRule = IssueRule.from(orchestrator); + @BeforeClass public static void analyzeProjects() { orchestrator.resetData(); @@ -119,11 +122,7 @@ public class DuplicationsTest { @Test public void issues_on_duplicated_blocks_are_generated_on_each_file() throws Exception { - List<Issue> issues = orchestrator.getServer().wsClient().issueClient() - .find(IssueQuery.create() - .rules("common-xoo:DuplicatedBlocks")) - .list(); - assertThat(issues).hasSize(13); + assertThat(issueRule.search(new SearchWsRequest().setRules(singletonList("common-xoo:DuplicatedBlocks"))).getIssuesList()).hasSize(13); } @Test @@ -138,19 +137,16 @@ public class DuplicationsTest { "api/duplications/show", "duplications_show-expected.json"); } - private static Resource getComponent(String key) { - Resource component = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(key, "duplicated_lines", "duplicated_blocks", "duplicated_files", "duplicated_lines_density")); - assertThat(component).isNotNull(); - return component; + private static Map<String, Double> getMeasures(String key) { + return getMeasuresAsDoubleByMetricKey(orchestrator, key, "duplicated_lines", "duplicated_blocks", "duplicated_files", "duplicated_lines_density"); } private static void verifyDuplicationMeasures(String componentKey, int duplicatedBlocks, int duplicatedLines, int duplicatedFiles, double duplicatedLinesDensity) { - Resource file = getComponent(componentKey); - assertThat(file.getMeasureValue("duplicated_blocks").intValue()).isEqualTo(duplicatedBlocks); - assertThat(file.getMeasureValue("duplicated_lines").intValue()).isEqualTo(duplicatedLines); - assertThat(file.getMeasureValue("duplicated_files").intValue()).isEqualTo(duplicatedFiles); - assertThat(file.getMeasureValue("duplicated_lines_density")).isEqualTo(duplicatedLinesDensity); + Map<String, Double> measures = getMeasures(componentKey); + assertThat(measures.get("duplicated_blocks").intValue()).isEqualTo(duplicatedBlocks); + assertThat(measures.get("duplicated_lines").intValue()).isEqualTo(duplicatedLines); + assertThat(measures.get("duplicated_files").intValue()).isEqualTo(duplicatedFiles); + assertThat(measures.get("duplicated_lines_density")).isEqualTo(duplicatedLinesDensity); } private static void analyzeProject(String projectKey, String... additionalProperties) { diff --git a/it/it-tests/src/test/java/it/duplication/NewDuplicationsTest.java b/it/it-tests/src/test/java/it/duplication/NewDuplicationsTest.java index b0c3bf917e6..0e30868ba00 100644 --- a/it/it-tests/src/test/java/it/duplication/NewDuplicationsTest.java +++ b/it/it-tests/src/test/java/it/duplication/NewDuplicationsTest.java @@ -21,14 +21,16 @@ package it.duplication; import com.sonar.orchestrator.Orchestrator; import it.Category4Suite; +import java.util.Map; import org.assertj.core.data.Offset; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; +import static java.lang.Double.parseDouble; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresWithVariationsByMetricKey; import static util.ItUtils.runProjectAnalysis; public class NewDuplicationsTest { @@ -51,35 +53,32 @@ public class NewDuplicationsTest { @Test public void new_duplications_on_project() throws Exception { - Resource project = getComponent("new-duplications"); - assertThat(project.getMeasure("new_lines").getVariation1()).isEqualTo(83d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_lines").getVariation1()).isEqualTo(71d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_lines_density").getVariation1()).isEqualTo(85.5d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_blocks").getVariation1()).isEqualTo(12d, DEFAULT_OFFSET); + Map<String, WsMeasures.Measure> measures = getMeasures("new-duplications"); + assertThat(parseDouble(measures.get("new_lines").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(83d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_lines").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(71d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_lines_density").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(85.5d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_blocks").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(12d, DEFAULT_OFFSET); } @Test public void new_duplications_on_directory() throws Exception { - Resource project = getComponent("new-duplications:src/main/xoo/duplicated_lines_with_other_dir1"); - assertThat(project.getMeasure("new_lines").getVariation1()).isEqualTo(24d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_lines").getVariation1()).isEqualTo(24d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_lines_density").getVariation1()).isEqualTo(100d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_blocks").getVariation1()).isEqualTo(7d, DEFAULT_OFFSET); + Map<String, WsMeasures.Measure> measures = getMeasures("new-duplications:src/main/xoo/duplicated_lines_with_other_dir1"); + assertThat(parseDouble(measures.get("new_lines").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(24d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_lines").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(24d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_lines_density").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(100d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_blocks").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(7d, DEFAULT_OFFSET); } @Test public void new_duplications_on_file() throws Exception { - Resource project = getComponent("new-duplications:src/main/xoo/duplicated_lines_within_same_file/DuplicatedLinesInSameFile.xoo"); - assertThat(project.getMeasure("new_lines").getVariation1()).isEqualTo(41d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_lines").getVariation1()).isEqualTo(29d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_lines_density").getVariation1()).isEqualTo(70.7d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_duplicated_blocks").getVariation1()).isEqualTo(2d, DEFAULT_OFFSET); + Map<String, WsMeasures.Measure> measures = getMeasures("new-duplications:src/main/xoo/duplicated_lines_within_same_file/DuplicatedLinesInSameFile.xoo"); + assertThat(parseDouble(measures.get("new_lines").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(41d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_lines").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(29d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_lines_density").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(70.7d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_duplicated_blocks").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(2d, DEFAULT_OFFSET); } - private static Resource getComponent(String key) { - Resource component = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(key, "new_lines", "new_duplicated_lines", "new_duplicated_lines_density", "new_duplicated_blocks").setIncludeTrends(true)); - assertThat(component).isNotNull(); - return component; + private static Map<String, WsMeasures.Measure> getMeasures(String key) { + return getMeasuresWithVariationsByMetricKey(orchestrator, key, "new_lines", "new_duplicated_lines", "new_duplicated_lines_density", "new_duplicated_blocks"); } } diff --git a/it/it-tests/src/test/java/it/issue/IssueFilterExtensionTest.java b/it/it-tests/src/test/java/it/issue/IssueFilterExtensionTest.java index d580259b668..b05c94c81aa 100644 --- a/it/it-tests/src/test/java/it/issue/IssueFilterExtensionTest.java +++ b/it/it-tests/src/test/java/it/issue/IssueFilterExtensionTest.java @@ -20,17 +20,17 @@ package it.issue; import java.util.List; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.sonar.wsclient.issue.Issue; import org.sonar.wsclient.issue.IssueQuery; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ProjectAnalysis; import util.ProjectAnalysisRule; +import util.issue.IssueRule; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasureAsDouble; /** * Tests the extension point IssueFilter @@ -40,6 +40,9 @@ public class IssueFilterExtensionTest extends AbstractIssueTest { @Rule public final ProjectAnalysisRule projectAnalysisRule = ProjectAnalysisRule.from(ORCHESTRATOR); + @ClassRule + public static final IssueRule issueRule = IssueRule.from(ORCHESTRATOR); + private final String manyRuleProfileKey = projectAnalysisRule.registerProfile("/issue/IssueFilterExtensionTest/xoo-with-many-rules.xml"); private final String xooMultiModuleProjectKey = projectAnalysisRule.registerProject("shared/xoo-multi-modules-sample"); private final ProjectAnalysis analysis = projectAnalysisRule.newProjectAnalysis(xooMultiModuleProjectKey) @@ -56,7 +59,7 @@ public class IssueFilterExtensionTest extends AbstractIssueTest { assertThat(issue.componentKey()).doesNotContain("HelloA1"); } - assertThat(getMeasure(xooMultiModuleProjectKey, "violations").getIntValue()).isEqualTo(issues.size()); + assertThat(getMeasureAsDouble(ORCHESTRATOR, xooMultiModuleProjectKey, "violations").intValue()).isEqualTo(issues.size()); } @Test @@ -86,17 +89,12 @@ public class IssueFilterExtensionTest extends AbstractIssueTest { } } - private static List<Issue> searchUnresolvedIssues(String projectName) { - return searchIssues(IssueQuery.create().componentRoots(projectName).resolved(true)); - } - - private static List<Issue> searchResolvedIssues(String projectName) { - return searchIssues(IssueQuery.create().componentRoots(projectName).resolved(false)); + private static List<Issue> searchUnresolvedIssues(String projectKey) { + return searchIssues(IssueQuery.create().componentRoots(projectKey).resolved(true)); } - private static Measure getMeasure(String projectKey, String metricKey) { - Resource resource = ORCHESTRATOR.getServer().getWsClient().find(ResourceQuery.createForMetrics(projectKey, metricKey)); - return resource == null ? null : resource.getMeasure(metricKey); + private static List<Issue> searchResolvedIssues(String projectKey) { + return searchIssues(IssueQuery.create().componentRoots(projectKey).resolved(false)); } private static int countModuleIssues(List<Issue> issues) { diff --git a/it/it-tests/src/test/java/it/issue/IssueFilterTest.java b/it/it-tests/src/test/java/it/issue/IssueFilterTest.java index 6482270f2c4..2f061f9b3ae 100644 --- a/it/it-tests/src/test/java/it/issue/IssueFilterTest.java +++ b/it/it-tests/src/test/java/it/issue/IssueFilterTest.java @@ -20,12 +20,12 @@ package it.issue; import com.sonar.orchestrator.locator.FileLocation; +import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; import static util.ItUtils.runProjectAnalysis; import static util.ItUtils.setServerProperties; @@ -105,13 +105,11 @@ public class IssueFilterTest extends AbstractIssueTest { } private void checkIssueCountBySeverity(int total, int perFile, int perCommonRule, int perModule) { - Resource project = ORCHESTRATOR.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(PROJECT_KEY, "violations", "major_violations", "blocker_violations", "critical_violations")); - assertThat(project.getMeasureIntValue("violations")).isEqualTo(total); - assertThat(project.getMeasureIntValue("major_violations")).isEqualTo(perFile); // One per file - assertThat(project.getMeasureIntValue("blocker_violations")).isEqualTo(perCommonRule); // On per common rule - // 'InsufficientCommentDensity' - assertThat(project.getMeasureIntValue("critical_violations")).isEqualTo(perModule); // One per module + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(ORCHESTRATOR, PROJECT_KEY, "violations", "major_violations", "blocker_violations", "critical_violations"); + assertThat(measures.get("violations").intValue()).isEqualTo(total); + assertThat(measures.get("major_violations").intValue()).isEqualTo(perFile); // One per file + assertThat(measures.get("blocker_violations").intValue()).isEqualTo(perCommonRule); // On per common rule + assertThat(measures.get("critical_violations").intValue()).isEqualTo(perModule); // One per module } } diff --git a/it/it-tests/src/test/java/it/issue/IssueMeasureTest.java b/it/it-tests/src/test/java/it/issue/IssueMeasureTest.java index 507d88cc349..e3f36cba061 100644 --- a/it/it-tests/src/test/java/it/issue/IssueMeasureTest.java +++ b/it/it-tests/src/test/java/it/issue/IssueMeasureTest.java @@ -21,15 +21,14 @@ package it.issue; import com.sonar.orchestrator.locator.FileLocation; import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.sonar.wsclient.Sonar; import org.sonar.wsclient.issue.Issue; import org.sonar.wsclient.issue.IssueQuery; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; import static util.ItUtils.runProjectAnalysis; public class IssueMeasureTest extends AbstractIssueTest { @@ -51,15 +50,15 @@ public class IssueMeasureTest extends AbstractIssueTest { assertThat(search(IssueQuery.create().componentRoots(MULTI_MODULE_SAMPLE_PROJECT_KEY)).paging().total()).isEqualTo(136); - Resource project = ORCHESTRATOR.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(MULTI_MODULE_SAMPLE_PROJECT_KEY, "violations", "info_violations", "minor_violations", "major_violations", - "blocker_violations", "critical_violations")); - assertThat(project.getMeasureIntValue("violations")).isEqualTo(136); - assertThat(project.getMeasureIntValue("info_violations")).isEqualTo(2); - assertThat(project.getMeasureIntValue("minor_violations")).isEqualTo(61); - assertThat(project.getMeasureIntValue("major_violations")).isEqualTo(65); - assertThat(project.getMeasureIntValue("blocker_violations")).isEqualTo(4); - assertThat(project.getMeasureIntValue("critical_violations")).isEqualTo(4); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(ORCHESTRATOR, MULTI_MODULE_SAMPLE_PROJECT_KEY, "violations", "info_violations", "minor_violations", + "major_violations", + "blocker_violations", "critical_violations"); + assertThat(measures.get("violations")).isEqualTo(136); + assertThat(measures.get("info_violations")).isEqualTo(2); + assertThat(measures.get("minor_violations")).isEqualTo(61); + assertThat(measures.get("major_violations")).isEqualTo(65); + assertThat(measures.get("blocker_violations")).isEqualTo(4); + assertThat(measures.get("critical_violations")).isEqualTo(4); } /** @@ -86,13 +85,13 @@ public class IssueMeasureTest extends AbstractIssueTest { // Re analyze the project to compute measures runProjectAnalysis(ORCHESTRATOR, "shared/xoo-sample"); - Resource project = ORCHESTRATOR.getServer().getWsClient().find( - ResourceQuery.createForMetrics(SAMPLE_PROJECT_KEY, "false_positive_issues", "wont_fix_issues", "open_issues", "reopened_issues", "confirmed_issues")); - assertThat(project.getMeasureIntValue("false_positive_issues")).isEqualTo(1); - assertThat(project.getMeasureIntValue("wont_fix_issues")).isEqualTo(1); - assertThat(project.getMeasureIntValue("open_issues")).isEqualTo(13); - assertThat(project.getMeasureIntValue("reopened_issues")).isEqualTo(1); - assertThat(project.getMeasureIntValue("confirmed_issues")).isEqualTo(1); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(ORCHESTRATOR, SAMPLE_PROJECT_KEY, "false_positive_issues", "wont_fix_issues", "open_issues", "reopened_issues", + "confirmed_issues"); + assertThat(measures.get("false_positive_issues")).isEqualTo(1); + assertThat(measures.get("wont_fix_issues")).isEqualTo(1); + assertThat(measures.get("open_issues")).isEqualTo(13); + assertThat(measures.get("reopened_issues")).isEqualTo(1); + assertThat(measures.get("confirmed_issues")).isEqualTo(1); } @Test @@ -105,9 +104,9 @@ public class IssueMeasureTest extends AbstractIssueTest { assertThat(searchIssuesByProject(SAMPLE_PROJECT_KEY)).isEmpty(); - Resource project = ORCHESTRATOR.getServer().getWsClient().find(ResourceQuery.createForMetrics(SAMPLE_PROJECT_KEY, "violations", "blocker_violations")); - assertThat(project.getMeasureIntValue("violations")).isEqualTo(0); - assertThat(project.getMeasureIntValue("blocker_violations")).isEqualTo(0); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(ORCHESTRATOR, SAMPLE_PROJECT_KEY, "violations", "blocker_violations"); + assertThat(measures.get("violations")).isEqualTo(0); + assertThat(measures.get("blocker_violations")).isEqualTo(0); } /** @@ -123,11 +122,9 @@ public class IssueMeasureTest extends AbstractIssueTest { ORCHESTRATOR.getServer().associateProjectToQualityProfile(projectKey, "xoo", "one-issue-per-file-profile"); runProjectAnalysis(ORCHESTRATOR, "shared/xoo-sample-with-tests"); - Sonar wsClient = ORCHESTRATOR.getServer().getAdminWsClient(); - // Store current number of issues - Resource project = wsClient.find(ResourceQuery.createForMetrics(testKey, "violations")); - assertThat(project.getMeasureIntValue("violations")).isEqualTo(1); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(ORCHESTRATOR, testKey, "violations"); + assertThat(measures.get("violations")).isEqualTo(1); } } diff --git a/it/it-tests/src/test/java/it/issue/NewIssuesMeasureTest.java b/it/it-tests/src/test/java/it/issue/NewIssuesMeasureTest.java index 39c4985a940..b28d7db8b2d 100644 --- a/it/it-tests/src/test/java/it/issue/NewIssuesMeasureTest.java +++ b/it/it-tests/src/test/java/it/issue/NewIssuesMeasureTest.java @@ -21,18 +21,20 @@ package it.issue; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; -import java.util.List; +import java.util.Map; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.sonar.wsclient.issue.IssueQuery; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; +import org.sonarqube.ws.WsMeasures.Measure; import util.ItUtils; +import static java.lang.Integer.parseInt; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresWithVariationsByMetricKey; +import static util.ItUtils.getPeriodMeasureValuesByIndex; import static util.ItUtils.projectDir; import static util.ItUtils.setServerProperty; @@ -72,19 +74,15 @@ public class NewIssuesMeasureTest extends AbstractIssueTest { ORCHESTRATOR.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); assertThat(ORCHESTRATOR.getServer().wsClient().issueClient().find(IssueQuery.create()).list()).isNotEmpty(); - Resource newIssues = ORCHESTRATOR.getServer().getWsClient() - .find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", "new_violations").setIncludeTrends(true)); - List<Measure> measures = newIssues.getMeasures(); - assertThat(measures.get(0).getVariation1().intValue()).isEqualTo(17); - assertThat(measures.get(0).getVariation2().intValue()).isEqualTo(17); + Map<Integer, Double> newIssues = getPeriodMeasureValuesByIndex(ORCHESTRATOR, "sample:src/main/xoo/sample/Sample.xoo", "new_violations"); + assertThat(newIssues.get(1)).isEqualTo(17); + assertThat(newIssues.get(2)).isEqualTo(17); // second analysis, with exactly the same profile -> no new issues ORCHESTRATOR.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); assertThat(ORCHESTRATOR.getServer().wsClient().issueClient().find(IssueQuery.create()).list()).isNotEmpty(); - newIssues = ORCHESTRATOR.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", "new_violations").setIncludeTrends(true)); - // No variation => measure is purged - assertThat(newIssues).isNull(); + assertThat(getPeriodMeasureValuesByIndex(ORCHESTRATOR, "sample:src/main/xoo/sample/Sample.xoo", "new_violations").values()).containsOnly(0d, 0d, 0d); } @Test @@ -99,11 +97,9 @@ public class NewIssuesMeasureTest extends AbstractIssueTest { ORCHESTRATOR.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); // new issues measures should be to 0 on project on 2 periods as new issues has been created - Resource file = ORCHESTRATOR.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample", "new_violations").setIncludeTrends(true)); - List<Measure> measures = file.getMeasures(); - Measure newIssues = find(measures, "new_violations"); - assertThat(newIssues.getVariation1().intValue()).isEqualTo(0); - assertThat(newIssues.getVariation2().intValue()).isEqualTo(0); + Map<Integer, Double> measures = getPeriodMeasureValuesByIndex(ORCHESTRATOR, "sample", "new_violations"); + assertThat(measures.get(1)).isZero(); + assertThat(measures.get(2)).isZero(); } /** @@ -123,21 +119,17 @@ public class NewIssuesMeasureTest extends AbstractIssueTest { ORCHESTRATOR.executeBuilds(SonarScanner.create(projectDir("shared/xoo-history-v2"))); assertThat(ORCHESTRATOR.getServer().wsClient().issueClient().find(IssueQuery.create()).list()).isNotEmpty(); - Resource file = ORCHESTRATOR.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample", "new_violations", "violations", "ncloc").setIncludeTrends(true)); - List<Measure> measures = file.getMeasures(); - Measure newIssues = find(measures, "new_violations"); - assertThat(newIssues.getVariation1().intValue()).isEqualTo(17); - assertThat(newIssues.getVariation2().intValue()).isEqualTo(17); - - Measure violations = find(measures, "violations"); - assertThat(violations.getValue().intValue()).isEqualTo(43); - assertThat(violations.getVariation1().intValue()).isEqualTo(17); - assertThat(violations.getVariation2().intValue()).isEqualTo(17); - - Measure ncloc = find(measures, "ncloc"); - assertThat(ncloc.getValue().intValue()).isEqualTo(40); - assertThat(ncloc.getVariation1().intValue()).isEqualTo(16); - assertThat(ncloc.getVariation2().intValue()).isEqualTo(16); + + Map<String, Measure> measures = getMeasuresWithVariationsByMetricKey(ORCHESTRATOR, "sample", "new_violations", "violations", "ncloc"); + assertThat(measures.get("new_violations").getPeriods().getPeriodsValueList()).extracting(WsMeasures.PeriodValue::getValue).containsOnly("17", "17", "17"); + + Measure violations = measures.get("violations"); + assertThat(parseInt(violations.getValue())).isEqualTo(43); + assertThat(violations.getPeriods().getPeriodsValueList()).extracting(periodValue -> parseInt(periodValue.getValue())).containsOnly(17, 17, 17); + + Measure ncloc = measures.get("ncloc"); + assertThat(parseInt(ncloc.getValue())).isEqualTo(40); + assertThat(ncloc.getPeriods().getPeriodsValueList()).extracting(periodValue -> parseInt(periodValue.getValue())).containsOnly(16, 16, 16); } @Test @@ -155,20 +147,8 @@ public class NewIssuesMeasureTest extends AbstractIssueTest { ORCHESTRATOR.getServer().associateProjectToQualityProfile("com.sonarsource.it.samples:multi-modules-sample", "xoo", "profile2"); ORCHESTRATOR.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - Resource project = ORCHESTRATOR.getServer().getWsClient() - .find(ResourceQuery.createForMetrics("com.sonarsource.it.samples:multi-modules-sample", "new_violations", "violations").setIncludeTrends(true)); - List<Measure> measures = project.getMeasures(); - Measure newIssues = find(measures, "new_violations"); - assertThat(newIssues.getVariation1().intValue()).isEqualTo(65); - } - - private Measure find(List<Measure> measures, String metricKey) { - for (Measure measure : measures) { - if (measure.getMetricKey().equals(metricKey)) { - return measure; - } - } - return null; + Map<Integer, Double> periodMeasures = getPeriodMeasureValuesByIndex(ORCHESTRATOR, "com.sonarsource.it.samples:multi-modules-sample", "new_violations"); + assertThat(periodMeasures.get(1)).isEqualTo(65); } } diff --git a/it/it-tests/src/test/java/it/measure/DecimalScaleMetricTest.java b/it/it-tests/src/test/java/it/measure/DecimalScaleMetricTest.java index b874aa16de1..455d4ec907f 100644 --- a/it/it-tests/src/test/java/it/measure/DecimalScaleMetricTest.java +++ b/it/it-tests/src/test/java/it/measure/DecimalScaleMetricTest.java @@ -23,11 +23,10 @@ import com.sonar.orchestrator.Orchestrator; import it.Category3Suite; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasureAsDouble; /** * SONAR-6939 @@ -49,10 +48,6 @@ public class DecimalScaleMetricTest { "sonar.projectKey", projectKey, "sonar.scanner.feedDecimalScaleMetric", String.valueOf(true)); - Resource resource = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(projectKey, metricKey)); - // Ability to define decimal scale of metrics was introduced in v5.3. By default it is 1. - assertThat(resource.getMeasureValue(metricKey)).isEqualTo(0.0001); - assertThat(resource.getMeasureFormattedValue(metricKey, null)).isEqualTo("0.0001"); + assertThat(getMeasureAsDouble(orchestrator, projectKey, metricKey)).isEqualTo(0.0001); } } diff --git a/it/it-tests/src/test/java/it/measureHistory/DifferentialPeriodsTest.java b/it/it-tests/src/test/java/it/measureHistory/DifferentialPeriodsTest.java index 81a757b9fda..50904a35831 100644 --- a/it/it-tests/src/test/java/it/measureHistory/DifferentialPeriodsTest.java +++ b/it/it-tests/src/test/java/it/measureHistory/DifferentialPeriodsTest.java @@ -19,33 +19,25 @@ */ package it.measureHistory; -import com.google.common.collect.ImmutableMap; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.locator.FileLocation; import it.Category1Suite; -import java.util.ArrayList; import java.util.Date; -import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; -import org.sonarqube.ws.WsMeasures; import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.measure.ComponentWsRequest; import pageobjects.Navigation; -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.apache.commons.lang.time.DateUtils.addDays; import static org.assertj.core.api.Assertions.assertThat; import static util.ItUtils.formatDate; +import static util.ItUtils.getMeasure; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; +import static util.ItUtils.getPeriodMeasureValuesByIndex; import static util.ItUtils.newAdminWsClient; import static util.ItUtils.resetPeriods; import static util.ItUtils.runProjectAnalysis; @@ -98,21 +90,16 @@ public class DifferentialPeriodsTest { runProjectAnalysis(orchestrator, "shared/xoo-sample", "sonar.projectDate", formatDate(addDays(new Date(), -20))); // New technical debt only comes from new issues - Resource newTechnicalDebt = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt").setIncludeTrends(true)); - List<Measure> measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation4()).isEqualTo(17); - assertThat(measures.get(0).getVariation5()).isEqualTo(17); + Map<Integer, Double> periodsMeasure = getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt"); + assertThat(periodsMeasure.get(4)).isEqualTo(17); + assertThat(periodsMeasure.get(5)).isEqualTo(17); // Third analysis, today, with exactly the same profile -> no new issues so no new technical debt orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line"); runProjectAnalysis(orchestrator, "shared/xoo-sample"); - newTechnicalDebt = orchestrator.getServer().getWsClient().find( - ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt").setIncludeTrends(true)); - // No variation => measure is purged - assertThat(newTechnicalDebt).isNull(); + assertThat(getMeasure(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt")).isNull(); } /** @@ -139,10 +126,8 @@ public class DifferentialPeriodsTest { runProjectAnalysis(orchestrator, "shared/xoo-sample"); // Project should have 17 new issues for period 1 - Resource newTechnicalDebt = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(PROJECT_KEY, "violations").setIncludeTrends(true)); - List<Measure> measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation1()).isEqualTo(17); + Map<Integer, Double> periodsMeasure = getPeriodMeasureValuesByIndex(orchestrator, PROJECT_KEY, "violations"); + assertThat(periodsMeasure.get(1)).isEqualTo(17); // Check on ui that it's possible to define leak period on project Navigation.get(orchestrator).openHomepage().logIn().asAdmin().openSettings("sample") @@ -171,10 +156,8 @@ public class DifferentialPeriodsTest { "sonar.modules", "module_a,module_b"); // Variation on module b should exist - Resource ncloc = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(MULTI_MODULE_PROJECT_KEY + ":module_b", "ncloc").setIncludeTrends(true)); - List<Measure> measures = ncloc.getMeasures(); - assertThat(measures.get(0).getVariation1()).isEqualTo(24); + Map<Integer, Double> periodsMeasure = getPeriodMeasureValuesByIndex(orchestrator, MULTI_MODULE_PROJECT_KEY + ":module_b", "ncloc"); + assertThat(periodsMeasure.get(1)).isEqualTo(24); } @Test @@ -214,25 +197,11 @@ public class DifferentialPeriodsTest { "sonar.scm.provider", "xoo", "sonar.scm.disabled", "false"); // New lines measures is zero - assertMeasures(projectKey, ImmutableMap.of("new_lines", 0, "new_lines_to_cover", 0)); - } - - private void assertMeasures(String projectKey, Map<String, Integer> expectedMeasures) { - WsMeasures.ComponentWsResponse response = CLIENT.measures().component(new ComponentWsRequest() - .setComponentKey(projectKey) - .setMetricKeys(new ArrayList<>(expectedMeasures.keySet())) - .setAdditionalFields(singletonList("periods"))); - Map<String, Integer> measures = response.getComponent().getMeasuresList().stream() - .collect(Collectors.toMap(WsMeasures.Measure::getMetric, m -> Integer.parseInt(m.getPeriods().getPeriodsValue(0).getValue()))); - assertThat(measures).isEqualTo(expectedMeasures); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, projectKey, "new_lines").get(1)).isEqualTo(0); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, projectKey, "new_lines_to_cover").get(1)).isEqualTo(0); } private void assertNoMeasures(String projectKey, String... metrics) { - WsMeasures.ComponentWsResponse response = CLIENT.measures().component(new ComponentWsRequest() - .setComponentKey(projectKey) - .setMetricKeys(asList(metrics)) - .setAdditionalFields(singletonList("periods"))); - assertThat(response.getComponent().getMeasuresList()).isEmpty(); + assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, projectKey, metrics)).isEmpty(); } - } diff --git a/it/it-tests/src/test/java/it/measureHistory/SincePreviousVersionHistoryTest.java b/it/it-tests/src/test/java/it/measureHistory/SincePreviousVersionHistoryTest.java index fdade6ab969..5fccc477dd5 100644 --- a/it/it-tests/src/test/java/it/measureHistory/SincePreviousVersionHistoryTest.java +++ b/it/it-tests/src/test/java/it/measureHistory/SincePreviousVersionHistoryTest.java @@ -31,12 +31,15 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; +import static java.lang.Integer.parseInt; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; +import static org.sonarqube.ws.WsMeasures.Measure; +import static org.sonarqube.ws.WsMeasures.PeriodValue; +import static util.ItUtils.getMeasureWithVariations; +import static util.ItUtils.getPeriodMeasureValuesByIndex; import static util.ItUtils.projectDir; import static util.ItUtils.setServerProperty; @@ -101,20 +104,16 @@ public class SincePreviousVersionHistoryTest { analyzeProject("1.0-SNAPSHOT"); analyzeProject("1.0-SNAPSHOT"); - Resource project = getProject("files"); - Measure measure = project.getMeasure("files"); + Measure measure = getMeasureWithVariations(orchestrator, PROJECT, "files"); // There are 4 files - assertThat(measure.getValue()).isEqualTo(4); - - // nothing changed in the previous analysis - assertThat(project.getPeriod1Mode()).isEqualTo("previous_analysis"); - assertThat(measure.getVariation1()).isEqualTo(0); - - // but 2 files were added since the first analysis which was version 0.9 - assertThat(project.getPeriod3Mode()).isEqualTo("previous_version"); - assertThat(project.getPeriod3Param()).isEqualTo("0.9"); - assertThat(measure.getVariation3()).isEqualTo(2); + assertThat(parseInt(measure.getValue())).isEqualTo(4); + assertThat(measure.getPeriods().getPeriodsValueList()).extracting(PeriodValue::getIndex, PeriodValue::getValue) + .contains( + // nothing changed in the previous analysis + tuple(1, "0"), + // but 2 files were added since the first analysis which was version 0.9 + tuple(3, "2")); } /** @@ -127,19 +126,15 @@ public class SincePreviousVersionHistoryTest { // Analyze project by excluding some files analyzeProject("1.0-SNAPSHOT", "**/*2.xoo", toStringDate(DateUtils.addDays(now, -2))); // No difference measure after first analysis - assertThat(getProject("files").getMeasure("files").getVariation3()).isNull(); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, PROJECT, "files").get(3)).isNull(); analyzeProjectWithDate("1.0-SNAPSHOT", toStringDate(DateUtils.addDays(now, -1))); // No new version, first analysis is used -> 2 new files - assertThat(getProject("files").getMeasure("files").getVariation3()).isEqualTo(2); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, PROJECT, "files").get(3)).isEqualTo(2); analyzeProjectWithDate("1.0-SNAPSHOT", toStringDate(now)); // Still no new version, first analysis is used -> 2 new files - assertThat(getProject("files").getMeasure("files").getVariation3()).isEqualTo(2); - } - - private Resource getProject(String... metricKeys) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT, metricKeys).setIncludeTrends(true)); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, PROJECT, "files").get(3)).isEqualTo(2); } } diff --git a/it/it-tests/src/test/java/it/measureHistory/SinceXDaysHistoryTest.java b/it/it-tests/src/test/java/it/measureHistory/SinceXDaysHistoryTest.java index 1e49b43399e..dd2dc29143c 100644 --- a/it/it-tests/src/test/java/it/measureHistory/SinceXDaysHistoryTest.java +++ b/it/it-tests/src/test/java/it/measureHistory/SinceXDaysHistoryTest.java @@ -31,12 +31,13 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; import util.ItUtils; +import static java.lang.Integer.parseInt; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; +import static util.ItUtils.getMeasureWithVariations; import static util.ItUtils.projectDir; import static util.ItUtils.setServerProperty; @@ -81,16 +82,6 @@ public class SinceXDaysHistoryTest { } @Test - public void periods_are_well_defined() throws Exception { - Resource project = getProject("files"); - - assertThat(project.getPeriod1Mode()).isEqualTo("previous_analysis"); - - assertThat(project.getPeriod2Mode()).isEqualTo("days"); - assertThat(project.getPeriod2Param()).isEqualTo("30"); - } - - @Test public void check_files_variation() throws Exception { checkMeasure("files", 2, 3); } @@ -105,16 +96,10 @@ public class SinceXDaysHistoryTest { checkMeasure("new_violations", 24, 45); } - private void checkMeasure(String measure, int variation1, int variation2){ - Resource project = getProject(measure); - Measure newTechnicalDebt = project.getMeasure(measure); - - assertThat(newTechnicalDebt.getVariation1().intValue()).isEqualTo(variation1); - assertThat(newTechnicalDebt.getVariation2().intValue()).isEqualTo(variation2); - } - - private Resource getProject(String... metricKeys) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT, metricKeys).setIncludeTrends(true)); + private void checkMeasure(String metric, int variation1, int variation2) { + WsMeasures.Measure measure = getMeasureWithVariations(orchestrator, PROJECT, metric); + assertThat(measure.getPeriods().getPeriodsValueList()).extracting(WsMeasures.PeriodValue::getIndex, periodValue -> parseInt(periodValue.getValue())) + .contains(tuple(1, variation1), tuple(2, variation2)); } private static void analyzeProject() { @@ -132,7 +117,7 @@ public class SinceXDaysHistoryTest { orchestrator.executeBuild(runner); } - private static String getPastDate(int nbPastDays){ + private static String getPastDate(int nbPastDays) { return new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.addDays(new Date(), nbPastDays * -1)); } diff --git a/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java b/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java index b6a2cbf8957..8e080218880 100644 --- a/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java +++ b/it/it-tests/src/test/java/it/measureHistory/TimeMachineTest.java @@ -25,18 +25,23 @@ import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; import it.Category1Suite; import java.util.Date; +import java.util.Map; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import org.sonar.wsclient.services.TimeMachine; import org.sonar.wsclient.services.TimeMachineCell; import org.sonar.wsclient.services.TimeMachineQuery; +import org.sonarqube.ws.WsMeasures.Measure; import util.ItUtils; +import util.ItUtils.ComponentNavigation; +import static java.lang.Double.parseDouble; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getComponentNavigation; +import static util.ItUtils.getMeasuresByMetricKey; +import static util.ItUtils.getMeasuresWithVariationsByMetricKey; import static util.ItUtils.projectDir; import static util.ItUtils.setServerProperty; @@ -75,8 +80,9 @@ public class TimeMachineTest { @Test public void projectIsAnalyzed() { - assertThat(orchestrator.getServer().getWsClient().find(new ResourceQuery(PROJECT)).getVersion()).isEqualTo("1.0-SNAPSHOT"); - assertThat(orchestrator.getServer().getWsClient().find(new ResourceQuery(PROJECT)).getDate().getMonth()).isEqualTo(10); // November + ComponentNavigation component = getComponentNavigation(orchestrator, PROJECT); + assertThat(component.getVersion()).isEqualTo("1.0-SNAPSHOT"); + assertThat(component.getDate().getMonth()).isEqualTo(10); // November } @Test @@ -143,35 +149,24 @@ public class TimeMachineTest { @Test public void test_measure_variations() { - Resource project = getProject("files", "ncloc", "violations"); - - // period 1 : previous analysis - assertThat(project.getPeriod1Mode()).isEqualTo("previous_analysis"); - assertThat(project.getPeriod1Date()).isNotNull(); - + Map<String, Measure> measures = getMeasuresWithVariationsByMetricKey(orchestrator, PROJECT, "files", "ncloc", "violations"); // variations from previous analysis - assertThat(project.getMeasure("files").getVariation1()).isEqualTo(1.0); - assertThat(project.getMeasure("ncloc").getVariation1()).isEqualTo(16.0); - assertThat(project.getMeasure("violations").getVariation1()).isGreaterThan(0.0); + assertThat(parseDouble(measures.get("files").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(1.0); + assertThat(parseDouble(measures.get("ncloc").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(16.0); + assertThat(parseDouble(measures.get("violations").getPeriods().getPeriodsValue(0).getValue())).isGreaterThan(0.0); } /** * SONAR-4962 */ @Test - public void measure_variations_are_only_meaningful_when_includetrends() { - String[] metricKeys = {"violations", "new_violations"}; - - Resource projectWithTrends = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT, metricKeys).setIncludeTrends(true)); - assertThat(projectWithTrends.getMeasure("violations")).isNotNull(); - assertThat(projectWithTrends.getMeasure("new_violations")).isNotNull(); - - Resource projectWithoutTrends = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT, metricKeys).setIncludeTrends(false)); - assertThat(projectWithoutTrends.getMeasure("violations")).isNotNull(); - assertThat(projectWithoutTrends.getMeasure("new_violations")).isNull(); - } - - private Resource getProject(String... metricKeys) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT, metricKeys).setIncludeTrends(true)); + public void measure_variations_are_only_meaningful_when_additional_fields_contains_periods() { + Map<String, Measure> measures = getMeasuresWithVariationsByMetricKey(orchestrator, PROJECT, "violations", "new_violations"); + assertThat(measures.get("violations")).isNotNull(); + assertThat(measures.get("new_violations")).isNotNull(); + + measures = getMeasuresByMetricKey(orchestrator, PROJECT, "violations", "new_violations"); + assertThat(measures.get("violations")).isNotNull(); + assertThat(measures.get("new_violations")).isNull(); } } diff --git a/it/it-tests/src/test/java/it/plugins/checks/Validation.java b/it/it-tests/src/test/java/it/plugins/checks/Validation.java index e923ec8b6a8..1f0eccd22c1 100644 --- a/it/it-tests/src/test/java/it/plugins/checks/Validation.java +++ b/it/it-tests/src/test/java/it/plugins/checks/Validation.java @@ -20,19 +20,22 @@ package it.plugins.checks; import com.google.common.base.Joiner; +import com.google.gson.Gson; import com.sonar.orchestrator.Orchestrator; import it.plugins.Project; import java.io.File; +import java.util.List; +import java.util.Map; import org.hamcrest.Matchers; import org.junit.rules.ErrorCollector; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; -import org.sonar.wsclient.services.Source; -import org.sonar.wsclient.services.SourceQuery; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.WsResponse; import static java.util.Arrays.asList; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; +import static util.ItUtils.newAdminWsClient; /** * @@ -76,14 +79,15 @@ public class Validation { } private void fileMustHaveMeasures(String filePath, String[] metricKeys, int min) { - Resource resource = getMeasureForComponentKey(filePathToKey(filePath), metricKeys); - errorCollector.checkThat("Measures " + Joiner.on(",").join(metricKeys) + " are set on file " + filePath, resource, notNullValue()); - if (resource != null) { + String componentKey = filePathToKey(filePath); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, componentKey, metricKeys); + errorCollector.checkThat("Measures " + Joiner.on(",").join(metricKeys) + " are set on file " + filePath, componentKey, notNullValue()); + if (!measures.isEmpty()) { for (String metricKey : metricKeys) { - Measure measure = resource.getMeasure(metricKey); + Double measure = measures.get(metricKey); errorCollector.checkThat("Measure " + metricKey + " is set on file " + filePath, measure, notNullValue()); - if (measure != null && measure.getIntValue() != null) { - errorCollector.checkThat("Measure " + metricKey + " is positive on file " + filePath, measure.getIntValue(), Matchers.greaterThanOrEqualTo(min)); + if (measure != null) { + errorCollector.checkThat("Measure " + metricKey + " is positive on file " + filePath, measure.intValue(), Matchers.greaterThanOrEqualTo(min)); } } } @@ -103,10 +107,11 @@ public class Validation { private void mustHaveSourceWithAtLeast(String path, int minLines) { for (String filePath : toFiles(path)) { - Source source = orchestrator.getServer().getWsClient().find(SourceQuery.create(filePathToKey(filePath))); - errorCollector.checkThat("Source is set on file " + filePath, source, notNullValue()); + WsResponse response = newAdminWsClient(orchestrator).wsConnector().call(new GetRequest("api/sources/lines").setParam("key", filePathToKey(filePath))); + errorCollector.checkThat("Source is set on file " + filePath, response.isSuccessful(), is(true)); + Sources source = Sources.parse(response.content()); if (source != null) { - errorCollector.checkThat("Source is not empty on file " + filePath, source.getLines().size(), Matchers.greaterThanOrEqualTo(minLines)); + errorCollector.checkThat("Source is empty on file " + filePath, source.getSources().size(), Matchers.greaterThanOrEqualTo(minLines)); } } } @@ -122,11 +127,37 @@ public class Validation { return asList(path); } - public Resource getMeasureForComponentKey(String resourceKey, String... metricKeys) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(resourceKey, metricKeys)); - } - private String filePathToKey(String filePath) { return "all-langs:" + filePath; } + + public static class Sources { + + private List<Source> sources; + + private Sources(List<Source> sources) { + this.sources = sources; + } + + public List<Source> getSources() { + return sources; + } + + public static Sources parse(String json) { + Gson gson = new Gson(); + return gson.fromJson(json, Sources.class); + } + + public static class Source { + private final String line; + + private Source(String line) { + this.line = line; + } + + public String getLine() { + return line; + } + } + } } diff --git a/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java b/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java index 190cc51517c..78b2b7d0221 100644 --- a/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java +++ b/it/it-tests/src/test/java/it/projectAdministration/ProjectAdministrationTest.java @@ -37,13 +37,13 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.wsclient.SonarClient; import org.sonar.wsclient.base.HttpException; -import org.sonar.wsclient.services.ResourceQuery; import org.sonar.wsclient.user.UserParameters; import pageobjects.Navigation; import pageobjects.settings.SettingsPage; import static org.apache.commons.lang.time.DateUtils.addDays; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getComponent; import static util.ItUtils.projectDir; import static util.selenium.Selenese.runSelenese; @@ -74,13 +74,13 @@ public class ProjectAdministrationTest { public void delete_project_by_web_service() { scanSampleWithDate(ANALYSIS_DATE); - assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(PROJECT_KEY))).isNotNull(); - assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(FILE_KEY))).isNotNull(); + assertThat(getComponent(orchestrator, PROJECT_KEY)).isNotNull(); + assertThat(getComponent(orchestrator, FILE_KEY)).isNotNull(); orchestrator.getServer().adminWsClient().post(DELETE_WS_ENDPOINT, "keys", PROJECT_KEY); - assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(PROJECT_KEY))).isNull(); - assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(FILE_KEY))).isNull(); + assertThat(getComponent(orchestrator, PROJECT_KEY)).isNull(); + assertThat(getComponent(orchestrator, FILE_KEY)).isNull(); } @Test @@ -88,8 +88,8 @@ public class ProjectAdministrationTest { expectedException.expect(HttpException.class); scanSampleWithDate(ANALYSIS_DATE); - assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(PROJECT_KEY))).isNotNull(); - assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(FILE_KEY))).isNotNull(); + assertThat(getComponent(orchestrator, PROJECT_KEY)).isNotNull(); + assertThat(getComponent(orchestrator, FILE_KEY)).isNotNull(); // it's forbidden to delete only some files orchestrator.getServer().adminWsClient().post(DELETE_WS_ENDPOINT, "keys", FILE_KEY); @@ -100,7 +100,7 @@ public class ProjectAdministrationTest { expectedException.expect(HttpException.class); scanSampleWithDate(ANALYSIS_DATE); - assertThat(orchestrator.getServer().getWsClient().find(ResourceQuery.create(PROJECT_KEY))).isNotNull(); + assertThat(getComponent(orchestrator, PROJECT_KEY)).isNotNull(); // use wsClient() instead of adminWsClient() orchestrator.getServer().wsClient().post(DELETE_WS_ENDPOINT, "keys", PROJECT_KEY); diff --git a/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java b/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java index bd02216a318..17fa0e3829a 100644 --- a/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java +++ b/it/it-tests/src/test/java/it/qualityGate/QualityGateNotificationTest.java @@ -32,17 +32,15 @@ import org.junit.Test; import org.sonar.wsclient.qualitygate.NewCondition; import org.sonar.wsclient.qualitygate.QualityGate; import org.sonar.wsclient.qualitygate.QualityGateClient; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.setting.SettingsService; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; import util.user.UserRule; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonarqube.ws.WsMeasures.Measure; +import static util.ItUtils.getMeasure; import static util.ItUtils.newAdminWsClient; import static util.ItUtils.newUserWsClient; import static util.ItUtils.projectDir; @@ -64,12 +62,8 @@ public class QualityGateNotificationTest { private static Wiser smtpServer; - private static SettingsService adminSettingsService; - @BeforeClass public static void init() throws Exception { - adminSettingsService = newAdminWsClient(orchestrator).settingsService(); - DEFAULT_QUALITY_GATE = qgClient().list().defaultGate().id(); setServerProperty(orchestrator, "sonar.timemachine.period1", "previous_analysis"); @@ -124,10 +118,10 @@ public class QualityGateNotificationTest { SonarScanner analysis = SonarScanner.create(projectDir("qualitygate/xoo-sample")); orchestrator.executeBuild(analysis); - assertThat(fetchGateStatus().getData()).isEqualTo("OK"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK"); orchestrator.executeBuild(analysis); - assertThat(fetchGateStatus().getData()).isEqualTo("WARN"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("WARN"); qgClient().unsetDefault(); qgClient().destroy(simple.id()); @@ -149,12 +143,8 @@ public class QualityGateNotificationTest { assertThat(emails.hasNext()).isFalse(); } - private Measure fetchGateStatus() { - return fetchResourceWithGateStatus().getMeasure("alert_status"); - } - - private Resource fetchResourceWithGateStatus() { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, "alert_status").setIncludeAlerts(true)); + private Measure getGateStatusMeasure() { + return getMeasure(orchestrator, PROJECT_KEY, "alert_status"); } private static QualityGateClient qgClient() { diff --git a/it/it-tests/src/test/java/it/qualityGate/QualityGateOnRatingMeasuresTest.java b/it/it-tests/src/test/java/it/qualityGate/QualityGateOnRatingMeasuresTest.java index 18d5f13230f..6863f459859 100644 --- a/it/it-tests/src/test/java/it/qualityGate/QualityGateOnRatingMeasuresTest.java +++ b/it/it-tests/src/test/java/it/qualityGate/QualityGateOnRatingMeasuresTest.java @@ -28,15 +28,14 @@ import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.sonar.wsclient.qualitygate.QualityGateClient; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.qualitygate.CreateConditionRequest; import org.sonarqube.ws.client.qualitygate.QualityGatesService; import org.sonarqube.ws.client.qualitygate.SelectWsRequest; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasure; import static util.ItUtils.newAdminWsClient; import static util.ItUtils.resetSettings; import static util.ItUtils.runProjectAnalysis; @@ -88,7 +87,7 @@ public class QualityGateOnRatingMeasuresTest { runProjectAnalysis(orchestrator, "qualitygate/xoo-sample"); - assertThat(fetchGateStatus().getData()).isEqualTo("WARN"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("WARN"); } @Test @@ -105,21 +104,17 @@ public class QualityGateOnRatingMeasuresTest { // Run first analysis with empty quality gate -> quality gate is green orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "empty"); runProjectAnalysis(orchestrator, "qualitygate/xoo-sample"); - assertThat(fetchGateStatus().getData()).isEqualTo("OK"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK"); // Run second analysis with some rules that makes Security Rating to E -> quality gate is red orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/qualityGate/QualityGateOnRatingMeasuresTest/with-many-rules.xml")); orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "with-many-rules"); runProjectAnalysis(orchestrator, "qualitygate/xoo-sample"); - assertThat(fetchGateStatus().getData()).isEqualTo("ERROR"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("ERROR"); } - private Measure fetchGateStatus() { - return fetchResourceWithGateStatus().getMeasure("alert_status"); - } - - private Resource fetchResourceWithGateStatus() { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, "alert_status").setIncludeAlerts(true)); + private WsMeasures.Measure getGateStatusMeasure() { + return getMeasure(orchestrator, PROJECT_KEY, "alert_status"); } private static QualityGateClient qgClient() { diff --git a/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java b/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java index 46b3160a667..c660fa89109 100644 --- a/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java +++ b/it/it-tests/src/test/java/it/qualityGate/QualityGateTest.java @@ -19,6 +19,7 @@ */ package it.qualityGate; +import com.google.gson.Gson; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.BuildResult; import com.sonar.orchestrator.build.SonarScanner; @@ -27,6 +28,7 @@ import java.io.File; import java.io.IOException; import java.io.StringReader; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.stream.Collectors; @@ -42,19 +44,20 @@ import org.sonar.wsclient.project.NewProject; import org.sonar.wsclient.qualitygate.NewCondition; import org.sonar.wsclient.qualitygate.QualityGate; import org.sonar.wsclient.qualitygate.QualityGateClient; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import org.sonarqube.ws.MediaTypes; import org.sonarqube.ws.WsCe; +import org.sonarqube.ws.WsMeasures.Measure; import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsResponse; import org.sonarqube.ws.client.qualitygate.ProjectStatusWsRequest; +import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; import static util.ItUtils.extractCeTaskId; +import static util.ItUtils.getMeasure; import static util.ItUtils.newAdminWsClient; import static util.ItUtils.projectDir; @@ -100,7 +103,7 @@ public class QualityGateTest { verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_NO_QG); - assertThat(fetchResourceWithGateStatus()).isNull(); + assertThat(getGateStatusMeasure()).isNull(); } @Test @@ -114,7 +117,7 @@ public class QualityGateTest { verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_OK); - assertThat(fetchGateStatus().getData()).isEqualTo("OK"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK"); } finally { qgClient().unsetDefault(); qgClient().destroy(empty.id()); @@ -133,7 +136,7 @@ public class QualityGateTest { verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_OK); - assertThat(fetchGateStatus().getData()).isEqualTo("OK"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("OK"); } finally { qgClient().unsetDefault(); qgClient().destroy(simple.id()); @@ -152,7 +155,7 @@ public class QualityGateTest { verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_WARN); - assertThat(fetchGateStatus().getData()).isEqualTo("WARN"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("WARN"); } finally { qgClient().unsetDefault(); qgClient().destroy(simple.id()); @@ -172,7 +175,7 @@ public class QualityGateTest { verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR); - assertThat(fetchGateStatus().getData()).isEqualTo("ERROR"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("ERROR"); } finally { qgClient().unsetDefault(); qgClient().destroy(simple.id()); @@ -195,7 +198,7 @@ public class QualityGateTest { verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_ERROR); - assertThat(fetchGateStatus().getData()).isEqualTo("ERROR"); + assertThat(getGateStatusMeasure().getValue()).isEqualTo("ERROR"); } finally { qgClient().unsetDefault(); qgClient().destroy(alert.id()); @@ -218,11 +221,13 @@ public class QualityGateTest { verifyQGStatusInPostTask(buildResult, TASK_STATUS_SUCCESS, QG_STATUS_WARN); - Measure alertStatus = fetchGateStatus(); - assertThat(alertStatus.getData()).isEqualTo("WARN"); - assertThat(alertStatus.getAlertText()) - .contains("Lines of Code > 10") - .contains("Duplicated Lines (%) > 20"); + Measure alertStatus = getGateStatusMeasure(); + assertThat(alertStatus.getValue()).isEqualTo("WARN"); + + String qualityGateDetailJson = ItUtils.getMeasure(orchestrator, PROJECT_KEY, "quality_gate_details").getValue(); + assertThat(QualityGateDetails.parse(qualityGateDetailJson).getConditions()) + .extracting(QualityGateDetails.Conditions::getMetric, QualityGateDetails.Conditions::getOp, QualityGateDetails.Conditions::getWarning) + .contains(tuple("ncloc", "GT", "10"), tuple("duplicated_lines_density", "GT", "20")); } finally { qgClient().unsetDefault(); qgClient().destroy(allTypes.id()); @@ -290,12 +295,8 @@ public class QualityGateTest { return props.getProperty("ceTaskId"); } - private Measure fetchGateStatus() { - return fetchResourceWithGateStatus().getMeasure("alert_status"); - } - - private Resource fetchResourceWithGateStatus() { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, "alert_status").setIncludeAlerts(true)); + private Measure getGateStatusMeasure() { + return getMeasure(orchestrator, PROJECT_KEY, "alert_status"); } private static QualityGateClient qgClient() { @@ -308,4 +309,60 @@ public class QualityGateTest { .filter(s -> s.contains(taskUuid)) .collect(Collectors.toList()); } + + static class QualityGateDetails { + + private String level; + + private List<Conditions> conditions = new ArrayList<>(); + + String getLevel() { + return level; + } + + List<Conditions> getConditions() { + return conditions; + } + + public static QualityGateDetails parse(String json) { + Gson gson = new Gson(); + return gson.fromJson(json, QualityGateDetails.class); + } + + public static class Conditions { + private final String metric; + private final String op; + private final String warning; + private final String actual; + private final String level; + + private Conditions(String metric, String op, String values, String actual, String level) { + this.metric = metric; + this.op = op; + this.warning = values; + this.actual = actual; + this.level = level; + } + + String getMetric() { + return metric; + } + + String getOp() { + return op; + } + + String getWarning() { + return warning; + } + + String getActual() { + return actual; + } + + String getLevel() { + return level; + } + } + } } diff --git a/it/it-tests/src/test/java/it/qualityModel/MaintainabilityMeasureTest.java b/it/it-tests/src/test/java/it/qualityModel/MaintainabilityMeasureTest.java index 18e8034a172..dde8b15bca5 100644 --- a/it/it-tests/src/test/java/it/qualityModel/MaintainabilityMeasureTest.java +++ b/it/it-tests/src/test/java/it/qualityModel/MaintainabilityMeasureTest.java @@ -27,11 +27,9 @@ import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasureAsDouble; import static util.ItUtils.projectDir; /** @@ -72,30 +70,30 @@ public class MaintainabilityMeasureTest { orchestrator.getServer().associateProjectToQualityProfile(PROJECT, "xoo", "with-many-rules"); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, CODE_SMELLS_METRIC).getValue()).isEqualTo(71); - assertThat(getMeasure(PROJECT, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC).getValue()).isEqualTo(445); - assertThat(getMeasure(PROJECT, MAINTAINABILITY_RATING_METRIC).getData()).isEqualTo("C"); - assertThat(getMeasure(PROJECT, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC).getValue()).isEqualTo(292); - - assertThat(getMeasure(MODULE, CODE_SMELLS_METRIC).getValue()).isEqualTo(43); - assertThat(getMeasure(MODULE, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC).getValue()).isEqualTo(231); - assertThat(getMeasure(MODULE, MAINTAINABILITY_RATING_METRIC).getData()).isEqualTo("C"); - assertThat(getMeasure(MODULE, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC).getValue()).isEqualTo(150); - - assertThat(getMeasure(SUB_MODULE, CODE_SMELLS_METRIC).getValue()).isEqualTo(19); - assertThat(getMeasure(SUB_MODULE, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC).getValue()).isEqualTo(113); - assertThat(getMeasure(SUB_MODULE, MAINTAINABILITY_RATING_METRIC).getData()).isEqualTo("C"); - assertThat(getMeasure(SUB_MODULE, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC).getValue()).isEqualTo(77); - - assertThat(getMeasure(DIRECTORY, CODE_SMELLS_METRIC).getValue()).isEqualTo(18); - assertThat(getMeasure(DIRECTORY, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC).getValue()).isEqualTo(28); - assertThat(getMeasure(DIRECTORY, MAINTAINABILITY_RATING_METRIC).getData()).isEqualTo("A"); - assertThat(getMeasure(DIRECTORY, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC).getValue()).isEqualTo(0); - - assertThat(getMeasure(FILE, CODE_SMELLS_METRIC).getValue()).isEqualTo(18); - assertThat(getMeasure(FILE, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC).getValue()).isEqualTo(28); - assertThat(getMeasure(FILE, MAINTAINABILITY_RATING_METRIC).getData()).isEqualTo("A"); - assertThat(getMeasure(FILE, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC)).isNull(); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, CODE_SMELLS_METRIC)).isEqualTo(71); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC)).isEqualTo(445); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, MAINTAINABILITY_RATING_METRIC)).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC)).isEqualTo(292); + + assertThat(getMeasureAsDouble(orchestrator, MODULE, CODE_SMELLS_METRIC)).isEqualTo(43); + assertThat(getMeasureAsDouble(orchestrator, MODULE, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC)).isEqualTo(231); + assertThat(getMeasureAsDouble(orchestrator, MODULE, MAINTAINABILITY_RATING_METRIC)).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, MODULE, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC)).isEqualTo(150); + + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, CODE_SMELLS_METRIC)).isEqualTo(19); + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC)).isEqualTo(113); + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, MAINTAINABILITY_RATING_METRIC)).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC)).isEqualTo(77); + + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, CODE_SMELLS_METRIC)).isEqualTo(18); + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC)).isEqualTo(28); + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, MAINTAINABILITY_RATING_METRIC)).isEqualTo(1); + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC)).isZero(); + + assertThat(getMeasureAsDouble(orchestrator, FILE, CODE_SMELLS_METRIC)).isEqualTo(18); + assertThat(getMeasureAsDouble(orchestrator, FILE, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC)).isEqualTo(28); + assertThat(getMeasureAsDouble(orchestrator, FILE, MAINTAINABILITY_RATING_METRIC)).isEqualTo(1); + assertThat(getMeasureAsDouble(orchestrator, FILE, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC)).isZero(); } @Test @@ -104,17 +102,10 @@ public class MaintainabilityMeasureTest { orchestrator.getServer().associateProjectToQualityProfile(PROJECT, "xoo", "without-type-code-smells"); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, CODE_SMELLS_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(PROJECT, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(PROJECT, MAINTAINABILITY_RATING_METRIC).getData()).isEqualTo("A"); - assertThat(getMeasure(PROJECT, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC).getIntValue()).isEqualTo(0); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, CODE_SMELLS_METRIC)).isZero(); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, MAINTAINABILITY_REMEDIATION_EFFORT_METRIC)).isZero(); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, MAINTAINABILITY_RATING_METRIC)).isEqualTo(1); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_METRIC)).isZero(); } - private Measure getMeasure(String resource, String metricKey) { - Resource res = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(resource, metricKey)); - if (res == null) { - return null; - } - return res.getMeasure(metricKey); - } } diff --git a/it/it-tests/src/test/java/it/qualityModel/MaintainabilityRatingMeasureTest.java b/it/it-tests/src/test/java/it/qualityModel/MaintainabilityRatingMeasureTest.java index aed422800cc..cd189580a5b 100644 --- a/it/it-tests/src/test/java/it/qualityModel/MaintainabilityRatingMeasureTest.java +++ b/it/it-tests/src/test/java/it/qualityModel/MaintainabilityRatingMeasureTest.java @@ -23,16 +23,13 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; import it.Category2Suite; -import javax.annotation.CheckForNull; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasureAsDouble; import static util.ItUtils.projectDir; /** @@ -68,20 +65,11 @@ public class MaintainabilityRatingMeasureTest { orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, "sqale_rating").getIntValue()).isEqualTo(3); - assertThat(getMeasure(PROJECT, "sqale_rating").getData()).isEqualTo("C"); - - assertThat(getMeasure(MODULE, "sqale_rating").getIntValue()).isEqualTo(3); - assertThat(getMeasure(MODULE, "sqale_rating").getData()).isEqualTo("C"); - - assertThat(getMeasure(SUB_MODULE, "sqale_rating").getIntValue()).isEqualTo(3); - assertThat(getMeasure(SUB_MODULE, "sqale_rating").getData()).isEqualTo("C"); - - assertThat(getMeasure(DIRECTORY, "sqale_rating").getIntValue()).isEqualTo(1); - assertThat(getMeasure(DIRECTORY, "sqale_rating").getData()).isEqualTo("A"); - - assertThat(getMeasure(FILE, "sqale_rating").getIntValue()).isEqualTo(1); - assertThat(getMeasure(FILE, "sqale_rating").getData()).isEqualTo("A"); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, "sqale_rating")).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, MODULE, "sqale_rating")).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, "sqale_rating")).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, "sqale_rating")).isEqualTo(1); + assertThat(getMeasureAsDouble(orchestrator, FILE, "sqale_rating")).isEqualTo(1); } @Test @@ -92,11 +80,11 @@ public class MaintainabilityRatingMeasureTest { orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, "sqale_debt_ratio").getValue()).isEqualTo(29.1d); - assertThat(getMeasure(MODULE, "sqale_debt_ratio").getValue()).isEqualTo(28.5d); - assertThat(getMeasure(SUB_MODULE, "sqale_debt_ratio").getValue()).isEqualTo(31.4d); - assertThat(getMeasure(DIRECTORY, "sqale_debt_ratio").getValue()).isEqualTo(7.8d); - assertThat(getMeasure(FILE, "sqale_debt_ratio").getValue()).isEqualTo(7.8d); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, "sqale_debt_ratio")).isEqualTo(29.1d); + assertThat(getMeasureAsDouble(orchestrator, MODULE, "sqale_debt_ratio")).isEqualTo(28.5d); + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, "sqale_debt_ratio")).isEqualTo(31.4d); + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, "sqale_debt_ratio")).isEqualTo(7.8d); + assertThat(getMeasureAsDouble(orchestrator, FILE, "sqale_debt_ratio")).isEqualTo(7.8d); } @Test @@ -107,16 +95,12 @@ public class MaintainabilityRatingMeasureTest { orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); - Measure rating = getMeasure("sample", "sqale_rating"); - assertThat(rating.getIntValue()).isEqualTo(1); - assertThat(rating.getData()).isEqualTo("A"); + assertThat(getMeasureAsDouble(orchestrator, "sample", "sqale_rating")).isEqualTo(1); debtConfiguration.updateDevelopmentCost(2); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); - rating = getMeasure("sample", "sqale_rating"); - assertThat(rating.getIntValue()).isEqualTo(4); - assertThat(rating.getData()).isEqualTo("D"); + assertThat(getMeasureAsDouble(orchestrator, "sample", "sqale_rating")).isEqualTo(4); } @Test @@ -127,18 +111,14 @@ public class MaintainabilityRatingMeasureTest { orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - Measure rating = getMeasure(PROJECT, "sqale_rating"); - assertThat(rating.getIntValue()).isEqualTo(1); - assertThat(rating.getData()).isEqualTo("A"); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, "sqale_rating")).isEqualTo(1); debtConfiguration.updateLanguageDevelopmentCost("xoo", 1); orchestrator.executeBuild( SonarScanner.create(projectDir("shared/xoo-multi-modules-sample")) .setProfile("one-issue-per-line")); - rating = getMeasure(PROJECT, "sqale_rating"); - assertThat(rating.getIntValue()).isEqualTo(5); - assertThat(rating.getData()).isEqualTo("E"); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, "sqale_rating")).isEqualTo(5); } @Test @@ -149,16 +129,12 @@ public class MaintainabilityRatingMeasureTest { orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); - Measure rating = getMeasure("sample", "sqale_rating"); - assertThat(rating.getIntValue()).isEqualTo(1); - assertThat(rating.getData()).isEqualTo("A"); + assertThat(getMeasureAsDouble(orchestrator, "sample", "sqale_rating")).isEqualTo(1); debtConfiguration.updateRatingGrid(0.001d, 0.005d, 0.01d, 0.015d); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); - rating = getMeasure("sample", "sqale_rating"); - assertThat(rating.getIntValue()).isEqualTo(5); - assertThat(rating.getData()).isEqualTo("E"); + assertThat(getMeasureAsDouble(orchestrator, "sample", "sqale_rating")).isEqualTo(5); } @Test @@ -169,30 +145,20 @@ public class MaintainabilityRatingMeasureTest { orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, "sqale_rating").getData()).isEqualTo("C"); - assertThat(getMeasure(PROJECT, "effort_to_reach_maintainability_rating_a").getIntValue()).isEqualTo(292); - - assertThat(getMeasure(MODULE, "sqale_rating").getData()).isEqualTo("C"); - assertThat(getMeasure(MODULE, "effort_to_reach_maintainability_rating_a").getIntValue()).isEqualTo(150); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, "sqale_rating")).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, PROJECT, "effort_to_reach_maintainability_rating_a")).isEqualTo(292); - assertThat(getMeasure(SUB_MODULE, "sqale_rating").getData()).isEqualTo("C"); - assertThat(getMeasure(SUB_MODULE, "effort_to_reach_maintainability_rating_a").getIntValue()).isEqualTo(77); + assertThat(getMeasureAsDouble(orchestrator, MODULE, "sqale_rating")).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, MODULE, "effort_to_reach_maintainability_rating_a")).isEqualTo(150); - assertThat(getMeasure(DIRECTORY, "sqale_rating").getData()).isEqualTo("A"); - assertThat(getMeasure(DIRECTORY, "effort_to_reach_maintainability_rating_a").getIntValue()).isEqualTo(0); + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, "sqale_rating")).isEqualTo(3); + assertThat(getMeasureAsDouble(orchestrator, SUB_MODULE, "effort_to_reach_maintainability_rating_a")).isEqualTo(77); - assertThat(getMeasure(FILE, "sqale_rating").getData()).isEqualTo("A"); - // Best value is 0 => no measure - assertThat(getMeasure(FILE, "effort_to_reach_maintainability_rating_a")).isNull(); - } + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, "sqale_rating")).isEqualTo(1); + assertThat(getMeasureAsDouble(orchestrator, DIRECTORY, "effort_to_reach_maintainability_rating_a")).isZero(); - @CheckForNull - private Measure getMeasure(String resource, String metricKey) { - Resource res = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(resource, metricKey)); - if (res == null) { - return null; - } - return res.getMeasure(metricKey); + assertThat(getMeasureAsDouble(orchestrator, FILE, "sqale_rating")).isEqualTo(1); + assertThat(getMeasureAsDouble(orchestrator, FILE, "effort_to_reach_maintainability_rating_a")).isZero(); } } diff --git a/it/it-tests/src/test/java/it/qualityModel/NewDebtRatioMeasureTest.java b/it/it-tests/src/test/java/it/qualityModel/NewDebtRatioMeasureTest.java index 05e96062aed..afac1617362 100644 --- a/it/it-tests/src/test/java/it/qualityModel/NewDebtRatioMeasureTest.java +++ b/it/it-tests/src/test/java/it/qualityModel/NewDebtRatioMeasureTest.java @@ -23,21 +23,19 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.locator.FileLocation; import it.Category2Suite; import java.util.Date; -import java.util.List; +import java.util.Map; import javax.annotation.Nullable; import org.junit.AfterClass; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; import static org.apache.commons.lang.time.DateUtils.addDays; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.within; import static util.ItUtils.formatDate; +import static util.ItUtils.getPeriodMeasureValuesByIndex; import static util.ItUtils.resetPeriods; import static util.ItUtils.setServerProperty; import static util.ItUtils.toDate; @@ -92,14 +90,13 @@ public class NewDebtRatioMeasureTest { } private void assertNoNewDebtRatio() { - assertThat(getFileResourceWithVariations(NEW_DEBT_RATIO_METRIC_KEY)).isNull(); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", NEW_DEBT_RATIO_METRIC_KEY)).isEmpty(); } private void assertNewDebtRatio(@Nullable Double valuePeriod1, @Nullable Double valuePeriod2) { - Resource newTechnicalDebt = getFileResourceWithVariations(NEW_DEBT_RATIO_METRIC_KEY); - List<Measure> measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation1()).isEqualTo(valuePeriod1, within(0.01)); - assertThat(measures.get(0).getVariation2()).isEqualTo(valuePeriod2, within(0.01)); + Map<Integer, Double> newTechnicalDebt = getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", NEW_DEBT_RATIO_METRIC_KEY); + assertThat(newTechnicalDebt.get(1)).isEqualTo(valuePeriod1, within(0.01)); + assertThat(newTechnicalDebt.get(2)).isEqualTo(valuePeriod2, within(0.01)); } private void setSampleProjectQualityProfile(String qualityProfileKey) { @@ -123,8 +120,4 @@ public class NewDebtRatioMeasureTest { "sonar.scm.disabled", "false")); } - private Resource getFileResourceWithVariations(String metricKey) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", metricKey).setIncludeTrends(true)); - } - } diff --git a/it/it-tests/src/test/java/it/qualityModel/ReliabilityMeasureTest.java b/it/it-tests/src/test/java/it/qualityModel/ReliabilityMeasureTest.java index a54cc223583..0114f41abb2 100644 --- a/it/it-tests/src/test/java/it/qualityModel/ReliabilityMeasureTest.java +++ b/it/it-tests/src/test/java/it/qualityModel/ReliabilityMeasureTest.java @@ -23,15 +23,15 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; import it.Category2Suite; -import javax.annotation.CheckForNull; +import java.util.Map; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; +import static java.lang.Double.parseDouble; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresByMetricKey; import static util.ItUtils.projectDir; public class ReliabilityMeasureTest { @@ -46,6 +46,8 @@ public class ReliabilityMeasureTest { private static final String RELIABILITY_REMEDIATION_EFFORT_METRIC = "reliability_remediation_effort"; private static final String RELIABILITY_RATING_METRIC = "reliability_rating"; + private static final String[] METRICS = new String[] {BUGS_METRIC, RELIABILITY_RATING_METRIC, RELIABILITY_REMEDIATION_EFFORT_METRIC}; + @ClassRule public static Orchestrator orchestrator = Category2Suite.ORCHESTRATOR; @@ -62,25 +64,11 @@ public class ReliabilityMeasureTest { orchestrator.getServer().associateProjectToQualityProfile(PROJECT, "xoo", "with-many-rules"); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, BUGS_METRIC).getIntValue()).isEqualTo(61); - assertThat(getMeasure(PROJECT, RELIABILITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(305); - assertThat(getMeasure(PROJECT, RELIABILITY_RATING_METRIC).getData()).isEqualTo("D"); - - assertThat(getMeasure(MODULE, BUGS_METRIC).getIntValue()).isEqualTo(37); - assertThat(getMeasure(MODULE, RELIABILITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(185); - assertThat(getMeasure(MODULE, RELIABILITY_RATING_METRIC).getData()).isEqualTo("D"); - - assertThat(getMeasure(SUB_MODULE, BUGS_METRIC).getIntValue()).isEqualTo(16); - assertThat(getMeasure(SUB_MODULE, RELIABILITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(80); - assertThat(getMeasure(SUB_MODULE, RELIABILITY_RATING_METRIC).getData()).isEqualTo("D"); - - assertThat(getMeasure(DIRECTORY, BUGS_METRIC).getIntValue()).isEqualTo(16); - assertThat(getMeasure(DIRECTORY, RELIABILITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(80); - assertThat(getMeasure(DIRECTORY, RELIABILITY_RATING_METRIC).getData()).isEqualTo("D"); - - assertThat(getMeasure(FILE, BUGS_METRIC).getIntValue()).isEqualTo(16); - assertThat(getMeasure(FILE, RELIABILITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(80); - assertThat(getMeasure(FILE, RELIABILITY_RATING_METRIC).getData()).isEqualTo("D"); + assertMeasures(PROJECT, 61, 305, 4); + assertMeasures(MODULE, 37, 185, 4); + assertMeasures(SUB_MODULE, 16, 80, 4); + assertMeasures(DIRECTORY, 16, 80, 4); + assertMeasures(FILE, 16, 80, 4); } @Test @@ -89,17 +77,14 @@ public class ReliabilityMeasureTest { orchestrator.getServer().associateProjectToQualityProfile(PROJECT, "xoo", "without-type-bug"); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, BUGS_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(PROJECT, RELIABILITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(PROJECT, RELIABILITY_RATING_METRIC).getData()).isEqualTo("A"); + assertMeasures(PROJECT, 0, 0, 1); } - @CheckForNull - private Measure getMeasure(String resource, String metricKey) { - Resource res = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(resource, metricKey)); - if (res == null) { - return null; - } - return res.getMeasure(metricKey); + private void assertMeasures(String componentKey, int expectedBugs, int expectedReliabilityRemediationEffort, int expectedReliabilityRating) { + Map<String, WsMeasures.Measure> measures = getMeasuresByMetricKey(orchestrator, componentKey, METRICS); + assertThat(parseDouble(measures.get(BUGS_METRIC).getValue())).isEqualTo(expectedBugs); + assertThat(parseDouble(measures.get(RELIABILITY_REMEDIATION_EFFORT_METRIC).getValue())).isEqualTo(expectedReliabilityRemediationEffort); + assertThat(parseDouble(measures.get(RELIABILITY_RATING_METRIC).getValue())).isEqualTo(expectedReliabilityRating); } + } diff --git a/it/it-tests/src/test/java/it/qualityModel/SecurityMeasureTest.java b/it/it-tests/src/test/java/it/qualityModel/SecurityMeasureTest.java index 6caf217bd47..554e37baed6 100644 --- a/it/it-tests/src/test/java/it/qualityModel/SecurityMeasureTest.java +++ b/it/it-tests/src/test/java/it/qualityModel/SecurityMeasureTest.java @@ -23,15 +23,15 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import com.sonar.orchestrator.locator.FileLocation; import it.Category2Suite; -import javax.annotation.CheckForNull; +import java.util.Map; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; +import static java.lang.Double.parseDouble; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresByMetricKey; import static util.ItUtils.projectDir; public class SecurityMeasureTest { @@ -46,6 +46,8 @@ public class SecurityMeasureTest { private static final String SECURITY_REMEDIATION_EFFORT_METRIC = "security_remediation_effort"; private static final String SECURITY_RATING_METRIC = "security_rating"; + private static final String[] METRICS = new String[] {VULNERABILITIES_METRIC, SECURITY_REMEDIATION_EFFORT_METRIC, SECURITY_RATING_METRIC}; + @ClassRule public static Orchestrator orchestrator = Category2Suite.ORCHESTRATOR; @@ -62,25 +64,11 @@ public class SecurityMeasureTest { orchestrator.getServer().associateProjectToQualityProfile(PROJECT, "xoo", "with-many-rules"); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, VULNERABILITIES_METRIC).getIntValue()).isEqualTo(4); - assertThat(getMeasure(PROJECT, SECURITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(340); - assertThat(getMeasure(PROJECT, SECURITY_RATING_METRIC).getData()).isEqualTo("E"); - - assertThat(getMeasure(MODULE, VULNERABILITIES_METRIC).getIntValue()).isEqualTo(2); - assertThat(getMeasure(MODULE, SECURITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(170); - assertThat(getMeasure(MODULE, SECURITY_RATING_METRIC).getData()).isEqualTo("E"); - - assertThat(getMeasure(SUB_MODULE, VULNERABILITIES_METRIC).getIntValue()).isEqualTo(1); - assertThat(getMeasure(SUB_MODULE, SECURITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(85); - assertThat(getMeasure(SUB_MODULE, SECURITY_RATING_METRIC).getData()).isEqualTo("E"); - - assertThat(getMeasure(DIRECTORY, VULNERABILITIES_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(DIRECTORY, SECURITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(DIRECTORY, SECURITY_RATING_METRIC).getData()).isEqualTo("A"); - - assertThat(getMeasure(FILE, VULNERABILITIES_METRIC)).isNull(); - assertThat(getMeasure(FILE, SECURITY_REMEDIATION_EFFORT_METRIC)).isNull(); - assertThat(getMeasure(FILE, SECURITY_RATING_METRIC).getData()).isEqualTo("A"); + assertMeasures(PROJECT, 4, 340, 5); + assertMeasures(MODULE, 2, 170, 5); + assertMeasures(SUB_MODULE, 1, 85, 5); + assertMeasures(DIRECTORY, 0, 0, 1); + assertMeasures(FILE, 0, 0, 1); } @Test @@ -89,17 +77,13 @@ public class SecurityMeasureTest { orchestrator.getServer().associateProjectToQualityProfile(PROJECT, "xoo", "without-type-vulnerability"); orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-multi-modules-sample"))); - assertThat(getMeasure(PROJECT, VULNERABILITIES_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(PROJECT, SECURITY_REMEDIATION_EFFORT_METRIC).getIntValue()).isEqualTo(0); - assertThat(getMeasure(PROJECT, SECURITY_RATING_METRIC).getData()).isEqualTo("A"); + assertMeasures(PROJECT, 0, 0, 1); } - @CheckForNull - private Measure getMeasure(String resource, String metricKey) { - Resource res = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(resource, metricKey)); - if (res == null) { - return null; - } - return res.getMeasure(metricKey); + private void assertMeasures(String componentKey, int expectedVulnerabilities, int expectedReliabilityRemediationEffort, int expectedReliabilityRating) { + Map<String, WsMeasures.Measure> measures = getMeasuresByMetricKey(orchestrator, componentKey, METRICS); + assertThat(parseDouble(measures.get(VULNERABILITIES_METRIC).getValue())).isEqualTo(expectedVulnerabilities); + assertThat(parseDouble(measures.get(SECURITY_REMEDIATION_EFFORT_METRIC).getValue())).isEqualTo(expectedReliabilityRemediationEffort); + assertThat(parseDouble(measures.get(SECURITY_RATING_METRIC).getValue())).isEqualTo(expectedReliabilityRating); } } diff --git a/it/it-tests/src/test/java/it/qualityModel/TechnicalDebtMeasureVariationTest.java b/it/it-tests/src/test/java/it/qualityModel/TechnicalDebtMeasureVariationTest.java index 5980edcfa97..c0281aec65d 100644 --- a/it/it-tests/src/test/java/it/qualityModel/TechnicalDebtMeasureVariationTest.java +++ b/it/it-tests/src/test/java/it/qualityModel/TechnicalDebtMeasureVariationTest.java @@ -22,18 +22,16 @@ package it.qualityModel; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.locator.FileLocation; import it.Category2Suite; -import java.util.List; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Measure; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import util.ItUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; +import static util.ItUtils.getPeriodMeasureValuesByIndex; import static util.ItUtils.setServerProperty; /** @@ -76,19 +74,14 @@ public class TechnicalDebtMeasureVariationTest { runSampleProjectAnalysis(); // New technical debt only comes from new issues - Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt"); - List<Measure> measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation1()).isEqualTo(17); - assertThat(measures.get(0).getVariation2()).isEqualTo(17); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt")) + .contains(entry(1, 17d), entry(2, 17d)); // Third analysis, with exactly the same profile -> no new issues so no new technical debt runSampleProjectAnalysis(); - - newTechnicalDebt = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt").setIncludeTrends(true)); - - // No variation => measure is purged - assertThat(newTechnicalDebt).isNull(); + // No variation => measure is 0 (best value) + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt")) + .contains(entry(1, 0d), entry(2, 0d)); } @Test @@ -104,17 +97,13 @@ public class TechnicalDebtMeasureVariationTest { // Third analysis, existing issues on OneIssuePerFile will have their technical debt updated with the effort to fix runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10"); - - Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt"); - List<Measure> measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation1()).isEqualTo(90); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt") + .get(1)).isEqualTo(90); // Fourth analysis, with exactly the same profile -> no new issues so no new technical debt since previous analysis runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10"); - - newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt"); - measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation1()).isEqualTo(0); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt") + .get(1)).isZero(); } @Test @@ -134,18 +123,14 @@ public class TechnicalDebtMeasureVariationTest { // Third analysis, existing issues on OneIssuePerFile will have their technical debt updated with the effort to fix runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10"); - - Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt"); - List<Measure> measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation2()).isEqualTo(90); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt") + .get(2)).isEqualTo(90); // Fourth analysis, with exactly the same profile -> no new issues so no new technical debt since previous analysis but still since 30 // days runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10"); - - newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt"); - measures = newTechnicalDebt.getMeasures(); - assertThat(measures.get(0).getVariation2()).isEqualTo(90); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt") + .get(2)).isEqualTo(90); } /** @@ -163,9 +148,8 @@ public class TechnicalDebtMeasureVariationTest { // Execute a second analysis with a smaller effort to fix -> Added technical debt should be 0, not negative runSampleProjectAnalysis("sonar.oneIssuePerFile.effortToFix", "10"); - - Resource newTechnicalDebt = getSampleProjectResourceWithVariations("new_technical_debt"); - assertThat(newTechnicalDebt).isNull(); + assertThat(getPeriodMeasureValuesByIndex(orchestrator, "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt")) + .contains(entry(1, 0d), entry(2, 0d)); } private void setSampleProjectQualityProfile(String qualityProfileKey) { @@ -184,8 +168,4 @@ public class TechnicalDebtMeasureVariationTest { ItUtils.runVerboseProjectAnalysis(TechnicalDebtMeasureVariationTest.orchestrator, "shared/xoo-sample", properties); } - private Resource getSampleProjectResourceWithVariations(String metricKey) { - return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", metricKey).setIncludeTrends(true)); - } - } diff --git a/it/it-tests/src/test/java/it/test/CoverageTest.java b/it/it-tests/src/test/java/it/test/CoverageTest.java index e6eee0c983e..848b38cf319 100644 --- a/it/it-tests/src/test/java/it/test/CoverageTest.java +++ b/it/it-tests/src/test/java/it/test/CoverageTest.java @@ -23,6 +23,7 @@ import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import it.Category2Suite; import java.io.File; +import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.filefilter.TrueFileFilter; @@ -31,10 +32,9 @@ import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; import org.skyscreamer.jsonassert.JSONAssert; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; import static util.ItUtils.projectDir; public class CoverageTest { @@ -58,18 +58,18 @@ public class CoverageTest { public void coverage() throws Exception { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-ut-coverage"))); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-ut-coverage", ALL_COVERAGE_METRICS)); - assertThat(project.getMeasureValue("line_coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("lines_to_cover")).isEqualTo(4); - assertThat(project.getMeasureValue("uncovered_lines")).isEqualTo(2); - assertThat(project.getMeasureValue("branch_coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("conditions_to_cover")).isEqualTo(2); - assertThat(project.getMeasureValue("uncovered_conditions")).isEqualTo(1); - assertThat(project.getMeasureValue("coverage")).isEqualTo(50.0); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-ut-coverage", ALL_COVERAGE_METRICS); + assertThat(measures.get("line_coverage")).isEqualTo(50.0); + assertThat(measures.get("lines_to_cover")).isEqualTo(4d); + assertThat(measures.get("uncovered_lines")).isEqualTo(2d); + assertThat(measures.get("branch_coverage")).isEqualTo(50.0); + assertThat(measures.get("conditions_to_cover")).isEqualTo(2d); + assertThat(measures.get("uncovered_conditions")).isEqualTo(1d); + assertThat(measures.get("coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("it_coverage")).isNull(); + assertThat(measures.get("it_coverage")).isNull(); - assertThat(project.getMeasureValue("overall_coverage")).isNull(); + assertThat(measures.get("overall_coverage")).isNull(); String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-ut-coverage:src/main/xoo/sample/Sample.xoo")); // Use strict checking to be sure IT coverage is not present @@ -89,18 +89,18 @@ public class CoverageTest { public void coverage_no_condition() throws Exception { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-ut-coverage-no-condition"))); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-ut-coverage", ALL_COVERAGE_METRICS)); - assertThat(project.getMeasureValue("line_coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("lines_to_cover")).isEqualTo(4); - assertThat(project.getMeasureValue("uncovered_lines")).isEqualTo(2); - assertThat(project.getMeasureValue("branch_coverage")).isNull(); - assertThat(project.getMeasureValue("conditions_to_cover")).isNull(); - assertThat(project.getMeasureValue("uncovered_conditions")).isNull(); - assertThat(project.getMeasureValue("coverage")).isEqualTo(50.0); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-ut-coverage", ALL_COVERAGE_METRICS); + assertThat(measures.get("line_coverage")).isEqualTo(50.0); + assertThat(measures.get("lines_to_cover")).isEqualTo(4d); + assertThat(measures.get("uncovered_lines")).isEqualTo(2d); + assertThat(measures.get("branch_coverage")).isNull(); + assertThat(measures.get("conditions_to_cover")).isNull(); + assertThat(measures.get("uncovered_conditions")).isNull(); + assertThat(measures.get("coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("it_coverage")).isNull(); + assertThat(measures.get("it_coverage")).isNull(); - assertThat(project.getMeasureValue("overall_coverage")).isNull(); + assertThat(measures.get("overall_coverage")).isNull(); String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-ut-coverage:src/main/xoo/sample/Sample.xoo")); // Use strict checking to be sure IT coverage is not present @@ -114,21 +114,21 @@ public class CoverageTest { public void it_coverage_imported_as_coverage() throws Exception { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-it-coverage"))); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-it-coverage", ALL_COVERAGE_METRICS)); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-it-coverage", ALL_COVERAGE_METRICS); // Since SQ 6.2 all coverage reports are merged as coverage - assertThat(project.getMeasureValue("line_coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("lines_to_cover")).isEqualTo(4); - assertThat(project.getMeasureValue("uncovered_lines")).isEqualTo(2); - assertThat(project.getMeasureValue("branch_coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("conditions_to_cover")).isEqualTo(2); - assertThat(project.getMeasureValue("uncovered_conditions")).isEqualTo(1); - assertThat(project.getMeasureValue("coverage")).isEqualTo(50.0); + assertThat(measures.get("line_coverage")).isEqualTo(50.0); + assertThat(measures.get("lines_to_cover")).isEqualTo(4d); + assertThat(measures.get("uncovered_lines")).isEqualTo(2d); + assertThat(measures.get("branch_coverage")).isEqualTo(50.0); + assertThat(measures.get("conditions_to_cover")).isEqualTo(2d); + assertThat(measures.get("uncovered_conditions")).isEqualTo(1d); + assertThat(measures.get("coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("it_coverage")).isNull(); + assertThat(measures.get("it_coverage")).isNull(); - assertThat(project.getMeasureValue("overall_coverage")).isNull(); + assertThat(measures.get("overall_coverage")).isNull(); String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-it-coverage:src/main/xoo/sample/Sample.xoo")); // Use strict checking to be sure UT coverage is not present @@ -143,18 +143,18 @@ public class CoverageTest { // Since SQ 6.2 all coverage reports are merged as coverage - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-overall-coverage", ALL_COVERAGE_METRICS)); - assertThat(project.getMeasureValue("line_coverage")).isEqualTo(75.0); - assertThat(project.getMeasureValue("lines_to_cover")).isEqualTo(4); - assertThat(project.getMeasureValue("uncovered_lines")).isEqualTo(1); - assertThat(project.getMeasureValue("branch_coverage")).isEqualTo(50.0); - assertThat(project.getMeasureValue("conditions_to_cover")).isEqualTo(4); - assertThat(project.getMeasureValue("uncovered_conditions")).isEqualTo(2); - assertThat(project.getMeasureValue("coverage")).isEqualTo(62.5); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-overall-coverage", ALL_COVERAGE_METRICS); + assertThat(measures.get("line_coverage")).isEqualTo(75.0); + assertThat(measures.get("lines_to_cover")).isEqualTo(4); + assertThat(measures.get("uncovered_lines")).isEqualTo(1); + assertThat(measures.get("branch_coverage")).isEqualTo(50.0); + assertThat(measures.get("conditions_to_cover")).isEqualTo(4); + assertThat(measures.get("uncovered_conditions")).isEqualTo(2); + assertThat(measures.get("coverage")).isEqualTo(62.5); - assertThat(project.getMeasureValue("it_coverage")).isNull(); + assertThat(measures.get("it_coverage")).isNull(); - assertThat(project.getMeasureValue("overall_coverage")).isNull(); + assertThat(measures.get("overall_coverage")).isNull(); String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-overall-coverage:src/main/xoo/sample/Sample.xoo")); // Use strict checking to be sure no extra coverage is present @@ -170,8 +170,7 @@ public class CoverageTest { public void should_compute_coverage_on_project() { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered"))); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS)); - assertThat(project.getMeasureValue("coverage")).isEqualTo(50.0); + assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS).get("coverage")).isEqualTo(50.0); verifyComputeEngineTempDirIsEmpty(); } @@ -184,8 +183,7 @@ public class CoverageTest { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered")) .setProperty("sonar.coverage.exclusions", "src/main/xoo/org/sonar/tests/halfcovered/UnCovered.xoo")); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS)); - assertThat(project.getMeasureValue("coverage")).isEqualTo(100.0); + assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS).get("coverage")).isEqualTo(100.0); verifyComputeEngineTempDirIsEmpty(); } @@ -198,8 +196,7 @@ public class CoverageTest { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered")) .setProperty("sonar.coverage.exclusions", "**/UnCovered*")); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS)); - assertThat(project.getMeasureValue("coverage")).isEqualTo(100.0); + assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS).get("coverage")).isEqualTo(100.0); verifyComputeEngineTempDirIsEmpty(); } @@ -212,8 +209,7 @@ public class CoverageTest { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered")) .setProperty("sonar.coverage.exclusions", "**/*")); - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS)); - assertThat(project.getMeasureValue("coverage")).isNull(); + assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS).get("coverage")).isNull(); verifyComputeEngineTempDirIsEmpty(); } diff --git a/it/it-tests/src/test/java/it/test/NewCoverageTest.java b/it/it-tests/src/test/java/it/test/NewCoverageTest.java index 9e12e33fe54..dd9adee1099 100644 --- a/it/it-tests/src/test/java/it/test/NewCoverageTest.java +++ b/it/it-tests/src/test/java/it/test/NewCoverageTest.java @@ -22,14 +22,16 @@ package it.test; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import it.Category2Suite; +import java.util.Map; import org.assertj.core.data.Offset; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; +import static java.lang.Double.parseDouble; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresWithVariationsByMetricKey; import static util.ItUtils.projectDir; public class NewCoverageTest { @@ -58,10 +60,10 @@ public class NewCoverageTest { @Test public void new_coverage() throws Exception { - Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(PROJECT_KEY, ALL_NEW_COVERAGE_METRICS).setIncludeTrends(true)); - assertThat(project.getMeasure("new_coverage").getVariation1()).isEqualTo(66.6d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_line_coverage").getVariation1()).isEqualTo(100d, DEFAULT_OFFSET); - assertThat(project.getMeasure("new_branch_coverage").getVariation1()).isEqualTo(42.8, DEFAULT_OFFSET); + Map<String, WsMeasures.Measure> measures = getMeasuresWithVariationsByMetricKey(orchestrator, PROJECT_KEY, ALL_NEW_COVERAGE_METRICS); + assertThat(parseDouble(measures.get("new_coverage").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(66.6d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_line_coverage").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(100d, DEFAULT_OFFSET); + assertThat(parseDouble(measures.get("new_branch_coverage").getPeriods().getPeriodsValue(0).getValue())).isEqualTo(42.8, DEFAULT_OFFSET); } } diff --git a/it/it-tests/src/test/java/it/test/TestExecutionTest.java b/it/it-tests/src/test/java/it/test/TestExecutionTest.java index 34c75e01a47..e273a076d51 100644 --- a/it/it-tests/src/test/java/it/test/TestExecutionTest.java +++ b/it/it-tests/src/test/java/it/test/TestExecutionTest.java @@ -22,15 +22,15 @@ package it.test; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.SonarScanner; import it.Category2Suite; +import java.util.Map; import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; import org.skyscreamer.jsonassert.JSONAssert; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.getMeasuresAsDoubleByMetricKey; import static util.ItUtils.projectDir; public class TestExecutionTest { @@ -47,14 +47,14 @@ public class TestExecutionTest { public void test_execution_details() throws Exception { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-with-tests-execution-details"))); - Resource project = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics("sample-with-tests", "test_success_density", "test_failures", "test_errors", "tests", "skipped_tests", "test_execution_time")); - assertThat(project.getMeasureValue("test_success_density")).isEqualTo(33.3); - assertThat(project.getMeasureIntValue("test_failures")).isEqualTo(1); - assertThat(project.getMeasureIntValue("test_errors")).isEqualTo(1); - assertThat(project.getMeasureIntValue("tests")).isEqualTo(3); - assertThat(project.getMeasureIntValue("skipped_tests")).isEqualTo(1); - assertThat(project.getMeasureIntValue("test_execution_time")).isEqualTo(8); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-with-tests", + "test_success_density", "test_failures", "test_errors", "tests", "skipped_tests", "test_execution_time"); + assertThat(measures.get("test_success_density")).isEqualTo(33.3); + assertThat(measures.get("test_failures")).isEqualTo(1); + assertThat(measures.get("test_errors")).isEqualTo(1); + assertThat(measures.get("tests")).isEqualTo(3); + assertThat(measures.get("skipped_tests")).isEqualTo(1); + assertThat(measures.get("test_execution_time")).isEqualTo(8); String json = orchestrator.getServer().adminWsClient().get("api/tests/list", "testFileKey", "sample-with-tests:src/test/xoo/sample/SampleTest.xoo"); JSONAssert.assertEquals(IOUtils.toString(this.getClass().getResourceAsStream("/test/TestExecutionTest/expected.json"), "UTF-8"), json, false); @@ -64,13 +64,13 @@ public class TestExecutionTest { public void test_execution_measures() throws Exception { orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-with-tests-execution-measures"))); - Resource project = orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics("sample-with-tests", "test_success_density", "test_failures", "test_errors", "tests", "skipped_tests", "test_execution_time")); - assertThat(project.getMeasureValue("test_success_density")).isEqualTo(33.3); - assertThat(project.getMeasureIntValue("test_failures")).isEqualTo(1); - assertThat(project.getMeasureIntValue("test_errors")).isEqualTo(1); - assertThat(project.getMeasureIntValue("tests")).isEqualTo(3); - assertThat(project.getMeasureIntValue("skipped_tests")).isEqualTo(1); - assertThat(project.getMeasureIntValue("test_execution_time")).isEqualTo(8); + Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-with-tests", + "test_success_density", "test_failures", "test_errors", "tests", "skipped_tests", "test_execution_time"); + assertThat(measures.get("test_success_density")).isEqualTo(33.3); + assertThat(measures.get("test_failures")).isEqualTo(1); + assertThat(measures.get("test_errors")).isEqualTo(1); + assertThat(measures.get("tests")).isEqualTo(3); + assertThat(measures.get("skipped_tests")).isEqualTo(1); + assertThat(measures.get("test_execution_time")).isEqualTo(8); } } diff --git a/it/it-tests/src/test/java/util/ItUtils.java b/it/it-tests/src/test/java/util/ItUtils.java index 48ab757068f..3fc95321bac 100644 --- a/it/it-tests/src/test/java/util/ItUtils.java +++ b/it/it-tests/src/test/java/util/ItUtils.java @@ -39,11 +39,13 @@ import java.util.Date; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -56,15 +58,24 @@ import org.sonar.wsclient.base.HttpException; import org.sonar.wsclient.issue.Issue; import org.sonar.wsclient.issue.IssueClient; import org.sonar.wsclient.issue.IssueQuery; +import org.sonarqube.ws.WsComponents.Component; +import org.sonarqube.ws.WsMeasures; +import org.sonarqube.ws.WsMeasures.Measure; +import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpConnector; import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsClientFactories; +import org.sonarqube.ws.client.component.ShowWsRequest; +import org.sonarqube.ws.client.measure.ComponentWsRequest; import org.sonarqube.ws.client.setting.ResetRequest; import org.sonarqube.ws.client.setting.SetRequest; import static com.google.common.base.Preconditions.checkState; import static com.sonar.orchestrator.container.Server.ADMIN_LOGIN; import static com.sonar.orchestrator.container.Server.ADMIN_PASSWORD; +import static java.lang.Double.parseDouble; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -244,6 +255,104 @@ public class ItUtils { resetSettings(orchestrator, null, "sonar.timemachine.period1", "sonar.timemachine.period2", "sonar.timemachine.period3"); } + @CheckForNull + public static Measure getMeasure(Orchestrator orchestrator, String componentKey, String metricKey) { + return getMeasuresByMetricKey(orchestrator, componentKey, metricKey).get(metricKey); + } + + @CheckForNull + public static Double getMeasureAsDouble(Orchestrator orchestrator, String componentKey, String metricKey) { + Measure measure = getMeasure(orchestrator, componentKey, metricKey); + return (measure == null) ? null : Double.parseDouble(measure.getValue()); + } + + public static Map<String, Measure> getMeasuresByMetricKey(Orchestrator orchestrator, String componentKey, String... metricKeys) { + return getStreamMeasures(orchestrator, componentKey, metricKeys) + .filter(Measure::hasValue) + .collect(Collectors.toMap(Measure::getMetric, Function.identity())); + } + + public static Map<String, Double> getMeasuresAsDoubleByMetricKey(Orchestrator orchestrator, String componentKey, String... metricKeys) { + return getStreamMeasures(orchestrator, componentKey, metricKeys) + .filter(Measure::hasValue) + .collect(Collectors.toMap(Measure::getMetric, measure -> parseDouble(measure.getValue()))); + } + + private static Stream<Measure> getStreamMeasures(Orchestrator orchestrator, String componentKey, String... metricKeys) { + return newWsClient(orchestrator).measures().component(new ComponentWsRequest() + .setComponentKey(componentKey) + .setMetricKeys(asList(metricKeys))) + .getComponent().getMeasuresList() + .stream(); + } + + @CheckForNull + public static Measure getMeasureWithVariations(Orchestrator orchestrator, String componentKey, String metricKey) { + WsMeasures.ComponentWsResponse response = newWsClient(orchestrator).measures().component(new ComponentWsRequest() + .setComponentKey(componentKey) + .setMetricKeys(singletonList(metricKey)) + .setAdditionalFields(singletonList("periods"))); + List<Measure> measures = response.getComponent().getMeasuresList(); + return measures.size() == 1 ? measures.get(0) : null; + } + + @CheckForNull + public static Map<String, Measure> getMeasuresWithVariationsByMetricKey(Orchestrator orchestrator, String componentKey, String... metricKeys) { + return newWsClient(orchestrator).measures().component(new ComponentWsRequest() + .setComponentKey(componentKey) + .setMetricKeys(asList(metricKeys)) + .setAdditionalFields(singletonList("periods"))).getComponent().getMeasuresList() + .stream() + .collect(Collectors.toMap(Measure::getMetric, Function.identity())); + } + + /** + * Return period values as string by period index (from 1 to 5) + */ + public static Map<Integer, Double> getPeriodMeasureValuesByIndex(Orchestrator orchestrator, String componentKey, String metricKey) { + return getMeasureWithVariations(orchestrator, componentKey, metricKey).getPeriods().getPeriodsValueList().stream() + .collect(Collectors.toMap(WsMeasures.PeriodValue::getIndex, measure -> parseDouble(measure.getValue()))); + } + + @CheckForNull + public static Component getComponent(Orchestrator orchestrator, String componentKey) { + try { + return newWsClient(orchestrator).components().show(new ShowWsRequest().setKey((componentKey))).getComponent(); + } catch (org.sonarqube.ws.client.HttpException e) { + if (e.code() == 404) { + return null; + } + throw new IllegalStateException(e); + } + } + + @CheckForNull + public static ComponentNavigation getComponentNavigation(Orchestrator orchestrator, String componentKey) { + // Waiting for SONAR-7745 to have version in api/components/show, we use internal api/navigation/component WS to get the component + // version + String content = newWsClient(orchestrator).wsConnector().call(new GetRequest("api/navigation/component").setParam("componentKey", componentKey)).failIfNotSuccessful() + .content(); + return ComponentNavigation.parse(content); + } + + public static class ComponentNavigation { + private String version; + private String snapshotDate; + + public String getVersion() { + return version; + } + + public Date getDate() { + return toDatetime(snapshotDate); + } + + public static ComponentNavigation parse(String json) { + Gson gson = new Gson(); + return gson.fromJson(json, ComponentNavigation.class); + } + } + /** * Concatenates a vararg to a String array. * diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java index 96000dd4acd..46a7ef74ad5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java @@ -100,7 +100,7 @@ public class ShowActionTest { @Test public void show_provided_project() { - componentDb.insertComponent(newProjectDto(db.organizations().insert(), "project-uuid")); + componentDb.insertComponent(newProjectDto(db.organizations().insert(), "project-uuid").setEnabled(false)); ShowWsResponse response = newRequest("project-uuid", null); diff --git a/tests/perf/pom.xml b/tests/perf/pom.xml index 7ef689fdb72..d385f5d48fe 100644 --- a/tests/perf/pom.xml +++ b/tests/perf/pom.xml @@ -57,6 +57,12 @@ <version>1.12.1</version> </dependency> <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>sonar-ws</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> diff --git a/tests/perf/src/test/java/org/sonarsource/sonarqube/perf/scanner/suite/DuplicationTest.java b/tests/perf/src/test/java/org/sonarsource/sonarqube/perf/scanner/suite/DuplicationTest.java index b309b7f5b8f..9b700a0b3a8 100644 --- a/tests/perf/src/test/java/org/sonarsource/sonarqube/perf/scanner/suite/DuplicationTest.java +++ b/tests/perf/src/test/java/org/sonarsource/sonarqube/perf/scanner/suite/DuplicationTest.java @@ -22,8 +22,9 @@ package org.sonarsource.sonarqube.perf.scanner.suite; import com.sonar.orchestrator.Orchestrator; import com.sonar.orchestrator.build.MavenBuild; import com.sonar.orchestrator.locator.FileLocation; -import org.sonarsource.sonarqube.perf.PerfTestCase; import java.io.IOException; +import java.util.Map; +import java.util.stream.Collectors; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -31,9 +32,15 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ErrorCollector; import org.junit.rules.TemporaryFolder; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures; +import org.sonarqube.ws.client.HttpConnector; +import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.WsClientFactories; +import org.sonarqube.ws.client.measure.ComponentWsRequest; +import org.sonarsource.sonarqube.perf.PerfTestCase; +import static java.lang.Double.parseDouble; +import static java.util.Arrays.asList; import static org.fest.assertions.Assertions.assertThat; public class DuplicationTest extends PerfTestCase { @@ -68,12 +75,22 @@ public class DuplicationTest extends PerfTestCase { .setProperty("sonar.sourceEncoding", "UTF-8") .setCleanSonarGoals(); orchestrator.executeBuild(build); - Resource file = getResource("com.sonarsource.it.samples:huge-file:src/main/java/huge/HugeFile.java"); - assertThat(file.getMeasureValue("duplicated_lines")).isGreaterThan(50000.0); + Map<String, Double> measure = getMeasures("com.sonarsource.it.samples:huge-file:src/main/java/huge/HugeFile.java"); + assertThat(measure.get("duplicated_lines")).isGreaterThan(50000.0); + } + + private Map<String, Double> getMeasures(String key) { + return newWsClient().measures().component(new ComponentWsRequest() + .setComponentKey(key) + .setMetricKeys(asList("duplicated_lines", "duplicated_blocks", "duplicated_files", "duplicated_lines_density"))) + .getComponent().getMeasuresList() + .stream() + .collect(Collectors.toMap(WsMeasures.Measure::getMetric, measure -> parseDouble(measure.getValue()))); } - private Resource getResource(String key) { - return orchestrator.getServer().getWsClient() - .find(ResourceQuery.createForMetrics(key, "duplicated_lines", "duplicated_blocks", "duplicated_files", "duplicated_lines_density", "useless-duplicated-lines")); + private WsClient newWsClient() { + return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() + .url(orchestrator.getServer().getUrl()) + .build()); } } diff --git a/tests/upgrade/pom.xml b/tests/upgrade/pom.xml index 59086bf5f6d..eddab9a40ae 100644 --- a/tests/upgrade/pom.xml +++ b/tests/upgrade/pom.xml @@ -18,9 +18,9 @@ <dependencies> <dependency> - <groupId>org.codehaus.sonar</groupId> - <artifactId>sonar-ws-client</artifactId> - <version>5.1</version> + <groupId>org.sonarsource.sonarqube</groupId> + <artifactId>sonar-ws</artifactId> + <version>${project.version}</version> <scope>test</scope> </dependency> <dependency> diff --git a/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java b/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java index bad5a01d7f7..480846415f3 100644 --- a/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java +++ b/tests/upgrade/src/test/java/org/sonarsource/sonarqube/upgrade/UpgradeTest.java @@ -32,18 +32,22 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Arrays; +import java.util.Collections; import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.Test; import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsMeasures.Measure; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpConnector; import org.sonarqube.ws.client.WsClient; import org.sonarqube.ws.client.WsClientFactories; import org.sonarqube.ws.client.WsResponse; +import org.sonarqube.ws.client.measure.ComponentWsRequest; import static com.codeborne.selenide.Condition.hasText; import static com.codeborne.selenide.Selenide.$; +import static java.lang.Integer.parseInt; import static org.assertj.core.api.Assertions.assertThat; public class UpgradeTest { @@ -129,7 +133,7 @@ public class UpgradeTest { checkUrlIsReturningOk("/api/system/db_migration_status"); checkUrlIsReturningOk("/api/webservices/list"); // TODO Reactivate when latest Sonarqube version will be in repox -// checkUrlIsReturningOkOnlyForDevVersion("/api/l10n/index", sqVersion); + // checkUrlIsReturningOkOnlyForDevVersion("/api/l10n/index", sqVersion); // These urls should not be available when system requires a migration checkUrlIsReturningNotFound("/api/issues/search?projectKeys=org.apache.struts%3Astruts-core"); @@ -212,6 +216,11 @@ public class UpgradeTest { } private int countFiles(String key) { + if (orchestrator.getConfiguration().getSonarVersion().isGreaterThanOrEquals("5.4")) { + Measure measure = newWsClient(orchestrator).measures().component(new ComponentWsRequest().setComponentKey(key).setMetricKeys(Collections.singletonList("files"))) + .getComponent().getMeasures(0); + return parseInt(measure.getValue()); + } return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(key, "files")).getMeasureIntValue("files"); } |