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.Branch;
30 import org.sonar.api.ce.posttask.CeTask;
31 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
32 import org.sonar.api.ce.posttask.Project;
33 import org.sonar.api.ce.posttask.QualityGate;
34 import org.sonar.api.ce.posttask.ScannerContext;
35 import org.sonar.api.platform.Server;
36 import org.sonar.server.computation.task.projectanalysis.api.posttask.BranchImpl;
38 import static java.util.Collections.emptyMap;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newCeTaskBuilder;
43 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newConditionBuilder;
44 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newProjectBuilder;
45 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newQualityGateBuilder;
46 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newScannerContextBuilder;
47 import static org.sonar.test.JsonAssert.assertJson;
49 public class WebhookPayloadFactoryImplTest {
51 private static final String PROJECT_KEY = "P1";
53 private Server server = mock(Server.class);
54 private WebhookPayloadFactory underTest = new WebhookPayloadFactoryImpl(server);
57 public void setUp() throws Exception {
58 when(server.getPublicRootUrl()).thenReturn("http://foo");
63 public void create_payload_for_successful_analysis() {
64 CeTask task = newCeTaskBuilder()
65 .setStatus(CeTask.Status.SUCCESS)
68 QualityGate gate = newQualityGateBuilder()
71 .setStatus(QualityGate.Status.WARN)
72 .add(newConditionBuilder()
73 .setMetricKey("coverage")
74 .setOperator(QualityGate.Operator.GREATER_THAN)
75 .setOnLeakPeriod(true)
76 .setWarningThreshold("75.0")
77 .setErrorThreshold("70.0")
78 .build(QualityGate.EvaluationStatus.WARN, "74.0"))
80 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, null, emptyMap());
82 WebhookPayload payload = underTest.create(analysis);
83 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
84 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/success.json"));
88 public void create_payload_with_gate_conditions_without_value() {
89 CeTask task = newCeTaskBuilder()
90 .setStatus(CeTask.Status.SUCCESS)
93 QualityGate gate = newQualityGateBuilder()
96 .setStatus(QualityGate.Status.WARN)
97 .add(newConditionBuilder()
98 .setMetricKey("coverage")
99 .setOperator(QualityGate.Operator.GREATER_THAN)
100 .setWarningThreshold("75.0")
101 .setErrorThreshold("70.0")
104 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, null, emptyMap());
106 WebhookPayload payload = underTest.create(analysis);
107 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
108 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/gate_condition_without_value.json"));
112 public void create_payload_with_analysis_properties() {
113 CeTask task = newCeTaskBuilder()
114 .setStatus(CeTask.Status.SUCCESS)
117 QualityGate gate = newQualityGateBuilder()
120 .setStatus(QualityGate.Status.WARN)
122 Map<String, String> scannerProperties = ImmutableMap.of(
123 "sonar.analysis.revision", "ab45d24",
124 "sonar.analysis.buildNumber", "B123",
125 "not.prefixed.with.sonar.analysis", "should be ignored",
126 "ignored", "should be ignored too");
127 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, null, scannerProperties);
129 WebhookPayload payload = underTest.create(analysis);
130 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/with_analysis_properties.json"));
131 assertThat(payload.getJson())
132 .doesNotContain("not.prefixed.with.sonar.analysis")
133 .doesNotContain("ignored");
137 public void create_payload_for_failed_analysis() {
138 CeTask ceTask = newCeTaskBuilder().setStatus(CeTask.Status.FAILED).setId("#1").build();
139 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(ceTask, null, null, emptyMap());
141 WebhookPayload payload = underTest.create(analysis);
143 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
144 assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/failed.json"));
148 public void create_payload_on_branch() {
149 CeTask task = newCeTaskBuilder()
150 .setStatus(CeTask.Status.SUCCESS)
153 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, null, new BranchImpl(false, "feature/foo", Branch.Type.SHORT), emptyMap());
155 WebhookPayload payload = underTest.create(analysis);
156 assertJson(payload.getJson()).isSimilarTo("{" +
158 " \"name\": \"feature/foo\"" +
159 " \"type\": \"SHORT\"" +
160 " \"isMain\": false" +
166 public void create_payload_on_main_branch_without_name() {
167 CeTask task = newCeTaskBuilder()
168 .setStatus(CeTask.Status.SUCCESS)
171 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, null, new BranchImpl(true, null, Branch.Type.LONG), emptyMap());
173 WebhookPayload payload = underTest.create(analysis);
174 assertJson(payload.getJson()).isSimilarTo("{" +
176 " \"type\": \"LONG\"" +
177 " \"isMain\": true" +
182 private static PostProjectAnalysisTask.ProjectAnalysis newAnalysis(CeTask task, @Nullable QualityGate gate, @Nullable Branch branch,
183 Map<String, String> scannerProperties) {
184 return new PostProjectAnalysisTask.ProjectAnalysis() {
186 public CeTask getCeTask() {
191 public Project getProject() {
192 return newProjectBuilder()
195 .setName("Project One")
200 public Optional<Branch> getBranch() {
201 return Optional.ofNullable(branch);
205 public QualityGate getQualityGate() {
210 public Date getDate() {
211 throw new UnsupportedOperationException();
215 public Optional<Date> getAnalysisDate() {
216 return Optional.of(new Date(1_500_000_000_000L));
220 public ScannerContext getScannerContext() {
221 return newScannerContextBuilder()
222 .addProperties(scannerProperties)