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;
22 import java.util.Arrays;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.Mockito;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.measure.ProjectLocDistributionDto;
28 import org.sonar.db.metric.MetricDto;
29 import org.sonar.telemetry.core.Dimension;
30 import org.sonar.telemetry.core.Granularity;
32 import static java.util.Arrays.asList;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
38 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
39 import static org.sonar.server.platform.telemetry.TelemetryNclocProvider.METRIC_KEY;
41 class TelemetryNclocProviderTest {
43 DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
44 private final DbSession dbSession = mock(DbSession.class);
47 void getUuidValues_returnsTheRightLanguageDistribution() {
48 TelemetryNclocProvider telemetryNclocProvider = new TelemetryNclocProvider(dbClient);
50 when(dbClient.openSession(false)).thenReturn(dbSession);
52 when(dbClient.metricDao().selectByKeys(dbSession, asList(NCLOC_KEY, NCLOC_LANGUAGE_DISTRIBUTION_KEY))).thenReturn(Arrays.asList(
53 new MetricDto().setKey(NCLOC_KEY).setUuid("ncloc_uuid"),
54 new MetricDto().setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY).setUuid("ncloc_distribution_uuid")
57 when(dbClient.liveMeasureDao().selectLargestBranchesLocDistribution(dbSession, "ncloc_uuid", "ncloc_distribution_uuid")).thenReturn(Arrays.asList(
58 new ProjectLocDistributionDto("project1", "branch1-p1", "java=5000;xml=1000;js=1000"),
59 new ProjectLocDistributionDto("project2", "branch1-p2", "java=10000;csharp=2000"),
60 new ProjectLocDistributionDto("project3", "branch1-p3", "java=7000;js=500")
63 assertEquals(METRIC_KEY, telemetryNclocProvider.getMetricKey());
64 assertEquals(Granularity.DAILY, telemetryNclocProvider.getGranularity());
65 assertEquals(Dimension.LANGUAGE, telemetryNclocProvider.getDimension());
67 assertThat(telemetryNclocProvider.getUuidValues()).containsOnlyKeys("java", "xml", "csharp", "js");
68 assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("java", 22000L);
69 assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("xml", 1000L);
70 assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("csharp", 2000L);
71 assertThat(telemetryNclocProvider.getUuidValues()).containsEntry("js", 1500L);