3 * Copyright (C) 2009-2024 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 com.sonar.server.common.management;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.project.ProjectDto;
29 import org.sonar.db.user.GroupDto;
30 import org.sonar.db.user.UserDto;
31 import org.sonar.server.common.management.ManagedInstanceChecker;
32 import org.sonar.server.exceptions.BadRequestException;
33 import org.sonar.server.management.ManagedInstanceService;
34 import org.sonar.server.management.ManagedProjectService;
36 import static org.assertj.core.api.Assertions.assertThatNoException;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
41 @RunWith(MockitoJUnitRunner.class)
42 public class ManagedInstanceCheckerTest {
44 private static final String INSTANCE_EXCEPTION_MESSAGE = "Operation not allowed when the instance is externally managed.";
45 private static final String PROJECT_EXCEPTION_MESSAGE = "Operation not allowed when the project is externally managed.";
48 private DbSession dbSession;
50 private ManagedInstanceService managedInstanceService;
52 private ManagedProjectService managedProjectService;
54 private ManagedInstanceChecker managedInstanceChecker;
57 public void throwIfInstanceIsManaged_whenInstanceExternallyManaged_shouldThrow() {
58 when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(true);
60 assertThatThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged())
61 .isInstanceOf(BadRequestException.class)
62 .hasMessage(INSTANCE_EXCEPTION_MESSAGE);
66 public void throwIfInstanceIsManaged_whenInstanceNotExternallyManaged_shouldNotThrow() {
67 when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(false);
69 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged());
73 public void throwIfProjectIsManaged_whenProjectIsManaged_shouldThrow() {
74 ProjectDto projectDto = mockManagedProject();
76 String projectUuid = projectDto.getUuid();
77 assertThatThrownBy(() -> managedInstanceChecker.throwIfProjectIsManaged(dbSession, projectUuid))
78 .isInstanceOf(BadRequestException.class)
79 .hasMessage(PROJECT_EXCEPTION_MESSAGE);
83 public void throwIfProjectIsManaged_whenProjectIsNotManaged_shouldNotThrow() {
84 ProjectDto projectDto = mockNotManagedProject();
86 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfProjectIsManaged(dbSession, projectDto.getUuid()));
90 public void throwIfUserIsManaged_whenUserIsManaged_shouldThrow() {
91 UserDto userDto = mockManagedUser();
93 String userUuid = userDto.getUuid();
94 assertThatThrownBy(() -> managedInstanceChecker.throwIfUserIsManaged(dbSession, userUuid))
95 .isInstanceOf(BadRequestException.class)
96 .hasMessage(INSTANCE_EXCEPTION_MESSAGE);
100 public void throwIfUserIsManaged_whenUserIsNotManaged_shouldNotThrow() {
101 UserDto userDto = mockNotManagedUser();
103 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserIsManaged(dbSession, userDto.getUuid()));
107 public void throwIfUserAndProjectAreManaged_whenUserAndProjectAreManaged_shouldThrow() {
108 ProjectDto projectDto = mockManagedProject();
109 UserDto userDto = mockManagedUser();
111 String userUuid = userDto.getUuid();
112 String projectUuid = projectDto.getUuid();
113 assertThatThrownBy(() -> managedInstanceChecker.throwIfUserAndProjectAreManaged(dbSession, userUuid, projectUuid))
114 .isInstanceOf(BadRequestException.class)
115 .hasMessage(PROJECT_EXCEPTION_MESSAGE);
119 public void throwIfUserAndProjectAreManaged_whenOnlyUserIsManaged_shouldNotThrow() {
120 ProjectDto projectDto = mockNotManagedProject();
121 UserDto userDto = mockManagedUser();
123 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserAndProjectAreManaged(dbSession, userDto.getUuid(), projectDto.getUuid()));
127 public void throwIfUserAndProjectAreManaged_whenOnlyProjectIsManaged_shouldNotThrow() {
128 ProjectDto projectDto = mockManagedProject();
129 UserDto userDto = mockNotManagedUser();
131 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserAndProjectAreManaged(dbSession, userDto.getUuid(), projectDto.getUuid()));
135 public void throwIfUserAndProjectAreManaged_whenNothingIsManaged_shouldNotThrow() {
136 ProjectDto projectDto = mockNotManagedProject();
137 UserDto userDto = mockNotManagedUser();
139 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserAndProjectAreManaged(dbSession, userDto.getUuid(), projectDto.getUuid()));
143 public void throwIfGroupAndProjectAreManaged_whenGroupAndProjectAreManaged_shouldThrow() {
144 ProjectDto projectDto = mockManagedProject();
145 GroupDto groupDto = mockManagedGroup();
147 String groupDtoUuid = groupDto.getUuid();
148 String projectDtoUuid = projectDto.getUuid();
149 assertThatThrownBy(() -> managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDtoUuid, projectDtoUuid))
150 .isInstanceOf(BadRequestException.class)
151 .hasMessage(PROJECT_EXCEPTION_MESSAGE);
155 public void throwIfGroupAndProjectAreManaged_whenOnlyGroupIsManaged_shouldNotThrow() {
156 ProjectDto projectDto = mockNotManagedProject();
157 GroupDto groupDto = mockManagedGroup();
159 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDto.getUuid(), projectDto.getUuid()));
163 public void throwIfGroupAndProjectAreManaged_whenOnlyProjectIsManaged_shouldNotThrow() {
164 ProjectDto projectDto = mockManagedProject();
165 GroupDto groupDto = mockNotManagedGroup();
167 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDto.getUuid(), projectDto.getUuid()));
171 public void throwIfGroupAndProjectAreManaged_whenNothingIsManaged_shouldNotThrow() {
172 ProjectDto projectDto = mockNotManagedProject();
173 GroupDto groupDto = mockNotManagedGroup();
175 assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDto.getUuid(), projectDto.getUuid()));
178 private ProjectDto mockManagedProject() {
179 return mockProject(true);
182 private ProjectDto mockNotManagedProject() {
183 return mockProject(false);
186 private ProjectDto mockProject(boolean isManaged) {
187 ProjectDto projectDto = mock(ProjectDto.class);
188 when(managedProjectService.isProjectManaged(dbSession, projectDto.getUuid())).thenReturn(isManaged);
192 private UserDto mockManagedUser() {
193 return mockUser(true);
196 private UserDto mockNotManagedUser() {
197 return mockUser(false);
200 private UserDto mockUser(boolean isManaged) {
201 UserDto userDto = mock(UserDto.class);
202 when(managedInstanceService.isUserManaged(dbSession, userDto.getUuid())).thenReturn(isManaged);
206 private GroupDto mockManagedGroup() {
207 return mockGroup(true);
210 private GroupDto mockNotManagedGroup() {
211 return mockGroup(false);
214 private GroupDto mockGroup(boolean isManaged) {
215 GroupDto groupDto = mock(GroupDto.class);
216 when(managedInstanceService.isGroupManaged(dbSession, groupDto.getUuid())).thenReturn(isManaged);