3 * Copyright (C) 2009-2017 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.computation.task.projectanalysis.webhook;
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Date;
25 import java.util.Optional;
26 import javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.sonar.api.ce.posttask.CeTask;
30 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
31 import org.sonar.api.ce.posttask.Project;
32 import org.sonar.api.ce.posttask.QualityGate;
33 import org.sonar.api.ce.posttask.ScannerContext;
34 import org.sonar.api.platform.Server;
36 import static java.util.Collections.emptyMap;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newCeTaskBuilder;
41 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newConditionBuilder;
42 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newProjectBuilder;
43 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newQualityGateBuilder;
44 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newScannerContextBuilder;
45 import static org.sonar.test.JsonAssert.assertJson;
47 public class WebhookPayloadFactoryImplTest {
49 private static final String PROJECT_KEY = "P1";
51 private Server server = mock(Server.class);
52 private WebhookPayloadFactory underTest = new WebhookPayloadFactoryImpl(server);
55 public void setUp() throws Exception {
56 when(server.getPublicRootUrl()).thenReturn("http://foo");
61 public void create_payload_for_successful_analysis() {
62 CeTask task = newCeTaskBuilder()
63 .setStatus(CeTask.Status.SUCCESS)
66 QualityGate gate = newQualityGateBuilder()
69 .setStatus(QualityGate.Status.WARN)
70 .add(newConditionBuilder()
71 .setMetricKey("coverage")
72 .setOperator(QualityGate.Operator.GREATER_THAN)
73 .setOnLeakPeriod(true)
74 .setWarningThreshold("75.0")
75 .setErrorThreshold("70.0")
76 .build(QualityGate.EvaluationStatus.WARN, "74.0"))
78 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, emptyMap());
80 WebhookPayload payload = underTest.create(analysis);
81 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
82 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/success.json"));
86 public void create_payload_with_gate_conditions_without_value() {
87 CeTask task = newCeTaskBuilder()
88 .setStatus(CeTask.Status.SUCCESS)
91 QualityGate gate = newQualityGateBuilder()
94 .setStatus(QualityGate.Status.WARN)
95 .add(newConditionBuilder()
96 .setMetricKey("coverage")
97 .setOperator(QualityGate.Operator.GREATER_THAN)
98 .setWarningThreshold("75.0")
99 .setErrorThreshold("70.0")
102 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, emptyMap());
104 WebhookPayload payload = underTest.create(analysis);
105 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
106 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/gate_condition_without_value.json"));
110 public void create_payload_with_analysis_properties() {
111 CeTask task = newCeTaskBuilder()
112 .setStatus(CeTask.Status.SUCCESS)
115 QualityGate gate = newQualityGateBuilder()
118 .setStatus(QualityGate.Status.WARN)
120 Map<String, String> scannerProperties = ImmutableMap.of(
121 "sonar.analysis.revision", "ab45d24",
122 "sonar.analysis.buildNumber", "B123",
123 "not.prefixed.with.sonar.analysis", "should be ignored",
124 "ignored", "should be ignored too"
126 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, scannerProperties);
128 WebhookPayload payload = underTest.create(analysis);
129 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/with_analysis_properties.json"));
130 assertThat(payload.getJson())
131 .doesNotContain("not.prefixed.with.sonar.analysis")
132 .doesNotContain("ignored");
136 public void create_payload_for_failed_analysis() {
137 CeTask ceTask = newCeTaskBuilder().setStatus(CeTask.Status.FAILED).setId("#1").build();
138 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(ceTask, null, emptyMap());
140 WebhookPayload payload = underTest.create(analysis);
142 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
143 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/failed.json"));
146 private static PostProjectAnalysisTask.ProjectAnalysis newAnalysis(CeTask task, @Nullable QualityGate gate,
147 Map<String, String> scannerProperties) {
148 return new PostProjectAnalysisTask.ProjectAnalysis() {
150 public CeTask getCeTask() {
155 public Project getProject() {
156 return newProjectBuilder()
159 .setName("Project One")
164 public QualityGate getQualityGate() {
169 public Date getDate() {
170 throw new UnsupportedOperationException();
174 public Optional<Date> getAnalysisDate() {
175 return Optional.of(new Date(1_500_000_000_000L));
179 public ScannerContext getScannerContext() {
180 return newScannerContextBuilder()
181 .addProperties(scannerProperties)