]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22641 update info for email config host telemetry
authorBogdana <bogdana.kushnir@sonarsource.com>
Wed, 14 Aug 2024 10:14:51 +0000 (12:14 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 16 Aug 2024 20:02:59 +0000 (20:02 +0000)
server/sonar-server-common/build.gradle
server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProvider.java
server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java

index a2ca516fb12bd441857ba1616dcd8b1c0c5b065b..fd8f433ded3d0b0cc448e2dffac0594c5eb302bb 100644 (file)
@@ -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'))
index 13be7e4d786a03a13d06f0ad0395feabec27e2cd..4486478fe0455151e267a130455b0b8a98d7b98f 100644 (file)
  */
 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<S
   @Override
   public Optional<String> 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";
+      };
     }
   }
 }
index 90a3b51d99778b0683b54dd3616a050d84a2cd92..1eb691373e20fa32ca43ccfcf9aa67d63acf4e42 100644 (file)
@@ -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"}
+    };
   }
 }