3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.metric;
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Collections;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.slf4j.event.Level;
28 import org.sonar.api.testfixtures.log.LogTester;
29 import org.sonar.api.utils.log.LoggerLevel;
30 import org.sonar.core.metric.ScannerMetrics;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35 import static org.sonar.api.measures.Metric.Builder;
36 import static org.sonar.api.measures.Metric.ValueType;
38 public class ReportMetricValidatorImplTest {
41 public LogTester logTester = new LogTester().setLevel(LoggerLevel.DEBUG);
43 static final String METRIC_KEY = "metric_key";
45 ScannerMetrics scannerMetrics = mock(ScannerMetrics.class);
48 public void before() {
49 logTester.setLevel(Level.DEBUG);
53 public void validate_metric() {
54 when(scannerMetrics.getMetrics()).thenReturn(ImmutableSet.of(new Builder(METRIC_KEY, "name", ValueType.INT).create()));
55 ReportMetricValidator validator = new ReportMetricValidatorImpl(scannerMetrics);
57 assertThat(validator.validate(METRIC_KEY)).isTrue();
58 assertThat(logTester.logs()).isEmpty();
62 public void not_validate_metric() {
63 when(scannerMetrics.getMetrics()).thenReturn(Collections.emptySet());
64 ReportMetricValidator validator = new ReportMetricValidatorImpl(scannerMetrics);
66 assertThat(validator.validate(METRIC_KEY)).isFalse();
67 assertThat(logTester.logs()).containsOnly("The metric 'metric_key' is ignored and should not be send in the batch report");
71 public void not_generate_new_log_when_validating_twice_the_same_metric() {
72 when(scannerMetrics.getMetrics()).thenReturn(Collections.emptySet());
73 ReportMetricValidator validator = new ReportMetricValidatorImpl(scannerMetrics);
75 assertThat(validator.validate(METRIC_KEY)).isFalse();
76 assertThat(logTester.logs()).hasSize(1);
77 assertThat(validator.validate(METRIC_KEY)).isFalse();
78 assertThat(logTester.logs()).hasSize(1);