]> source.dussan.org Git - sonarqube.git/blob
f96a624f8608263eab19a270ad7d6e8b879a04f8
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info 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 com.sonar.server.common.management;
21
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;
35
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;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class ManagedInstanceCheckerTest {
43
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.";
46
47   @Mock
48   private DbSession dbSession;
49   @Mock
50   private ManagedInstanceService managedInstanceService;
51   @Mock
52   private ManagedProjectService managedProjectService;
53   @InjectMocks
54   private ManagedInstanceChecker managedInstanceChecker;
55
56   @Test
57   public void throwIfInstanceIsManaged_whenInstanceExternallyManaged_shouldThrow() {
58     when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(true);
59
60     assertThatThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged())
61       .isInstanceOf(BadRequestException.class)
62       .hasMessage(INSTANCE_EXCEPTION_MESSAGE);
63   }
64
65   @Test
66   public void throwIfInstanceIsManaged_whenInstanceNotExternallyManaged_shouldNotThrow() {
67     when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(false);
68
69     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged());
70   }
71
72   @Test
73   public void throwIfProjectIsManaged_whenProjectIsManaged_shouldThrow() {
74     ProjectDto projectDto = mockManagedProject();
75
76     String projectUuid = projectDto.getUuid();
77     assertThatThrownBy(() -> managedInstanceChecker.throwIfProjectIsManaged(dbSession, projectUuid))
78       .isInstanceOf(BadRequestException.class)
79       .hasMessage(PROJECT_EXCEPTION_MESSAGE);
80   }
81
82   @Test
83   public void throwIfProjectIsManaged_whenProjectIsNotManaged_shouldNotThrow() {
84     ProjectDto projectDto = mockNotManagedProject();
85
86     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfProjectIsManaged(dbSession, projectDto.getUuid()));
87   }
88
89   @Test
90   public void throwIfUserIsManaged_whenUserIsManaged_shouldThrow() {
91     UserDto userDto = mockManagedUser();
92
93     String userUuid = userDto.getUuid();
94     assertThatThrownBy(() -> managedInstanceChecker.throwIfUserIsManaged(dbSession, userUuid))
95       .isInstanceOf(BadRequestException.class)
96       .hasMessage(INSTANCE_EXCEPTION_MESSAGE);
97   }
98
99   @Test
100   public void throwIfUserIsManaged_whenUserIsNotManaged_shouldNotThrow() {
101     UserDto userDto = mockNotManagedUser();
102
103     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserIsManaged(dbSession, userDto.getUuid()));
104   }
105
106   @Test
107   public void throwIfUserAndProjectAreManaged_whenUserAndProjectAreManaged_shouldThrow() {
108     ProjectDto projectDto = mockManagedProject();
109     UserDto userDto = mockManagedUser();
110
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);
116   }
117
118   @Test
119   public void throwIfUserAndProjectAreManaged_whenOnlyUserIsManaged_shouldNotThrow() {
120     ProjectDto projectDto = mockNotManagedProject();
121     UserDto userDto = mockManagedUser();
122
123     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserAndProjectAreManaged(dbSession, userDto.getUuid(), projectDto.getUuid()));
124   }
125
126   @Test
127   public void throwIfUserAndProjectAreManaged_whenOnlyProjectIsManaged_shouldNotThrow() {
128     ProjectDto projectDto = mockManagedProject();
129     UserDto userDto = mockNotManagedUser();
130
131     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserAndProjectAreManaged(dbSession, userDto.getUuid(), projectDto.getUuid()));
132   }
133
134   @Test
135   public void throwIfUserAndProjectAreManaged_whenNothingIsManaged_shouldNotThrow() {
136     ProjectDto projectDto = mockNotManagedProject();
137     UserDto userDto = mockNotManagedUser();
138
139     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfUserAndProjectAreManaged(dbSession, userDto.getUuid(), projectDto.getUuid()));
140   }
141
142   @Test
143   public void throwIfGroupAndProjectAreManaged_whenGroupAndProjectAreManaged_shouldThrow() {
144     ProjectDto projectDto = mockManagedProject();
145     GroupDto groupDto = mockManagedGroup();
146
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);
152   }
153
154   @Test
155   public void throwIfGroupAndProjectAreManaged_whenOnlyGroupIsManaged_shouldNotThrow() {
156     ProjectDto projectDto = mockNotManagedProject();
157     GroupDto groupDto = mockManagedGroup();
158
159     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDto.getUuid(), projectDto.getUuid()));
160   }
161
162   @Test
163   public void throwIfGroupAndProjectAreManaged_whenOnlyProjectIsManaged_shouldNotThrow() {
164     ProjectDto projectDto = mockManagedProject();
165     GroupDto groupDto = mockNotManagedGroup();
166
167     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDto.getUuid(), projectDto.getUuid()));
168   }
169
170   @Test
171   public void throwIfGroupAndProjectAreManaged_whenNothingIsManaged_shouldNotThrow() {
172     ProjectDto projectDto = mockNotManagedProject();
173     GroupDto groupDto = mockNotManagedGroup();
174
175     assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDto.getUuid(), projectDto.getUuid()));
176   }
177
178   private ProjectDto mockManagedProject() {
179     return mockProject(true);
180   }
181
182   private ProjectDto mockNotManagedProject() {
183     return mockProject(false);
184   }
185
186   private ProjectDto mockProject(boolean isManaged) {
187     ProjectDto projectDto = mock(ProjectDto.class);
188     when(managedProjectService.isProjectManaged(dbSession, projectDto.getUuid())).thenReturn(isManaged);
189     return projectDto;
190   }
191
192   private UserDto mockManagedUser() {
193     return mockUser(true);
194   }
195
196   private UserDto mockNotManagedUser() {
197     return mockUser(false);
198   }
199
200   private UserDto mockUser(boolean isManaged) {
201     UserDto userDto = mock(UserDto.class);
202     when(managedInstanceService.isUserManaged(dbSession, userDto.getUuid())).thenReturn(isManaged);
203     return userDto;
204   }
205
206   private GroupDto mockManagedGroup() {
207     return mockGroup(true);
208   }
209
210   private GroupDto mockNotManagedGroup() {
211     return mockGroup(false);
212   }
213
214   private GroupDto mockGroup(boolean isManaged) {
215     GroupDto groupDto = mock(GroupDto.class);
216     when(managedInstanceService.isGroupManaged(dbSession, groupDto.getUuid())).thenReturn(isManaged);
217     return groupDto;
218   }
219
220 }