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 org.sonar.server.common.almsettings.bitbucketcloud;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.alm.setting.ALM;
30 import org.sonar.db.alm.setting.AlmSettingDto;
31 import org.sonar.server.common.almintegration.ProjectKeyGenerator;
32 import org.sonar.server.common.almsettings.DevOpsProjectCreator;
33 import org.sonar.server.common.almsettings.DevOpsProjectDescriptor;
34 import org.sonar.server.common.project.ProjectCreator;
35 import org.sonar.server.user.UserSession;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
41 @ExtendWith(MockitoExtension.class)
42 class BitbucketCloudProjectCreatorFactoryTest {
45 private DbClient dbClient;
47 private UserSession userSession;
49 private BitbucketCloudRestClient bitbucketCloudRestClient;
51 private ProjectCreator projectCreator;
53 private ProjectKeyGenerator projectKeyGenerator;
56 private BitbucketCloudProjectCreatorFactory underTest;
59 void getDevOpsProjectCreator_whenAlmIsNotBitbucketCloud_shouldReturnEmpty() {
60 AlmSettingDto almSettingDto = mock(AlmSettingDto.class);
61 when(almSettingDto.getAlm()).thenReturn(ALM.GITLAB);
62 DevOpsProjectDescriptor devOpsProjectDescriptor = new DevOpsProjectDescriptor(ALM.GITLAB, null, "bitbucket_project");
64 assertThat(underTest.getDevOpsProjectCreator(almSettingDto, devOpsProjectDescriptor)).isEmpty();
68 void getDevOpsProjectCreator_whenAlmItBitbucketCloud_shouldReturnProjectCreator() {
69 AlmSettingDto almSettingDto = mock(AlmSettingDto.class);
70 when(almSettingDto.getAlm()).thenReturn(ALM.BITBUCKET_CLOUD);
71 DevOpsProjectDescriptor devOpsProjectDescriptor = new DevOpsProjectDescriptor(ALM.BITBUCKET_CLOUD, null, "bitbucket_project");
73 DevOpsProjectCreator expectedProjectCreator = new BitbucketCloudProjectCreator(dbClient, almSettingDto, devOpsProjectDescriptor, userSession, bitbucketCloudRestClient,
74 projectCreator, projectKeyGenerator);
75 DevOpsProjectCreator devOpsProjectCreator = underTest.getDevOpsProjectCreator(almSettingDto, devOpsProjectDescriptor).orElseThrow();
77 assertThat(devOpsProjectCreator).usingRecursiveComparison().isEqualTo(expectedProjectCreator);