]> source.dussan.org Git - sonarqube.git/blob
f2c691afdd358ec877833006f0d57b24e9207ada
[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 org.sonar.server.almintegration.ws.bitbucketserver;
21
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
31 import org.sonar.alm.client.bitbucketserver.Branch;
32 import org.sonar.alm.client.bitbucketserver.BranchesList;
33 import org.sonar.alm.client.bitbucketserver.Project;
34 import org.sonar.alm.client.bitbucketserver.Repository;
35 import org.sonar.api.server.ws.WebService;
36 import org.sonar.api.utils.System2;
37 import org.sonar.core.platform.EditionProvider;
38 import org.sonar.core.platform.PlatformEditionProvider;
39 import org.sonar.core.util.SequenceUuidFactory;
40 import org.sonar.db.DbTester;
41 import org.sonar.db.alm.setting.AlmSettingDto;
42 import org.sonar.db.component.BranchDto;
43 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
44 import org.sonar.db.project.CreationMethod;
45 import org.sonar.db.project.ProjectDto;
46 import org.sonar.db.user.UserDto;
47 import org.sonar.server.almintegration.ws.ImportHelper;
48 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
49 import org.sonar.server.component.ComponentUpdater;
50 import org.sonar.server.es.TestIndexers;
51 import org.sonar.server.exceptions.BadRequestException;
52 import org.sonar.server.exceptions.ForbiddenException;
53 import org.sonar.server.exceptions.NotFoundException;
54 import org.sonar.server.exceptions.UnauthorizedException;
55 import org.sonar.server.favorite.FavoriteUpdater;
56 import org.sonar.server.l18n.I18nRule;
57 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
58 import org.sonar.server.permission.PermissionService;
59 import org.sonar.server.permission.PermissionTemplateService;
60 import org.sonar.server.permission.PermissionUpdater;
61 import org.sonar.server.project.DefaultBranchNameResolver;
62 import org.sonar.server.project.ProjectDefaultVisibility;
63 import org.sonar.server.project.Visibility;
64 import org.sonar.server.tester.UserSessionRule;
65 import org.sonar.server.ws.TestRequest;
66 import org.sonar.server.ws.WsActionTester;
67 import org.sonarqube.ws.Projects;
68
69 import static java.util.Objects.requireNonNull;
70 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
71 import static org.apache.commons.lang.math.JVMRandom.nextLong;
72 import static org.assertj.core.api.Assertions.assertThat;
73 import static org.assertj.core.api.Assertions.assertThatNoException;
74 import static org.assertj.core.api.Assertions.assertThatThrownBy;
75 import static org.assertj.core.api.Assertions.tuple;
76 import static org.mockito.ArgumentMatchers.any;
77 import static org.mockito.Mockito.mock;
78 import static org.mockito.Mockito.verify;
79 import static org.mockito.Mockito.when;
80 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
81 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
82 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
83 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
84 import static org.sonar.db.permission.GlobalPermission.SCAN;
85 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
86 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
87
88 public class ImportBitbucketServerProjectActionIT {
89   private static final String GENERATED_PROJECT_KEY = "TEST_PROJECT_KEY";
90
91   @Rule
92   public UserSessionRule userSession = UserSessionRule.standalone();
93   @Rule
94   public DbTester db = DbTester.create();
95   @Rule
96   public final I18nRule i18n = new I18nRule();
97
98   private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
99   private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
100   private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
101   private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
102   private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
103
104   private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE,
105     mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(),
106     defaultBranchNameResolver, mock(PermissionUpdater.class), mock(PermissionService.class));
107
108   private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
109   private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
110   private final WsActionTester ws = new WsActionTester(new ImportBitbucketServerProjectAction(db.getDbClient(), userSession,
111     bitbucketServerRestClient, projectDefaultVisibility, componentUpdater, importHelper, projectKeyGenerator, newCodeDefinitionResolver, defaultBranchNameResolver));
112
113   private static BranchesList defaultBranchesList;
114
115   @BeforeClass
116   public static void beforeAll() {
117     Branch defaultBranch = new Branch("default", true);
118     defaultBranchesList = new BranchesList(Collections.singletonList(defaultBranch));
119   }
120
121   @Before
122   public void before() {
123     when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
124     when(projectKeyGenerator.generateUniqueProjectKey(any(), any())).thenReturn(GENERATED_PROJECT_KEY);
125     when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
126   }
127
128   @Test
129   public void import_project() {
130     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
131     Project project = getGsonBBSProject();
132     Repository repo = mockBitbucketServerRepo(project);
133
134     Projects.CreateWsResponse response = ws.newRequest()
135       .setParam("almSetting", almSetting.getKey())
136       .setParam("projectKey", "projectKey")
137       .setParam("repositorySlug", "repo-slug")
138       .executeProtobuf(Projects.CreateWsResponse.class);
139
140     Projects.CreateWsResponse.Project result = response.getProject();
141     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
142     assertThat(result.getName()).isEqualTo(repo.getName());
143
144     ProjectDto projectDto = getProjectDto(result);
145     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
146
147     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto)).isPresent();
148     verify(projectKeyGenerator).generateUniqueProjectKey(requireNonNull(project.getKey()), repo.getSlug());
149   }
150
151   @Test
152   public void importProject_whenCallIsNotFromBrowser_shouldFlagTheProjectAsCreatedFromApi() {
153     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
154     Project project = getGsonBBSProject();
155     mockBitbucketServerRepo(project);
156
157     Projects.CreateWsResponse response = ws.newRequest()
158       .setParam("almSetting", almSetting.getKey())
159       .setParam("projectKey", "projectKey")
160       .setParam("repositorySlug", "repo-slug")
161       .executeProtobuf(Projects.CreateWsResponse.class);
162
163     ProjectDto projectDto = getProjectDto(response.getProject());
164     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
165   }
166
167   @Test
168   public void importProject_whenCallIsFromBrowser_shouldFlagTheProjectAsCreatedFromBrowser() {
169     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
170     userSession.flagSessionAsGui();
171     Project project = getGsonBBSProject();
172     mockBitbucketServerRepo(project);
173
174     Projects.CreateWsResponse response = ws.newRequest()
175       .setParam("almSetting", almSetting.getKey())
176       .setParam("projectKey", "projectKey")
177       .setParam("repositorySlug", "repo-slug")
178       .executeProtobuf(Projects.CreateWsResponse.class);
179
180     ProjectDto projectDto = getProjectDto(response.getProject());
181     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_BROWSER);
182   }
183
184   @Test
185   public void import_project_with_NCD_developer_edition_sets_project_NCD() {
186     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
187
188     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
189     Project project = getGsonBBSProject();
190     Repository repo = mockBitbucketServerRepo(project);
191
192     Projects.CreateWsResponse response = ws.newRequest()
193       .setParam("almSetting", almSetting.getKey())
194       .setParam("projectKey", "projectKey")
195       .setParam("repositorySlug", "repo-slug")
196       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
197       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
198       .executeProtobuf(Projects.CreateWsResponse.class);
199
200     Projects.CreateWsResponse.Project result = response.getProject();
201     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
202     assertThat(result.getName()).isEqualTo(repo.getName());
203
204     ProjectDto projectDto = getProjectDto(result);
205     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto)).isPresent();
206     verify(projectKeyGenerator).generateUniqueProjectKey(requireNonNull(project.getKey()), repo.getSlug());
207
208     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
209       .isPresent()
210       .get()
211       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
212       .containsExactly(NUMBER_OF_DAYS, "30", null);
213   }
214
215   @Test
216   public void import_project_with_NCD_community_edition_sets_branch_NCD() {
217     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
218
219     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
220     Project project = getGsonBBSProject();
221     mockBitbucketServerRepo(project);
222
223     Projects.CreateWsResponse response = ws.newRequest()
224       .setParam("almSetting", almSetting.getKey())
225       .setParam("projectKey", "projectKey")
226       .setParam("repositorySlug", "repo-slug")
227       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
228       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
229       .executeProtobuf(Projects.CreateWsResponse.class);
230
231     Projects.CreateWsResponse.Project result = response.getProject();
232
233     ProjectDto projectDto = getProjectDto(result);
234     BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.getUuid()).orElseThrow();
235
236     String projectUuid = projectDto.getUuid();
237     assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
238       .isPresent()
239       .get()
240       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
241       .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
242   }
243
244   @Test
245   public void import_project_reference_branch_ncd_no_default_branch() {
246     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
247     when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn("default-branch");
248
249     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
250     Project project = getGsonBBSProject();
251     mockBitbucketServerRepo(project, new BranchesList());
252
253     Projects.CreateWsResponse response = ws.newRequest()
254       .setParam("almSetting", almSetting.getKey())
255       .setParam("projectKey", "projectKey")
256       .setParam("repositorySlug", "repo-slug")
257       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
258       .executeProtobuf(Projects.CreateWsResponse.class);
259
260     Projects.CreateWsResponse.Project result = response.getProject();
261
262     ProjectDto projectDto = getProjectDto(result);
263
264     String projectUuid = projectDto.getUuid();
265     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectUuid))
266       .isPresent()
267       .get()
268       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
269       .containsExactly(REFERENCE_BRANCH, "default-branch");
270   }
271
272   @Test
273   public void import_project_reference_branch_ncd() {
274     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
275
276     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
277     Project project = getGsonBBSProject();
278     mockBitbucketServerRepo(project);
279
280     Projects.CreateWsResponse response = ws.newRequest()
281       .setParam("almSetting", almSetting.getKey())
282       .setParam("projectKey", "projectKey")
283       .setParam("repositorySlug", "repo-slug")
284       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
285       .executeProtobuf(Projects.CreateWsResponse.class);
286
287     Projects.CreateWsResponse.Project result = response.getProject();
288
289     ProjectDto projectDto = getProjectDto(result);
290
291     String projectUuid = projectDto.getUuid();
292     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectUuid))
293       .isPresent()
294       .get()
295       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
296       .containsExactly(REFERENCE_BRANCH, "default");
297   }
298
299   @Test
300   public void fail_project_already_exist() {
301     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
302     Project project = getGsonBBSProject();
303     mockBitbucketServerRepo(project);
304     db.components().insertPublicProject(p -> p.setKey(GENERATED_PROJECT_KEY)).getMainBranchComponent();
305
306     assertThatThrownBy(() -> {
307
308       ws.newRequest()
309         .setParam("almSetting", almSetting.getKey())
310         .setParam("projectKey", "projectKey")
311         .setParam("repositorySlug", "repo-slug")
312         .execute();
313     })
314       .isInstanceOf(BadRequestException.class)
315       .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", GENERATED_PROJECT_KEY, GENERATED_PROJECT_KEY);
316   }
317
318   @Test
319   public void fail_when_not_logged_in() {
320     assertThatThrownBy(() -> {
321       ws.newRequest()
322         .setParam("almSetting", "sdgfdshfjztutz")
323         .setParam("projectKey", "projectKey")
324         .setParam("repositorySlug", "repo-slug")
325         .execute();
326     })
327       .isInstanceOf(UnauthorizedException.class);
328   }
329
330   @Test
331   public void fail_when_missing_project_creator_permission() {
332     UserDto user = db.users().insertUser();
333     userSession.logIn(user).addPermission(SCAN);
334
335     assertThatThrownBy(() -> {
336       ws.newRequest()
337         .setParam("almSetting", "sdgfdshfjztutz")
338         .setParam("projectKey", "projectKey")
339         .setParam("repositorySlug", "repo-slug")
340         .execute();
341     })
342       .isInstanceOf(ForbiddenException.class)
343       .hasMessage("Insufficient privileges");
344   }
345
346   @Test
347   public void check_pat_is_missing() {
348     UserDto user = db.users().insertUser();
349     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
350     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
351
352     assertThatThrownBy(() -> {
353       ws.newRequest()
354         .setParam("almSetting", almSetting.getKey())
355         .execute();
356     })
357       .isInstanceOf(IllegalArgumentException.class)
358       .hasMessage("personal access token for '" + almSetting.getKey() + "' is missing");
359   }
360
361   @Test
362   public void fail_when_no_creation_project_permission() {
363     UserDto user = db.users().insertUser();
364     userSession.logIn(user);
365
366     assertThatThrownBy(() -> {
367       ws.newRequest()
368         .setParam("almSetting", "anyvalue")
369         .execute();
370     })
371       .isInstanceOf(ForbiddenException.class)
372       .hasMessage("Insufficient privileges");
373   }
374
375   @Test
376   public void handle_givenNoDefaultBranchFound_doNotUpdateDefaultBranchName() {
377     BranchesList branchesList = new BranchesList();
378     Branch branch = new Branch("not_a_master", false);
379     branchesList.addBranch(branch);
380
381     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
382     Project project = getGsonBBSProject();
383     mockBitbucketServerRepo(project, branchesList);
384
385     Projects.CreateWsResponse response = ws.newRequest()
386       .setParam("almSetting", almSetting.getKey())
387       .setParam("projectKey", "projectKey")
388       .setParam("repositorySlug", "repo-slug")
389       .executeProtobuf(Projects.CreateWsResponse.class);
390
391     Projects.CreateWsResponse.Project result = response.getProject();
392
393     ProjectDto projectDto = getProjectDto(result);
394     Collection<BranchDto> branchDtos = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto);
395     List<BranchDto> collect = branchDtos.stream().filter(BranchDto::isMain).toList();
396     String mainBranchName = collect.iterator().next().getKey();
397     assertThat(mainBranchName).isEqualTo(DEFAULT_MAIN_BRANCH_NAME);
398   }
399
400   @Test
401   public void handle_givenDefaultBranchNamedDefault_updateDefaultBranchNameToDefault() {
402     BranchesList branchesList = new BranchesList();
403     Branch branch = new Branch("default", true);
404     branchesList.addBranch(branch);
405
406     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
407     Project project = getGsonBBSProject();
408     mockBitbucketServerRepo(project, branchesList);
409
410     Projects.CreateWsResponse response = ws.newRequest()
411       .setParam("almSetting", almSetting.getKey())
412       .setParam("projectKey", "projectKey")
413       .setParam("repositorySlug", "repo-slug")
414       .executeProtobuf(Projects.CreateWsResponse.class);
415
416     Projects.CreateWsResponse.Project result = response.getProject();
417
418     ProjectDto projectDto = getProjectDto(result);
419     Collection<BranchDto> branchDtos = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto);
420     List<BranchDto> collect = branchDtos.stream().filter(BranchDto::isMain).toList();
421     String mainBranchName = collect.iterator().next().getKey();
422     assertThat(mainBranchName).isEqualTo("default");
423   }
424
425   @Test
426   public void importProject_whenAlmSettingKeyDoesNotExist_shouldThrow() {
427     UserDto user = db.users().insertUser();
428     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
429
430     TestRequest request = ws.newRequest()
431       .setParam("almSetting", "unknown")
432       .setParam("projectKey", "projectKey")
433       .setParam("repositorySlug", "repo-slug");
434
435     assertThatThrownBy(request::execute)
436       .isInstanceOf(NotFoundException.class)
437       .hasMessage("DevOps Platform configuration 'unknown' not found.");
438   }
439
440   @Test
441   public void importProject_whenNoAlmSettingKeyAndNoConfig_shouldThrow() {
442     UserDto user = db.users().insertUser();
443     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
444
445     TestRequest request = ws.newRequest()
446       .setParam("projectKey", "projectKey")
447       .setParam("repositorySlug", "repo-slug");
448
449     assertThatThrownBy(request::execute)
450       .isInstanceOf(NotFoundException.class)
451       .hasMessage("There is no BITBUCKET configuration for DevOps Platform. Please add one.");
452   }
453
454   @Test
455   public void importProject_whenNoAlmSettingKeyAndMultipleConfigs_shouldThrow() {
456     UserDto user = db.users().insertUser();
457     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
458
459     db.almSettings().insertBitbucketAlmSetting();
460     db.almSettings().insertBitbucketAlmSetting();
461
462     TestRequest request = ws.newRequest()
463       .setParam("projectKey", "projectKey")
464       .setParam("repositorySlug", "repo-slug");
465
466     assertThatThrownBy(request::execute)
467       .isInstanceOf(IllegalArgumentException.class)
468       .hasMessage("Parameter almSetting is required as there are multiple DevOps Platform configurations.");
469   }
470
471   @Test
472   public void importProject_whenNoAlmSettingKeyAndOnlyOneConfig_shouldImport() {
473     configureUserAndPatAndAlmSettings();
474     Project project = getGsonBBSProject();
475     mockBitbucketServerRepo(project);
476
477     TestRequest request = ws.newRequest()
478       .setParam("projectKey", "projectKey")
479       .setParam("repositorySlug", "repo-slug");
480
481     assertThatNoException().isThrownBy(request::execute);
482   }
483
484   @Test
485   public void definition() {
486     WebService.Action def = ws.getDef();
487
488     assertThat(def.since()).isEqualTo("8.2");
489     assertThat(def.isPost()).isTrue();
490     assertThat(def.params())
491       .extracting(WebService.Param::key, WebService.Param::isRequired)
492       .containsExactlyInAnyOrder(
493         tuple("almSetting", false),
494         tuple("repositorySlug", true),
495         tuple("projectKey", true),
496         tuple(PARAM_NEW_CODE_DEFINITION_TYPE, false),
497         tuple(PARAM_NEW_CODE_DEFINITION_VALUE, false));
498   }
499
500   private AlmSettingDto configureUserAndPatAndAlmSettings() {
501     UserDto user = db.users().insertUser();
502     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
503     AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
504     db.almPats().insert(dto -> {
505       dto.setAlmSettingUuid(almSetting.getUuid());
506       dto.setUserUuid(user.getUuid());
507     });
508     return almSetting;
509   }
510
511   private Repository mockBitbucketServerRepo(Project project) {
512     return mockBitbucketServerRepo(project, defaultBranchesList);
513   }
514
515   private Repository mockBitbucketServerRepo(Project project, BranchesList branchesList) {
516     Repository bbsResult = new Repository();
517     bbsResult.setProject(project);
518     bbsResult.setSlug(randomAlphanumeric(5));
519     bbsResult.setName(randomAlphanumeric(5));
520     bbsResult.setId(nextLong(100));
521     when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(bbsResult);
522     when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(branchesList);
523     return bbsResult;
524   }
525
526   private Project getGsonBBSProject() {
527     return new Project()
528       .setKey(randomAlphanumeric(5))
529       .setId(nextLong(100))
530       .setName(randomAlphanumeric(5));
531   }
532
533   private ProjectDto getProjectDto(Projects.CreateWsResponse.Project result) {
534     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
535     assertThat(projectDto).isPresent();
536     return projectDto.orElseThrow();
537   }
538
539 }