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.server.platform.telemetry;
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;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
37 import static org.sonar.api.measures.Metric.ValueType.STRING;
39 class ProjectCppAutoconfigTelemetryProviderIT {
41 private final System2 system2 = new AlwaysIncreasingSystem2(1000L);
44 public final DbTester db = DbTester.create(system2);
46 ProjectCppAutoconfigTelemetryProvider underTest = new ProjectCppAutoconfigTelemetryProvider(db.getDbClient());
49 void getValues_whenNoProjects_returnEmptyList() {
50 assertThat(underTest.getValues()).isEmpty();
54 void getValues_whenNoCppAndCProjects_returnEmptyMap() {
55 Consumer<MetricDto> configureMetric = metric -> metric
56 .setValueType(STRING.name())
57 .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY);
59 MetricDto metric = db.measures().insertMetric(configureMetric);
61 ProjectData project1 = db.components().insertPrivateProject();
62 ProjectData project2 = db.components().insertPrivateProject();
64 insertLiveMeasure("java", metric).accept(project1);
65 insertLiveMeasure("cobol", metric).accept(project2);
68 assertThat(underTest.getValues()).isEmpty();
72 void getValues_when1CppAnd1CProject_returnMapWithSize2AndAutoconfigByDefault() {
73 Consumer<MetricDto> configureMetric = metric -> metric
74 .setValueType(STRING.name())
75 .setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY);
77 MetricDto metric = db.measures().insertMetric(configureMetric);
79 ProjectData project1 = db.components().insertPrivateProject();
80 ProjectData project2 = db.components().insertPrivateProject();
81 ProjectData project3 = db.components().insertPrivateProject();
82 ProjectData project4 = db.components().insertPrivateProject();
84 insertLiveMeasure("c", metric).accept(project1);
85 insertLiveMeasure("cpp", metric).accept(project2);
86 insertLiveMeasure("java", metric).accept(project3);
87 insertLiveMeasure("cobol", metric).accept(project4);
89 Map<String, String> actualResult = underTest.getValues();
91 assertThat(actualResult).hasSize(2)
92 .containsExactlyInAnyOrderEntriesOf(
93 Map.of(project1.getProjectDto().getUuid(), "AUTOCONFIG", project2.getProjectDto().getUuid(), "AUTOCONFIG")
98 void getValues_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.getValues();
120 assertThat(actualResult).hasSize(2)
121 .containsExactlyInAnyOrderEntriesOf(
122 Map.of(project1.getProjectDto().getUuid(), "BW_DEPRECATED", project2.getProjectDto().getUuid(), "COMPDB")
126 private Consumer<LiveMeasureDto> configureLiveMeasure(String language, MetricDto metric, ComponentDto componentDto) {
127 return liveMeasure -> liveMeasure
128 .setMetricUuid(metric.getUuid())
129 .setComponentUuid(componentDto.uuid())
130 .setProjectUuid(componentDto.uuid())
131 .setData(language + "=" + 100);
134 private Consumer<ProjectData> insertLiveMeasure(String language, MetricDto metric) {
135 return projectData -> db.measures().insertLiveMeasure(projectData.getMainBranchComponent(), metric,
136 configureLiveMeasure(language, metric, projectData.getMainBranchComponent()));