-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
UserIndexTest.java
152 lines (129 loc) · 6.68 KB
/
UserIndexTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.user.index;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.SearchOptions;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.server.user.index.UserIndexDefinition.INDEX;
import static org.sonar.server.user.index.UserIndexDefinition.TYPE_USER;
public class UserIndexTest {
private static final String USER1_LOGIN = "user1";
private static final String USER2_LOGIN = "user2";
private static final long DATE_1 = 1_500_000_000_000L;
private static final long DATE_2 = 1_500_000_000_001L;
@ClassRule
public static EsTester esTester = new EsTester().addDefinitions(new UserIndexDefinition(new Settings()));
private UserIndex index;
@Before
public void setUp() {
esTester.truncateIndices();
index = new UserIndex(esTester.client());
}
@Test
public void get_nullable_by_login() throws Exception {
UserDoc user1 = newUser(USER1_LOGIN, asList("scmA", "scmB"));
esTester.index(INDEX, TYPE_USER, USER1_LOGIN, user1.getFields());
esTester.index(INDEX, TYPE_USER, USER2_LOGIN, newUser(USER2_LOGIN, Collections.<String>emptyList()).getFields());
UserDoc userDoc = index.getNullableByLogin(USER1_LOGIN);
assertThat(userDoc).isNotNull();
assertThat(userDoc.login()).isEqualTo(user1.login());
assertThat(userDoc.name()).isEqualTo(user1.name());
assertThat(userDoc.email()).isEqualTo(user1.email());
assertThat(userDoc.active()).isTrue();
assertThat(userDoc.scmAccounts()).isEqualTo(user1.scmAccounts());
assertThat(userDoc.createdAt()).isEqualTo(user1.createdAt());
assertThat(userDoc.updatedAt()).isEqualTo(user1.updatedAt());
assertThat(index.getNullableByLogin("")).isNull();
assertThat(index.getNullableByLogin("unknown")).isNull();
}
@Test
public void get_nullable_by_login_should_be_case_sensitive() throws Exception {
UserDoc user1 = newUser(USER1_LOGIN, asList("scmA", "scmB"));
esTester.index(INDEX, TYPE_USER, USER1_LOGIN, user1.getFields());
assertThat(index.getNullableByLogin(USER1_LOGIN)).isNotNull();
assertThat(index.getNullableByLogin("UsEr1")).isNull();
}
@Test
public void getAtMostThreeActiveUsersForScmAccount() throws Exception {
UserDoc user1 = newUser("user1", asList("user_1", "u1"));
UserDoc user2 = newUser("user_with_same_email_as_user1", asList("user_2")).setEmail(user1.email());
UserDoc user3 = newUser("inactive_user_with_same_scm_as_user1", user1.scmAccounts()).setActive(false);
esTester.index(INDEX, TYPE_USER, user1.login(), user1.getFields());
esTester.index(INDEX, TYPE_USER, user2.login(), user2.getFields());
esTester.index(INDEX, TYPE_USER, user3.login(), user3.getFields());
assertThat(index.getAtMostThreeActiveUsersForScmAccount(user1.scmAccounts().get(0))).extractingResultOf("login").containsOnly(user1.login());
assertThat(index.getAtMostThreeActiveUsersForScmAccount(user1.login())).extractingResultOf("login").containsOnly(user1.login());
// both users share the same email
assertThat(index.getAtMostThreeActiveUsersForScmAccount(user1.email())).extractingResultOf("login").containsOnly(user1.login(), user2.login());
assertThat(index.getAtMostThreeActiveUsersForScmAccount("")).isEmpty();
assertThat(index.getAtMostThreeActiveUsersForScmAccount("unknown")).isEmpty();
}
@Test
public void getAtMostThreeActiveUsersForScmAccount_ignore_inactive_user() throws Exception {
String scmAccount = "scmA";
UserDoc user = newUser(USER1_LOGIN, asList(scmAccount)).setActive(false);
esTester.index(INDEX, TYPE_USER, user.login(), user.getFields());
assertThat(index.getAtMostThreeActiveUsersForScmAccount(user.login())).isEmpty();
assertThat(index.getAtMostThreeActiveUsersForScmAccount(scmAccount)).isEmpty();
}
@Test
public void getAtMostThreeActiveUsersForScmAccount_max_three() throws Exception {
String email = "user@mail.com";
UserDoc user1 = newUser("user1", Collections.<String>emptyList()).setEmail(email);
UserDoc user2 = newUser("user2", Collections.<String>emptyList()).setEmail(email);
UserDoc user3 = newUser("user3", Collections.<String>emptyList()).setEmail(email);
UserDoc user4 = newUser("user4", Collections.<String>emptyList()).setEmail(email);
esTester.index(INDEX, TYPE_USER, user1.login(), user1.getFields());
esTester.index(INDEX, TYPE_USER, user2.login(), user2.getFields());
esTester.index(INDEX, TYPE_USER, user3.login(), user3.getFields());
esTester.index(INDEX, TYPE_USER, user4.login(), user4.getFields());
// restrict results to 3 users
assertThat(index.getAtMostThreeActiveUsersForScmAccount(email)).hasSize(3);
}
@Test
public void searchUsers() throws Exception {
esTester.index(INDEX, TYPE_USER, USER1_LOGIN, newUser(USER1_LOGIN, Arrays.asList("user_1", "u1")).getFields());
esTester.index(INDEX, TYPE_USER, USER2_LOGIN, newUser(USER2_LOGIN, Collections.<String>emptyList()).getFields());
assertThat(index.search(null, new SearchOptions()).getDocs()).hasSize(2);
assertThat(index.search("user", new SearchOptions()).getDocs()).hasSize(2);
assertThat(index.search("ser", new SearchOptions()).getDocs()).hasSize(2);
assertThat(index.search(USER1_LOGIN, new SearchOptions()).getDocs()).hasSize(1);
assertThat(index.search(USER2_LOGIN, new SearchOptions()).getDocs()).hasSize(1);
}
private static UserDoc newUser(String login, List<String> scmAccounts) {
return new UserDoc()
.setLogin(login)
.setName(login.toUpperCase(Locale.ENGLISH))
.setEmail(login + "@mail.com")
.setActive(true)
.setScmAccounts(scmAccounts)
.setCreatedAt(DATE_1)
.setUpdatedAt(DATE_2);
}
}