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.assertThatThrownBy;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
39 public class DelegatingDevOpsPlatformServiceTest {
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()));
46 private DbSession dbSession;
49 public void getDevOpsPlatform_shouldThrow() {
50 assertThatThrownBy(NO_DEVOPS_PLATFORMS::getDevOpsPlatform)
51 .isInstanceOf(NotImplementedException.class);
55 public void getDevOpsProjectDescriptor_whenNoDelegates_shouldReturnOptionalEmpty() {
56 Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = NO_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of());
58 assertThat(devOpsProjectDescriptor).isEmpty();
62 public void getDevOpsProjectDescriptor_whenDelegates_shouldReturnDelegateResponse() {
63 Optional<DevOpsProjectDescriptor> devOpsProjectDescriptor = MULTIPLE_DEVOPS_PLATFORMS.getDevOpsProjectDescriptor(Map.of(
64 "githubUrl", "githubUrl"
67 assertThat(devOpsProjectDescriptor)
69 .get().usingRecursiveComparison().isEqualTo(new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
73 public void getValidAlmSettingDto_whenNoDelegates_shouldReturnOptionalEmpty() {
74 Optional<AlmSettingDto> almSettingDto = NO_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, mock(DevOpsProjectDescriptor.class));
76 assertThat(almSettingDto).isEmpty();
80 public void getValidAlmSettingDto_whenDelegates_shouldReturnDelegateResponse() {
81 Optional<AlmSettingDto> almSettingDto = MULTIPLE_DEVOPS_PLATFORMS.getValidAlmSettingDto(dbSession, new DevOpsProjectDescriptor(ALM.GITHUB, "githubUrl", "githubRepo"));
83 assertThat(almSettingDto)
86 .usingRecursiveComparison().isEqualTo(new AlmSettingDto().setAlm(ALM.GITHUB));
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;
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;