]> source.dussan.org Git - sonarqube.git/blob
cda5b3809e8f299f7478a32a741ea499f7152f94
[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.Map;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.apache.commons.lang.NotImplementedException;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.alm.setting.ALM;
30 import org.sonar.db.alm.setting.AlmSettingDto;
31
32 import static java.util.Collections.emptySet;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
35 import static org.assertj.core.api.Assertions.assertThatThrownBy;
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39
40 public class DelegatingDevOpsPlatformServiceTest {
41
42   private static final DelegatingDevOpsPlatformService NO_DEVOPS_PLATFORMS = new DelegatingDevOpsPlatformService(emptySet());
43   private static final DelegatingDevOpsPlatformService MULTIPLE_DEVOPS_PLATFORMS = new DelegatingDevOpsPlatformService(
44     Set.of(mockGitHubDevOpsPlatformService(), mockAzureDevOpsPlatformService()));
45
46   @Mock
47   private DbSession dbSession;
48
49   @Test
50   public void getDevOpsPlatform_shouldThrow() {
51     assertThatThrownBy(NO_DEVOPS_PLATFORMS::getDevOpsPlatform)
52       .isInstanceOf(NotImplementedException.class);
53   }
54
55   @Test
56   public void getDevOpsProjectDescriptor_whenNoDelegates_shouldReturnOptionalEmpty() {
57     Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = NO_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of());
58
59     assertThat(devOpsProjectDescriptor).isEmpty();
60   }
61
62   @Test
63   public void getDevOpsProjectDescriptor_whenDelegates_shouldReturnDelegateResponse() {
64     Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = MULTIPLE_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of(
65       "githubUrl", "githubUrl"
66     ));
67
68     assertThat(devOpsProjectDescriptor)
69       .isPresent()
70       .get().usingRecursiveComparison().isEqualTo(new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
71   }
72
73   @Test
74   public void getValidAlmSettingDto_whenNoDelegates_shouldReturnOptionalEmpty() {
75     Optional<AlmSettingDto> almSettingDto = NO_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, mock(DevOpsProjectDescriptor.class));
76
77     assertThat(almSettingDto).isEmpty();
78   }
79
80   @Test
81   public void getValidAlmSettingDto_whenDelegates_shouldReturnDelegateResponse() {
82     Optional<AlmSettingDto> almSettingDto = MULTIPLE_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
83
84     assertThat(almSettingDto)
85       .isPresent()
86       .get()
87       .usingRecursiveComparison().isEqualTo(new AlmSettingDto().setAlm(ALM.GITHUB));
88   }
89
90   @Test
91   public void isScanAllowedUsingPermissionsFromDevopsPlatform_whenNoDelegates_shouldThrow() {
92     DevOpsProjectDescriptor devOpsProjectDescriptor = mock();
93     when(devOpsProjectDescriptor.alm()).thenReturn(ALM.GITHUB);
94
95     assertThatIllegalStateException()
96       .isThrownBy(() -> NO_DEVOPS_PLATFORMS.isScanAllowedUsingPermissionsFromDevopsPlatform(mock(), devOpsProjectDescriptor))
97       .withMessage("No delegate found to handle projects on GITHUB");
98   }
99
100   @Test
101   public void isScanAllowedUsingPermissionsFromDevopsPlatform_whenDelegates_shouldReturnDelegateResponse() {
102     DevOpsProjectDescriptor devOpsProjectDescriptor = mock();
103     when(devOpsProjectDescriptor.alm()).thenReturn(ALM.GITHUB);
104
105     boolean isScanAllowed = MULTIPLE_DEVOPS_PLATFORMS.isScanAllowedUsingPermissionsFromDevopsPlatform(mock(), devOpsProjectDescriptor);
106
107     assertThat(isScanAllowed).isTrue();
108   }
109
110   private static DevOpsPlatformService mockGitHubDevOpsPlatformService() {
111     DevOpsPlatformService mockDevOpsPlatformService = mock();
112     when(mockDevOpsPlatformService.getDevOpsPlatform()).thenReturn(ALM.GITHUB);
113     when(mockDevOpsPlatformService.getDevOpsProjectDescriptor(Map.of("githubUrl", "githubUrl")))
114       .thenReturn(Optional.of(new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo")));
115     when(mockDevOpsPlatformService.getValidAlmSettingDto(any(), any()))
116       .thenReturn(Optional.of(new AlmSettingDto().setAlm(ALM.GITHUB)));
117     when(mockDevOpsPlatformService.isScanAllowedUsingPermissionsFromDevopsPlatform(any(), any())).thenReturn(true);
118     return mockDevOpsPlatformService;
119   }
120
121   private static DevOpsPlatformService mockAzureDevOpsPlatformService() {
122     DevOpsPlatformService mockDevOpsPlatformService = mock();
123     when(mockDevOpsPlatformService.getDevOpsPlatform()).thenReturn(ALM.AZURE_DEVOPS);
124     when(mockDevOpsPlatformService.getDevOpsProjectDescriptor(Map.of("azureUrl", "azureUrl")))
125       .thenReturn(Optional.of(new DevOpsProjectDescriptor(ALM.AZURE_DEVOPS, "azureUrl", "azureProject")));
126     when(mockDevOpsPlatformService.getValidAlmSettingDto(any(), any()))
127       .thenReturn(Optional.of(new AlmSettingDto().setAlm(ALM.AZURE_DEVOPS)));
128     return mockDevOpsPlatformService;
129   }
130
131 }