]> source.dussan.org Git - sonarqube.git/blob
02812a4adb07011a1a217291b0ebb9569933a83e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
6  * This program 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  * This program 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.server.webhook;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Map;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.sonar.api.measures.Metric;
28 import org.sonar.api.platform.Server;
29 import org.sonar.api.utils.System2;
30 import org.sonar.server.qualitygate.Condition;
31 import org.sonar.server.qualitygate.EvaluatedCondition;
32 import org.sonar.server.qualitygate.EvaluatedQualityGate;
33 import org.sonar.server.qualitygate.QualityGate;
34
35 import static java.util.Collections.emptyMap;
36 import static java.util.Collections.emptySet;
37 import static java.util.Collections.singleton;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.test.JsonAssert.assertJson;
42
43 public class WebhookPayloadFactoryImplTest {
44
45   private static final String PROJECT_KEY = "P1";
46
47   private Server server = mock(Server.class);
48   private System2 system2 = mock(System2.class);
49   private WebhookPayloadFactory underTest = new WebhookPayloadFactoryImpl(server, system2);
50
51   @Before
52   public void setUp() throws Exception {
53     when(server.getPublicRootUrl()).thenReturn("http://foo");
54     when(system2.now()).thenReturn(1_500_999L);
55   }
56
57   @Test
58   public void create_payload_for_successful_analysis() {
59     CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
60     Condition condition = new Condition("coverage", Condition.Operator.GREATER_THAN, "70.0", "75.0");
61     EvaluatedQualityGate gate = EvaluatedQualityGate.newBuilder()
62       .setQualityGate(new QualityGate("G1", "Gate One", singleton(condition)))
63       .setStatus(Metric.Level.WARN)
64       .addCondition(condition, EvaluatedCondition.EvaluationStatus.WARN, "74.0")
65       .build();
66     ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, emptyMap());
67
68     WebhookPayload payload = underTest.create(analysis);
69     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
70     assertJson(payload.getJson())
71       .isSimilarTo("{" +
72         "  \"serverUrl\": \"http://foo\"," +
73         "  \"taskId\": \"#1\"," +
74         "  \"status\": \"SUCCESS\"," +
75         "  \"analysedAt\": \"2017-07-14T04:40:00+0200\"," +
76         "  \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
77         "  \"project\": {" +
78         "    \"key\": \"P1\"," +
79         "    \"name\": \"Project One\"," +
80         "    \"url\": \"http://foo/dashboard?id=P1\"" +
81         "  }," +
82         "  \"qualityGate\": {" +
83         "    \"name\": \"Gate One\"," +
84         "    \"status\": \"WARN\"," +
85         "    \"conditions\": [" +
86         "      {" +
87         "        \"metric\": \"coverage\"," +
88         "        \"operator\": \"GREATER_THAN\"," +
89         "        \"value\": \"74.0\"," +
90         "        \"status\": \"WARN\"," +
91         "        \"errorThreshold\": \"70.0\"," +
92         "        \"warningThreshold\": \"75.0\"" +
93         "      }" +
94         "    ]" +
95         "  }," +
96         "  \"properties\": {" +
97         "  }" +
98         "}");
99   }
100
101   @Test
102   public void create_payload_with_gate_conditions_without_value() {
103     CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
104
105     Condition condition = new Condition("coverage", Condition.Operator.GREATER_THAN, "70.0", "75.0");
106     EvaluatedQualityGate gate = EvaluatedQualityGate.newBuilder()
107       .setQualityGate(new QualityGate("G1", "Gate One", singleton(condition)))
108       .setStatus(Metric.Level.WARN)
109       .addCondition(condition, EvaluatedCondition.EvaluationStatus.NO_VALUE, null)
110       .build();
111     ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, emptyMap());
112
113     WebhookPayload payload = underTest.create(analysis);
114     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
115     assertJson(payload.getJson())
116       .isSimilarTo("{" +
117         "  \"serverUrl\": \"http://foo\"," +
118         "  \"taskId\": \"#1\"," +
119         "  \"status\": \"SUCCESS\"," +
120         "  \"analysedAt\": \"2017-07-14T04:40:00+0200\"," +
121         "  \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
122         "  \"project\": {" +
123         "    \"key\": \"P1\"," +
124         "    \"name\": \"Project One\"," +
125         "    \"url\": \"http://foo/dashboard?id=P1\"" +
126         "  }," +
127         "  \"qualityGate\": {" +
128         "    \"name\": \"Gate One\"," +
129         "    \"status\": \"WARN\"," +
130         "    \"conditions\": [" +
131         "      {" +
132         "        \"metric\": \"coverage\"," +
133         "        \"operator\": \"GREATER_THAN\"," +
134         "        \"status\": \"NO_VALUE\"," +
135         "        \"errorThreshold\": \"70.0\"," +
136         "        \"warningThreshold\": \"75.0\"" +
137         "      }" +
138         "    ]" +
139         "  }" +
140         "}");
141   }
142
143   @Test
144   public void create_payload_with_analysis_properties() {
145     CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
146     EvaluatedQualityGate gate = EvaluatedQualityGate.newBuilder()
147       .setQualityGate(new QualityGate("G1", "Gate One", emptySet()))
148       .setStatus(Metric.Level.WARN)
149       .build();
150     Map<String, String> scannerProperties = ImmutableMap.of(
151       "sonar.analysis.revision", "ab45d24",
152       "sonar.analysis.buildNumber", "B123",
153       "not.prefixed.with.sonar.analysis", "should be ignored",
154       "ignored", "should be ignored too");
155     ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, scannerProperties);
156
157     WebhookPayload payload = underTest.create(analysis);
158     assertJson(payload.getJson())
159       .isSimilarTo("{" +
160         "  \"serverUrl\": \"http://foo\"," +
161         "  \"taskId\": \"#1\"," +
162         "  \"status\": \"SUCCESS\"," +
163         "  \"analysedAt\": \"2017-07-14T04:40:00+0200\"," +
164         "  \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
165         "  \"project\": {" +
166         "    \"key\": \"P1\"," +
167         "    \"name\": \"Project One\"," +
168         "    \"url\": \"http://foo/dashboard?id=P1\"" +
169         "  }," +
170         "  \"qualityGate\": {" +
171         "    \"name\": \"Gate One\"," +
172         "    \"status\": \"WARN\"," +
173         "    \"conditions\": [" +
174         "    ]" +
175         "  }," +
176         "  \"properties\": {" +
177         "    \"sonar.analysis.revision\": \"ab45d24\"," +
178         "    \"sonar.analysis.buildNumber\": \"B123\"" +
179         "  }" +
180         "}");
181     assertThat(payload.getJson())
182       .doesNotContain("not.prefixed.with.sonar.analysis")
183       .doesNotContain("ignored");
184   }
185
186   @Test
187   public void create_payload_for_failed_analysis() {
188     CeTask ceTask = new CeTask("#1", CeTask.Status.FAILED);
189     ProjectAnalysis analysis = newAnalysis(ceTask, null, null, 1_500_000_000_000L, emptyMap());
190
191     WebhookPayload payload = underTest.create(analysis);
192
193     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
194     assertJson(payload.getJson())
195       .isSimilarTo("{" +
196         "  \"serverUrl\": \"http://foo\"," +
197         "  \"taskId\": \"#1\"," +
198         "  \"status\": \"FAILED\"," +
199         "  \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
200         "  \"project\": {" +
201         "    \"key\": \"P1\"," +
202         "    \"name\": \"Project One\"," +
203         "    \"url\": \"http://foo/dashboard?id=P1\"" +
204         "  }," +
205         "  \"properties\": {" +
206         "  }" +
207         "}");
208   }
209
210   @Test
211   public void create_payload_for_no_analysis_date() {
212     CeTask ceTask = new CeTask("#1", CeTask.Status.FAILED);
213     ProjectAnalysis analysis = newAnalysis(ceTask, null, null, null, emptyMap());
214
215     WebhookPayload payload = underTest.create(analysis);
216
217     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
218     assertJson(payload.getJson())
219       .isSimilarTo("{" +
220         "  \"serverUrl\": \"http://foo\"," +
221         "  \"taskId\": \"#1\"," +
222         "  \"status\": \"FAILED\"," +
223         "  \"changedAt\": \"1970-01-01T01:25:00+0100\"," +
224         "  \"project\": {" +
225         "    \"key\": \"P1\"," +
226         "    \"name\": \"Project One\"" +
227         "  }," +
228         "  \"properties\": {" +
229         "  }" +
230         "}");
231   }
232
233   @Test
234   public void create_payload_on_short_branch() {
235     CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
236     ProjectAnalysis analysis = newAnalysis(task, null, new Branch(false, "feature/foo", Branch.Type.SHORT), 1_500_000_000_000L, emptyMap());
237
238     WebhookPayload payload = underTest.create(analysis);
239     assertJson(payload.getJson())
240       .isSimilarTo("{" +
241         "\"branch\": {" +
242         "  \"name\": \"feature/foo\"," +
243         "  \"type\": \"SHORT\"," +
244         "  \"isMain\": false," +
245         "  \"url\": \"http://foo/project/issues?branch=feature%2Ffoo&id=P1&resolved=false\"" +
246         "}" +
247         "}");
248   }
249
250   @Test
251   public void create_payload_on_pull_request() {
252     CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
253     ProjectAnalysis analysis = newAnalysis(task, null, new Branch(false, "pr/foo", Branch.Type.PULL_REQUEST), 1_500_000_000_000L, emptyMap());
254
255     WebhookPayload payload = underTest.create(analysis);
256     assertJson(payload.getJson())
257       .isSimilarTo("{" +
258         "\"branch\": {" +
259         "  \"name\": \"pr/foo\"," +
260         "  \"type\": \"PULL_REQUEST\"," +
261         "  \"isMain\": false," +
262         "  \"url\": \"http://foo/project/issues?pullRequest=pr%2Ffoo&id=P1&resolved=false\"" +
263         "}" +
264         "}");
265   }
266
267   @Test
268   public void create_without_ce_task() {
269     ProjectAnalysis analysis = newAnalysis(null, null, null, null, emptyMap());
270
271     WebhookPayload payload = underTest.create(analysis);
272     String json = payload.getJson();
273     assertThat(json).doesNotContain("taskId");
274     assertJson(json)
275       .isSimilarTo("{" +
276         "  \"serverUrl\": \"http://foo\"," +
277         "  \"status\": \"SUCCESS\"," +
278         "  \"changedAt\": \"1970-01-01T01:25:00+0100\"," +
279         "  \"project\": {" +
280         "    \"key\": \"P1\"," +
281         "    \"name\": \"Project One\"" +
282         "  }," +
283         "  \"properties\": {" +
284         "  }" +
285         "}");
286   }
287
288   @Test
289   public void create_payload_on_long_branch() {
290     CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
291     ProjectAnalysis analysis = newAnalysis(task, null, new Branch(false, "feature/foo", Branch.Type.LONG), 1_500_000_000_000L, emptyMap());
292
293     WebhookPayload payload = underTest.create(analysis);
294     assertJson(payload.getJson())
295       .isSimilarTo("{" +
296         "\"branch\": {" +
297         "  \"name\": \"feature/foo\"" +
298         "  \"type\": \"LONG\"" +
299         "  \"isMain\": false," +
300         "  \"url\": \"http://foo/dashboard?branch=feature%2Ffoo&id=P1\"" +
301         "}" +
302         "}");
303   }
304
305   @Test
306   public void create_payload_on_main_branch_without_name() {
307     CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
308     ProjectAnalysis analysis = newAnalysis(task, null, new Branch(true, null, Branch.Type.LONG), 1_500_000_000_000L, emptyMap());
309
310     WebhookPayload payload = underTest.create(analysis);
311     assertJson(payload.getJson())
312       .isSimilarTo("{" +
313         "\"branch\": {" +
314         "  \"type\": \"LONG\"" +
315         "  \"isMain\": true," +
316         "  \"url\": \"http://foo/dashboard?id=P1\"" +
317         "}" +
318         "}");
319   }
320
321   private static ProjectAnalysis newAnalysis(@Nullable CeTask task, @Nullable EvaluatedQualityGate gate,
322     @Nullable Branch branch, @Nullable Long analysisDate, Map<String, String> scannerProperties) {
323     return new ProjectAnalysis(new Project("P1_UUID", PROJECT_KEY, "Project One"), task, analysisDate == null ? null : new Analysis("A_UUID1", analysisDate), branch,
324       gate, analysisDate, scannerProperties);
325   }
326 }