3 * Copyright (C) 2009-2024 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.telemetry.project;
23 import java.util.function.Consumer;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.RegisterExtension;
26 import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.component.ProjectData;
31 import org.sonar.db.measure.LiveMeasureDto;
32 import org.sonar.db.metric.MetricDto;
33 import org.sonar.db.project.ProjectDto;
34 import org.sonar.telemetry.project.ProjectCppAutoconfigTelemetryProvider;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
38 import static org.sonar.api.measures.Metric.ValueType.STRING;
40 class ProjectCppAutoconfigTelemetryProviderIT {
42 private final System2 system2 = new AlwaysIncreasingSystem2(1000L);
45 public final DbTester db = DbTester.create(system2);
47 ProjectCppAutoconfigTelemetryProvider underTest = new ProjectCppAutoconfigTelemetryProvider(db.getDbClient());
50 void getUuidValues_whenNoProjects_returnEmptyList() {
51 assertThat(underTest.getUuidValues()).isEmpty();
55 void getUuidValues_whenNoCppAndCProjects_returnEmptyMap() {
56 Consumer<MetricDto> configureMetric = metric -> metric
57 .setValueType(STRING.name())
58 .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY);
60 MetricDto metric = db.measures().insertMetric(configureMetric);
62 ProjectData project1 = db.components().insertPrivateProject();
63 ProjectData project2 = db.components().insertPrivateProject();
65 insertLiveMeasure("java", metric).accept(project1);
66 insertLiveMeasure("cobol", metric).accept(project2);
69 assertThat(underTest.getUuidValues()).isEmpty();
73 void getUuidValues_when1CppAnd1CProject_returnMapWithSize2AndAutoconfigByDefault() {
74 Consumer<MetricDto> configureMetric = metric -> metric
75 .setValueType(STRING.name())
76 .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY);
78 MetricDto metric = db.measures().insertMetric(configureMetric);
80 ProjectData project1 = db.components().insertPrivateProject();
81 ProjectData project2 = db.components().insertPrivateProject();
82 ProjectData project3 = db.components().insertPrivateProject();
83 ProjectData project4 = db.components().insertPrivateProject();
85 insertLiveMeasure("c", metric).accept(project1);
86 insertLiveMeasure("cpp", metric).accept(project2);
87 insertLiveMeasure("java", metric).accept(project3);
88 insertLiveMeasure("cobol", metric).accept(project4);
90 Map<String, String> actualResult = underTest.getUuidValues();
92 assertThat(actualResult).hasSize(2);
93 assertThat(actualResult).containsExactlyInAnyOrderEntriesOf(Map.of(project1.getProjectDto().getUuid(), "AUTOCONFIG",
94 project2.getProjectDto().getUuid(), "AUTOCONFIG"));
98 void getUuidValues_whenCAndCppProjectsWithDifferentConfig_returnMapWithSize2AndNotAutoconfig() {
99 Consumer<MetricDto> configureMetric = metric -> metric
100 .setValueType(STRING.name())
101 .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY);
103 MetricDto metric = db.measures().insertMetric(configureMetric);
105 ProjectData project1 = db.components().insertPrivateProject();
106 ProjectData project2 = db.components().insertPrivateProject();
107 ProjectData project3 = db.components().insertPrivateProject();
108 ProjectData project4 = db.components().insertPrivateProject();
110 insertLiveMeasure("c", metric).accept(project1);
111 insertLiveMeasure("cpp", metric).accept(project2);
112 insertLiveMeasure("java", metric).accept(project3);
113 insertLiveMeasure("cobol", metric).accept(project4);
115 db.properties().insertProperty("sonar.cfamily.build-wrapper-output", "anyvalue", project1.getProjectDto().getUuid());
116 db.properties().insertProperty("sonar.cfamily.compile-commands", "anyvalue", project2.getProjectDto().getUuid());
118 Map<String, String> actualResult = underTest.getUuidValues();
120 assertThat(actualResult).hasSize(2);
121 assertThat(actualResult).containsExactlyInAnyOrderEntriesOf(Map.of(project1.getProjectDto().getUuid(), "BW_DEPRECATED",
122 project2.getProjectDto().getUuid(), "COMPDB"));
125 private Consumer<LiveMeasureDto> configureLiveMeasure(String language, MetricDto metric, ProjectDto project, ComponentDto componentDto) {
126 return liveMeasure -> liveMeasure
127 .setMetricUuid(metric.getUuid())
128 .setComponentUuid(componentDto.uuid())
129 .setProjectUuid(project.getUuid())
130 .setData(language + "=" + 100);
133 private Consumer<ProjectData> insertLiveMeasure(String language, MetricDto metric) {
134 return projectData -> db.measures().insertLiveMeasure(projectData.getMainBranchComponent(), metric,
135 configureLiveMeasure(language, metric, projectData.getProjectDto(), projectData.getMainBranchComponent()));