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.almsettings.ws;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.alm.setting.AlmSettingDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.almsettings.ws.ListDefinitionsAction;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.tester.UserSessionRule;
33 import org.sonar.server.ws.WsActionTester;
34 import org.sonarqube.ws.AlmSettings;
35 import org.sonarqube.ws.AlmSettings.AlmSettingAzure;
36 import org.sonarqube.ws.AlmSettings.AlmSettingGithub;
37 import org.sonarqube.ws.AlmSettings.AlmSettingGitlab;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.core.api.Assertions.tuple;
41 import static org.junit.rules.ExpectedException.none;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.server.tester.UserSessionRule.standalone;
45 import static org.sonar.test.JsonAssert.assertJson;
46 import static org.sonarqube.ws.AlmSettings.ListDefinitionsWsResponse;
48 public class ListDefinitionsActionTest {
51 public ExpectedException expectedException = none();
53 public UserSessionRule userSession = standalone();
55 private final System2 system2 = mock(System2.class);
58 public DbTester db = DbTester.create(system2);
60 private final WsActionTester ws = new WsActionTester(new ListDefinitionsAction(db.getDbClient(), userSession));
63 public void list_github_settings() {
64 UserDto user = db.users().insertUser();
65 userSession.logIn(user).setSystemAdministrator();
66 AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting();
67 AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_id").setClientSecret("client_secret"));
69 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
71 assertThat(wsResponse.getGithubList())
72 .extracting(AlmSettingGithub::getKey, AlmSettingGithub::getUrl, AlmSettingGithub::getAppId, AlmSettingGithub::getClientId)
73 .containsExactlyInAnyOrder(
74 tuple(almSetting1.getKey(), almSetting1.getUrl(), almSetting1.getAppId(), ""),
75 tuple(almSetting2.getKey(), almSetting2.getUrl(), almSetting2.getAppId(), "client_id"));
79 public void list_gitlab_settings() {
80 UserDto user = db.users().insertUser();
81 userSession.logIn(user).setSystemAdministrator();
82 AlmSettingDto almSetting1 = db.almSettings().insertGitlabAlmSetting();
83 AlmSettingDto almSetting2 = db.almSettings().insertGitlabAlmSetting(setting -> setting.setUrl(null));
85 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
87 assertThat(wsResponse.getGitlabList())
88 .extracting(AlmSettingGitlab::getKey, AlmSettingGitlab::getUrl)
89 .containsExactlyInAnyOrder(
90 tuple(almSetting1.getKey(), almSetting1.getUrl()),
91 tuple(almSetting2.getKey(), ""));
95 public void list_azure_settings() {
96 UserDto user = db.users().insertUser();
97 userSession.logIn(user).setSystemAdministrator();
98 AlmSettingDto almSetting1 = db.almSettings().insertAzureAlmSetting();
99 AlmSettingDto almSetting2 = db.almSettings().insertAzureAlmSetting(setting -> setting.setUrl(null));
101 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
103 assertThat(wsResponse.getAzureList())
104 .extracting(AlmSettingAzure::getKey, AlmSettingAzure::getUrl)
105 .containsExactlyInAnyOrder(
106 tuple(almSetting1.getKey(), almSetting1.getUrl()),
107 tuple(almSetting2.getKey(), ""));
111 public void list_bitbucket_cloud_settings() {
112 UserDto user = db.users().insertUser();
113 userSession.logIn(user).setSystemAdministrator();
114 AlmSettingDto almSetting1 = db.almSettings().insertBitbucketCloudAlmSetting(alm -> alm.setClientId("1").setClientSecret("2"));
115 AlmSettingDto almSetting2 = db.almSettings().insertBitbucketCloudAlmSetting(alm -> alm.setClientId("client_id").setClientSecret("client_secret"));
117 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
119 assertThat(wsResponse.getBitbucketcloudList())
120 .extracting(AlmSettings.AlmSettingBitbucketCloud::getKey, AlmSettings.AlmSettingBitbucketCloud::getClientId)
121 .containsExactlyInAnyOrder(
122 tuple(almSetting1.getKey(), "1"),
123 tuple(almSetting2.getKey(), "client_id"));
127 public void list_is_ordered_by_create_date() {
128 UserDto user = db.users().insertUser();
129 userSession.logIn(user).setSystemAdministrator();
130 when(system2.now()).thenReturn(10_000_000_000L);
131 AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting();
132 when(system2.now()).thenReturn(30_000_000_000L);
133 AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting();
134 when(system2.now()).thenReturn(20_000_000_000L);
135 AlmSettingDto almSetting3 = db.almSettings().insertGitHubAlmSetting();
137 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
139 assertThat(wsResponse.getGithubList())
140 .extracting(AlmSettingGithub::getKey)
141 .containsExactly(almSetting1.getKey(), almSetting3.getKey(), almSetting2.getKey());
145 public void return_empty_list_when_no_settings() {
146 UserDto user = db.users().insertUser();
147 userSession.logIn(user).setSystemAdministrator();
149 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
151 assertThat(wsResponse.getGithubList()).isEmpty();
155 public void fail_when_user_is_not_system_administrator() {
156 UserDto user = db.users().insertUser();
157 userSession.logIn(user);
158 db.almSettings().insertGitHubAlmSetting();
160 expectedException.expect(ForbiddenException.class);
162 ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
166 public void json_example() {
167 UserDto user = db.users().insertUser();
168 userSession.logIn(user).setSystemAdministrator();
169 db.almSettings().insertGitHubAlmSetting(
170 almSettingDto -> almSettingDto
171 .setKey("GitHub Server - Dev Team")
172 .setUrl("https://github.enterprise.com")
174 .setPrivateKey("54684654")
175 .setClientId("client_id")
176 .setClientSecret("client_secret"));
177 db.almSettings().insertAzureAlmSetting(
178 a -> a.setKey("Azure Devops Server - Dev Team")
179 .setPersonalAccessToken("12345")
180 .setUrl("https://ado.sonarqube.com/"));
181 db.almSettings().insertBitbucketAlmSetting(
182 a -> a.setKey("Bitbucket Server - Dev Team")
183 .setUrl("https://bitbucket.enterprise.com")
184 .setPersonalAccessToken("abcdef"));
185 db.almSettings().insertGitlabAlmSetting(
186 a -> a.setKey("Gitlab - Dev Team")
187 .setPersonalAccessToken("12345"));
189 String response = ws.newRequest().execute().getInput();
191 assertJson(response).isSimilarTo(getClass().getResource("list_definitions-example.json"));
195 public void definition() {
196 WebService.Action def = ws.getDef();
198 assertThat(def.since()).isEqualTo("8.1");
199 assertThat(def.params()).isEmpty();
200 assertThat(def.changelog()).hasSize(3);
201 assertThat(def.isPost()).isFalse();
202 assertThat(def.responseExampleAsString()).isNotEmpty();