]> source.dussan.org Git - sonarqube.git/blob
5304321981b837477b8c806f1a062941e05fb1ba
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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 org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.server.ws.WebService;
25 import org.sonar.db.DbTester;
26 import org.sonar.db.alm.setting.AlmSettingDto;
27 import org.sonar.db.project.ProjectDto;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
30 import org.sonar.server.component.ComponentFinder;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.exceptions.NotFoundException;
33 import org.sonar.server.tester.UserSessionRule;
34 import org.sonar.server.ws.WsActionTester;
35 import org.sonarqube.ws.AlmSettings.CountBindingWsResponse;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatThrownBy;
39 import static org.assertj.core.groups.Tuple.tuple;
40 import static org.mockito.Mockito.mock;
41 import static org.sonar.test.JsonAssert.assertJson;
42
43 public class CountBindingActionTest {
44
45   @Rule
46   public UserSessionRule userSession = UserSessionRule.standalone();
47   @Rule
48   public DbTester db = DbTester.create();
49
50   private WsActionTester ws = new WsActionTester(new CountBindingAction(db.getDbClient(), userSession,
51     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
52       mock(MultipleAlmFeatureProvider.class))));
53
54   @Test
55   public void count_github_binding() {
56     UserDto user = db.users().insertUser();
57     userSession.logIn(user).setSystemAdministrator();
58     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
59     ProjectDto project1 = db.components().insertPrivateProjectDto();
60     ProjectDto project2 = db.components().insertPrivateProjectDto();
61     db.almSettings().insertGitHubProjectAlmSetting(almSetting, project1);
62     db.almSettings().insertGitHubProjectAlmSetting(almSetting, project2);
63
64     CountBindingWsResponse response = ws.newRequest()
65       .setParam("almSetting", almSetting.getKey())
66       .executeProtobuf(CountBindingWsResponse.class);
67
68     assertThat(response.getKey()).isEqualTo(almSetting.getKey());
69     assertThat(response.getProjects()).isEqualTo(2);
70   }
71
72   @Test
73   public void count_azure_binding() {
74     UserDto user = db.users().insertUser();
75     userSession.logIn(user).setSystemAdministrator();
76     AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
77     ProjectDto project1 = db.components().insertPrivateProjectDto();
78     db.almSettings().insertAzureProjectAlmSetting(almSetting, project1);
79
80     CountBindingWsResponse response = ws.newRequest()
81       .setParam("almSetting", almSetting.getKey())
82       .executeProtobuf(CountBindingWsResponse.class);
83
84     assertThat(response.getKey()).isEqualTo(almSetting.getKey());
85     assertThat(response.getProjects()).isOne();
86   }
87
88   @Test
89   public void fail_when_alm_setting_does_not_exist() {
90     UserDto user = db.users().insertUser();
91     userSession.logIn(user).setSystemAdministrator();
92
93     assertThatThrownBy(() -> ws.newRequest()
94       .setParam("almSetting", "unknown")
95       .execute())
96       .isInstanceOf(NotFoundException.class)
97       .hasMessageContaining("ALM setting with key 'unknown' cannot be found");
98   }
99
100   @Test
101   public void fail_when_missing_system_administer_permission() {
102     UserDto user = db.users().insertUser();
103     userSession.logIn(user);
104     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
105
106     assertThatThrownBy(() -> ws.newRequest()
107       .setParam("almSetting", githubAlmSetting.getKey())
108       .execute())
109       .isInstanceOf(ForbiddenException.class);
110   }
111
112   @Test
113   public void json_example() {
114     UserDto user = db.users().insertUser();
115     userSession.logIn(user).setSystemAdministrator();
116     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(
117       almSettingDto -> almSettingDto
118         .setKey("GitHub Server - Dev Team")
119         .setAppId("12345")
120         .setPrivateKey("54684654"));
121     db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProjectDto());
122     db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProjectDto());
123     db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProjectDto());
124
125     String response = ws.newRequest()
126       .setParam("almSetting", githubAlmSetting.getKey())
127       .execute().getInput();
128
129     assertJson(response).isSimilarTo(getClass().getResource("count_binding-example.json"));
130   }
131
132   @Test
133   public void definition() {
134     WebService.Action def = ws.getDef();
135
136     assertThat(def.since()).isEqualTo("8.1");
137     assertThat(def.isPost()).isFalse();
138     assertThat(def.params())
139       .extracting(WebService.Param::key, WebService.Param::isRequired)
140       .containsExactlyInAnyOrder(tuple("almSetting", true));
141   }
142
143 }