3 * Copyright (C) 2009-2021 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.almintegration.validator;
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;
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;
34 public class GithubGlobalSettingsValidatorTest {
35 private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class);
36 private final GithubGlobalSettingsValidator underTest = new GithubGlobalSettingsValidator(appClient);
39 public void github_global_settings_validation() {
40 AlmSettingDto almSettingDto = newGithubAlmSettingDto()
41 .setClientId("clientId")
42 .setClientSecret("clientSecret");
44 GithubAppConfiguration configuration = underTest.validate(almSettingDto);
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());
54 public void github_validation_checks_invalid_appId() {
55 AlmSettingDto almSettingDto = newGithubAlmSettingDto()
57 .setClientId("clientId")
58 .setClientSecret("clientSecret");
60 assertThatThrownBy(() -> underTest.validate(almSettingDto))
61 .isInstanceOf(IllegalArgumentException.class)
62 .hasMessage("Invalid appId; For input string: \"abc\"");
66 public void github_validation_checks_missing_appId() {
67 AlmSettingDto almSettingDto = newGithubAlmSettingDto().setAppId(null);
69 assertThatThrownBy(() -> underTest.validate(almSettingDto))
70 .isInstanceOf(IllegalArgumentException.class)
71 .hasMessage("Missing appId");
75 public void github_validation_checks_missing_clientId() {
76 AlmSettingDto almSettingDto = newGithubAlmSettingDto().setClientId(null);
78 assertThatThrownBy(() -> underTest.validate(almSettingDto))
79 .isInstanceOf(IllegalArgumentException.class)
80 .hasMessage("Missing Client Id");
84 public void github_validation_checks_missing_clientSecret() {
85 AlmSettingDto almSettingDto = newGithubAlmSettingDto().setClientSecret(null);
87 assertThatThrownBy(() -> underTest.validate(almSettingDto))
88 .isInstanceOf(IllegalArgumentException.class)
89 .hasMessage("Missing Client Secret");