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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * SonarSource :: IT :: SonarScanner CLI
  3. * Copyright (C) 2009-2021 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.http.HttpMethod;
  24. import com.sonar.orchestrator.version.Version;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.IOException;
  28. import java.time.Instant;
  29. import java.time.ZoneId;
  30. import java.time.format.DateTimeFormatter;
  31. import java.time.temporal.ChronoUnit;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Properties;
  35. import java.util.function.Function;
  36. import java.util.stream.Collectors;
  37. import javax.annotation.CheckForNull;
  38. import org.apache.commons.lang.StringUtils;
  39. import org.junit.After;
  40. import org.junit.ClassRule;
  41. import org.junit.Rule;
  42. import org.junit.rules.ExpectedException;
  43. import org.slf4j.Logger;
  44. import org.slf4j.LoggerFactory;
  45. import org.sonarqube.ws.Components.Component;
  46. import org.sonarqube.ws.Measures;
  47. import org.sonarqube.ws.Measures.Measure;
  48. import org.sonarqube.ws.client.HttpConnector;
  49. import org.sonarqube.ws.client.WsClient;
  50. import org.sonarqube.ws.client.WsClientFactories;
  51. import org.sonarqube.ws.client.components.ShowRequest;
  52. import org.sonarqube.ws.client.measures.ComponentRequest;
  53. import static java.util.Arrays.asList;
  54. import static java.util.Collections.singletonList;
  55. public abstract class ScannerTestCase {
  56. private static final Logger LOG = LoggerFactory
  57. .getLogger(ScannerTestCase.class);
  58. @Rule
  59. public ExpectedException thrown = ExpectedException.none();
  60. @ClassRule
  61. public static Orchestrator orchestrator = SonarScannerTestSuite.ORCHESTRATOR;
  62. private static Version artifactVersion;
  63. private static Version artifactVersion() {
  64. if (artifactVersion == null) {
  65. String scannerVersion = System.getProperty("scanner.version");
  66. if (StringUtils.isNotBlank(scannerVersion)) {
  67. LOG.info("Use provided Scanner version: " + scannerVersion);
  68. artifactVersion = Version.create(scannerVersion);
  69. } else if (StringUtils.isNotBlank(System.getenv("PROJECT_VERSION"))) {
  70. scannerVersion = System.getenv("PROJECT_VERSION");
  71. LOG.info("Use Scanner version from environment: " + scannerVersion);
  72. artifactVersion = Version.create(scannerVersion);
  73. } else {
  74. try (FileInputStream fis = new FileInputStream(
  75. new File("../target/maven-archiver/pom.properties"))) {
  76. Properties props = new Properties();
  77. props.load(fis);
  78. artifactVersion = Version.create(props.getProperty("version"));
  79. } catch (IOException e) {
  80. throw new IllegalStateException(e);
  81. }
  82. }
  83. }
  84. return artifactVersion;
  85. }
  86. @After
  87. public void resetData() {
  88. // We add one day to ensure that today's entries are deleted.
  89. Instant instant = Instant.now().plus(1, ChronoUnit.DAYS);
  90. // The expected format is yyyy-MM-dd.
  91. String currentDateTime = DateTimeFormatter.ISO_LOCAL_DATE
  92. .withZone(ZoneId.of("UTC"))
  93. .format(instant);
  94. orchestrator.getServer()
  95. .newHttpCall("/api/projects/bulk_delete")
  96. .setAdminCredentials()
  97. .setMethod(HttpMethod.POST)
  98. .setParams("analyzedBefore", currentDateTime)
  99. .execute();
  100. }
  101. SonarScanner newScanner(File baseDir, String... keyValueProperties) {
  102. SonarScanner scannerCli = SonarScanner.create(baseDir, keyValueProperties);
  103. scannerCli.setScannerVersion(artifactVersion().toString());
  104. return scannerCli;
  105. }
  106. @CheckForNull
  107. static Map<String, Measure> getMeasures(String componentKey,
  108. String... metricKeys) {
  109. return newWsClient().measures().component(new ComponentRequest()
  110. .setComponent(componentKey)
  111. .setMetricKeys(asList(metricKeys)))
  112. .getComponent().getMeasuresList()
  113. .stream()
  114. .collect(Collectors.toMap(Measure::getMetric, Function.identity()));
  115. }
  116. @CheckForNull
  117. static Measure getMeasure(String componentKey, String metricKey) {
  118. Measures.ComponentWsResponse response = newWsClient().measures()
  119. .component(new ComponentRequest()
  120. .setComponent(componentKey)
  121. .setMetricKeys(singletonList(metricKey)));
  122. List<Measure> measures = response.getComponent().getMeasuresList();
  123. return measures.size() == 1 ? measures.get(0) : null;
  124. }
  125. @CheckForNull
  126. static Integer getMeasureAsInteger(String componentKey, String metricKey) {
  127. Measure measure = getMeasure(componentKey, metricKey);
  128. return (measure == null) ? null : Integer.parseInt(measure.getValue());
  129. }
  130. @CheckForNull
  131. static Double getMeasureAsDouble(String componentKey, String metricKey) {
  132. Measure measure = getMeasure(componentKey, metricKey);
  133. return (measure == null) ? null : Double.parseDouble(measure.getValue());
  134. }
  135. @CheckForNull
  136. static Component getComponent(String componentKey) {
  137. return newWsClient().components()
  138. .show(new ShowRequest().setComponent(componentKey)).getComponent();
  139. }
  140. static WsClient newWsClient() {
  141. return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
  142. .url(orchestrator.getServer().getUrl())
  143. .build());
  144. }
  145. }