3 * Copyright (C) 2009-2023 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.v2.api.user.converter;
22 import java.util.List;
23 import java.util.Optional;
24 import org.jetbrains.annotations.Nullable;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.sonar.api.utils.DateUtils;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.common.PaginationInformation;
33 import org.sonar.server.common.user.service.UserInformation;
34 import org.sonar.server.user.UserSession;
35 import org.sonar.server.v2.api.response.PageRestResponse;
36 import org.sonar.server.v2.api.user.model.RestUserForAdmins;
37 import org.sonar.server.v2.api.user.model.RestUserForAnonymousUsers;
38 import org.sonar.server.v2.api.user.model.RestUserForLoggedInUsers;
39 import org.sonar.server.v2.api.user.response.UsersSearchRestResponse;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.server.common.PaginationInformation.forPageIndex;
47 @RunWith(MockitoJUnitRunner.class)
48 public class UsersSearchRestResponseGeneratorTest {
51 private UserSession userSession;
54 private UsersSearchRestResponseGenerator usersSearchRestResponseGenerator;
57 public void toUsersForResponse_whenNoResults_mapsCorrectly() {
58 PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
60 UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(), paging);
62 assertThat(usersForResponse.users()).isEmpty();
63 assertPaginationInformationAreCorrect(paging, usersForResponse.page());
67 public void toUsersForResponse_whenAdmin_mapsAllFields() {
68 when(userSession.isLoggedIn()).thenReturn(true);
69 when(userSession.isSystemAdministrator()).thenReturn(true);
71 PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
73 UserInformation userInformation1 = mockSearchResult(1, true);
74 UserInformation userInformation2 = mockSearchResult(2, false);
76 UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(userInformation1, userInformation2), paging);
78 RestUserForAdmins expectUser1 = buildExpectedResponseForAdmin(userInformation1);
79 RestUserForAdmins expectUser2 = buildExpectedResponseForAdmin(userInformation2);
80 assertThat(usersForResponse.users()).containsExactly(expectUser1, expectUser2);
81 assertPaginationInformationAreCorrect(paging, usersForResponse.page());
84 private static RestUserForAdmins buildExpectedResponseForAdmin(UserInformation userInformation) {
85 UserDto userDto = userInformation.userDto();
86 return new RestUserForAdmins(
93 userInformation.managed(),
94 userDto.getExternalLogin(),
95 userDto.getExternalIdentityProvider(),
96 userInformation.avatar().orElse(null),
97 toDateTime(userDto.getLastConnectionDate()),
98 toDateTime(userDto.getLastSonarlintConnectionDate()),
99 userInformation.userDto().getSortedScmAccounts()
104 public void toUsersForResponse_whenNonAdmin_mapsNonAdminFields() {
105 when(userSession.isLoggedIn()).thenReturn(true);
107 PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
109 UserInformation userInformation1 = mockSearchResult(1, true);
110 UserInformation userInformation2 = mockSearchResult(2, false);
112 UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(userInformation1, userInformation2), paging);
114 RestUserForLoggedInUsers expectUser1 = buildExpectedResponseForUser(userInformation1);
115 RestUserForLoggedInUsers expectUser2 = buildExpectedResponseForUser(userInformation2);
116 assertThat(usersForResponse.users()).containsExactly(expectUser1, expectUser2);
117 assertPaginationInformationAreCorrect(paging, usersForResponse.page());
120 private static RestUserForLoggedInUsers buildExpectedResponseForUser(UserInformation userInformation) {
121 UserDto userDto = userInformation.userDto();
122 return new RestUserForLoggedInUsers(
129 userDto.getExternalIdentityProvider(),
130 userInformation.avatar().orElse(null)
135 public void toUsersForResponse_whenAnonymous_returnsOnlyNameAndLogin() {
136 PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
138 UserInformation userInformation1 = mockSearchResult(1, true);
139 UserInformation userInformation2 = mockSearchResult(2, false);
141 UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(userInformation1, userInformation2), paging);
143 RestUserForAnonymousUsers expectUser1 = buildExpectedResponseForAnonymous(userInformation1);
144 RestUserForAnonymousUsers expectUser2 = buildExpectedResponseForAnonymous(userInformation2);
145 assertThat(usersForResponse.users()).containsExactly(expectUser1, expectUser2);
146 assertPaginationInformationAreCorrect(paging, usersForResponse.page());
149 private static RestUserForAnonymousUsers buildExpectedResponseForAnonymous(UserInformation userInformation) {
150 UserDto userDto = userInformation.userDto();
151 return new RestUserForAnonymousUsers(
158 private static String toDateTime(@Nullable Long dateTimeMs) {
159 return Optional.ofNullable(dateTimeMs).map(DateUtils::formatDateTime).orElse(null);
162 private static UserInformation mockSearchResult(int i, boolean booleanFlagsValue) {
163 UserInformation userInformation = mock(UserInformation.class, RETURNS_DEEP_STUBS);
164 UserDto user1 = new UserDto()
165 .setUuid("uuid_" + i)
166 .setLogin("login_" + i)
167 .setName("name_" + i)
168 .setEmail("email@" + i)
169 .setExternalId("externalId" + i)
170 .setExternalLogin("externalLogin" + 1)
171 .setExternalIdentityProvider("exernalIdp_" + i)
172 .setLastConnectionDate(100L + i)
173 .setLastSonarlintConnectionDate(200L + i)
174 .setLocal(booleanFlagsValue)
175 .setActive(booleanFlagsValue);
177 when(userInformation.userDto()).thenReturn(user1);
178 when(userInformation.managed()).thenReturn(booleanFlagsValue);
179 return userInformation;
182 private static void assertPaginationInformationAreCorrect(PaginationInformation paginationInformation, PageRestResponse pageRestResponse) {
183 assertThat(pageRestResponse.pageIndex()).isEqualTo(paginationInformation.pageIndex());
184 assertThat(pageRestResponse.pageSize()).isEqualTo(paginationInformation.pageSize());
185 assertThat(pageRestResponse.total()).isEqualTo(paginationInformation.total());