3 * Copyright (C) 2009-2023 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.almsettings.ws;
22 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.sonar.alm.client.github.GithubApplicationClient;
31 import org.sonar.alm.client.github.GithubGlobalSettingsValidator;
32 import org.sonar.alm.client.github.config.GithubAppConfiguration;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.alm.setting.ALM;
35 import org.sonar.db.alm.setting.AlmSettingDao;
36 import org.sonar.db.alm.setting.AlmSettingDto;
38 import static java.util.Collections.emptyList;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.ArgumentMatchers.any;
41 import static org.mockito.ArgumentMatchers.eq;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.server.almsettings.ws.GitHubDevOpsPlatformService.DEVOPS_PLATFORM_PROJECT_IDENTIFIER;
45 import static org.sonar.server.almsettings.ws.GitHubDevOpsPlatformService.DEVOPS_PLATFORM_URL;
48 @RunWith(MockitoJUnitRunner.class)
49 public class GitHubDevOpsPlatformServiceTest {
51 private static final DevOpsProjectDescriptor GITHUB_PROJECT_DESCRIPTOR = new DevOpsProjectDescriptor(ALM.GITHUB, "url", "repo");
54 private DbSession dbSession;
56 private AlmSettingDao almSettingDao;
58 private GithubGlobalSettingsValidator githubGlobalSettingsValidator;
60 private GithubApplicationClient githubApplicationClient;
62 private GitHubDevOpsPlatformService gitHubDevOpsPlatformService;
65 public void getDevOpsPlatform_shouldReturnGitHub() {
66 assertThat(gitHubDevOpsPlatformService.getDevOpsPlatform())
67 .isEqualTo(ALM.GITHUB);
71 public void getDevOpsProjectDescriptor_whenNoCharacteristics_shouldReturnEmpty() {
72 Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = gitHubDevOpsPlatformService.getDevOpsProjectDescriptor(Map.of());
74 assertThat(devOpsProjectDescriptor).isEmpty();
78 public void getDevOpsProjectDescriptor_whenValidCharacteristics_shouldReturn() {
79 Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = gitHubDevOpsPlatformService.getDevOpsProjectDescriptor(Map.of(
80 DEVOPS_PLATFORM_URL, GITHUB_PROJECT_DESCRIPTOR.url(),
81 DEVOPS_PLATFORM_PROJECT_IDENTIFIER, GITHUB_PROJECT_DESCRIPTOR.projectIdentifier()
84 assertThat(devOpsProjectDescriptor)
86 .get().usingRecursiveComparison().isEqualTo(GITHUB_PROJECT_DESCRIPTOR);
90 public void getValidAlmSettingDto_whenNoAlmSetting_shouldReturnEmpty() {
91 when(almSettingDao.selectByAlm(dbSession, ALM.GITHUB)).thenReturn(emptyList());
93 Optional<AlmSettingDto> almSettingDto = gitHubDevOpsPlatformService.getValidAlmSettingDto(dbSession, GITHUB_PROJECT_DESCRIPTOR);
95 assertThat(almSettingDto).isEmpty();
99 public void getValidAlmSettingDto_whenMultipleAlmSetting_shouldReturnTheRightOne() {
100 AlmSettingDto mockGitHubAlmSettingDtoNoAccess = mockGitHubAlmSettingDto(false);
101 AlmSettingDto mockGitHubAlmSettingDtoAccess = mockGitHubAlmSettingDto(true);
102 when(almSettingDao.selectByAlm(dbSession, ALM.GITHUB)).thenReturn(List.of(mockGitHubAlmSettingDtoNoAccess, mockGitHubAlmSettingDtoAccess));
104 Optional<AlmSettingDto> almSettingDto = gitHubDevOpsPlatformService.getValidAlmSettingDto(dbSession, GITHUB_PROJECT_DESCRIPTOR);
106 assertThat(almSettingDto)
108 .get().isEqualTo(mockGitHubAlmSettingDtoAccess);
111 private AlmSettingDto mockGitHubAlmSettingDto(boolean repoAccess) {
112 AlmSettingDto mockAlmSettingDto = mock();
113 when(mockAlmSettingDto.getUrl()).thenReturn(GITHUB_PROJECT_DESCRIPTOR.url());
114 GithubAppConfiguration mockGithubAppConfiguration = mock(GithubAppConfiguration.class);
115 when(githubGlobalSettingsValidator.validate(mockAlmSettingDto)).thenReturn(mockGithubAppConfiguration);
116 when(githubApplicationClient.getInstallationId(eq(mockGithubAppConfiguration), any())).thenReturn(repoAccess ? Optional.of(1L) : Optional.empty());
117 return mockAlmSettingDto;