3 * Copyright (C) 2009-2017 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.computation.task.projectanalysis.issue;
22 import java.util.Collections;
23 import org.junit.Test;
24 import org.sonar.api.config.internal.MapSettings;
25 import org.sonar.api.utils.log.LogTester;
26 import org.sonar.api.utils.log.LoggerLevel;
27 import org.sonar.server.es.EsTester;
28 import org.sonar.server.user.index.UserDoc;
29 import org.sonar.server.user.index.UserIndex;
30 import org.sonar.server.user.index.UserIndexDefinition;
32 import static java.util.Arrays.asList;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.junit.Assert.fail;
36 public class ScmAccountToUserLoaderTest {
39 public EsTester esTester = new EsTester(new UserIndexDefinition(new MapSettings().asConfig()));
42 public LogTester logTester = new LogTester();
45 public void load_login_for_scm_account() throws Exception {
46 UserDoc user = new UserDoc()
49 .setEmail("charlie@hebdo.com")
51 .setScmAccounts(asList("charlie", "jesuis@charlie.com"));
52 esTester.putDocuments(UserIndexDefinition.INDEX_TYPE_USER.getIndex(), UserIndexDefinition.INDEX_TYPE_USER.getType(), user);
54 UserIndex index = new UserIndex(esTester.client());
55 ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
57 assertThat(underTest.load("missing")).isNull();
58 assertThat(underTest.load("jesuis@charlie.com")).isEqualTo("charlie");
62 public void warn_if_multiple_users_share_the_same_scm_account() throws Exception {
63 UserDoc user1 = new UserDoc()
66 .setEmail("charlie@hebdo.com")
68 .setScmAccounts(asList("charlie", "jesuis@charlie.com"));
69 esTester.putDocuments(UserIndexDefinition.INDEX_TYPE_USER.getIndex(), UserIndexDefinition.INDEX_TYPE_USER.getType(), user1);
71 UserDoc user2 = new UserDoc()
72 .setLogin("another.charlie")
73 .setName("Another Charlie")
75 .setScmAccounts(asList("charlie"));
76 esTester.putDocuments(UserIndexDefinition.INDEX_TYPE_USER.getIndex(), UserIndexDefinition.INDEX_TYPE_USER.getType(), user2);
78 UserIndex index = new UserIndex(esTester.client());
79 ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
81 assertThat(underTest.load("charlie")).isNull();
82 assertThat(logTester.logs(LoggerLevel.WARN)).contains("Multiple users share the SCM account 'charlie': another.charlie, charlie");
86 public void load_by_multiple_scm_accounts_is_not_supported_yet() {
87 UserIndex index = new UserIndex(esTester.client());
88 ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
90 underTest.loadAll(Collections.<String>emptyList());
92 } catch (UnsupportedOperationException ignored) {