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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact 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 it.settings;
import com.google.gson.Gson;
import com.sonar.orchestrator.Orchestrator;
import it.Category1Suite;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsResponse;
import util.user.UserRule;
import static java.net.URLEncoder.encode;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static util.ItUtils.newAdminWsClient;
import static util.ItUtils.newUserWsClient;
import static util.ItUtils.newWsClient;
import static util.ItUtils.resetSettings;
import static util.ItUtils.runProjectAnalysis;
public class DeprecatedPropertiesWsTest {
private final static String PROJECT_KEY = "com.sonarsource.it.samples:multi-modules-sample";
private static final String MODULE_KEY = "com.sonarsource.it.samples:multi-modules-sample:module_a";
private static final String SUB_MODULE_KEY = "com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1";
private static final String PROJECT_SETTING_KEY = "sonar.dbcleaner.hoursBeforeKeepingOnlyOneSnapshotByDay";
private static String USER_LOGIN = "john";
@ClassRule
public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
@ClassRule
public static UserRule userRule = UserRule.from(orchestrator);
static WsClient adminWsClient;
static WsClient userWsClient;
static WsClient anonymousWsClient;
@BeforeClass
public static void init() throws Exception {
orchestrator.resetData();
userRule.createUser(USER_LOGIN, "password");
adminWsClient = newAdminWsClient(orchestrator);
userWsClient = newUserWsClient(orchestrator, USER_LOGIN, "password");
anonymousWsClient = newWsClient(orchestrator);
runProjectAnalysis(orchestrator, "shared/xoo-multi-modules-sample");
}
@AfterClass
public static void resetAfterClass() throws Exception {
doResetSettings();
userRule.deactivateUsers(USER_LOGIN);
}
@Before
public void resetBefore() throws Exception {
doResetSettings();
}
private static void doResetSettings() {
resetSettings(orchestrator, null, "some-property", "int", "multi", "boolean", "hidden", "not_defined", "setting.secured", "setting.license", "list");
resetSettings(orchestrator, PROJECT_KEY, PROJECT_SETTING_KEY, "sonar.coverage.exclusions");
}
@Test
public void get_default_global_value() throws Exception {
assertThat(getProperty("some-property", null).getValue()).isEqualTo("aDefaultValue");
}
@Test
public void get_and_set_global_value() throws Exception {
setProperty("some-property", "value", null);
assertThat(getProperty("some-property", null).getValue()).isEqualTo("value");
}
@Test
public void get_and_set_multi_values() throws Exception {
setProperty("multi", "value1,value2", null);
Properties.Property setting = getProperty("multi", null);
assertThat(setting.getValue()).isEqualTo("value1,value2");
assertThat(setting.getValues()).containsOnly("value1", "value2");
}
@Test
public void get_and_set_hidden_setting() throws Exception {
setProperty("hidden", "value", null);
assertThat(getProperty("hidden", null).getValue()).isEqualTo("value");
}
@Test
public void get_and_set_secured_setting() throws Exception {
setProperty("setting.secured", "value", null);
assertThat(getProperty("setting.secured", null).getValue()).isEqualTo("value");
}
@Test
public void get_and_set_license_setting() throws Exception {
setProperty("setting.license", "value", null);
assertThat(getProperty("setting.license", null).getValue()).isEqualTo("value");
}
@Test
public void get_and_set_not_defined_setting() throws Exception {
setProperty("not_defined", "value", null);
assertThat(getProperty("not_defined", null).getValue()).isEqualTo("value");
}
@Test
public void secured_setting_not_returned_to_not_admin() throws Exception {
setProperty("setting.secured", "value", null);
// Admin can see the secured setting
assertThat(getProperties(null)).extracting(Properties.Property::getKey).contains("setting.secured");
// Not admin cannot see the secured setting
assertThat(getProperties(userWsClient, null)).extracting(Properties.Property::getKey).doesNotContain("setting.secured");
assertThat(getProperties(anonymousWsClient, null)).extracting(Properties.Property::getKey).doesNotContain("setting.secured");
}
@Test
public void license_setting_not_returned_to_not_logged() throws Exception {
setProperty("setting.license", "value", null);
// Admin and user can see the license setting
assertThat(getProperties(null)).extracting(Properties.Property::getKey).contains("setting.license");
assertThat(getProperties(userWsClient, null)).extracting(Properties.Property::getKey).contains("setting.license");
// Anonymous cannot see the license setting
// FIXME Don't understand why it fails ???
// assertThat(getProperties(anonymousWsClient, null)).extracting(Properties.Property::getKey).doesNotContain("setting.license");
}
@Test
public void validate_setting() throws Exception {
assertUpdateFails("list", "Z", "Not a valid option");
assertUpdateFails("int", "not an int", "Only digits are allowed");
assertUpdateFails("boolean", "not a boolean", "Valid options are \\\"true\\\" and \\\"false\\\"");
}
@Test
public void delete_global_value() throws Exception {
setProperty("int", "10", null);
deleteProperty("int", null);
assertPropertyDoesNotExist("int", null);
}
@Test
public void get_all_global_settings() throws Exception {
List<Properties.Property> properties = getProperties(null);
assertThat(properties).isNotEmpty();
assertThat(properties).extracting("key")
.contains("sonar.core.id", "some-property", "boolean")
.doesNotContain("hidden");
}
@Test
public void get_default_component_value() throws Exception {
// Check default value is returned
assertThat(getProperty(PROJECT_SETTING_KEY, PROJECT_KEY).getValue()).isEqualTo("24");
assertThat(getProperty(PROJECT_SETTING_KEY, MODULE_KEY).getValue()).isEqualTo("24");
assertThat(getProperty(PROJECT_SETTING_KEY, SUB_MODULE_KEY).getValue()).isEqualTo("24");
}
@Test
public void get_global_component_value() throws Exception {
// Check global value is returned
setProperty(PROJECT_SETTING_KEY, "30", null);
assertThat(getProperty(PROJECT_SETTING_KEY, PROJECT_KEY).getValue()).isEqualTo("30");
assertThat(getProperty(PROJECT_SETTING_KEY, MODULE_KEY).getValue()).isEqualTo("30");
assertThat(getProperty(PROJECT_SETTING_KEY, SUB_MODULE_KEY).getValue()).isEqualTo("30");
}
@Test
public void get_and_set_component_value() throws Exception {
setProperty("sonar.coverage.exclusions", "file", PROJECT_KEY);
assertThat(getProperty("sonar.coverage.exclusions", PROJECT_KEY).getValue()).isEqualTo("file");
}
@Test
public void delete_component_value() throws Exception {
setProperty("sonar.coverage.exclusions", "file", PROJECT_KEY);
deleteProperty("sonar.coverage.exclusions", PROJECT_KEY);
assertPropertyDoesNotExist("sonar.coverage.exclusions", PROJECT_KEY);
}
@Test
public void get_all_component_settings() throws Exception {
List<Properties.Property> properties = getProperties(PROJECT_KEY);
assertThat(properties).isNotEmpty();
assertThat(properties).extracting("key")
.contains("sonar.dbcleaner.cleanDirectory", "sonar.dbcleaner.weeksBeforeDeletingAllSnapshots")
.doesNotContain("hidden");
}
private static void setProperty(String key, String value, @Nullable String componentKey) {
adminWsClient.wsConnector().call(
new PostRequest("api/properties")
.setParam("id", key)
.setParam("value", value)
.setParam("resource", componentKey))
.failIfNotSuccessful();
}
private static void deleteProperty(String key, @Nullable String componentKey) {
adminWsClient.wsConnector().call(
new PostRequest("api/properties/destroy")
.setParam("id", key)
.setParam("resource", componentKey))
.failIfNotSuccessful();
}
private static List<Properties.Property> getProperties(@Nullable String componentKey) {
return getProperties(adminWsClient, componentKey);
}
private static List<Properties.Property> getProperties(WsClient wsClient, @Nullable String componentKey) {
WsResponse response = wsClient.wsConnector()
.call(new GetRequest("api/properties")
.setParam("resource", componentKey))
.failIfNotSuccessful();
return asList(Properties.parse(response.content()));
}
private static Properties.Property getProperty(String key, @Nullable String componentKey) throws UnsupportedEncodingException {
return getProperty(adminWsClient, key, componentKey);
}
private static Properties.Property getProperty(WsClient wsClient, String key, @Nullable String componentKey) throws UnsupportedEncodingException {
WsResponse response = wsClient.wsConnector()
.call(new GetRequest("api/properties/" + encode(key, "UTF-8"))
.setParam("resource", componentKey))
.failIfNotSuccessful();
Properties.Property[] properties = Properties.parse(response.content());
return Arrays.stream(properties).findFirst().orElseThrow(() -> new IllegalArgumentException("Property does not exist : " + key));
}
private static void assertPropertyDoesNotExist(String key, @Nullable String componentKey) {
Optional<Properties.Property> property = getProperties(componentKey).stream().filter(p -> p.getKey().equals(key)).findFirst();
assertThat(property.isPresent()).isFalse();
}
private static void assertUpdateFails(String key, String value, String expectedError) {
WsResponse response = adminWsClient.wsConnector().call(
new PostRequest("api/properties")
.setParam("id", key)
.setParam("value", value));
assertThat(response.code()).isEqualTo(400);
assertThat(response.content()).contains(expectedError);
}
public static class Properties {
private List<Property> properties;
private Properties(List<Property> properties) {
this.properties = properties;
}
public List<Property> getProperties() {
return properties;
}
public static Property[] parse(String json) {
Gson gson = new Gson();
return gson.fromJson(json, Property[].class);
}
public static class Property {
private final String key;
private final String value;
private final String[] values;
private Property(String key, String value, String[] values) {
this.key = key;
this.value = value;
this.values = values;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public String[] getValues() {
return values;
}
}
}
}
|