3 * Copyright (C) 2009-2020 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.
21 package org.sonar.server.authentication.purge;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.ScheduledFuture;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.config.Configuration;
30 import org.sonar.api.config.internal.MapSettings;
31 import org.sonar.api.impl.utils.TestSystem2;
32 import org.sonar.api.utils.log.LogAndArguments;
33 import org.sonar.api.utils.log.LogTester;
34 import org.sonar.api.utils.log.LoggerLevel;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.user.SessionTokenDto;
37 import org.sonar.db.user.UserDto;
38 import org.sonar.server.util.AbstractStoppableExecutorService;
39 import org.sonar.server.util.GlobalLockManager;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.ArgumentMatchers.anyString;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
46 public class SessionTokensCleanerTest {
48 private static final long NOW = 1_000_000_000L;
50 private TestSystem2 system2 = new TestSystem2().setNow(NOW);
52 public DbTester db = DbTester.create(system2);
54 public LogTester logTester = new LogTester();
56 private GlobalLockManager lockManager = mock(GlobalLockManager.class);
58 private final MapSettings settings = new MapSettings();
59 private final Configuration configuration = settings.asConfig();
61 private SyncSessionTokensCleanerExecutorService executorService = new SyncSessionTokensCleanerExecutorService();
63 private SessionTokensCleaner underTest = new SessionTokensCleaner(executorService, db.getDbClient(), configuration, lockManager);
66 public void purge_expired_session_tokens() {
67 when(lockManager.tryLock(anyString())).thenReturn(true);
68 UserDto user = db.users().insertUser();
69 SessionTokenDto validSessionToken = db.users().insertSessionToken(user);
70 SessionTokenDto expiredSessionToken = db.users().insertSessionToken(user, st -> st.setExpirationDate(NOW - 1_000_000L));
73 executorService.runCommand();
75 assertThat(db.getDbClient().sessionTokensDao().selectByUuid(db.getSession(), validSessionToken.getUuid())).isPresent();
76 assertThat(db.getDbClient().sessionTokensDao().selectByUuid(db.getSession(), expiredSessionToken.getUuid())).isNotPresent();
77 assertThat(logTester.getLogs(LoggerLevel.INFO))
78 .extracting(LogAndArguments::getFormattedMsg)
79 .containsOnly("Purge of expired session tokens has removed 1 elements");
83 public void do_not_execute_purge_when_fail_to_get_lock() {
84 when(lockManager.tryLock(anyString())).thenReturn(false);
85 SessionTokenDto expiredSessionToken = db.users().insertSessionToken(db.users().insertUser(), st -> st.setExpirationDate(NOW - 1_000_000L));
88 executorService.runCommand();
90 assertThat(db.getDbClient().sessionTokensDao().selectByUuid(db.getSession(), expiredSessionToken.getUuid())).isPresent();
93 private static class SyncSessionTokensCleanerExecutorService extends AbstractStoppableExecutorService<ScheduledExecutorService> implements SessionTokensCleanerExecutorService {
95 private Runnable command;
97 public SyncSessionTokensCleanerExecutorService() {
101 public void runCommand() {
106 public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
107 this.command = command;
112 public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
117 public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
122 public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {