]> source.dussan.org Git - sonarqube.git/blob
67a0cc4564e62e5c0152a99b70022cbe96004796
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info 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 com.google.common.collect.ImmutableMap;
23 import java.util.Date;
24 import java.util.Map;
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;
37
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;
48
49 public class WebhookPayloadFactoryImplTest {
50
51   private static final String PROJECT_KEY = "P1";
52
53   private Server server = mock(Server.class);
54   private WebhookPayloadFactory underTest = new WebhookPayloadFactoryImpl(server);
55
56   @Before
57   public void setUp() throws Exception {
58     when(server.getPublicRootUrl()).thenReturn("http://foo");
59
60   }
61
62   @Test
63   public void create_payload_for_successful_analysis() {
64     CeTask task = newCeTaskBuilder()
65       .setStatus(CeTask.Status.SUCCESS)
66       .setId("#1")
67       .build();
68     QualityGate gate = newQualityGateBuilder()
69       .setId("G1")
70       .setName("Gate One")
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"))
79       .build();
80     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, null, emptyMap());
81
82     WebhookPayload payload = underTest.create(analysis);
83     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
84     assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/success.json"));
85   }
86
87   @Test
88   public void create_payload_with_gate_conditions_without_value() {
89     CeTask task = newCeTaskBuilder()
90       .setStatus(CeTask.Status.SUCCESS)
91       .setId("#1")
92       .build();
93     QualityGate gate = newQualityGateBuilder()
94       .setId("G1")
95       .setName("Gate One")
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")
102         .buildNoValue())
103       .build();
104     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, null, emptyMap());
105
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"));
109   }
110
111   @Test
112   public void create_payload_with_analysis_properties() {
113     CeTask task = newCeTaskBuilder()
114       .setStatus(CeTask.Status.SUCCESS)
115       .setId("#1")
116       .build();
117     QualityGate gate = newQualityGateBuilder()
118       .setId("G1")
119       .setName("Gate One")
120       .setStatus(QualityGate.Status.WARN)
121       .build();
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);
128
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");
134   }
135
136   @Test
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());
140
141     WebhookPayload payload = underTest.create(analysis);
142
143     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
144     assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/failed.json"));
145   }
146
147   @Test
148   public void create_payload_on_branch() {
149     CeTask task = newCeTaskBuilder()
150       .setStatus(CeTask.Status.SUCCESS)
151       .setId("#1")
152       .build();
153     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, null, new BranchImpl(false, "feature/foo", Branch.Type.SHORT), emptyMap());
154
155     WebhookPayload payload = underTest.create(analysis);
156     assertJson(payload.getJson()).isSimilarTo("{" +
157       "\"branch\": {" +
158       "  \"name\": \"feature/foo\"" +
159       "  \"type\": \"SHORT\"" +
160       "  \"isMain\": false" +
161       "}" +
162       "}");
163   }
164
165   @Test
166   public void create_payload_on_main_branch_without_name() {
167     CeTask task = newCeTaskBuilder()
168       .setStatus(CeTask.Status.SUCCESS)
169       .setId("#1")
170       .build();
171     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, null, new BranchImpl(true, null, Branch.Type.LONG), emptyMap());
172
173     WebhookPayload payload = underTest.create(analysis);
174     assertJson(payload.getJson()).isSimilarTo("{" +
175       "\"branch\": {" +
176       "  \"type\": \"LONG\"" +
177       "  \"isMain\": true" +
178       "}" +
179       "}");
180   }
181
182   private static PostProjectAnalysisTask.ProjectAnalysis newAnalysis(CeTask task, @Nullable QualityGate gate, @Nullable Branch branch,
183                                                                      Map<String, String> scannerProperties) {
184     return new PostProjectAnalysisTask.ProjectAnalysis() {
185       @Override
186       public CeTask getCeTask() {
187         return task;
188       }
189
190       @Override
191       public Project getProject() {
192         return newProjectBuilder()
193           .setUuid("P1_UUID")
194           .setKey(PROJECT_KEY)
195           .setName("Project One")
196           .build();
197       }
198
199       @Override
200       public Optional<Branch> getBranch() {
201         return Optional.ofNullable(branch);
202       }
203
204       @Override
205       public QualityGate getQualityGate() {
206         return gate;
207       }
208
209       @Override
210       public Date getDate() {
211         throw new UnsupportedOperationException();
212       }
213
214       @Override
215       public Optional<Date> getAnalysisDate() {
216         return Optional.of(new Date(1_500_000_000_000L));
217       }
218
219       @Override
220       public ScannerContext getScannerContext() {
221         return newScannerContextBuilder()
222           .addProperties(scannerProperties)
223           .build();
224       }
225     };
226   }
227
228 }