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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * SonarSource :: IT :: SonarScanner CLI
  3. * Copyright (C) 2009-2022 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.LocalDateTime;
  29. import java.time.ZoneId;
  30. import java.time.ZonedDateTime;
  31. import java.time.format.DateTimeFormatter;
  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 String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
  57. private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern(DATETIME_FORMAT);
  58. private static final Logger LOG = LoggerFactory
  59. .getLogger(ScannerTestCase.class);
  60. @Rule
  61. public ExpectedException thrown = ExpectedException.none();
  62. @ClassRule
  63. public static Orchestrator orchestrator = SonarScannerTestSuite.ORCHESTRATOR;
  64. private static Version artifactVersion;
  65. private static Version artifactVersion() {
  66. if (artifactVersion == null) {
  67. String scannerVersion = System.getProperty("scanner.version");
  68. if (StringUtils.isNotBlank(scannerVersion)) {
  69. LOG.info("Use provided Scanner version: " + scannerVersion);
  70. artifactVersion = Version.create(scannerVersion);
  71. } else if (StringUtils.isNotBlank(System.getenv("PROJECT_VERSION"))) {
  72. scannerVersion = System.getenv("PROJECT_VERSION");
  73. LOG.info("Use Scanner version from environment: " + scannerVersion);
  74. artifactVersion = Version.create(scannerVersion);
  75. } else {
  76. try (FileInputStream fis = new FileInputStream(
  77. new File("../target/maven-archiver/pom.properties"))) {
  78. Properties props = new Properties();
  79. props.load(fis);
  80. artifactVersion = Version.create(props.getProperty("version"));
  81. } catch (IOException e) {
  82. throw new IllegalStateException(e);
  83. }
  84. }
  85. }
  86. return artifactVersion;
  87. }
  88. @After
  89. public void resetData() {
  90. String currentDateTime = ZonedDateTime.now().format(DATETIME_FORMATTER);
  91. orchestrator.getServer()
  92. .newHttpCall("/api/projects/bulk_delete")
  93. .setAdminCredentials()
  94. .setMethod(HttpMethod.POST)
  95. .setParams("analyzedBefore", currentDateTime)
  96. .execute();
  97. }
  98. SonarScanner newScanner(File baseDir, String... keyValueProperties) {
  99. SonarScanner scannerCli = SonarScanner.create(baseDir, keyValueProperties);
  100. scannerCli.setScannerVersion(artifactVersion().toString());
  101. return scannerCli;
  102. }
  103. @CheckForNull
  104. static Map<String, Measure> getMeasures(String componentKey,
  105. String... metricKeys) {
  106. return newWsClient().measures().component(new ComponentRequest()
  107. .setComponent(componentKey)
  108. .setMetricKeys(asList(metricKeys)))
  109. .getComponent().getMeasuresList()
  110. .stream()
  111. .collect(Collectors.toMap(Measure::getMetric, Function.identity()));
  112. }
  113. @CheckForNull
  114. static Measure getMeasure(String componentKey, String metricKey) {
  115. Measures.ComponentWsResponse response = newWsClient().measures()
  116. .component(new ComponentRequest()
  117. .setComponent(componentKey)
  118. .setMetricKeys(singletonList(metricKey)));
  119. List<Measure> measures = response.getComponent().getMeasuresList();
  120. return measures.size() == 1 ? measures.get(0) : null;
  121. }
  122. @CheckForNull
  123. static Integer getMeasureAsInteger(String componentKey, String metricKey) {
  124. Measure measure = getMeasure(componentKey, metricKey);
  125. return (measure == null) ? null : Integer.parseInt(measure.getValue());
  126. }
  127. @CheckForNull
  128. static Double getMeasureAsDouble(String componentKey, String metricKey) {
  129. Measure measure = getMeasure(componentKey, metricKey);
  130. return (measure == null) ? null : Double.parseDouble(measure.getValue());
  131. }
  132. @CheckForNull
  133. static Component getComponent(String componentKey) {
  134. return newWsClient().components()
  135. .show(new ShowRequest().setComponent(componentKey)).getComponent();
  136. }
  137. static WsClient newWsClient() {
  138. return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
  139. .url(orchestrator.getServer().getUrl())
  140. .build());
  141. }
  142. }