-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
CreateActionTest.java
213 lines (186 loc) · 8.04 KB
/
CreateActionTest.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
/*
* 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.rule.ws;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.config.MapSettings;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.organization.TestOrganizationFlags;
import org.sonar.server.rule.RuleCreator;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.text.MacroInterpreter;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.db.rule.RuleTesting.newCustomRule;
import static org.sonar.db.rule.RuleTesting.newTemplateRule;
import static org.sonar.server.util.TypeValidationsTesting.newFullTypeValidations;
import static org.sonar.test.JsonAssert.assertJson;
public class CreateActionTest {
private System2 system2 = mock(System2.class);
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(system2);
@Rule
public EsTester es = new EsTester(new RuleIndexDefinition(new MapSettings()));
private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone();
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private WsActionTester ws = new WsActionTester(new CreateAction(db.getDbClient(),
new RuleCreator(system2, new RuleIndexer(es.client(), db.getDbClient()), db.getDbClient(), newFullTypeValidations(),
TestDefaultOrganizationProvider.from(db)),
new RuleMapper(new Languages(), createMacroInterpreter()), organizationFlags,
new RuleWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
@Test
public void create_custom_rule() {
logInAsQProfileAdministrator();
organizationFlags.setEnabled(false);
// Template rule
RuleDto templateRule = newTemplateRule(RuleKey.of("java", "S001"), db.getDefaultOrganization());
db.rules().insert(templateRule.getDefinition());
db.rules().insertOrUpdateMetadata(templateRule.getMetadata().setRuleId(templateRule.getId()));
db.rules().insertRuleParam(templateRule.getDefinition(), param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"));
String result = ws.newRequest()
.setParam("custom_key", "MY_CUSTOM")
.setParam("template_key", templateRule.getKey().toString())
.setParam("name", "My custom rule")
.setParam("markdown_description", "Description")
.setParam("severity", "MAJOR")
.setParam("status", "BETA")
.setParam("params", "regex=a.*")
.execute().getInput();
assertJson(result).isSimilarTo("{\n" +
" \"rule\": {\n" +
" \"key\": \"java:MY_CUSTOM\",\n" +
" \"repo\": \"java\",\n" +
" \"name\": \"My custom rule\",\n" +
" \"htmlDesc\": \"Description\",\n" +
" \"severity\": \"MAJOR\",\n" +
" \"status\": \"BETA\",\n" +
" \"internalKey\": \"InternalKeyS001\",\n" +
" \"isTemplate\": false,\n" +
" \"templateKey\": \"java:S001\",\n" +
" \"sysTags\": [\"systag1\", \"systag2\"],\n" +
" \"lang\": \"js\",\n" +
" \"params\": [\n" +
" {\n" +
" \"key\": \"regex\",\n" +
" \"htmlDesc\": \"Reg ex\",\n" +
" \"defaultValue\": \"a.*\",\n" +
" \"type\": \"STRING\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n");
}
@Test
public void create_custom_rule_with_prevent_reactivation_param_to_true() {
logInAsQProfileAdministrator();
organizationFlags.setEnabled(false);
RuleDefinitionDto templateRule = newTemplateRule(RuleKey.of("java", "S001")).getDefinition();
db.rules().insert(templateRule);
// insert a removed rule
RuleDefinitionDto customRule = newCustomRule(templateRule)
.setRuleKey("MY_CUSTOM")
.setStatus(RuleStatus.REMOVED)
.setName("My custom rule")
.setDescription("Description")
.setDescriptionFormat(RuleDto.Format.MARKDOWN)
.setSeverity(Severity.MAJOR);
db.rules().insert(customRule);
TestResponse response = ws.newRequest()
.setParam("custom_key", "MY_CUSTOM")
.setParam("template_key", templateRule.getKey().toString())
.setParam("name", "My custom rule")
.setParam("markdown_description", "Description")
.setParam("severity", "MAJOR")
.setParam("prevent_reactivation", "true")
.execute();
assertThat(response.getStatus()).isEqualTo(409);
assertJson(response.getInput()).isSimilarTo("{\n" +
" \"rule\": {\n" +
" \"key\": \"java:MY_CUSTOM\",\n" +
" \"repo\": \"java\",\n" +
" \"name\": \"My custom rule\",\n" +
" \"htmlDesc\": \"Description\",\n" +
" \"severity\": \"MAJOR\",\n" +
" \"status\": \"REMOVED\",\n" +
" \"isTemplate\": false\n" +
" }\n" +
"}\n");
}
@Test
public void fail_to_create_rule_when_organizations_are_enabled() throws Exception {
logInAsQProfileAdministrator();
organizationFlags.setEnabled(true);
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Organization support is enabled");
ws.newRequest()
.setParam("custom_key", "MY_CUSTOM")
.setParam("template_key", "java:S001")
.setParam("name", "My custom rule")
.setParam("markdown_description", "Description")
.setParam("severity", "MAJOR")
.execute();
}
@Test
public void throw_ForbiddenException_if_not_profile_administrator() throws Exception {
userSession.logIn();
expectedException.expect(ForbiddenException.class);
ws.newRequest().execute();
}
@Test
public void throw_UnauthorizedException_if_not_logged_in() throws Exception {
expectedException.expect(UnauthorizedException.class);
ws.newRequest().execute();
}
private static MacroInterpreter createMacroInterpreter() {
MacroInterpreter macroInterpreter = mock(MacroInterpreter.class);
doAnswer(returnsFirstArg()).when(macroInterpreter).interpret(anyString());
return macroInterpreter;
}
private void logInAsQProfileAdministrator() {
userSession
.logIn()
.addPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganizationProvider.get().getUuid());
}
}