You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScannerTestCase.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * SonarSource :: IT :: SonarScanner CLI
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package com.sonarsource.scanner.it;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import com.sonar.orchestrator.build.SonarScanner;
  23. import com.sonar.orchestrator.version.Version;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Properties;
  30. import java.util.function.Function;
  31. import java.util.stream.Collectors;
  32. import javax.annotation.CheckForNull;
  33. import org.apache.commons.lang.StringUtils;
  34. import org.junit.ClassRule;
  35. import org.junit.Rule;
  36. import org.junit.rules.ExpectedException;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.sonarqube.ws.Components.Component;
  40. import org.sonarqube.ws.Measures;
  41. import org.sonarqube.ws.Measures.Measure;
  42. import org.sonarqube.ws.client.HttpConnector;
  43. import org.sonarqube.ws.client.WsClient;
  44. import org.sonarqube.ws.client.WsClientFactories;
  45. import org.sonarqube.ws.client.components.ShowRequest;
  46. import org.sonarqube.ws.client.measures.ComponentRequest;
  47. import static java.util.Arrays.asList;
  48. import static java.util.Collections.singletonList;
  49. public abstract class ScannerTestCase {
  50. private static final Logger LOG = LoggerFactory
  51. .getLogger(ScannerTestCase.class);
  52. @Rule
  53. public ExpectedException thrown = ExpectedException.none();
  54. @ClassRule
  55. public static Orchestrator orchestrator = SonarScannerTestSuite.ORCHESTRATOR;
  56. private static Version artifactVersion;
  57. private static Version artifactVersion() {
  58. if (artifactVersion == null) {
  59. String scannerVersion = System.getProperty("scanner.version");
  60. if (StringUtils.isNotBlank(scannerVersion)) {
  61. LOG.info("Use provided Scanner version: " + scannerVersion);
  62. artifactVersion = Version.create(scannerVersion);
  63. } else if (StringUtils.isNotBlank(System.getenv("PROJECT_VERSION"))) {
  64. scannerVersion = System.getenv("PROJECT_VERSION");
  65. LOG.info("Use Scanner version from environment: " + scannerVersion);
  66. artifactVersion = Version.create(scannerVersion);
  67. } else {
  68. try (FileInputStream fis = new FileInputStream(
  69. new File("../target/maven-archiver/pom.properties"))) {
  70. Properties props = new Properties();
  71. props.load(fis);
  72. artifactVersion = Version.create(props.getProperty("version"));
  73. } catch (IOException e) {
  74. throw new IllegalStateException(e);
  75. }
  76. }
  77. }
  78. return artifactVersion;
  79. }
  80. SonarScanner newScanner(File baseDir, String... keyValueProperties) {
  81. SonarScanner scannerCli = SonarScanner.create(baseDir, keyValueProperties);
  82. scannerCli.setScannerVersion(artifactVersion().toString());
  83. return scannerCli;
  84. }
  85. @CheckForNull
  86. static Map<String, Measure> getMeasures(String componentKey,
  87. String... metricKeys) {
  88. return newWsClient().measures().component(new ComponentRequest()
  89. .setComponent(componentKey)
  90. .setMetricKeys(asList(metricKeys)))
  91. .getComponent().getMeasuresList()
  92. .stream()
  93. .collect(Collectors.toMap(Measure::getMetric, Function.identity()));
  94. }
  95. @CheckForNull
  96. static Measure getMeasure(String componentKey, String metricKey) {
  97. Measures.ComponentWsResponse response = newWsClient().measures()
  98. .component(new ComponentRequest()
  99. .setComponent(componentKey)
  100. .setMetricKeys(singletonList(metricKey)));
  101. List<Measure> measures = response.getComponent().getMeasuresList();
  102. return measures.size() == 1 ? measures.get(0) : null;
  103. }
  104. @CheckForNull
  105. static Integer getMeasureAsInteger(String componentKey, String metricKey) {
  106. Measure measure = getMeasure(componentKey, metricKey);
  107. return (measure == null) ? null : Integer.parseInt(measure.getValue());
  108. }
  109. @CheckForNull
  110. static Double getMeasureAsDouble(String componentKey, String metricKey) {
  111. Measure measure = getMeasure(componentKey, metricKey);
  112. return (measure == null) ? null : Double.parseDouble(measure.getValue());
  113. }
  114. @CheckForNull
  115. static Component getComponent(String componentKey) {
  116. return newWsClient().components()
  117. .show(new ShowRequest().setComponent(componentKey)).getComponent();
  118. }
  119. static WsClient newWsClient() {
  120. return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
  121. .url(orchestrator.getServer().getUrl())
  122. .build());
  123. }
  124. }