]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22641 fix values for telemetry smtp config when Optional.empty() returned
authorBogdana <bogdana.kushnir@sonarsource.com>
Thu, 29 Aug 2024 15:53:16 +0000 (17:53 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 2 Sep 2024 20:02:50 +0000 (20:02 +0000)
server/sonar-server-common/src/main/java/org/sonar/server/notification/email/telemetry/EmailConfigAuthMethodTelemetryProvider.java
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/EmailConfigAuthMethodTelemetryProviderTest.java
server/sonar-server-common/src/test/java/org/sonar/server/notification/email/telemetry/EmailConfigHostTelemetryProviderTest.java

index c360e9eef1ee733ba593933187372c265927c368..4ed588eb9b510a3864fe23c5ffd5500451f57e4d 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.notification.email.telemetry;
 
 import java.util.Optional;
-
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.telemetry.core.Dimension;
@@ -60,7 +59,7 @@ public class EmailConfigAuthMethodTelemetryProvider implements TelemetryDataProv
   @Override
   public Optional<String> getValue() {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      return dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_AUTH_METHOD);
+      return Optional.of(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_AUTH_METHOD).orElse("NOT_SET"));
     }
   }
 }
index 4486478fe0455151e267a130455b0b8a98d7b98f..e2fe55be6af03ea77d6948a31134ccd782c107d0 100644 (file)
@@ -21,7 +21,6 @@ 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;
@@ -50,21 +49,22 @@ public class EmailConfigHostTelemetryProvider implements TelemetryDataProvider<S
   }
 
   @Override
-  public Granularity getGranularity() {
-    return Granularity.WEEKLY;
+  public TelemetryDataType getType() {
+    return TelemetryDataType.STRING;
   }
 
   @Override
-  public TelemetryDataType getType() {
-    return TelemetryDataType.STRING;
+  public Granularity getGranularity() {
+    return Granularity.WEEKLY;
   }
 
   @Override
   public Optional<String> getValue() {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      return dbClient.internalPropertiesDao()
+      return Optional.of(dbClient.internalPropertiesDao()
         .selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST)
-        .map(EmailConfigHostTelemetryProvider::extractDomain);
+        .map(EmailConfigHostTelemetryProvider::extractDomain)
+        .orElse("NOT_SET"));
     }
   }
 
index 6efc28365e0682def77323bb2316b094c511839f..3e0d1b00376bc8ca7fb6d6c73b7e1410a2afe801 100644 (file)
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_AUTH_METHOD;
 import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_AUTH_METHOD_DEFAULT;
+
 import org.sonar.telemetry.core.Dimension;
 import org.sonar.telemetry.core.Granularity;
 
@@ -49,4 +50,14 @@ class EmailConfigAuthMethodTelemetryProviderTest {
     assertThat(emailConfigAuthMethodTelemetryProvider.getDimension()).isEqualTo(Dimension.INSTALLATION);
     assertThat(emailConfigAuthMethodTelemetryProvider.getValue()).isEqualTo(Optional.of("BASIC"));
   }
+
+  @Test
+  void getValue_returnsNotSetWhenEmpty() {
+    EmailConfigAuthMethodTelemetryProvider emailConfigAuthMethodTelemetryProvider = new EmailConfigAuthMethodTelemetryProvider(dbClient);
+
+    when(dbClient.openSession(false)).thenReturn(dbSession);
+    when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_AUTH_METHOD)).thenReturn(Optional.empty());
+
+    assertThat(emailConfigAuthMethodTelemetryProvider.getValue()).isEqualTo(Optional.of("NOT_SET"));
+  }
 }
index 1eb691373e20fa32ca43ccfcf9aa67d63acf4e42..c0af9a1a6d809ad39ff4dc0926e1b1dccfd653d7 100644 (file)
@@ -63,8 +63,18 @@ class EmailConfigHostTelemetryProviderTest {
     assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of(domain));
   }
 
+  @Test
+  void getValue_returnsNotSetWhenEmpty() {
+    EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient);
+
+    when(dbClient.openSession(false)).thenReturn(dbSession);
+    when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST)).thenReturn(Optional.empty());
+
+    assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of("NOT_SET"));
+  }
+
   private static Object[][] hostAndExpectedDomainValues() {
-    return new Object[][]{
+    return new Object[][] {
       {"", "EMPTY_DOMAIN"},
       {"smtpasad.org.sdf", "DOMAIN_NOT_UNDER_PUBLIC_SUFFIX"},
       {"127.0.0.0", "NOT_VALID_DOMAIN_NAME"},