3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.server.webhook;
22 import com.google.common.collect.ImmutableMap;
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;
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;
43 public class WebhookPayloadFactoryImplTest {
45 private static final String PROJECT_KEY = "P1";
47 private Server server = mock(Server.class);
48 private System2 system2 = mock(System2.class);
49 private WebhookPayloadFactory underTest = new WebhookPayloadFactoryImpl(server, system2);
52 public void setUp() throws Exception {
53 when(server.getPublicRootUrl()).thenReturn("http://foo");
54 when(system2.now()).thenReturn(1_500_999L);
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")
66 ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, emptyMap());
68 WebhookPayload payload = underTest.create(analysis);
69 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
70 assertJson(payload.getJson())
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\"," +
79 " \"name\": \"Project One\"," +
80 " \"url\": \"http://foo/dashboard?id=P1\"" +
82 " \"qualityGate\": {" +
83 " \"name\": \"Gate One\"," +
84 " \"status\": \"WARN\"," +
85 " \"conditions\": [" +
87 " \"metric\": \"coverage\"," +
88 " \"operator\": \"GREATER_THAN\"," +
89 " \"value\": \"74.0\"," +
90 " \"status\": \"WARN\"," +
91 " \"errorThreshold\": \"70.0\"," +
92 " \"warningThreshold\": \"75.0\"" +
96 " \"properties\": {" +
102 public void create_payload_with_gate_conditions_without_value() {
103 CeTask task = new CeTask("#1", CeTask.Status.SUCCESS);
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)
111 ProjectAnalysis analysis = newAnalysis(task, gate, null, 1_500_000_000_000L, emptyMap());
113 WebhookPayload payload = underTest.create(analysis);
114 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
115 assertJson(payload.getJson())
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\"," +
123 " \"key\": \"P1\"," +
124 " \"name\": \"Project One\"," +
125 " \"url\": \"http://foo/dashboard?id=P1\"" +
127 " \"qualityGate\": {" +
128 " \"name\": \"Gate One\"," +
129 " \"status\": \"WARN\"," +
130 " \"conditions\": [" +
132 " \"metric\": \"coverage\"," +
133 " \"operator\": \"GREATER_THAN\"," +
134 " \"status\": \"NO_VALUE\"," +
135 " \"errorThreshold\": \"70.0\"," +
136 " \"warningThreshold\": \"75.0\"" +
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)
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);
157 WebhookPayload payload = underTest.create(analysis);
158 assertJson(payload.getJson())
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\"," +
166 " \"key\": \"P1\"," +
167 " \"name\": \"Project One\"," +
168 " \"url\": \"http://foo/dashboard?id=P1\"" +
170 " \"qualityGate\": {" +
171 " \"name\": \"Gate One\"," +
172 " \"status\": \"WARN\"," +
173 " \"conditions\": [" +
176 " \"properties\": {" +
177 " \"sonar.analysis.revision\": \"ab45d24\"," +
178 " \"sonar.analysis.buildNumber\": \"B123\"" +
181 assertThat(payload.getJson())
182 .doesNotContain("not.prefixed.with.sonar.analysis")
183 .doesNotContain("ignored");
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());
191 WebhookPayload payload = underTest.create(analysis);
193 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
194 assertJson(payload.getJson())
196 " \"serverUrl\": \"http://foo\"," +
197 " \"taskId\": \"#1\"," +
198 " \"status\": \"FAILED\"," +
199 " \"changedAt\": \"2017-07-14T04:40:00+0200\"," +
201 " \"key\": \"P1\"," +
202 " \"name\": \"Project One\"," +
203 " \"url\": \"http://foo/dashboard?id=P1\"" +
205 " \"properties\": {" +
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());
215 WebhookPayload payload = underTest.create(analysis);
217 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
218 assertJson(payload.getJson())
220 " \"serverUrl\": \"http://foo\"," +
221 " \"taskId\": \"#1\"," +
222 " \"status\": \"FAILED\"," +
223 " \"changedAt\": \"1970-01-01T01:25:00+0100\"," +
225 " \"key\": \"P1\"," +
226 " \"name\": \"Project One\"" +
228 " \"properties\": {" +
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());
238 WebhookPayload payload = underTest.create(analysis);
239 assertJson(payload.getJson())
242 " \"name\": \"feature/foo\"," +
243 " \"type\": \"SHORT\"," +
244 " \"isMain\": false," +
245 " \"url\": \"http://foo/project/issues?branch=feature%2Ffoo&id=P1&resolved=false\"" +
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());
255 WebhookPayload payload = underTest.create(analysis);
256 assertJson(payload.getJson())
259 " \"name\": \"pr/foo\"," +
260 " \"type\": \"PULL_REQUEST\"," +
261 " \"isMain\": false," +
262 " \"url\": \"http://foo/project/issues?pullRequest=pr%2Ffoo&id=P1&resolved=false\"" +
268 public void create_without_ce_task() {
269 ProjectAnalysis analysis = newAnalysis(null, null, null, null, emptyMap());
271 WebhookPayload payload = underTest.create(analysis);
272 String json = payload.getJson();
273 assertThat(json).doesNotContain("taskId");
276 " \"serverUrl\": \"http://foo\"," +
277 " \"status\": \"SUCCESS\"," +
278 " \"changedAt\": \"1970-01-01T01:25:00+0100\"," +
280 " \"key\": \"P1\"," +
281 " \"name\": \"Project One\"" +
283 " \"properties\": {" +
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());
293 WebhookPayload payload = underTest.create(analysis);
294 assertJson(payload.getJson())
297 " \"name\": \"feature/foo\"" +
298 " \"type\": \"LONG\"" +
299 " \"isMain\": false," +
300 " \"url\": \"http://foo/dashboard?branch=feature%2Ffoo&id=P1\"" +
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());
310 WebhookPayload payload = underTest.create(analysis);
311 assertJson(payload.getJson())
314 " \"type\": \"LONG\"" +
315 " \"isMain\": true," +
316 " \"url\": \"http://foo/dashboard?id=P1\"" +
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);