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;
23 import java.util.Optional;
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;
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;
40 public class DelegatingDevOpsPlatformServiceTest {
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()));
47 private DbSession dbSession;
50 public void getDevOpsPlatform_shouldThrow() {
51 assertThatThrownBy(NO_DEVOPS_PLATFORMS::getDevOpsPlatform)
52 .isInstanceOf(NotImplementedException.class);
56 public void getDevOpsProjectDescriptor_whenNoDelegates_shouldReturnOptionalEmpty() {
57 Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = NO_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of());
59 assertThat(devOpsProjectDescriptor).isEmpty();
63 public void getDevOpsProjectDescriptor_whenDelegates_shouldReturnDelegateResponse() {
64 Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = MULTIPLE_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of(
65 "githubUrl", "githubUrl"
68 assertThat(devOpsProjectDescriptor)
70 .get().usingRecursiveComparison().isEqualTo(new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
74 public void getValidAlmSettingDto_whenNoDelegates_shouldReturnOptionalEmpty() {
75 Optional<AlmSettingDto> almSettingDto = NO_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, mock(DevOpsProjectDescriptor.class));
77 assertThat(almSettingDto).isEmpty();
81 public void getValidAlmSettingDto_whenDelegates_shouldReturnDelegateResponse() {
82 Optional<AlmSettingDto> almSettingDto = MULTIPLE_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
84 assertThat(almSettingDto)
87 .usingRecursiveComparison().isEqualTo(new AlmSettingDto().setAlm(ALM.GITHUB));
91 public void isScanAllowedUsingPermissionsFromDevopsPlatform_whenNoDelegates_shouldThrow() {
92 DevOpsProjectDescriptor devOpsProjectDescriptor = mock();
93 when(devOpsProjectDescriptor.alm()).thenReturn(ALM.GITHUB);
95 assertThatIllegalStateException()
96 .isThrownBy(() -> NO_DEVOPS_PLATFORMS.isScanAllowedUsingPermissionsFromDevopsPlatform(mock(), devOpsProjectDescriptor))
97 .withMessage("No delegate found to handle projects on GITHUB");
101 public void isScanAllowedUsingPermissionsFromDevopsPlatform_whenDelegates_shouldReturnDelegateResponse() {
102 DevOpsProjectDescriptor devOpsProjectDescriptor = mock();
103 when(devOpsProjectDescriptor.alm()).thenReturn(ALM.GITHUB);
105 boolean isScanAllowed = MULTIPLE_DEVOPS_PLATFORMS.isScanAllowedUsingPermissionsFromDevopsPlatform(mock(), devOpsProjectDescriptor);
107 assertThat(isScanAllowed).isTrue();
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;
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;