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'))
*/
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;
@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";
+ };
}
}
}
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;
private final DbSession dbSession = mock(DbSession.class);
@Test
- void getValue_returnsTheRightEmailConfHost() {
+ void emailConfigHostTelemetryProvider_returnCorrectFields() {
EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient);
String host = "smtp.office365.com";
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"}
+ };
}
}