2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.wsclient.qualitygate.internal;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.wsclient.MockHttpServerInterceptor;
25 import org.sonar.wsclient.internal.HttpRequestFactory;
26 import org.sonar.wsclient.qualitygate.*;
28 import java.net.HttpURLConnection;
29 import java.util.Collection;
30 import java.util.Iterator;
32 import static org.fest.assertions.Assertions.assertThat;
33 import static org.fest.assertions.MapAssert.entry;
35 public class DefaultQualityGateClientTest {
38 public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
41 public void should_create_qualitygate() {
42 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
44 httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}");
46 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
47 QualityGate result = client.create("Ninth");
49 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create");
50 assertThat(httpServer.requestParams()).includes(
51 entry("name", "Ninth")
53 assertThat(result).isNotNull();
54 assertThat(result.id()).isEqualTo(666L);
55 assertThat(result.name()).isEqualTo("Ninth");
59 public void should_list_qualitygates() {
60 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
62 httpServer.stubResponseBody(
63 "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}]}");
65 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
66 QualityGates qGates = client.list();
68 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list");
69 assertThat(httpServer.requestParams()).isEmpty();
70 assertThat(qGates.qualityGates()).hasSize(3);
71 assertThat(qGates.defaultGate()).isNull();
75 public void should_rename_qualitygate() {
76 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
78 httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}");
80 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
81 QualityGate result = client.rename(666L, "Hell");
83 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/rename");
84 assertThat(httpServer.requestParams()).includes(
88 assertThat(result).isNotNull();
92 public void should_show_qualitygate_by_id() {
93 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
95 httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\",\"conditions\":["
96 + "{\"id\":6,\"metric\":\"blocker_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
97 + "{\"id\":7,\"metric\":\"critical_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
98 + "{\"id\":10,\"metric\":\"test_errors\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
99 + "{\"id\":11,\"metric\":\"test_failures\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
100 + "{\"id\":12,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"\",\"error\":\"80%\",\"period\":3},"
101 + "{\"id\":13,\"metric\":\"open_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
102 + "{\"id\":14,\"metric\":\"reopened_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
103 + "{\"id\":15,\"metric\":\"skipped_tests\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"}"
106 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
108 QualityGateDetails qGate = client.show(5L);
109 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?id=5");
110 assertThat(qGate.id()).isEqualTo(5L);
111 assertThat(qGate.name()).isEqualTo("Sonar way");
113 Collection<QualityGateCondition> conditions = qGate.conditions();
114 assertThat(conditions).hasSize(8);
115 Iterator<QualityGateCondition> condIterator = conditions.iterator();
116 QualityGateCondition first = condIterator.next();
117 assertThat(first.id()).isEqualTo(6L);
118 QualityGateCondition second = condIterator.next();
119 assertThat(second.period()).isNull();
120 QualityGateCondition third = condIterator.next();
121 assertThat(third.metricKey()).isEqualTo("test_errors");
122 QualityGateCondition fourth = condIterator.next();
123 assertThat(fourth.operator()).isEqualTo("GT");
124 QualityGateCondition fifth = condIterator.next();
125 assertThat(fifth.errorThreshold()).isEqualTo("80%");
126 assertThat(fifth.period()).isEqualTo(3);
127 QualityGateCondition sixth = condIterator.next();
128 assertThat(sixth.warningThreshold()).isEqualTo("0");
132 public void should_show_qualitygate_by_name() {
133 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
135 httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\",\"conditions\":["
136 + "{\"id\":6,\"metric\":\"blocker_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
137 + "{\"id\":7,\"metric\":\"critical_violations\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
138 + "{\"id\":10,\"metric\":\"test_errors\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
139 + "{\"id\":11,\"metric\":\"test_failures\",\"op\":\"GT\",\"warning\":\"\",\"error\":\"0\"},"
140 + "{\"id\":12,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"\",\"error\":\"80%\",\"period\":3},"
141 + "{\"id\":13,\"metric\":\"open_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
142 + "{\"id\":14,\"metric\":\"reopened_issues\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"},"
143 + "{\"id\":15,\"metric\":\"skipped_tests\",\"op\":\"GT\",\"warning\":\"0\",\"error\":\"\"}"
146 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
148 QualityGateDetails qGate = client.show("Sonar way");
149 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?name=Sonar%20way");
150 assertThat(qGate.id()).isEqualTo(5L);
151 assertThat(qGate.name()).isEqualTo("Sonar way");
153 Collection<QualityGateCondition> conditions = qGate.conditions();
154 assertThat(conditions).hasSize(8);
155 Iterator<QualityGateCondition> condIterator = conditions.iterator();
156 QualityGateCondition first = condIterator.next();
157 assertThat(first.id()).isEqualTo(6L);
158 QualityGateCondition second = condIterator.next();
159 assertThat(second.period()).isNull();
160 QualityGateCondition third = condIterator.next();
161 assertThat(third.metricKey()).isEqualTo("test_errors");
162 QualityGateCondition fourth = condIterator.next();
163 assertThat(fourth.operator()).isEqualTo("GT");
164 QualityGateCondition fifth = condIterator.next();
165 assertThat(fifth.errorThreshold()).isEqualTo("80%");
166 assertThat(fifth.period()).isEqualTo(3);
167 QualityGateCondition sixth = condIterator.next();
168 assertThat(sixth.warningThreshold()).isEqualTo("0");
172 public void should_show_empty_qualitygate_by_id() {
173 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
175 httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\"}");
177 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
179 QualityGateDetails qGate = client.show(5L);
180 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/show?id=5");
181 assertThat(qGate.id()).isEqualTo(5L);
182 assertThat(qGate.name()).isEqualTo("Sonar way");
184 Collection<QualityGateCondition> conditions = qGate.conditions();
185 assertThat(conditions).isEmpty();
189 public void should_destroy_qualitygate() {
190 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
192 httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
194 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
195 client.destroy(666L);
197 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/destroy");
198 assertThat(httpServer.requestParams()).includes(
204 public void should_set_default_qualitygate() {
205 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
207 httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
209 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
210 client.setDefault(666L);
212 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/set_as_default");
213 assertThat(httpServer.requestParams()).includes(
219 public void should_unset_default_qualitygate() {
220 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
222 httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
224 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
225 client.unsetDefault();
227 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/unset_default");
228 assertThat(httpServer.requestParams()).isEmpty();
232 public void should_create_condition_on_qualitygate() {
233 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
235 httpServer.stubResponseBody("{\"id\":42,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"90\",\"error\":\"80\",\"period\":3}");
237 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
238 QualityGateCondition result = client.createCondition(NewCondition.create(12345L)
239 .metricKey("new_coverage").operator("LT").warningThreshold("90").errorThreshold("80").period(3));
241 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create_condition");
242 assertThat(httpServer.requestParams()).includes(
243 entry("gateId", "12345"),
244 entry("metric", "new_coverage"),
246 entry("warning", "90"),
247 entry("error", "80"),
250 assertThat(result).isNotNull();
251 assertThat(result.id()).isEqualTo(42L);
255 public void should_update_condition() {
256 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
258 httpServer.stubResponseBody("{\"id\":12345,\"metric\":\"ncloc\",\"op\":\"GT\",\"warning\":\"1000\",\"error\":\"2000\",\"period\":1}");
260 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
261 QualityGateCondition result = client.updateCondition(UpdateCondition.create(12345L)
262 .metricKey("ncloc").operator("GT").warningThreshold("1000").errorThreshold("2000").period(1));
264 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/update_condition");
265 assertThat(httpServer.requestParams()).includes(
266 entry("id", "12345"),
267 entry("metric", "ncloc"),
269 entry("warning", "1000"),
270 entry("error", "2000"),
273 assertThat(result).isNotNull();
274 assertThat(result.id()).isEqualTo(12345L);
278 public void should_delete_condition() {
279 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
281 httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
283 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
284 client.deleteCondition(666L);;
286 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/delete_condition");
287 assertThat(httpServer.requestParams()).includes(
293 public void should_select_project() {
294 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
296 httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
298 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
299 client.selectProject(666L, 999L);
301 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/select");
302 assertThat(httpServer.requestParams()).includes(
303 entry("gateId", "666"),
304 entry("projectId", "999")
309 public void should_deselect_project() {
310 HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
312 httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
314 QualityGateClient client = new DefaultQualityGateClient(requestFactory);
315 client.deselectProject(666L, 999L);
317 assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/deselect");
318 assertThat(httpServer.requestParams()).includes(
319 entry("gateId", "666"),
320 entry("projectId", "999")