/
DestroyActionTest.java
215 lines (176 loc) · 8.42 KB
/
DestroyActionTest.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
/*
* 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.qualitygate.ws;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static java.lang.String.format;
import static java.lang.String.valueOf;
import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION;
public class DestroyActionTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
private DbClient dbClient = db.getDbClient();
private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
private QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(db.getDbClient(), userSession, organizationProvider);
private DbSession dbSession = db.getSession();
private DestroyAction underTest = new DestroyAction(dbClient, wsSupport, qualityGateFinder);
private WsActionTester ws = new WsActionTester(underTest);
@Test
public void delete_quality_gate() {
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getId().toString())
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull();
assertThat(db.countRowsOfTable(dbSession, "org_quality_gates")).isZero();
}
@Test
public void delete_quality_gate_if_non_default_when_a_default_exist() {
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate();
db.qualityGates().setDefaultQualityGate(defaultQualityGate);
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGate.getId()))
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGate.getId())).isNull();
}
@Test
public void does_not_delete_built_in_quality_gate() {
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setBuiltIn(true));
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()));
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGate.getId()))
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void default_organization_is_used_when_no_organization_parameter() {
userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
OrganizationDto otherOrganization = db.organizations().insert();
QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGate.getId()))
.execute();
assertThat(db.getDbClient().qualityGateDao().selectByOrganizationAndId(dbSession, db.getDefaultOrganization(), qualityGate.getId())).isNull();
}
@Test
public void fail_when_invalid_id() {
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
String invalidId = "invalid-id";
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(format("The 'id' parameter cannot be parsed as a long value: %s", invalidId));
ws.newRequest()
.setParam(PARAM_ID, valueOf(invalidId))
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_when_missing_id() {
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
expectedException.expect(IllegalArgumentException.class);
ws.newRequest()
.setParam(PARAM_ID, valueOf(EMPTY))
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_to_delete_default_quality_gate() {
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
db.qualityGates().setDefaultQualityGate(qualityGate);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("The default quality gate cannot be removed");
ws.newRequest()
.setParam(PARAM_ID, valueOf(qualityGate.getId()))
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_on_unknown_quality_gate() {
OrganizationDto organization = db.organizations().insert();
userSession.addPermission(ADMINISTER_QUALITY_GATES, organization);
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam(PARAM_ID, "123")
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void fail_when_not_quality_gates_administer() {
OrganizationDto organization = db.organizations().insert();
userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization);
QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam(PARAM_ID, qualityGate.getId().toString())
.setParam(PARAM_ORGANIZATION, organization.getKey())
.execute();
}
@Test
public void definition() {
WebService.Action action = ws.getDef();
assertThat(action.since()).isEqualTo("4.3");
assertThat(action.isPost()).isTrue();
assertThat(action.params())
.extracting(WebService.Param::key, WebService.Param::isRequired)
.containsExactlyInAnyOrder(
tuple("id", true),
tuple("organization", false));
}
}