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.telemetry;
23 import org.junit.Rule;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
27 import org.sonar.api.utils.System2;
28 import org.sonar.db.DbTester;
30 import static org.assertj.core.api.Assertions.assertThat;
32 class TelemetryUserEnabledProviderIT {
34 private final System2 system2 = new AlwaysIncreasingSystem2();
37 public final DbTester db = DbTester.create(system2);
40 private final TelemetryUserEnabledProvider underTest = new TelemetryUserEnabledProvider(db.getDbClient());
43 public void beforeEach() {
44 db.executeUpdateSql("delete from users");
48 void getUuidValues_whenNoUsersInDatabase_shouldReturnEmptyMap() {
49 Map<String, Boolean> uuidValues = underTest.getUuidValues();
51 assertThat(uuidValues).isEmpty();
55 void getUuidValues_whenSomeUsersActive_shouldReturnBothBooleanValues() {
56 db.users().insertUser(user -> user.setUuid("uuid1").setActive(true));
57 db.users().insertUser(user -> user.setUuid("uuid1").setActive(false));
58 db.getSession().commit();
60 Map<String, Boolean> uuidValues = underTest.getUuidValues();
62 assertThat(uuidValues).hasSize(2);
63 assertThat(uuidValues.values().stream().filter(Boolean::booleanValue)).hasSize(1);
64 assertThat(uuidValues.values().stream().filter(b -> !b)).hasSize(1);
68 void getUuidValues_when10ActiveUsers_shouldReturn10BooleanValues() {
69 for (int i = 0; i < 10; i++) {
70 db.users().insertUser(user -> user.setActive(true));
72 db.getSession().commit();
74 Map<String, Boolean> uuidValues = underTest.getUuidValues();
76 assertThat(uuidValues).hasSize(10);
77 assertThat(uuidValues.values().stream().filter(Boolean::booleanValue)).hasSize(10);