]> source.dussan.org Git - sonarqube.git/blob
1eb691373e20fa32ca43ccfcf9aa67d63acf4e42
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.notification.email.telemetry;
21
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;
31
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;
36
37 class EmailConfigHostTelemetryProviderTest {
38   DbClient dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS);
39   private final DbSession dbSession = mock(DbSession.class);
40
41   @Test
42   void emailConfigHostTelemetryProvider_returnCorrectFields() {
43     EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient);
44
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));
48
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"));
53   }
54
55   @ParameterizedTest
56   @MethodSource("hostAndExpectedDomainValues")
57   void getValue_returnsTheCorrectlyExtractedTopLevelDomain(String host, String domain) {
58     EmailConfigHostTelemetryProvider emailConfigHostTelemetryProvider = new EmailConfigHostTelemetryProvider(dbClient);
59
60     when(dbClient.openSession(false)).thenReturn(dbSession);
61     when(dbClient.internalPropertiesDao().selectByKey(dbSession, EMAIL_CONFIG_SMTP_HOST)).thenReturn(Optional.of(host));
62
63     assertThat(emailConfigHostTelemetryProvider.getValue()).isEqualTo(Optional.of(domain));
64   }
65
66   private static Object[][] hostAndExpectedDomainValues() {
67     return new Object[][]{
68       {"", "EMPTY_DOMAIN"},
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"}
76     };
77   }
78 }