3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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 java.util.Date;
23 import java.util.Optional;
24 import javax.annotation.Nullable;
25 import org.junit.Test;
26 import org.sonar.api.ce.posttask.CeTask;
27 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
28 import org.sonar.api.ce.posttask.Project;
29 import org.sonar.api.ce.posttask.QualityGate;
30 import org.sonar.api.ce.posttask.ScannerContext;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newCeTaskBuilder;
34 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newConditionBuilder;
35 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newProjectBuilder;
36 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newQualityGateBuilder;
37 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newScannerContextBuilder;
38 import static org.sonar.test.JsonAssert.assertJson;
40 public class WebhookPayloadTest {
42 private static final String PROJECT_KEY = "P1";
45 public void create_payload_for_successful_analysis() {
46 CeTask task = newCeTaskBuilder()
47 .setStatus(CeTask.Status.SUCCESS)
50 QualityGate gate = newQualityGateBuilder()
53 .setStatus(QualityGate.Status.WARN)
54 .add(newConditionBuilder()
55 .setMetricKey("coverage")
56 .setOperator(QualityGate.Operator.GREATER_THAN)
57 .setOnLeakPeriod(true)
58 .setWarningThreshold("75.0")
59 .setErrorThreshold("70.0")
60 .build(QualityGate.EvaluationStatus.WARN, "74.0"))
62 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate);
64 WebhookPayload payload = WebhookPayload.from(analysis);
65 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
66 assertJson(payload.toJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/success.json"));
70 public void create_payload_for_failed_analysis() {
71 CeTask ceTask = newCeTaskBuilder().setStatus(CeTask.Status.FAILED).setId("#1").build();
72 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(ceTask, null);
74 WebhookPayload payload = WebhookPayload.from(analysis);
76 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
77 assertJson(payload.toJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/failed.json"));
81 public void create_payload_with_gate_conditions_without_value() {
82 CeTask task = newCeTaskBuilder()
83 .setStatus(CeTask.Status.SUCCESS)
86 QualityGate gate = newQualityGateBuilder()
89 .setStatus(QualityGate.Status.WARN)
90 .add(newConditionBuilder()
91 .setMetricKey("coverage")
92 .setOperator(QualityGate.Operator.GREATER_THAN)
93 .setWarningThreshold("75.0")
94 .setErrorThreshold("70.0")
97 PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate);
99 WebhookPayload payload = WebhookPayload.from(analysis);
100 assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
101 assertJson(payload.toJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/gate_condition_without_value.json"));
104 private static PostProjectAnalysisTask.ProjectAnalysis newAnalysis(CeTask task, @Nullable QualityGate gate) {
105 return new PostProjectAnalysisTask.ProjectAnalysis() {
107 public CeTask getCeTask() {
112 public Project getProject() {
113 return newProjectBuilder()
116 .setName("Project One")
121 public QualityGate getQualityGate() {
126 public Date getDate() {
127 throw new UnsupportedOperationException();
131 public Optional<Date> getAnalysisDate() {
132 return Optional.of(new Date(1_500_000_000_000L));
136 public ScannerContext getScannerContext() {
137 return newScannerContextBuilder().build();