From dc296aebbaae00a6e519879846bb281bba817732 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 16 Jan 2017 14:27:47 +0100 Subject: [PATCH] SONAR-8574 Replace usage of api/resources/index by api/measures/component in ITs --- it/pom.xml | 5 ++ .../java/com/sonar/runner/it/JavaTest.java | 60 +++++++------ .../com/sonar/runner/it/MultimoduleTest.java | 87 +++++-------------- .../com/sonar/runner/it/ScannerTestCase.java | 59 +++++++++++++ 4 files changed, 119 insertions(+), 92 deletions(-) diff --git a/it/pom.xml b/it/pom.xml index 7c8a536..e5e66aa 100644 --- a/it/pom.xml +++ b/it/pom.xml @@ -45,6 +45,11 @@ ${sonar.buildVersion} test + + org.sonarsource.sonarqube + sonar-ws + 5.6 + org.easytesting fest-assert diff --git a/it/src/test/java/com/sonar/runner/it/JavaTest.java b/it/src/test/java/com/sonar/runner/it/JavaTest.java index ce1d63f..3519159 100644 --- a/it/src/test/java/com/sonar/runner/it/JavaTest.java +++ b/it/src/test/java/com/sonar/runner/it/JavaTest.java @@ -25,6 +25,8 @@ import com.sonar.orchestrator.locator.ResourceLocation; import java.io.File; import java.io.IOException; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.lang.SystemUtils; import org.junit.After; import org.junit.Rule; @@ -32,9 +34,10 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; 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.WsComponents.Component; +import org.sonarqube.ws.WsMeasures.Measure; +import static java.lang.Integer.parseInt; import static org.fest.assertions.Assertions.assertThat; public class JavaTest extends ScannerTestCase { @@ -64,21 +67,23 @@ public class JavaTest extends ScannerTestCase { build.setProperty("sonar.host.url", orchestrator.getServer().getUrl() + "/"); orchestrator.executeBuild(build); - Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:sample").setMetrics("files", "ncloc", "classes", "lcom4", "violations")); - // SONARPLUGINS-2399 + Component project = getComponent("java:sample"); assertThat(project.getName()).isEqualTo("Java Sample, with comma"); assertThat(project.getDescription()).isEqualTo("This is a Java sample"); - assertThat(project.getVersion()).isEqualTo("1.2.3"); - assertThat(project.getMeasureIntValue("files")).isEqualTo(2); - assertThat(project.getMeasureIntValue("classes")).isEqualTo(2); - assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(10); - assertThat(project.getMeasureIntValue("violations")).isGreaterThan(0); - - Resource file = orchestrator.getServer().getWsClient() - .find(new ResourceQuery("java:sample:src/basic/Hello.java").setMetrics("files", "ncloc", "classes", "violations")); + + Map projectMeasures = getMeasures("java:sample", "files", "ncloc", "classes", "violations"); + // SONARPLUGINS-2399 + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2); + assertThat(parseInt(projectMeasures.get("classes").getValue())).isEqualTo(2); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(10); + assertThat(parseInt(projectMeasures.get("violations").getValue())).isGreaterThan(0); + + Component file = getComponent("java:sample:src/basic/Hello.java"); assertThat(file.getName()).isEqualTo("Hello.java"); - assertThat(file.getMeasureIntValue("ncloc")).isEqualTo(7); - assertThat(file.getMeasureIntValue("violations")).isGreaterThan(0); + + Map fileMeasures = getMeasures("java:sample:src/basic/Hello.java", "files", "ncloc", "classes", "violations"); + assertThat(parseInt(fileMeasures.get("ncloc").getValue())).isEqualTo(7); + assertThat(parseInt(fileMeasures.get("violations").getValue())).isGreaterThan(0); } @Test @@ -90,13 +95,14 @@ public class JavaTest extends ScannerTestCase { SonarScanner build = newScanner(new File("projects/java-bytecode")); orchestrator.executeBuild(build); - Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:bytecode").setMetrics("lcom4", "violations")); + Component project = getComponent("java:bytecode"); assertThat(project.getName()).isEqualTo("Java Bytecode Sample"); + + Map projectMeasures = getMeasures("java:bytecode", "violations"); // the squid rules enabled in sonar-way-profile do not exist in SQ 3.0 - assertThat(project.getMeasureIntValue("violations")).isGreaterThan(0); + assertThat(parseInt(projectMeasures.get("violations").getValue())).isGreaterThan(0); - Resource file = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:bytecode:src/HasFindbugsViolation.java").setMetrics("lcom4", "violations")); - assertThat(file.getMeasureIntValue("violations")).isGreaterThan(0); + assertThat(getMeasureAsInteger("java:bytecode:src/HasFindbugsViolation.java", "violations")).isGreaterThan(0); // findbugs is executed on bytecode List issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("squid:S1147")).list(); @@ -118,9 +124,9 @@ public class JavaTest extends ScannerTestCase { SonarScanner build = newScanner(new File("projects/basedir-with-source")); orchestrator.executeBuild(build); - Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:basedir-with-source").setMetrics("files", "ncloc")); - assertThat(project.getMeasureIntValue("files")).isEqualTo(1); - assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(1); + Map projectMeasures = getMeasures("java:basedir-with-source", "files", "ncloc"); + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); } /** @@ -136,9 +142,9 @@ public class JavaTest extends ScannerTestCase { .setProjectKey("SAMPLE"); orchestrator.executeBuild(build); - Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("SAMPLE").setMetrics("files", "ncloc")); - assertThat(project.getMeasureIntValue("files")).isEqualTo(2); - assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(1); + Map projectMeasures = getMeasures("SAMPLE", "files", "ncloc"); + assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2); + assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1); } /** @@ -254,9 +260,9 @@ public class JavaTest extends ScannerTestCase { .addArguments("-e"); orchestrator.executeBuild(build); - Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:sample").setMetrics("files", "ncloc", "classes", "lcom4", "violations")); - assertThat(project.getDescription()).isEqualTo("This is a Java sample"); - assertThat(project.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("java:sample").getDescription()).isEqualTo("This is a Java sample"); + Map projectMeasures = getMeasures("java:sample", "files", "ncloc", "classes", "violations"); + assertThat(projectMeasures.values().stream().filter(measure -> measure.getValue() != null).collect(Collectors.toList())).hasSize(4); } @Test diff --git a/it/src/test/java/com/sonar/runner/it/MultimoduleTest.java b/it/src/test/java/com/sonar/runner/it/MultimoduleTest.java index 2e02948..57e8d1d 100644 --- a/it/src/test/java/com/sonar/runner/it/MultimoduleTest.java +++ b/it/src/test/java/com/sonar/runner/it/MultimoduleTest.java @@ -24,8 +24,7 @@ import com.sonar.orchestrator.build.SonarScanner; import java.io.File; import org.junit.After; import org.junit.Test; -import org.sonar.wsclient.services.Resource; -import org.sonar.wsclient.services.ResourceQuery; +import org.sonarqube.ws.WsComponents.Component; import static org.fest.assertions.Assertions.assertThat; @@ -45,18 +44,12 @@ public class MultimoduleTest extends ScannerTestCase { orchestrator.executeBuild(build); - Resource rootProject = findResource("simplest-with-props-on-root"); - assertThat(rootProject.getName()).isEqualTo("Simplest multi-module project with all properties set on the root project"); - assertThat(rootProject.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("simplest-with-props-on-root").getName()).isEqualTo("Simplest multi-module project with all properties set on the root project"); // Verify that we have the modules - Resource module1 = findResource("simplest-with-props-on-root:module1"); - assertThat(module1.getName()).isEqualTo("module1"); - assertThat(module1.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("simplest-with-props-on-root:module1").getName()).isEqualTo("module1"); - Resource module2 = findResource("simplest-with-props-on-root:module2"); - assertThat(module2.getName()).isEqualTo("module2"); - assertThat(module2.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("simplest-with-props-on-root:module2").getName()).isEqualTo("module2"); // And verify that the working directories are all located in the root folder File workDir = new File("projects/multi-module/simplest/simplest-with-props-on-root/.sonar"); @@ -76,18 +69,12 @@ public class MultimoduleTest extends ScannerTestCase { orchestrator.executeBuild(build); - Resource rootProject = findResource("multi-language"); - assertThat(rootProject.getName()).isEqualTo("Simplest multi-language project"); - assertThat(rootProject.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("multi-language").getName()).isEqualTo("Simplest multi-language project"); // Verify that we have the modules - Resource module1 = findResource("multi-language:java-module"); - assertThat(module1.getName()).isEqualTo("java-module"); - assertThat(module1.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("multi-language:java-module").getName()).isEqualTo("java-module"); - Resource module2 = findResource("multi-language:js-module"); - assertThat(module2.getName()).isEqualTo("js-module"); - assertThat(module2.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("multi-language:js-module").getName()).isEqualTo("js-module"); } /** @@ -99,18 +86,12 @@ public class MultimoduleTest extends ScannerTestCase { orchestrator.executeBuild(build); - Resource rootProject = findResource("simplest-with-props-each-module"); - assertThat(rootProject.getName()).isEqualTo("Simplest multi-module project with properties set on each module"); - assertThat(rootProject.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("simplest-with-props-each-module").getName()).isEqualTo("Simplest multi-module project with properties set on each module"); // Verify that we have the modules - Resource module1 = findResource("simplest-with-props-each-module:module1"); - assertThat(module1.getName()).isEqualTo("module1"); - assertThat(module1.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("simplest-with-props-each-module:module1").getName()).isEqualTo("module1"); - Resource module2 = findResource("simplest-with-props-each-module:overridden-key-for-module2"); - assertThat(module2.getName()).isEqualTo("Module 2"); - assertThat(module2.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("simplest-with-props-each-module:overridden-key-for-module2").getName()).isEqualTo("Module 2"); } /** @@ -132,18 +113,12 @@ public class MultimoduleTest extends ScannerTestCase { orchestrator.executeBuild(build); - Resource rootProject = findResource("deep-path-for-modules"); - assertThat(rootProject.getName()).isEqualTo("Project with deep path for modules"); - assertThat(rootProject.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("deep-path-for-modules").getName()).isEqualTo("Project with deep path for modules"); // Verify that we have the modules - Resource module1 = findResource("deep-path-for-modules:mod1"); - assertThat(module1.getName()).isEqualTo("Module 1"); - assertThat(module1.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("deep-path-for-modules:mod1").getName()).isEqualTo("Module 1"); - Resource module2 = findResource("deep-path-for-modules:mod2"); - assertThat(module2.getName()).isEqualTo("Module 2"); - assertThat(module2.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("deep-path-for-modules:mod2").getName()).isEqualTo("Module 2"); } /** @@ -155,18 +130,12 @@ public class MultimoduleTest extends ScannerTestCase { orchestrator.executeBuild(build); - Resource rootProject = findResource("module-path-with-space"); - assertThat(rootProject.getName()).isEqualTo("Project with module path that contain spaces"); - assertThat(rootProject.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("module-path-with-space").getName()).isEqualTo("Project with module path that contain spaces"); // Verify that we have the modules - Resource module1 = findResource("module-path-with-space:module1"); - assertThat(module1.getName()).isEqualTo("Module 1"); - assertThat(module1.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("module-path-with-space:module1").getName()).isEqualTo("Module 1"); - Resource module2 = findResource("module-path-with-space:module2"); - assertThat(module2.getName()).isEqualTo("Module 2"); - assertThat(module2.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("module-path-with-space:module2").getName()).isEqualTo("Module 2"); } /** @@ -178,20 +147,17 @@ public class MultimoduleTest extends ScannerTestCase { orchestrator.executeBuild(build); - Resource rootProject = findResource("overwriting-parent-properties"); + Component rootProject = getComponent("overwriting-parent-properties"); assertThat(rootProject.getName()).isEqualTo("Project with modules that overwrite properties"); - assertThat(rootProject.getVersion()).isEqualTo("1.2.3"); assertThat(rootProject.getDescription()).isEqualTo("Description of root project"); // Verify that we have the modules - Resource module1 = findResource("overwriting-parent-properties:module1-new-key"); + Component module1 = getComponent("overwriting-parent-properties:module1-new-key"); assertThat(module1.getName()).isEqualTo("Module 1"); - assertThat(module1.getVersion()).isEqualTo("1.2.3"); assertThat(module1.getDescription()).isEqualTo("Description of module 1"); - Resource module2 = findResource("overwriting-parent-properties:module2-new-key"); + Component module2 = getComponent("overwriting-parent-properties:module2-new-key"); assertThat(module2.getName()).isEqualTo("Module 2"); - assertThat(module2.getVersion()).isEqualTo("1.2.3"); assertThat(module2.getDescription()).isEqualTo("Description of module 2"); } @@ -204,18 +170,12 @@ public class MultimoduleTest extends ScannerTestCase { orchestrator.executeBuild(build); - Resource rootProject = findResource("using-config-file-prop"); - assertThat(rootProject.getName()).isEqualTo("Advanced use case - mostly used by the Ant task"); - assertThat(rootProject.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("using-config-file-prop").getName()).isEqualTo("Advanced use case - mostly used by the Ant task"); // Verify that we have the modules - Resource module1 = findResource("using-config-file-prop:module1"); - assertThat(module1.getName()).isEqualTo("Module 1"); - assertThat(module1.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("using-config-file-prop:module1").getName()).isEqualTo("Module 1"); - Resource module2 = findResource("using-config-file-prop:module2"); - assertThat(module2.getName()).isEqualTo("Module 2"); - assertThat(module2.getVersion()).isEqualTo("1.2.3"); + assertThat(getComponent("using-config-file-prop:module2").getName()).isEqualTo("Module 2"); } /** @@ -247,7 +207,4 @@ public class MultimoduleTest extends ScannerTestCase { assertThat(result.getLogs()).contains("The properties file of the module 'module1' does not exist"); } - private Resource findResource(String resourceKey) { - return orchestrator.getServer().getWsClient().find(new ResourceQuery(resourceKey)); - } } diff --git a/it/src/test/java/com/sonar/runner/it/ScannerTestCase.java b/it/src/test/java/com/sonar/runner/it/ScannerTestCase.java index 4562a42..c7db62d 100644 --- a/it/src/test/java/com/sonar/runner/it/ScannerTestCase.java +++ b/it/src/test/java/com/sonar/runner/it/ScannerTestCase.java @@ -25,13 +25,30 @@ import com.sonar.orchestrator.version.Version; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.function.Function; +import java.util.stream.Collectors; +import javax.annotation.CheckForNull; import org.apache.commons.lang.StringUtils; import org.junit.ClassRule; import org.junit.Rule; import org.junit.rules.ExpectedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.sonarqube.ws.WsComponents; +import org.sonarqube.ws.WsComponents.Component; +import org.sonarqube.ws.WsMeasures; +import org.sonarqube.ws.WsMeasures.Measure; +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 static java.util.Arrays.asList; +import static java.util.Collections.singletonList; public abstract class ScannerTestCase { @@ -71,4 +88,46 @@ public abstract class ScannerTestCase { return scannerCli; } + @CheckForNull + static Map getMeasures(String componentKey, String... metricKeys) { + return newWsClient().measures().component(new ComponentWsRequest() + .setComponentKey(componentKey) + .setMetricKeys(asList(metricKeys))) + .getComponent().getMeasuresList() + .stream() + .collect(Collectors.toMap(Measure::getMetric, Function.identity())); + } + + @CheckForNull + static Measure getMeasure(String componentKey, String metricKey) { + WsMeasures.ComponentWsResponse response = newWsClient().measures().component(new ComponentWsRequest() + .setComponentKey(componentKey) + .setMetricKeys(singletonList(metricKey))); + List measures = response.getComponent().getMeasuresList(); + return measures.size() == 1 ? measures.get(0) : null; + } + + @CheckForNull + static Integer getMeasureAsInteger(String componentKey, String metricKey) { + Measure measure = getMeasure(componentKey, metricKey); + return (measure == null) ? null : Integer.parseInt(measure.getValue()); + } + + @CheckForNull + static Double getMeasureAsDouble(String componentKey, String metricKey) { + Measure measure = getMeasure(componentKey, metricKey); + return (measure == null) ? null : Double.parseDouble(measure.getValue()); + } + + @CheckForNull + static Component getComponent(String componentKey) { + return newWsClient().components().show(new ShowWsRequest().setKey(componentKey)).getComponent(); + } + + static WsClient newWsClient() { + return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() + .url(orchestrator.getServer().getUrl()) + .build()); + } + } -- 2.39.5