3 * Copyright (C) 2009-2022 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;
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;
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;
43 public class CountBindingActionTest {
46 public UserSessionRule userSession = UserSessionRule.standalone();
48 public DbTester db = DbTester.create();
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))));
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);
64 CountBindingWsResponse response = ws.newRequest()
65 .setParam("almSetting", almSetting.getKey())
66 .executeProtobuf(CountBindingWsResponse.class);
68 assertThat(response.getKey()).isEqualTo(almSetting.getKey());
69 assertThat(response.getProjects()).isEqualTo(2);
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);
80 CountBindingWsResponse response = ws.newRequest()
81 .setParam("almSetting", almSetting.getKey())
82 .executeProtobuf(CountBindingWsResponse.class);
84 assertThat(response.getKey()).isEqualTo(almSetting.getKey());
85 assertThat(response.getProjects()).isOne();
89 public void fail_when_alm_setting_does_not_exist() {
90 UserDto user = db.users().insertUser();
91 userSession.logIn(user).setSystemAdministrator();
93 assertThatThrownBy(() -> ws.newRequest()
94 .setParam("almSetting", "unknown")
96 .isInstanceOf(NotFoundException.class)
97 .hasMessageContaining("ALM setting with key 'unknown' cannot be found");
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();
106 assertThatThrownBy(() -> ws.newRequest()
107 .setParam("almSetting", githubAlmSetting.getKey())
109 .isInstanceOf(ForbiddenException.class);
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")
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());
125 String response = ws.newRequest()
126 .setParam("almSetting", githubAlmSetting.getKey())
127 .execute().getInput();
129 assertJson(response).isSimilarTo(getClass().getResource("count_binding-example.json"));
133 public void definition() {
134 WebService.Action def = ws.getDef();
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));