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.

ReportMetricValidatorImplTest.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.sonar.ce.task.projectanalysis.metric;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.Collections;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.utils.log.LogTester;
  26. import org.sonar.api.utils.log.LoggerLevel;
  27. import org.sonar.core.metric.ScannerMetrics;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. import static org.sonar.api.measures.Metric.Builder;
  32. import static org.sonar.api.measures.Metric.ValueType;
  33. public class ReportMetricValidatorImplTest {
  34. @Rule
  35. public LogTester logTester = new LogTester().setLevel(LoggerLevel.DEBUG);
  36. static final String METRIC_KEY = "metric_key";
  37. ScannerMetrics scannerMetrics = mock(ScannerMetrics.class);
  38. @Test
  39. public void validate_metric() {
  40. when(scannerMetrics.getMetrics()).thenReturn(ImmutableSet.of(new Builder(METRIC_KEY, "name", ValueType.INT).create()));
  41. ReportMetricValidator validator = new ReportMetricValidatorImpl(scannerMetrics);
  42. assertThat(validator.validate(METRIC_KEY)).isTrue();
  43. assertThat(logTester.logs()).isEmpty();
  44. }
  45. @Test
  46. public void not_validate_metric() {
  47. when(scannerMetrics.getMetrics()).thenReturn(Collections.emptySet());
  48. ReportMetricValidator validator = new ReportMetricValidatorImpl(scannerMetrics);
  49. assertThat(validator.validate(METRIC_KEY)).isFalse();
  50. assertThat(logTester.logs()).containsOnly("The metric 'metric_key' is ignored and should not be send in the batch report");
  51. }
  52. @Test
  53. public void not_generate_new_log_when_validating_twice_the_same_metric() {
  54. when(scannerMetrics.getMetrics()).thenReturn(Collections.emptySet());
  55. ReportMetricValidator validator = new ReportMetricValidatorImpl(scannerMetrics);
  56. assertThat(validator.validate(METRIC_KEY)).isFalse();
  57. assertThat(logTester.logs()).hasSize(1);
  58. assertThat(validator.validate(METRIC_KEY)).isFalse();
  59. assertThat(logTester.logs()).hasSize(1);
  60. }
  61. }