3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.notification.email.telemetry;
22 import java.util.Optional;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.params.ParameterizedTest;
25 import org.junit.jupiter.params.provider.MethodSource;
26 import org.mockito.Mockito;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.telemetry.core.Dimension;
30 import org.sonar.telemetry.core.Granularity;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35 import static org.sonar.server.email.EmailSmtpConfiguration.EMAIL_CONFIG_SMTP_HOST;
37 class EmailConfigHostTelemetryProviderTest {
38 DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
39 private final DbSession dbSession = mock(DbSession.class);
42 void emailConfigHostTelemetryProvider_returnCorrectFields() {
43 EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient);
45 String host = "smtp.office365.com";
46 when(dbClient.openSession(false)).thenReturn(dbSession);
47 when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST)).thenReturn(Optional.of(host));
49 assertThat(emailConfigHostTelemetryProvider.getMetricKey()).isEqualTo("email_conf_host");
50 assertThat(emailConfigHostTelemetryProvider.getGranularity()).isEqualTo(Granularity.WEEKLY);
51 assertThat(emailConfigHostTelemetryProvider.getDimension()).isEqualTo(Dimension.INSTALLATION);
52 assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of("office365.com"));
56 @MethodSource("hostAndExpectedDomainValues")
57 void getValue_returnsTheCorrectlyExtractedTopLevelDomain(String host, String domain) {
58 EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient);
60 when(dbClient.openSession(false)).thenReturn(dbSession);
61 when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST)).thenReturn(Optional.of(host));
63 assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of(domain));
66 private static Object[][] hostAndExpectedDomainValues() {
67 return new Object[][]{
69 {"smtpasad.org.sdf", "DOMAIN_NOT_UNDER_PUBLIC_SUFFIX"},
70 {"127.0.0.0", "NOT_VALID_DOMAIN_NAME"},
71 {"smtp.office365.com", "office365.com"},
72 {"outlook.office365.com/smthng/extra", "office365.com"},
73 {"my.domain.org/", "domain.org"},
74 {"http://smtp.gmail.com", "gmail.com"},
75 {"https://smtp.office365.com/smthng/extra", "office365.com"}