From b92557753cae7fbd44dc105b121268ae3f3f6557 Mon Sep 17 00:00:00 2001 From: Bogdana Date: Wed, 14 Aug 2024 12:14:51 +0200 Subject: SONAR-22641 update info for email config host telemetry --- server/sonar-server-common/build.gradle | 1 + .../EmailConfigHostTelemetryProvider.java | 22 +++++++++++++++- .../EmailConfigHostTelemetryProviderTest.java | 30 ++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/server/sonar-server-common/build.gradle b/server/sonar-server-common/build.gradle index a2ca516fb12..fd8f433ded3 100644 --- a/server/sonar-server-common/build.gradle +++ b/server/sonar-server-common/build.gradle @@ -44,6 +44,7 @@ dependencies { testImplementation 'org.assertj:assertj-core' testImplementation 'org.hamcrest:hamcrest-core' testImplementation 'org.junit.jupiter:junit-jupiter-api' + testImplementation 'org.junit.jupiter:junit-jupiter-params' testImplementation 'org.mockito:mockito-core' testImplementation 'org.sonarsource.api.plugin:sonar-plugin-api-test-fixtures' testImplementation testFixtures(project(':server:sonar-db-dao')) diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java index 13be7e4d786..4486478fe04 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java @@ -19,8 +19,10 @@ */ package org.sonar.server.notification.email.telemetry; +import com.google.common.net.InternetDomainName; import java.util.Optional; +import org.apache.commons.lang3.StringUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.telemetry.core.Dimension; @@ -60,7 +62,25 @@ public class EmailConfigHostTelemetryProvider implements TelemetryDataProvider getValue() { try (DbSession dbSession = dbClient.openSession(false)) { - return dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST); + return dbClient.internalPropertiesDao() + .selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST) + .map(EmailConfigHostTelemetryProvider::extractDomain); + } + } + + private static String extractDomain(String host) { + if (StringUtils.isEmpty(host)) { + return "EMPTY_DOMAIN"; + } + try { + host = host.contains("://") ? StringUtils.substringBefore(StringUtils.substringAfter(host, "://"), "/") : StringUtils.substringBefore(host, "/"); + return InternetDomainName.from(host).topPrivateDomain().toString(); + } catch (RuntimeException e) { + return switch (StringUtils.substringBefore(e.getMessage(), ":")) { + case "Not a valid domain name" -> "NOT_VALID_DOMAIN_NAME"; + case "Not under a public suffix" -> "DOMAIN_NOT_UNDER_PUBLIC_SUFFIX"; + default -> "NOT_VALID_DOMAIN"; + }; } } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java index 90a3b51d997..1eb691373e2 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java @@ -21,6 +21,8 @@ package org.sonar.server.notification.email.telemetry; import java.util.Optional; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mockito; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -37,7 +39,7 @@ class EmailConfigHostTelemetryProviderTest { private final DbSession dbSession = mock(DbSession.class); @Test - void getValue_returnsTheRightEmailConfHost() { + void emailConfigHostTelemetryProvider_returnCorrectFields() { EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient); String host = "smtp.office365.com"; @@ -47,6 +49,30 @@ class EmailConfigHostTelemetryProviderTest { assertThat(emailConfigHostTelemetryProvider.getMetricKey()).isEqualTo("email_conf_host"); assertThat(emailConfigHostTelemetryProvider.getGranularity()).isEqualTo(Granularity.WEEKLY); assertThat(emailConfigHostTelemetryProvider.getDimension()).isEqualTo(Dimension.INSTALLATION); - assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of(host)); + assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of("office365.com")); + } + + @ParameterizedTest + @MethodSource("hostAndExpectedDomainValues") + void getValue_returnsTheCorrectlyExtractedTopLevelDomain(String host, String domain) { + EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient); + + when(dbClient.openSession(false)).thenReturn(dbSession); + when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST)).thenReturn(Optional.of(host)); + + assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of(domain)); + } + + private static Object[][] hostAndExpectedDomainValues() { + return new Object[][]{ + {"", "EMPTY_DOMAIN"}, + {"smtpasad.org.sdf", "DOMAIN_NOT_UNDER_PUBLIC_SUFFIX"}, + {"127.0.0.0", "NOT_VALID_DOMAIN_NAME"}, + {"smtp.office365.com", "office365.com"}, + {"outlook.office365.com/smthng/extra", "office365.com"}, + {"my.domain.org/", "domain.org"}, + {"http://smtp.gmail.com", "gmail.com"}, + {"https://smtp.office365.com/smthng/extra", "office365.com"} + }; } } -- cgit v1.2.3