]> source.dussan.org Git - sonarqube.git/blob
0a7e7460cf33a3f71c769913d373cfb421cbd9c7
[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.assertThatThrownBy;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39 public class DelegatingDevOpsPlatformServiceTest {
40
41   private static final DelegatingDevOpsPlatformService NO_DEVOPS_PLATFORMS = new DelegatingDevOpsPlatformService(emptySet());
42   private static final DelegatingDevOpsPlatformService MULTIPLE_DEVOPS_PLATFORMS = new DelegatingDevOpsPlatformService(
43     Set.of(mockGitHubDevOpsPlatformService(), mockAzureDevOpsPlatformService()));
44
45   @Mock
46   private DbSession dbSession;
47
48   @Test
49   public void getDevOpsPlatform_shouldThrow() {
50     assertThatThrownBy(NO_DEVOPS_PLATFORMS::getDevOpsPlatform)
51       .isInstanceOf(NotImplementedException.class);
52   }
53
54   @Test
55   public void getDevOpsProjectDescriptor_whenNoDelegates_shouldReturnOptionalEmpty() {
56     Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = NO_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of());
57
58     assertThat(devOpsProjectDescriptor).isEmpty();
59   }
60
61   @Test
62   public void getDevOpsProjectDescriptor_whenDelegates_shouldReturnDelegateResponse() {
63     Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = MULTIPLE_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of(
64       "githubUrl", "githubUrl"
65     ));
66
67     assertThat(devOpsProjectDescriptor)
68       .isPresent()
69       .get().usingRecursiveComparison().isEqualTo(new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
70   }
71
72   @Test
73   public void getValidAlmSettingDto_whenNoDelegates_shouldReturnOptionalEmpty() {
74     Optional<AlmSettingDto> almSettingDto = NO_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, mock(DevOpsProjectDescriptor.class));
75
76     assertThat(almSettingDto).isEmpty();
77   }
78
79   @Test
80   public void getValidAlmSettingDto_whenDelegates_shouldReturnDelegateResponse() {
81     Optional<AlmSettingDto> almSettingDto = MULTIPLE_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
82
83     assertThat(almSettingDto)
84       .isPresent()
85       .get()
86       .usingRecursiveComparison().isEqualTo(new AlmSettingDto().setAlm(ALM.GITHUB));
87   }
88
89   private static DevOpsPlatformService mockGitHubDevOpsPlatformService() {
90     DevOpsPlatformService mockDevOpsPlatformService = mock();
91     when(mockDevOpsPlatformService.getDevOpsPlatform()).thenReturn(ALM.GITHUB);
92     when(mockDevOpsPlatformService.getDevOpsProjectDescriptor(Map.of("githubUrl", "githubUrl")))
93       .thenReturn(Optional.of(new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo")));
94     when(mockDevOpsPlatformService.getValidAlmSettingDto(any(), any()))
95       .thenReturn(Optional.of(new AlmSettingDto().setAlm(ALM.GITHUB)));
96     return mockDevOpsPlatformService;
97   }
98
99   private static DevOpsPlatformService mockAzureDevOpsPlatformService() {
100     DevOpsPlatformService mockDevOpsPlatformService = mock();
101     when(mockDevOpsPlatformService.getDevOpsPlatform()).thenReturn(ALM.AZURE_DEVOPS);
102     when(mockDevOpsPlatformService.getDevOpsProjectDescriptor(Map.of("azureUrl", "azureUrl")))
103       .thenReturn(Optional.of(new DevOpsProjectDescriptor(ALM.AZURE_DEVOPS, "azureUrl", "azureProject")));
104     when(mockDevOpsPlatformService.getValidAlmSettingDto(any(), any()))
105       .thenReturn(Optional.of(new AlmSettingDto().setAlm(ALM.AZURE_DEVOPS)));
106     return mockDevOpsPlatformService;
107   }
108
109 }