]> source.dussan.org Git - sonarqube.git/blob
de8301e91da924ec565613543e7f123891c3977b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.computation.task.projectanalysis.issue;
21
22 import java.util.Collections;
23 import org.junit.Test;
24 import org.sonar.api.config.Settings;
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;
31
32 import static java.util.Arrays.asList;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.junit.Assert.fail;
35
36 public class ScmAccountToUserLoaderTest {
37
38   @org.junit.Rule
39   public EsTester esTester = new EsTester(new UserIndexDefinition(new Settings()));
40
41   @org.junit.Rule
42   public LogTester logTester = new LogTester();
43
44   @Test
45   public void load_login_for_scm_account() throws Exception {
46     UserDoc user = new UserDoc()
47       .setLogin("charlie")
48       .setName("Charlie")
49       .setEmail("charlie@hebdo.com")
50       .setActive(true)
51       .setScmAccounts(asList("charlie", "jesuis@charlie.com"));
52     esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, user);
53
54     UserIndex index = new UserIndex(esTester.client());
55     ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
56
57     assertThat(underTest.load("missing")).isNull();
58     assertThat(underTest.load("jesuis@charlie.com")).isEqualTo("charlie");
59   }
60
61   @Test
62   public void warn_if_multiple_users_share_the_same_scm_account() throws Exception {
63     UserDoc user1 = new UserDoc()
64       .setLogin("charlie")
65       .setName("Charlie")
66       .setEmail("charlie@hebdo.com")
67       .setActive(true)
68       .setScmAccounts(asList("charlie", "jesuis@charlie.com"));
69     esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, user1);
70
71     UserDoc user2 = new UserDoc()
72       .setLogin("another.charlie")
73       .setName("Another Charlie")
74       .setActive(true)
75       .setScmAccounts(asList("charlie"));
76     esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, user2);
77
78     UserIndex index = new UserIndex(esTester.client());
79     ScmAccountToUserLoader underTest = new ScmAccountToUserLoader(index);
80
81     assertThat(underTest.load("charlie")).isNull();
82     assertThat(logTester.logs(LoggerLevel.WARN)).contains("Multiple users share the SCM account 'charlie': another.charlie, charlie");
83   }
84
85   @Test
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);
89     try {
90       underTest.loadAll(Collections.<String>emptyList());
91       fail();
92     } catch (UnsupportedOperationException ignored) {
93     }
94   }
95 }