From 2715d0441a88972fe4c43de841c95d14c2eeff36 Mon Sep 17 00:00:00 2001 From: OrlovAlexander <35396155+OrlovAlexander85@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:18:29 +0200 Subject: [PATCH] SONAR-22730 Telemetry on legacy mode property --- server/sonar-webserver/build.gradle | 1 + .../platformlevel/PlatformLevel4.java | 5 +- .../TelemetryLegacyModePropertyProvider.java | 67 +++++++++++++++++ ...lemetryLegacyModePropertyProviderTest.java | 71 +++++++++++++++++++ 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProvider.java create mode 100644 server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProviderTest.java diff --git a/server/sonar-webserver/build.gradle b/server/sonar-webserver/build.gradle index 6b055e5d06a..9a0e7599971 100644 --- a/server/sonar-webserver/build.gradle +++ b/server/sonar-webserver/build.gradle @@ -33,6 +33,7 @@ dependencies { testImplementation 'com.github.spotbugs:spotbugs-annotations' testImplementation 'com.tngtech.java:junit-dataprovider' testImplementation 'org.junit.jupiter:junit-jupiter-api' + testImplementation 'org.junit.jupiter:junit-jupiter-params' testImplementation 'org.mockito:mockito-core' testImplementation 'org.eclipse.jetty:jetty-server' testImplementation 'org.eclipse.jetty:jetty-servlet' diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 03e3dd084cd..0e3a90538be 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -58,6 +58,7 @@ import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.documentation.DefaultDocumentationLinkGenerator; import org.sonar.core.extension.CoreExtensionsInstaller; import org.sonar.core.language.LanguagesProvider; +import org.sonar.core.metric.SoftwareQualitiesMetrics; import org.sonar.core.platform.PlatformEditionProvider; import org.sonar.core.platform.SpringComponentContainer; import org.sonar.server.almintegration.ws.AlmIntegrationsWSModule; @@ -159,7 +160,6 @@ import org.sonar.server.measure.index.ProjectsEsModule; import org.sonar.server.measure.live.LiveMeasureModule; import org.sonar.server.measure.ws.MeasuresWsModule; import org.sonar.server.metric.IssueCountMetrics; -import org.sonar.core.metric.SoftwareQualitiesMetrics; import org.sonar.server.metric.UnanalyzedLanguageMetrics; import org.sonar.server.metric.ws.MetricsWsModule; import org.sonar.server.monitoring.ComputeEngineMetricStatusTask; @@ -188,7 +188,7 @@ import org.sonar.server.platform.SystemInfoWriterModule; import org.sonar.server.platform.WebCoreExtensionsInstaller; import org.sonar.server.platform.db.CheckAnyonePermissionsAtStartup; import org.sonar.server.platform.telemetry.ProjectCppAutoconfigTelemetryProvider; -import org.sonar.server.platform.telemetry.TelemetryFipsEnabledProvider; +import org.sonar.server.platform.telemetry.TelemetryLegacyModePropertyProvider; import org.sonar.server.platform.telemetry.TelemetryNclocProvider; import org.sonar.server.platform.telemetry.TelemetryUserEnabledProvider; import org.sonar.server.platform.telemetry.TelemetryVersionProvider; @@ -669,6 +669,7 @@ public class PlatformLevel4 extends PlatformLevel { // new telemetry metrics ProjectCppAutoconfigTelemetryProvider.class, TelemetryVersionProvider.class, + TelemetryLegacyModePropertyProvider.class, TelemetryNclocProvider.class, TelemetryUserEnabledProvider.class, TelemetryFipsEnabledProvider.class, diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProvider.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProvider.java new file mode 100644 index 00000000000..97c8bb80af7 --- /dev/null +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProvider.java @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.telemetry; + +import java.util.Optional; +import org.sonar.db.DbClient; +import org.sonar.db.property.PropertyDto; +import org.sonar.telemetry.core.Dimension; +import org.sonar.telemetry.core.Granularity; +import org.sonar.telemetry.core.TelemetryDataProvider; +import org.sonar.telemetry.core.TelemetryDataType; + +import static org.sonar.core.config.LegacyRatingConstants.LEGACY_RATING_MODE_ENABLED; +import static org.sonar.telemetry.core.Dimension.INSTALLATION; +import static org.sonar.telemetry.core.Granularity.WEEKLY; +import static org.sonar.telemetry.core.TelemetryDataType.BOOLEAN; + +public class TelemetryLegacyModePropertyProvider implements TelemetryDataProvider { + private final DbClient dbClient; + + public TelemetryLegacyModePropertyProvider(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public String getMetricKey() { + return "legacy_rating_mode_enabled"; + } + + @Override + public Dimension getDimension() { + return INSTALLATION; + } + + @Override + public Granularity getGranularity() { + return WEEKLY; + } + + @Override + public TelemetryDataType getType() { + return BOOLEAN; + } + + @Override + public Optional getValue() { + PropertyDto property = dbClient.propertiesDao().selectGlobalProperty(LEGACY_RATING_MODE_ENABLED); + return property == null ? Optional.of(false) : Optional.of(Boolean.valueOf(property.getValue())); + } +} diff --git a/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProviderTest.java b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProviderTest.java new file mode 100644 index 00000000000..7a1aadac4f5 --- /dev/null +++ b/server/sonar-webserver/src/test/java/org/sonar/server/platform/telemetry/TelemetryLegacyModePropertyProviderTest.java @@ -0,0 +1,71 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.telemetry; + +import java.util.Optional; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.sonar.db.DbClient; +import org.sonar.db.property.PropertiesDao; +import org.sonar.db.property.PropertyDto; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.core.config.LegacyRatingConstants.LEGACY_RATING_MODE_ENABLED; +import static org.sonar.telemetry.core.Dimension.INSTALLATION; +import static org.sonar.telemetry.core.Granularity.WEEKLY; +import static org.sonar.telemetry.core.TelemetryDataType.BOOLEAN; + +class TelemetryLegacyModePropertyProviderTest { + private final DbClient dbClient = mock(); + private final PropertiesDao propertiesDao = mock(); + private final TelemetryLegacyModePropertyProvider underTest = new TelemetryLegacyModePropertyProvider(dbClient); + + @ParameterizedTest + @MethodSource("getValues") + void getter_should_return_correct_values(Boolean value, Boolean expected) { + when(dbClient.propertiesDao()).thenReturn(propertiesDao); + if (value == null) { + when(dbClient.propertiesDao().selectGlobalProperty(LEGACY_RATING_MODE_ENABLED)) + .thenReturn(null); + } else { + when(dbClient.propertiesDao().selectGlobalProperty(LEGACY_RATING_MODE_ENABLED)) + .thenReturn(new PropertyDto().setValue(value.toString())); + } + + assertEquals("legacy_rating_mode_enabled", underTest.getMetricKey()); + assertEquals(INSTALLATION, underTest.getDimension()); + assertEquals(WEEKLY, underTest.getGranularity()); + assertEquals(BOOLEAN, underTest.getType()); + assertEquals(Optional.of(expected), underTest.getValue()); + } + + public static Stream getValues() { + return Stream.of( + Arguments.of(true, true), + Arguments.of(false, false), + Arguments.of(null, false) + ); + } + +} -- 2.39.5