]> source.dussan.org Git - sonarqube.git/blob
efc2679fe28dfa3ad8dbc4717997640101d2189b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.almintegration.validator;
21
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.sonar.alm.client.github.GithubApplicationClientImpl;
25 import org.sonar.alm.client.github.config.GithubAppConfiguration;
26 import org.sonar.db.alm.setting.AlmSettingDto;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatThrownBy;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto;
33
34 public class GithubGlobalSettingsValidatorTest {
35   private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class);
36   private final GithubGlobalSettingsValidator underTest = new GithubGlobalSettingsValidator(appClient);
37
38   @Test
39   public void github_global_settings_validation() {
40     AlmSettingDto almSettingDto = newGithubAlmSettingDto()
41       .setClientId("clientId")
42       .setClientSecret("clientSecret");
43
44     GithubAppConfiguration configuration = underTest.validate(almSettingDto);
45
46     ArgumentCaptor<GithubAppConfiguration> configurationArgumentCaptor = ArgumentCaptor.forClass(GithubAppConfiguration.class);
47     verify(appClient).checkApiEndpoint(configurationArgumentCaptor.capture());
48     verify(appClient).checkAppPermissions(configurationArgumentCaptor.capture());
49     assertThat(configuration.getId()).isEqualTo(configurationArgumentCaptor.getAllValues().get(0).getId());
50     assertThat(configuration.getId()).isEqualTo(configurationArgumentCaptor.getAllValues().get(1).getId());
51   }
52
53   @Test
54   public void github_validation_checks_invalid_appId() {
55     AlmSettingDto almSettingDto = newGithubAlmSettingDto()
56       .setAppId("abc")
57       .setClientId("clientId")
58       .setClientSecret("clientSecret");
59
60     assertThatThrownBy(() -> underTest.validate(almSettingDto))
61       .isInstanceOf(IllegalArgumentException.class)
62       .hasMessage("Invalid appId; For input string: \"abc\"");
63   }
64
65   @Test
66   public void github_validation_checks_missing_appId() {
67     AlmSettingDto almSettingDto = newGithubAlmSettingDto().setAppId(null);
68
69     assertThatThrownBy(() -> underTest.validate(almSettingDto))
70       .isInstanceOf(IllegalArgumentException.class)
71       .hasMessage("Missing appId");
72   }
73
74   @Test
75   public void github_validation_checks_missing_clientId() {
76     AlmSettingDto almSettingDto = newGithubAlmSettingDto().setClientId(null);
77
78     assertThatThrownBy(() -> underTest.validate(almSettingDto))
79       .isInstanceOf(IllegalArgumentException.class)
80       .hasMessage("Missing Client Id");
81   }
82
83   @Test
84   public void github_validation_checks_missing_clientSecret() {
85     AlmSettingDto almSettingDto = newGithubAlmSettingDto().setClientSecret(null);
86
87     assertThatThrownBy(() -> underTest.validate(almSettingDto))
88       .isInstanceOf(IllegalArgumentException.class)
89       .hasMessage("Missing Client Secret");
90
91   }
92 }