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.
20 package org.sonar.server.almsettings;
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.db.DbTester;
27 import org.sonar.db.alm.setting.AlmSettingDto;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.exceptions.ForbiddenException;
30 import org.sonar.server.tester.UserSessionRule;
31 import org.sonar.server.ws.WsActionTester;
32 import org.sonarqube.ws.AlmSettings.AlmSettingGithub;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.tuple;
36 import static org.junit.rules.ExpectedException.none;
37 import static org.sonar.server.tester.UserSessionRule.standalone;
38 import static org.sonar.test.JsonAssert.assertJson;
39 import static org.sonarqube.ws.AlmSettings.ListDefinitionsWsResponse;
41 public class ListDefinitionsActionTest {
44 public ExpectedException expectedException = none();
46 public UserSessionRule userSession = standalone();
48 public DbTester db = DbTester.create();
50 private WsActionTester ws = new WsActionTester(new ListDefinitionsAction(db.getDbClient(), userSession));
53 public void list_github_settings() {
54 UserDto user = db.users().insertUser();
55 userSession.logIn(user).setSystemAdministrator();
56 AlmSettingDto almSetting1 = db.almSettings().insertGitHubAlmSetting();
57 AlmSettingDto almSetting2 = db.almSettings().insertGitHubAlmSetting();
59 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
61 assertThat(wsResponse.getGithubList())
62 .extracting(AlmSettingGithub::getKey, AlmSettingGithub::getUrl, AlmSettingGithub::getAppId, AlmSettingGithub::getPrivateKey)
63 .containsExactlyInAnyOrder(
64 tuple(almSetting1.getKey(), almSetting1.getUrl(), almSetting1.getAppId(), almSetting1.getPrivateKey()),
65 tuple(almSetting2.getKey(), almSetting2.getUrl(), almSetting2.getAppId(), almSetting2.getPrivateKey()));
69 public void return_empty_list_when_no_settings() {
70 UserDto user = db.users().insertUser();
71 userSession.logIn(user).setSystemAdministrator();
73 ListDefinitionsWsResponse wsResponse = ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
75 assertThat(wsResponse.getGithubList()).isEmpty();
79 public void fail_when_user_is_not_system_administrator() {
80 UserDto user = db.users().insertUser();
81 userSession.logIn(user);
82 db.almSettings().insertGitHubAlmSetting();
84 expectedException.expect(ForbiddenException.class);
86 ws.newRequest().executeProtobuf(ListDefinitionsWsResponse.class);
90 public void json_example() {
91 UserDto user = db.users().insertUser();
92 userSession.logIn(user).setSystemAdministrator();
93 db.almSettings().insertGitHubAlmSetting(
94 almSettingDto -> almSettingDto
95 .setKey("GitHub Server - Dev Team")
96 .setUrl("https://github.enterprise.com")
98 .setPrivateKey("54684654"));
100 db.almSettings().insertAzureAlmSetting(
101 a -> a.setKey("Azure Devops Server - Dev Team")
102 .setPersonalAccessToken("12345")
104 String response = ws.newRequest().execute().getInput();
106 assertJson(response).isSimilarTo(getClass().getResource("list_definitions-example.json"));
110 public void definition() {
111 WebService.Action def = ws.getDef();
113 assertThat(def.since()).isEqualTo("8.1");
114 assertThat(def.params()).isEmpty();
115 assertThat(def.isPost()).isFalse();