-
Notifications
You must be signed in to change notification settings - Fork 2k
/
QProfilesWsTest.java
216 lines (194 loc) · 8.48 KB
/
QProfilesWsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.qualityprofile.ws;
import java.io.Reader;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.profiles.ProfileImporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.ValidationMessages;
import org.sonar.db.DbClient;
import org.sonar.server.language.LanguageTesting;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
public class QProfilesWsTest {
@Rule
public UserSessionRule userSessionRule = UserSessionRule.standalone();
private WebService.Controller controller;
private String xoo1Key = "xoo1";
private String xoo2Key = "xoo2";
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.fromUuid("ORG1");
private QProfileWsSupport wsSupport = new QProfileWsSupport(mock(DbClient.class), userSessionRule, defaultOrganizationProvider);
@Before
public void setUp() {
DbClient dbClient = mock(DbClient.class);
Languages languages = LanguageTesting.newLanguages(xoo1Key, xoo2Key);
ProfileImporter[] importers = createImporters(languages);
controller = new WsTester(new QProfilesWs(
new CreateAction(null, null, null, languages, wsSupport, userSessionRule, null, importers),
new ImportersAction(importers),
new SearchAction(null, languages, dbClient, wsSupport),
new SetDefaultAction(languages, null, null, wsSupport),
new ProjectsAction(null, userSessionRule),
new ChangelogAction(null, wsSupport, languages, dbClient),
new ChangeParentAction(dbClient, null, languages, wsSupport, userSessionRule),
new CompareAction(null, null, languages),
new DeleteAction(languages, null, null, userSessionRule, wsSupport),
new ExportersAction(),
new InheritanceAction(null, null, null, languages),
new RenameAction(dbClient, userSessionRule))).controller(QProfilesWs.API_ENDPOINT);
}
private ProfileImporter[] createImporters(Languages languages) {
class NoopImporter extends ProfileImporter {
public NoopImporter(Language language) {
super(language.getKey(), language.getName());
}
@Override
public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
return null;
}
}
return new ProfileImporter[] {
new NoopImporter(languages.get(xoo1Key)),
new NoopImporter(languages.get(xoo2Key))
};
}
@Test
public void define_controller() {
assertThat(controller).isNotNull();
assertThat(controller.path()).isEqualTo(QProfilesWs.API_ENDPOINT);
assertThat(controller.description()).isNotEmpty();
assertThat(controller.actions()).isNotEmpty();
}
@Test
public void define_search() {
WebService.Action search = controller.action("search");
assertThat(search).isNotNull();
assertThat(search.isPost()).isFalse();
assertThat(search.params()).hasSize(5);
assertThat(search.param("language").possibleValues()).containsOnly(xoo1Key, xoo2Key);
assertThat(search.param("language").deprecatedSince()).isEqualTo("6.4");
assertThat(search.param("profileName").deprecatedSince()).isEqualTo("6.4");
assertThat(search.param("organization").since()).isEqualTo("6.4");
}
@Test
public void define_set_default_action() {
WebService.Action setDefault = controller.action("set_default");
assertThat(setDefault).isNotNull();
assertThat(setDefault.isPost()).isTrue();
assertThat(setDefault.params()).hasSize(4);
assertThat(setDefault.param("organization").since()).isEqualTo("6.4");
}
@Test
public void define_projects_action() {
WebService.Action projects = controller.action("projects");
assertThat(projects).isNotNull();
assertThat(projects.isPost()).isFalse();
assertThat(projects.params()).hasSize(5);
assertThat(projects.responseExampleAsString()).isNotEmpty();
}
@Test
public void define_create_action() {
WebService.Action create = controller.action("create");
assertThat(create).isNotNull();
assertThat(create.isPost()).isTrue();
assertThat(create.params()).hasSize(5);
assertThat(create.param("organization")).isNotNull();
assertThat(create.param("organization").isRequired()).isFalse();
assertThat(create.param("organization").isInternal()).isTrue();
assertThat(create.param("profileName")).isNotNull();
assertThat(create.param("profileName").isRequired()).isTrue();
assertThat(create.param("language").possibleValues()).containsOnly(xoo1Key, xoo2Key);
assertThat(create.param("language").isRequired()).isTrue();
assertThat(create.param("backup_" + xoo1Key)).isNotNull();
assertThat(create.param("backup_" + xoo1Key).isRequired()).isFalse();
assertThat(create.param("backup_" + xoo2Key)).isNotNull();
assertThat(create.param("backup_" + xoo2Key).isRequired()).isFalse();
}
@Test
public void define_importers_action() {
WebService.Action importers = controller.action("importers");
assertThat(importers).isNotNull();
assertThat(importers.isPost()).isFalse();
assertThat(importers.params()).isEmpty();
assertThat(importers.responseExampleAsString()).isNotEmpty();
}
@Test
public void define_changelog_action() {
WebService.Action changelog = controller.action("changelog");
assertThat(changelog).isNotNull();
assertThat(changelog.isPost()).isFalse();
assertThat(changelog.params().size()).isPositive();
assertThat(changelog.responseExampleAsString()).isNotEmpty();
assertThat(changelog.param("organization").since()).isEqualTo("6.4");
assertThat(changelog.param("organization").isInternal()).isTrue();
}
@Test
public void define_compare_action() {
WebService.Action compare = controller.action("compare");
assertThat(compare).isNotNull();
assertThat(compare.isPost()).isFalse();
assertThat(compare.isInternal()).isTrue();
assertThat(compare.params()).hasSize(2).extracting("key").containsOnly(
"leftKey", "rightKey");
assertThat(compare.responseExampleAsString()).isNotEmpty();
}
@Test
public void define_delete_action() {
WebService.Action delete = controller.action("delete");
assertThat(delete).isNotNull();
assertThat(delete.isPost()).isTrue();
assertThat(delete.params()).hasSize(4).extracting("key").containsOnly(
"organization", "profileKey", "language", "profileName");
}
@Test
public void define_exporters_action() {
WebService.Action exporters = controller.action("exporters");
assertThat(exporters).isNotNull();
assertThat(exporters.isPost()).isFalse();
assertThat(exporters.params()).isEmpty();
assertThat(exporters.responseExampleAsString()).isNotEmpty();
}
@Test
public void define_inheritance_action() {
WebService.Action inheritance = controller.action("inheritance");
assertThat(inheritance).isNotNull();
assertThat(inheritance.isPost()).isFalse();
assertThat(inheritance.params()).hasSize(4).extracting("key").containsExactlyInAnyOrder(
"organization", "profileKey", "language", "profileName");
assertThat(inheritance.responseExampleAsString()).isNotEmpty();
}
@Test
public void define_rename_action() {
WebService.Action rename = controller.action("rename");
assertThat(rename).isNotNull();
assertThat(rename.isPost()).isTrue();
assertThat(rename.params()).hasSize(2).extracting("key").containsOnly(
"key", "name");
}
}