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.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.utils.System2;
25 import org.sonar.api.utils.log.LogTester;
26 import org.sonar.api.utils.log.LoggerLevel;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.es.EsTester;
30 import org.sonar.server.user.index.UserIndex;
31 import org.sonar.server.user.index.UserIndexer;
33 import static java.util.Arrays.asList;
34 import static java.util.Collections.emptyList;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.junit.Assert.fail;
38 public class ScmAccountToUserLoaderTest {
40 private static final String ORGANIZATION_UUID = "my-organization";
43 public DbTester db = DbTester.create();
45 public EsTester es = EsTester.create();
47 public LogTester logTester = new LogTester();
49 private UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client());
52 public void load_login_for_scm_account() {
53 UserDto user = db.users().insertUser(u -> u.setScmAccounts(asList("charlie", "jesuis@charlie.com")));
54 userIndexer.indexOnStartup(null);
56 UserIndex index = new UserIndex(es.client(), System2.INSTANCE);
57 ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
59 assertThat(underTest.load("missing")).isNull();
60 assertThat(underTest.load("jesuis@charlie.com")).isEqualTo(user.getUuid());
64 public void warn_if_multiple_users_share_the_same_scm_account() {
65 db.users().insertUser(u -> u.setLogin("charlie").setScmAccounts(asList("charlie", "jesuis@charlie.com")));
66 db.users().insertUser(u -> u.setLogin("another.charlie").setScmAccounts(asList("charlie")));
67 userIndexer.indexOnStartup(null);
69 UserIndex index = new UserIndex(es.client(), System2.INSTANCE);
70 ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
72 assertThat(underTest.load("charlie")).isNull();
73 assertThat(logTester.logs(LoggerLevel.WARN)).contains("Multiple users share the SCM account 'charlie': another.charlie, charlie");
77 public void load_by_multiple_scm_accounts_is_not_supported_yet() {
78 UserIndex index = new UserIndex(es.client(), System2.INSTANCE);
79 ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
81 underTest.loadAll(emptyList());
83 } catch (UnsupportedOperationException ignored) {