]> source.dussan.org Git - sonarqube.git/blob
8ea3c03fc4570562efcb13e1a386748d154ca4cd
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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
21 package org.sonar.server.almsettings;
22
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;
37
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;
41
42 public class CountBindingActionTest {
43
44   @Rule
45   public ExpectedException expectedException = ExpectedException.none();
46   @Rule
47   public UserSessionRule userSession = UserSessionRule.standalone();
48   @Rule
49   public DbTester db = DbTester.create();
50
51   private WsActionTester ws = new WsActionTester(new CountBindingAction(db.getDbClient(), userSession,
52     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null))));
53
54   @Test
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);
63
64     CountBindingWsResponse response = ws.newRequest()
65       .setParam("almSetting", githubAlmSetting.getKey())
66       .executeProtobuf(CountBindingWsResponse.class);
67
68     assertThat(response.getKey()).isEqualTo(githubAlmSetting.getKey());
69     assertThat(response.getProjects()).isEqualTo(2);
70   }
71
72   @Test
73   public void fail_when_alm_setting_does_not_exist() {
74     UserDto user = db.users().insertUser();
75     userSession.logIn(user).setSystemAdministrator();
76
77     expectedException.expect(NotFoundException.class);
78     expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
79
80     ws.newRequest()
81       .setParam("almSetting", "unknown")
82       .execute();
83   }
84
85   @Test
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();
90
91     expectedException.expect(ForbiddenException.class);
92
93     ws.newRequest()
94       .setParam("almSetting", githubAlmSetting.getKey())
95       .execute();
96   }
97
98   @Test
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")
105         .setAppId("12345")
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());
110
111     String response = ws.newRequest()
112       .setParam("almSetting", githubAlmSetting.getKey())
113       .execute().getInput();
114
115     assertJson(response).isSimilarTo(getClass().getResource("count_binding-example.json"));
116   }
117
118   @Test
119   public void definition() {
120     WebService.Action def = ws.getDef();
121
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));
127   }
128
129 }