]> source.dussan.org Git - sonarqube.git/blob
2cf2a07f83f6ccb76b9f066c24b35233ed0c54fc
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.almsettings.ws;
21
22 import java.util.List;
23 import java.util.Map;
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;
37
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;
46
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class GitHubDevOpsPlatformServiceTest {
50
51   private static final DevOpsProjectDescriptor GITHUB_PROJECT_DESCRIPTOR = new DevOpsProjectDescriptor(ALM.GITHUB, "url", "repo");
52
53   @Mock
54   private DbSession dbSession;
55   @Mock
56   private AlmSettingDao almSettingDao;
57   @Mock
58   private GithubGlobalSettingsValidator githubGlobalSettingsValidator;
59   @Mock
60   private GithubApplicationClient githubApplicationClient;
61   @InjectMocks
62   private GitHubDevOpsPlatformService gitHubDevOpsPlatformService;
63
64   @Test
65   public void getDevOpsPlatform_shouldReturnGitHub() {
66     assertThat(gitHubDevOpsPlatformService.getDevOpsPlatform())
67       .isEqualTo(ALM.GITHUB);
68   }
69
70   @Test
71   public void getDevOpsProjectDescriptor_whenNoCharacteristics_shouldReturnEmpty() {
72     Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = gitHubDevOpsPlatformService.getDevOpsProjectDescriptor(Map.of());
73
74     assertThat(devOpsProjectDescriptor).isEmpty();
75   }
76
77   @Test
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()
82     ));
83
84     assertThat(devOpsProjectDescriptor)
85       .isPresent()
86       .get().usingRecursiveComparison().isEqualTo(GITHUB_PROJECT_DESCRIPTOR);
87   }
88
89   @Test
90   public void getValidAlmSettingDto_whenNoAlmSetting_shouldReturnEmpty() {
91     when(almSettingDao.selectByAlm(dbSession, ALM.GITHUB)).thenReturn(emptyList());
92
93     Optional<AlmSettingDto> almSettingDto = gitHubDevOpsPlatformService.getValidAlmSettingDto(dbSession, GITHUB_PROJECT_DESCRIPTOR);
94
95     assertThat(almSettingDto).isEmpty();
96   }
97
98   @Test
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));
103
104     Optional<AlmSettingDto> almSettingDto = gitHubDevOpsPlatformService.getValidAlmSettingDto(dbSession, GITHUB_PROJECT_DESCRIPTOR);
105
106     assertThat(almSettingDto)
107       .isPresent()
108       .get().isEqualTo(mockGitHubAlmSettingDtoAccess);
109   }
110
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;
118   }
119
120 }