]> source.dussan.org Git - sonarqube.git/blob
f01d81d401d75dff1bc290cb4359a5e7c5229044
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.computation.task.projectanalysis.webhook;
21
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;
31
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;
39
40 public class WebhookPayloadTest {
41
42   private static final String PROJECT_KEY = "P1";
43
44   @Test
45   public void create_payload_for_successful_analysis() {
46     CeTask task = newCeTaskBuilder()
47       .setStatus(CeTask.Status.SUCCESS)
48       .setId("#1")
49       .build();
50     QualityGate gate = newQualityGateBuilder()
51       .setId("G1")
52       .setName("Gate One")
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"))
61       .build();
62     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate);
63
64     WebhookPayload payload = WebhookPayload.from(analysis);
65     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
66     assertJson(payload.toJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/success.json"));
67   }
68
69   @Test
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);
73
74     WebhookPayload payload = WebhookPayload.from(analysis);
75
76     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
77     assertJson(payload.toJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/failed.json"));
78   }
79
80   @Test
81   public void create_payload_with_gate_conditions_without_value() {
82     CeTask task = newCeTaskBuilder()
83       .setStatus(CeTask.Status.SUCCESS)
84       .setId("#1")
85       .build();
86     QualityGate gate = newQualityGateBuilder()
87       .setId("G1")
88       .setName("Gate One")
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")
95         .buildNoValue())
96       .build();
97     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate);
98
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"));
102   }
103
104   private static PostProjectAnalysisTask.ProjectAnalysis newAnalysis(CeTask task, @Nullable QualityGate gate) {
105     return new PostProjectAnalysisTask.ProjectAnalysis() {
106       @Override
107       public CeTask getCeTask() {
108         return task;
109       }
110
111       @Override
112       public Project getProject() {
113         return newProjectBuilder()
114           .setUuid("P1_UUID")
115           .setKey(PROJECT_KEY)
116           .setName("Project One")
117           .build();
118       }
119
120       @Override
121       public QualityGate getQualityGate() {
122         return gate;
123       }
124
125       @Override
126       public Date getDate() {
127         throw new UnsupportedOperationException();
128       }
129
130       @Override
131       public Optional<Date> getAnalysisDate() {
132         return Optional.of(new Date(1_500_000_000_000L));
133       }
134
135       @Override
136       public ScannerContext getScannerContext() {
137         return newScannerContextBuilder().build();
138       }
139     };
140   }
141
142 }