-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ComponentServiceTest.java
342 lines (278 loc) · 13.5 KB
/
ComponentServiceTest.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* 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.component;
import com.google.common.base.Optional;
import java.util.Arrays;
import java.util.Map;
import org.assertj.core.api.Fail;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDao;
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.component.ResourceIndexDao;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.i18n.I18nRule;
import org.sonar.server.tester.UserSessionRule;
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
public class ComponentServiceTest {
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
ComponentDbTester componentDb = new ComponentDbTester(dbTester);
DbClient dbClient = dbTester.getDbClient();
DbSession dbSession = dbTester.getSession();
I18nRule i18n = new I18nRule();
ComponentService underTest;
@Before
public void setUp() {
i18n.put("qualifier.TRK", "Project");
underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
}
@Test
public void get_by_key() {
ComponentDto project = insertSampleProject();
assertThat(underTest.getByKey(project.getKey())).isNotNull();
}
@Test
public void get_nullable_by_key() {
ComponentDto project = insertSampleProject();
assertThat(underTest.getNullableByKey(project.getKey())).isNotNull();
assertThat(underTest.getNullableByKey("unknown")).isNull();
}
@Test
public void get_by_uuid() {
ComponentDto project = insertSampleProject();
assertThat(underTest.getNonNullByUuid(project.uuid())).isNotNull();
}
@Test
public void get_nullable_by_uuid() {
ComponentDto project = insertSampleProject();
assertThat(underTest.getByUuid(project.uuid())).isPresent();
assertThat(underTest.getByUuid("unknown")).isAbsent();
}
@Test
public void check_module_keys_before_renaming() {
ComponentDto project = insertSampleProject();
ComponentDto module = ComponentTesting.newModuleDto(project).setKey("sample:root:module");
dbClient.componentDao().insert(dbSession, module);
ComponentDto file = newFileDto(module, null).setKey("sample:root:module:src/File.xoo");
dbClient.componentDao().insert(dbSession, file);
dbSession.commit();
userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
Map<String, String> result = underTest.checkModuleKeysBeforeRenaming(project.key(), "sample", "sample2");
assertThat(result).hasSize(2);
assertThat(result.get("sample:root")).isEqualTo("sample2:root");
assertThat(result.get("sample:root:module")).isEqualTo("sample2:root:module");
}
@Test
public void check_module_keys_before_renaming_return_duplicate_key() {
ComponentDto project = insertSampleProject();
ComponentDto module = ComponentTesting.newModuleDto(project).setKey("sample:root:module");
dbClient.componentDao().insert(dbSession, module);
ComponentDto module2 = ComponentTesting.newModuleDto(project).setKey("foo:module");
dbClient.componentDao().insert(dbSession, module2);
dbSession.commit();
userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
Map<String, String> result = underTest.checkModuleKeysBeforeRenaming(project.key(), "sample:root", "foo");
assertThat(result).hasSize(2);
assertThat(result.get("sample:root")).isEqualTo("foo");
assertThat(result.get("sample:root:module")).isEqualTo("#duplicate_key#");
}
@Test
public void fail_to_check_module_keys_before_renaming_without_admin_permission() {
expectedException.expect(ForbiddenException.class);
ComponentDto project = insertSampleProject();
userSession.login("john").addProjectUuidPermissions(UserRole.USER, project.uuid());
underTest.checkModuleKeysBeforeRenaming(project.key(), "sample", "sample2");
}
@Test
public void create_project() {
userSession.login("john").setGlobalPermissions(PROVISIONING);
String key = underTest.create(NewComponent.create("struts", "Struts project")).getKey();
ComponentDto project = underTest.getNullableByKey(key);
assertThat(project.key()).isEqualTo("struts");
assertThat(project.deprecatedKey()).isEqualTo("struts");
assertThat(project.uuid()).isNotNull();
assertThat(project.projectUuid()).isEqualTo(project.uuid());
assertThat(project.moduleUuid()).isNull();
assertThat(project.moduleUuidPath()).isEqualTo("." + project.uuid() + ".");
assertThat(project.name()).isEqualTo("Struts project");
assertThat(project.longName()).isEqualTo("Struts project");
assertThat(project.scope()).isEqualTo("PRJ");
assertThat(project.qualifier()).isEqualTo("TRK");
assertThat(project.getCreatedAt()).isNotNull();
}
@Test
public void create_new_project_with_branch() {
userSession.login("john").setGlobalPermissions(PROVISIONING);
String key = underTest.create(NewComponent.create("struts", "Struts project").setBranch("origin/branch")).getKey();
ComponentDto project = underTest.getNullableByKey(key);
assertThat(project.key()).isEqualTo("struts:origin/branch");
assertThat(project.deprecatedKey()).isEqualTo("struts:origin/branch");
}
@Test
public void create_view() {
userSession.login("john").setGlobalPermissions(PROVISIONING);
String key = underTest.create(NewComponent.create("all-project", "All Projects").setQualifier(Qualifiers.VIEW)).getKey();
ComponentDto project = underTest.getNullableByKey(key);
assertThat(project.key()).isEqualTo("all-project");
assertThat(project.deprecatedKey()).isEqualTo("all-project");
assertThat(project.uuid()).isNotNull();
assertThat(project.projectUuid()).isEqualTo(project.uuid());
assertThat(project.moduleUuid()).isNull();
assertThat(project.moduleUuidPath()).isEqualTo("." + project.uuid() + ".");
assertThat(project.name()).isEqualTo("All Projects");
assertThat(project.longName()).isEqualTo("All Projects");
assertThat(project.scope()).isEqualTo("PRJ");
assertThat(project.qualifier()).isEqualTo("VW");
assertThat(project.getCreatedAt()).isNotNull();
}
@Test
public void create_developer() throws Exception {
// No permission should be required to create a developer
userSession.anonymous();
String key = underTest.createDeveloper(dbTester.getSession(), NewComponent.create("DEV:jon.name@mail.com", "John").setQualifier("DEV")).getKey();
dbTester.getSession().commit();
ComponentDto dev = underTest.getNullableByKey(key);
assertThat(dev.key()).isEqualTo("DEV:jon.name@mail.com");
assertThat(dev.deprecatedKey()).isEqualTo("DEV:jon.name@mail.com");
assertThat(dev.uuid()).isNotNull();
assertThat(dev.projectUuid()).isEqualTo(dev.uuid());
assertThat(dev.moduleUuid()).isNull();
assertThat(dev.moduleUuidPath()).isEqualTo("." + dev.uuid() + ".");
assertThat(dev.name()).isEqualTo("John");
assertThat(dev.longName()).isEqualTo("John");
assertThat(dev.scope()).isEqualTo("PRJ");
assertThat(dev.qualifier()).isEqualTo("DEV");
assertThat(dev.getCreatedAt()).isNotNull();
}
@Test
public void fail_to_create_new_component_on_invalid_key() {
userSession.login("john").setGlobalPermissions(PROVISIONING);
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("Malformed key for Project: struts?parent. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
underTest.create(NewComponent.create("struts?parent", "Struts project"));
}
@Test
public void fail_to_create_new_component_on_invalid_branch() {
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("Malformed branch for Project: origin?branch. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.");
userSession.login("john").setGlobalPermissions(PROVISIONING);
underTest.create(NewComponent.create("struts", "Struts project").setBranch("origin?branch"));
}
@Test
public void fail_to_create_new_component_if_key_already_exists() {
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("Could not create Project, key already exists: struts");
userSession.login("john").setGlobalPermissions(PROVISIONING);
ComponentDto project = ComponentTesting.newProjectDto().setKey("struts");
dbClient.componentDao().insert(dbSession, project);
dbSession.commit();
underTest.create(NewComponent.create("struts", "Struts project"));
}
@Test
public void remove_duplicated_components_when_creating_project() throws Exception {
String projectKey = "PROJECT_KEY";
userSession.login("john").setGlobalPermissions(PROVISIONING);
DbSession session = mock(DbSession.class);
ComponentDao componentDao = mock(ComponentDao.class);
when(componentDao.selectByKey(session, projectKey)).thenReturn(Optional.<ComponentDto>absent());
DbClient dbClient = mock(DbClient.class);
when(dbClient.openSession(false)).thenReturn(session);
when(dbClient.componentDao()).thenReturn(componentDao);
when(dbClient.componentIndexDao()).thenReturn(mock(ResourceIndexDao.class));
doAnswer(invocation -> {
((ComponentDto) invocation.getArguments()[1]).setId(1L);
return null;
}).when(componentDao).insert(eq(session), any(ComponentDto.class));
when(componentDao.selectComponentsHavingSameKeyOrderedById(session, projectKey)).thenReturn(newArrayList(
ComponentTesting.newProjectDto().setId(1L).setKey(projectKey),
ComponentTesting.newProjectDto().setId(2L).setKey(projectKey),
ComponentTesting.newProjectDto().setId(3L).setKey(projectKey)));
underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
underTest.create(NewComponent.create(projectKey, projectKey));
verify(componentDao).delete(session, 2L);
verify(componentDao).delete(session, 3L);
}
@Test
public void should_return_project_uuids() {
ComponentDto project = insertSampleProject();
String moduleKey = "sample:root:module";
ComponentDto module = ComponentTesting.newModuleDto(project).setKey(moduleKey);
dbClient.componentDao().insert(dbSession, module);
String fileKey = "sample:root:module:Foo.xoo";
ComponentDto file = newFileDto(module, null).setKey(fileKey);
dbClient.componentDao().insert(dbSession, file);
dbSession.commit();
assertThat(underTest.componentUuids(Arrays.asList(moduleKey, fileKey))).hasSize(2);
assertThat(underTest.componentUuids(null)).isEmpty();
assertThat(underTest.componentUuids(Arrays.<String>asList())).isEmpty();
}
@Test
public void should_fail_on_components_not_found() {
String moduleKey = "sample:root:module";
String fileKey = "sample:root:module:Foo.xoo";
try {
underTest.componentUuids(Arrays.asList(moduleKey, fileKey));
Fail.fail("Should throw NotFoundException");
} catch (NotFoundException notFound) {
assertThat(notFound.getMessage()).contains(moduleKey).contains(fileKey);
}
}
@Test
public void should_fail_silently_on_components_not_found_if_told_so() {
String moduleKey = "sample:root:module";
String fileKey = "sample:root:module:Foo.xoo";
assertThat(underTest.componentUuids(dbSession, Arrays.asList(moduleKey, fileKey), true)).isEmpty();
}
private ComponentDto insertSampleProject() {
return componentDb.insertComponent(newProjectDto().setKey("sample:root"));
}
private void setGlobalAdminPermission() {
userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
}
}