]> source.dussan.org Git - sonarqube.git/blob
7a9624be8d960d90029bf8d781285ea00dc784e5
[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.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;
35
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;
46
47 public class WebhookPayloadFactoryImplTest {
48
49   private static final String PROJECT_KEY = "P1";
50
51   private Server server = mock(Server.class);
52   private WebhookPayloadFactory underTest = new WebhookPayloadFactoryImpl(server);
53
54   @Before
55   public void setUp() throws Exception {
56     when(server.getPublicRootUrl()).thenReturn("http://foo");
57
58   }
59
60   @Test
61   public void create_payload_for_successful_analysis() {
62     CeTask task = newCeTaskBuilder()
63       .setStatus(CeTask.Status.SUCCESS)
64       .setId("#1")
65       .build();
66     QualityGate gate = newQualityGateBuilder()
67       .setId("G1")
68       .setName("Gate One")
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"))
77       .build();
78     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, emptyMap());
79
80     WebhookPayload payload = underTest.create(analysis);
81     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
82     assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/success.json"));
83   }
84
85   @Test
86   public void create_payload_with_gate_conditions_without_value() {
87     CeTask task = newCeTaskBuilder()
88       .setStatus(CeTask.Status.SUCCESS)
89       .setId("#1")
90       .build();
91     QualityGate gate = newQualityGateBuilder()
92       .setId("G1")
93       .setName("Gate One")
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")
100         .buildNoValue())
101       .build();
102     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, emptyMap());
103
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"));
107   }
108
109   @Test
110   public void create_payload_with_analysis_properties() {
111     CeTask task = newCeTaskBuilder()
112       .setStatus(CeTask.Status.SUCCESS)
113       .setId("#1")
114       .build();
115     QualityGate gate = newQualityGateBuilder()
116       .setId("G1")
117       .setName("Gate One")
118       .setStatus(QualityGate.Status.WARN)
119       .build();
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"
125     );
126     PostProjectAnalysisTask.ProjectAnalysis analysis = newAnalysis(task, gate, scannerProperties);
127
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");
133   }
134
135   @Test
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());
139
140     WebhookPayload payload = underTest.create(analysis);
141
142     assertThat(payload.getProjectKey()).isEqualTo(PROJECT_KEY);
143     assertJson(payload.getJson()).isSimilarTo(getClass().getResource("WebhookPayloadTest/failed.json"));
144   }
145
146   private static PostProjectAnalysisTask.ProjectAnalysis newAnalysis(CeTask task, @Nullable QualityGate gate,
147     Map<String, String> scannerProperties) {
148     return new PostProjectAnalysisTask.ProjectAnalysis() {
149       @Override
150       public CeTask getCeTask() {
151         return task;
152       }
153
154       @Override
155       public Project getProject() {
156         return newProjectBuilder()
157           .setUuid("P1_UUID")
158           .setKey(PROJECT_KEY)
159           .setName("Project One")
160           .build();
161       }
162
163       @Override
164       public QualityGate getQualityGate() {
165         return gate;
166       }
167
168       @Override
169       public Date getDate() {
170         throw new UnsupportedOperationException();
171       }
172
173       @Override
174       public Optional<Date> getAnalysisDate() {
175         return Optional.of(new Date(1_500_000_000_000L));
176       }
177
178       @Override
179       public ScannerContext getScannerContext() {
180         return newScannerContextBuilder()
181           .addProperties(scannerProperties)
182           .build();
183       }
184     };
185   }
186
187 }