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.

MeasureTester.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 org.sonarqube.ws.tester;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.function.Function;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.Stream;
  27. import javax.annotation.CheckForNull;
  28. import org.sonarqube.ws.Measures;
  29. import org.sonarqube.ws.Measures.Measure;
  30. import org.sonarqube.ws.client.measures.ComponentRequest;
  31. import static java.lang.Double.parseDouble;
  32. import static java.util.Collections.singletonList;
  33. public class MeasureTester {
  34. private final TesterSession session;
  35. MeasureTester(TesterSession session) {
  36. this.session = session;
  37. }
  38. @CheckForNull
  39. public Measure getMeasure(String componentKey, String metricKey) {
  40. return getMeasuresByMetricKey(componentKey, metricKey).get(metricKey);
  41. }
  42. @CheckForNull
  43. public Double getMeasureAsDouble(String componentKey, String metricKey) {
  44. Measure measure = getMeasure(componentKey, metricKey);
  45. return (measure == null) ? null : parseDouble(measure.getValue());
  46. }
  47. public Map<String, Measure> getMeasuresByMetricKey(String componentKey, String... metricKeys) {
  48. return getStreamMeasures(componentKey, metricKeys)
  49. .filter(Measure::hasValue)
  50. .collect(Collectors.toMap(Measure::getMetric, Function.identity()));
  51. }
  52. public Map<String, Double> getMeasuresAsDoubleByMetricKey(String componentKey, String... metricKeys) {
  53. return getStreamMeasures(componentKey, metricKeys)
  54. .filter(Measure::hasValue)
  55. .collect(Collectors.toMap(Measure::getMetric, measure -> parseDouble(measure.getValue())));
  56. }
  57. private Stream<Measure> getStreamMeasures(String componentKey, String... metricKeys) {
  58. return session.wsClient().measures().component(new ComponentRequest()
  59. .setComponent(componentKey)
  60. .setMetricKeys(Arrays.asList(metricKeys)))
  61. .getComponent().getMeasuresList()
  62. .stream();
  63. }
  64. @CheckForNull
  65. public Measure getMeasureWithVariation(String componentKey, String metricKey) {
  66. Measures.ComponentWsResponse response = session.wsClient().measures().component(new ComponentRequest()
  67. .setComponent(componentKey)
  68. .setMetricKeys(singletonList(metricKey))
  69. .setAdditionalFields(singletonList("period")));
  70. List<Measure> measures = response.getComponent().getMeasuresList();
  71. return measures.size() == 1 ? measures.get(0) : null;
  72. }
  73. @CheckForNull
  74. public Map<String, Measure> getMeasuresWithVariationsByMetricKey(String componentKey, String... metricKeys) {
  75. return session.wsClient().measures().component(new ComponentRequest()
  76. .setComponent(componentKey)
  77. .setMetricKeys(Arrays.asList(metricKeys))
  78. .setAdditionalFields(singletonList("period"))).getComponent().getMeasuresList()
  79. .stream()
  80. .collect(Collectors.toMap(Measure::getMetric, Function.identity()));
  81. }
  82. /**
  83. * Return leak period value
  84. */
  85. @CheckForNull
  86. public Double getLeakPeriodValue(String componentKey, String metricKey) {
  87. Measures.PeriodValue periodsValue = getMeasureWithVariation(componentKey, metricKey).getPeriod();
  88. return parseDouble(periodsValue.getValue());
  89. }
  90. }