]> source.dussan.org Git - sonarqube.git/blob
ddf9c29e9d47c463ef448337e5d3d22ac4231a85
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.wsclient.qualitygate.internal;
21
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.*;
27
28 import java.net.HttpURLConnection;
29 import java.util.Collection;
30 import java.util.Iterator;
31
32 import static org.fest.assertions.Assertions.assertThat;
33 import static org.fest.assertions.MapAssert.entry;
34
35 public class DefaultQualityGateClientTest {
36
37   @Rule
38   public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
39
40   @Test
41   public void should_create_qualitygate() {
42     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
43
44     httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}");
45
46     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
47     QualityGate result = client.create("Ninth");
48
49     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create");
50     assertThat(httpServer.requestParams()).includes(
51         entry("name", "Ninth")
52         );
53     assertThat(result).isNotNull();
54     assertThat(result.id()).isEqualTo(666L);
55     assertThat(result.name()).isEqualTo("Ninth");
56   }
57
58   @Test
59   public void should_list_qualitygates() {
60     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
61
62     httpServer.stubResponseBody(
63         "{\"qualitygates\":[{\"id\":666,\"name\":\"Ninth\"},{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"}]}");
64
65     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
66     QualityGates qGates = client.list();
67
68     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/list");
69     assertThat(httpServer.requestParams()).isEmpty();
70     assertThat(qGates.qualityGates()).hasSize(3);
71     assertThat(qGates.defaultGate()).isNull();
72   }
73
74   @Test
75   public void should_rename_qualitygate() {
76     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
77
78     httpServer.stubResponseBody("{\"id\":666,\"name\":\"Ninth\"}");
79
80     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
81     QualityGate result = client.rename(666L, "Hell");
82
83     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/rename");
84     assertThat(httpServer.requestParams()).includes(
85         entry("id", "666"),
86         entry("name", "Hell")
87         );
88     assertThat(result).isNotNull();
89   }
90
91   @Test
92   public void should_show_qualitygate_by_id() {
93     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
94
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\":\"\"}"
104       + "]}");
105
106     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
107
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");
112
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");
129   }
130
131   @Test
132   public void should_show_qualitygate_by_name() {
133     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
134
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\":\"\"}"
144       + "]}");
145
146     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
147
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");
152
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");
169   }
170
171   @Test
172   public void should_show_empty_qualitygate_by_id() {
173     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
174
175     httpServer.stubResponseBody("{\"id\":5,\"name\":\"Sonar way\"}");
176
177     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
178
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");
183
184     Collection<QualityGateCondition> conditions = qGate.conditions();
185     assertThat(conditions).isEmpty();
186   }
187
188   @Test
189   public void should_destroy_qualitygate() {
190     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
191
192     httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
193
194     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
195     client.destroy(666L);
196
197     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/destroy");
198     assertThat(httpServer.requestParams()).includes(
199         entry("id", "666")
200         );
201   }
202
203   @Test
204   public void should_set_default_qualitygate() {
205     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
206
207     httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
208
209     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
210     client.setDefault(666L);
211
212     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/set_as_default");
213     assertThat(httpServer.requestParams()).includes(
214         entry("id", "666")
215         );
216   }
217
218   @Test
219   public void should_unset_default_qualitygate() {
220     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
221
222     httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
223
224     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
225     client.unsetDefault();
226
227     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/unset_default");
228     assertThat(httpServer.requestParams()).isEmpty();
229   }
230
231   @Test
232   public void should_create_condition_on_qualitygate() {
233     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
234
235     httpServer.stubResponseBody("{\"id\":42,\"metric\":\"new_coverage\",\"op\":\"LT\",\"warning\":\"90\",\"error\":\"80\",\"period\":3}");
236
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));
240
241     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/create_condition");
242     assertThat(httpServer.requestParams()).includes(
243         entry("gateId", "12345"),
244         entry("metric", "new_coverage"),
245         entry("op", "LT"),
246         entry("warning", "90"),
247         entry("error", "80"),
248         entry("period", "3")
249         );
250     assertThat(result).isNotNull();
251     assertThat(result.id()).isEqualTo(42L);
252   }
253
254   @Test
255   public void should_update_condition() {
256     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
257
258     httpServer.stubResponseBody("{\"id\":12345,\"metric\":\"ncloc\",\"op\":\"GT\",\"warning\":\"1000\",\"error\":\"2000\",\"period\":1}");
259
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));
263
264     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/update_condition");
265     assertThat(httpServer.requestParams()).includes(
266         entry("id", "12345"),
267         entry("metric", "ncloc"),
268         entry("op", "GT"),
269         entry("warning", "1000"),
270         entry("error", "2000"),
271         entry("period", "1")
272         );
273     assertThat(result).isNotNull();
274     assertThat(result.id()).isEqualTo(12345L);
275   }
276
277   @Test
278   public void should_delete_condition() {
279     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
280
281     httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
282
283     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
284     client.deleteCondition(666L);;
285
286     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/delete_condition");
287     assertThat(httpServer.requestParams()).includes(
288         entry("id", "666")
289         );
290   }
291
292   @Test
293   public void should_select_project() {
294     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
295
296     httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
297
298     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
299     client.selectProject(666L, 999L);
300
301     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/select");
302     assertThat(httpServer.requestParams()).includes(
303         entry("gateId", "666"),
304         entry("projectId", "999")
305         );
306   }
307
308   @Test
309   public void should_deselect_project() {
310     HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url());
311
312     httpServer.stubStatusCode(HttpURLConnection.HTTP_NO_CONTENT);
313
314     QualityGateClient client = new DefaultQualityGateClient(requestFactory);
315     client.deselectProject(666L, 999L);
316
317     assertThat(httpServer.requestedPath()).isEqualTo("/api/qualitygates/deselect");
318     assertThat(httpServer.requestParams()).includes(
319         entry("gateId", "666"),
320         entry("projectId", "999")
321         );
322   }
323 }