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.management;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.sonar.db.DbSession;
30 import static java.util.Collections.emptySet;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
33 import static org.assertj.core.api.Assertions.assertThatNoException;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35 import static org.mockito.ArgumentMatchers.anyString;
36 import static org.mockito.Mockito.doReturn;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.never;
39 import static org.mockito.Mockito.spy;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
42 import static org.mockito.Mockito.withSettings;
44 @RunWith(MockitoJUnitRunner.class)
45 public class DelegatingManagedServicesTest {
47 private static final DelegatingManagedServices NO_MANAGED_SERVICES = new DelegatingManagedServices(emptySet());
50 private DbSession dbSession;
53 public void getProviderName_whenNotManaged_shouldThrow() {
54 assertThatThrownBy(NO_MANAGED_SERVICES::getProviderName)
55 .isInstanceOf(IllegalStateException.class)
56 .hasMessage("This instance is not managed.");
60 public void getProviderName_whenManaged_shouldReturnName() {
61 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new AlwaysManagedInstanceService()));
63 assertThat(managedInstanceService.getProviderName()).isEqualTo("Always");
67 public void isInstanceExternallyManaged_whenNoManagedInstanceService_returnsFalse() {
68 assertThat(NO_MANAGED_SERVICES.isInstanceExternallyManaged()).isFalse();
72 public void isInstanceExternallyManaged_whenAllManagedInstanceServiceReturnsFalse_returnsFalse() {
73 Set<ManagedInstanceService> delegates = Set.of(new NeverManagedInstanceService(), new NeverManagedInstanceService());
74 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(delegates);
76 assertThat(managedInstanceService.isInstanceExternallyManaged()).isFalse();
80 public void isInstanceExternallyManaged_whenOneManagedInstanceServiceReturnsTrue_returnsTrue() {
81 Set<ManagedInstanceService> delegates = Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService());
82 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(delegates);
84 assertThat(managedInstanceService.isInstanceExternallyManaged()).isTrue();
88 public void getUserUuidToManaged_whenNoDelegates_setAllUsersAsNonManaged() {
89 Set<String> userUuids = Set.of("a", "b");
91 Map<String, Boolean> userUuidToManaged = NO_MANAGED_SERVICES.getUserUuidToManaged(dbSession, userUuids);
93 assertThat(userUuidToManaged).containsExactlyInAnyOrderEntriesOf(Map.of("a", false, "b", false));
97 public void getUserUuidToManaged_delegatesToRightService_andPropagateAnswer() {
98 Set<String> userUuids = Set.of("a", "b");
99 Map<String, Boolean> serviceResponse = Map.of("a", false, "b", true);
101 ManagedInstanceService anotherManagedInstanceService = getManagedInstanceService(userUuids, serviceResponse);
102 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), anotherManagedInstanceService));
104 Map<String, Boolean> userUuidToManaged = managedInstanceService.getUserUuidToManaged(dbSession, userUuids);
106 assertThat(userUuidToManaged).containsExactlyInAnyOrderEntriesOf(serviceResponse);
110 public void getGroupUuidToManaged_whenNoDelegates_setAllUsersAsNonManaged() {
111 Set<String> groupUuids = Set.of("a", "b");
112 DelegatingManagedServices managedInstanceService = NO_MANAGED_SERVICES;
114 Map<String, Boolean> groupUuidToManaged = managedInstanceService.getGroupUuidToManaged(dbSession, groupUuids);
116 assertThat(groupUuidToManaged).containsExactlyInAnyOrderEntriesOf(Map.of("a", false, "b", false));
120 public void isUserManaged_delegatesToRightService_andPropagateAnswer() {
121 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
123 assertThat(managedInstanceService.isUserManaged(dbSession, "whatever")).isTrue();
127 public void isUserManaged_whenNoDelegates_returnsFalse() {
128 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of());
130 assertThat(managedInstanceService.isUserManaged(dbSession, "whatever")).isFalse();
134 public void isGroupManaged_delegatesToRightService_andPropagateAnswer() {
135 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
137 assertThat(managedInstanceService.isGroupManaged(dbSession, "whatever")).isTrue();
141 public void isGroupManaged_whenNoDelegates_returnsFalse() {
142 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of());
144 assertThat(managedInstanceService.isGroupManaged(dbSession, "whatever")).isFalse();
148 public void getGroupUuidToManaged_delegatesToRightService_andPropagateAnswer() {
149 Set<String> groupUuids = Set.of("a", "b");
150 Map<String, Boolean> serviceResponse = Map.of("a", false, "b", true);
152 ManagedInstanceService anotherManagedInstanceService = getManagedInstanceService(groupUuids, serviceResponse);
153 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), anotherManagedInstanceService));
155 Map<String, Boolean> groupUuidToManaged = managedInstanceService.getGroupUuidToManaged(dbSession, groupUuids);
157 assertThat(groupUuidToManaged).containsExactlyInAnyOrderEntriesOf(serviceResponse);
161 public void getGroupUuidToManaged_ifMoreThanOneDelegatesActivated_throws() {
162 Set<ManagedInstanceService> managedInstanceServices = Set.of(new AlwaysManagedInstanceService(), new AlwaysManagedInstanceService());
163 DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
164 assertThatIllegalStateException()
165 .isThrownBy(() -> delegatingManagedServices.getGroupUuidToManaged(dbSession, Set.of("a")))
166 .withMessage("The instance can't be managed by more than one identity provider and 2 were found.");
170 public void getUserUuidToManaged_ifMoreThanOneDelegatesActivated_throws() {
171 Set<ManagedInstanceService> managedInstanceServices = Set.of(new AlwaysManagedInstanceService(), new AlwaysManagedInstanceService());
172 DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
173 assertThatIllegalStateException()
174 .isThrownBy(() -> delegatingManagedServices.getUserUuidToManaged(dbSession, Set.of("a")))
175 .withMessage("The instance can't be managed by more than one identity provider and 2 were found.");
179 public void getManagedUsersSqlFilter_whenNoDelegates_throws() {
180 Set<ManagedInstanceService> managedInstanceServices = emptySet();
181 DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
182 assertThatIllegalStateException()
183 .isThrownBy(() -> delegatingManagedServices.getManagedUsersSqlFilter(true))
184 .withMessage("This instance is not managed.");
188 public void getManagedUsersSqlFilter_delegatesToRightService_andPropagateAnswer() {
189 AlwaysManagedInstanceService alwaysManagedInstanceService = new AlwaysManagedInstanceService();
190 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), alwaysManagedInstanceService));
192 assertThat(managedInstanceService.getManagedUsersSqlFilter(true)).isNotNull().isEqualTo(alwaysManagedInstanceService.getManagedUsersSqlFilter(
197 public void getManagedGroupsSqlFilter_whenNoDelegates_throws() {
198 Set<ManagedInstanceService> managedInstanceServices = emptySet();
199 DelegatingManagedServices delegatingManagedServices = new DelegatingManagedServices(managedInstanceServices);
200 assertThatIllegalStateException()
201 .isThrownBy(() -> delegatingManagedServices.getManagedGroupsSqlFilter(true))
202 .withMessage("This instance is not managed.");
206 public void getManagedGroupsSqlFilter_delegatesToRightService_andPropagateAnswer() {
207 AlwaysManagedInstanceService alwaysManagedInstanceService = new AlwaysManagedInstanceService();
208 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), alwaysManagedInstanceService));
210 assertThat(managedInstanceService.getManagedGroupsSqlFilter(true)).isNotNull().isEqualTo(alwaysManagedInstanceService.getManagedGroupsSqlFilter(
214 private ManagedInstanceService getManagedInstanceService(Set<String> userUuids, Map<String, Boolean> uuidToManaged) {
215 ManagedInstanceService anotherManagedInstanceService = mock(ManagedInstanceService.class);
216 when(anotherManagedInstanceService.isInstanceExternallyManaged()).thenReturn(true);
217 when(anotherManagedInstanceService.getGroupUuidToManaged(dbSession, userUuids)).thenReturn(uuidToManaged);
218 when(anotherManagedInstanceService.getUserUuidToManaged(dbSession, userUuids)).thenReturn(uuidToManaged);
219 return anotherManagedInstanceService;
223 public void getProjectUuidToManaged_whenNoDelegates_setAllProjectsAsNonManaged() {
224 Set<String> projectUuids = Set.of("a", "b");
225 DelegatingManagedServices managedInstanceService = NO_MANAGED_SERVICES;
227 Map<String, Boolean> projectUuidToManaged = managedInstanceService.getProjectUuidToManaged(dbSession, projectUuids);
229 assertThat(projectUuidToManaged).containsExactlyInAnyOrderEntriesOf(Map.of("a", false, "b", false));
233 public void getProjectUuidToManaged_delegatesToRightService_andPropagateAnswer() {
234 Set<String> projectUuids = Set.of("a", "b");
235 Map<String, Boolean> serviceResponse = Map.of("a", false, "b", true);
237 ManagedInstanceService anotherManagedProjectService = getManagedProjectService(projectUuids, serviceResponse);
238 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), anotherManagedProjectService));
240 Map<String, Boolean> projectUuidToManaged = managedInstanceService.getProjectUuidToManaged(dbSession, projectUuids);
242 assertThat(projectUuidToManaged).containsExactlyInAnyOrderEntriesOf(serviceResponse);
245 private ManagedInstanceService getManagedProjectService(Set<String> projectUuids, Map<String, Boolean> uuidsToManaged) {
246 ManagedInstanceService anotherManagedProjectService = mock(ManagedInstanceService.class, withSettings().extraInterfaces(ManagedProjectService.class));
247 when(anotherManagedProjectService.isInstanceExternallyManaged()).thenReturn(true);
248 doReturn(uuidsToManaged).when((ManagedProjectService) anotherManagedProjectService).getProjectUuidToManaged(dbSession, projectUuids);
249 return anotherManagedProjectService;
253 public void isProjectManaged_whenManagedInstanceServices_shouldDelegatesToRightService() {
254 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
256 assertThat(managedInstanceService.isProjectManaged(dbSession, "whatever")).isTrue();
260 public void isProjectManaged_whenManagedNoInstanceServices_returnsFalse() {
261 assertThat(NO_MANAGED_SERVICES.isProjectManaged(dbSession, "whatever")).isFalse();
265 public void queuePermissionSyncTask_whenManagedNoInstanceServices_doesNotFail() {
266 assertThatNoException().isThrownBy(() -> NO_MANAGED_SERVICES.queuePermissionSyncTask("userUuid", "componentUuid", "projectUuid"));
270 public void queuePermissionSyncTask_whenManagedInstanceServices_shouldDelegatesToRightService() {
271 NeverManagedInstanceService neverManagedInstanceService = spy(new NeverManagedInstanceService());
272 AlwaysManagedInstanceService alwaysManagedInstanceService = spy(new AlwaysManagedInstanceService());
273 Set<ManagedInstanceService> delegates = Set.of(neverManagedInstanceService, alwaysManagedInstanceService);
274 DelegatingManagedServices managedInstanceService = new DelegatingManagedServices(delegates);
276 managedInstanceService.queuePermissionSyncTask("userUuid", "componentUuid", "projectUuid");
277 verify(neverManagedInstanceService, never()).queuePermissionSyncTask(anyString(), anyString(), anyString());
278 verify(alwaysManagedInstanceService).queuePermissionSyncTask("userUuid", "componentUuid", "projectUuid");
281 private static class NeverManagedInstanceService implements ManagedInstanceService, ManagedProjectService {
284 public boolean isInstanceExternallyManaged() {
289 public String getProviderName() {
294 public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
299 public Map<String, Boolean> getGroupUuidToManaged(DbSession dbSession, Set<String> groupUuids) {
304 public String getManagedUsersSqlFilter(boolean filterByManaged) {
309 public String getManagedGroupsSqlFilter(boolean filterByManaged) {
314 public boolean isUserManaged(DbSession dbSession, String userUuid) {
319 public boolean isGroupManaged(DbSession dbSession, String groupUuid) {
324 public Map<String, Boolean> getProjectUuidToManaged(DbSession dbSession, Set<String> projectUuids) {
329 public boolean isProjectManaged(DbSession dbSession, String projectUuid) {
334 public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {
339 private static class AlwaysManagedInstanceService implements ManagedInstanceService, ManagedProjectService {
342 public boolean isInstanceExternallyManaged() {
347 public String getProviderName() {
352 public Map<String, Boolean> getUserUuidToManaged(DbSession dbSession, Set<String> userUuids) {
357 public Map<String, Boolean> getGroupUuidToManaged(DbSession dbSession, Set<String> groupUuids) {
362 public String getManagedUsersSqlFilter(boolean filterByManaged) {
367 public String getManagedGroupsSqlFilter(boolean filterByManaged) {
372 public boolean isUserManaged(DbSession dbSession, String userUuid) {
377 public boolean isGroupManaged(DbSession dbSession, String groupUuid) {
382 public Map<String, Boolean> getProjectUuidToManaged(DbSession dbSession, Set<String> projectUuids) {
387 public boolean isProjectManaged(DbSession dbSession, String projectUuid) {
392 public void queuePermissionSyncTask(String submitterUuid, String componentUuid, String projectUuid) {