3 * Copyright (C) 2009-2019 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.
21 package org.sonar.server.almsettings;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.alm.setting.AlmSettingDto;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.user.UserDto;
31 import org.sonar.server.component.ComponentFinder;
32 import org.sonar.server.exceptions.ForbiddenException;
33 import org.sonar.server.exceptions.NotFoundException;
34 import org.sonar.server.tester.UserSessionRule;
35 import org.sonar.server.ws.WsActionTester;
36 import org.sonarqube.ws.AlmSettings.CountBindingWsResponse;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.groups.Tuple.tuple;
40 import static org.sonar.test.JsonAssert.assertJson;
42 public class CountBindingActionTest {
45 public ExpectedException expectedException = ExpectedException.none();
47 public UserSessionRule userSession = UserSessionRule.standalone();
49 public DbTester db = DbTester.create();
51 private WsActionTester ws = new WsActionTester(new CountBindingAction(db.getDbClient(), userSession,
52 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null))));
55 public void count_binding() {
56 UserDto user = db.users().insertUser();
57 userSession.logIn(user).setSystemAdministrator();
58 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
59 ComponentDto project1 = db.components().insertPrivateProject();
60 ComponentDto project2 = db.components().insertPrivateProject();
61 db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, project1);
62 db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, project2);
64 CountBindingWsResponse response = ws.newRequest()
65 .setParam("almSetting", githubAlmSetting.getKey())
66 .executeProtobuf(CountBindingWsResponse.class);
68 assertThat(response.getKey()).isEqualTo(githubAlmSetting.getKey());
69 assertThat(response.getProjects()).isEqualTo(2);
73 public void fail_when_alm_setting_does_not_exist() {
74 UserDto user = db.users().insertUser();
75 userSession.logIn(user).setSystemAdministrator();
77 expectedException.expect(NotFoundException.class);
78 expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
81 .setParam("almSetting", "unknown")
86 public void fail_when_missing_system_administer_permission() {
87 UserDto user = db.users().insertUser();
88 userSession.logIn(user);
89 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
91 expectedException.expect(ForbiddenException.class);
94 .setParam("almSetting", githubAlmSetting.getKey())
99 public void json_example() {
100 UserDto user = db.users().insertUser();
101 userSession.logIn(user).setSystemAdministrator();
102 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting(
103 almSettingDto -> almSettingDto
104 .setKey("GitHub Server - Dev Team")
106 .setPrivateKey("54684654"));
107 db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProject());
108 db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProject());
109 db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, db.components().insertPrivateProject());
111 String response = ws.newRequest()
112 .setParam("almSetting", githubAlmSetting.getKey())
113 .execute().getInput();
115 assertJson(response).isSimilarTo(getClass().getResource("count_binding-example.json"));
119 public void definition() {
120 WebService.Action def = ws.getDef();
122 assertThat(def.since()).isEqualTo("8.1");
123 assertThat(def.isPost()).isFalse();
124 assertThat(def.params())
125 .extracting(WebService.Param::key, WebService.Param::isRequired)
126 .containsExactlyInAnyOrder(tuple("almSetting", true));