]> source.dussan.org Git - sonarqube.git/blob
e967a56a6f2b5efaadbddd396cdff450ca666583
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.ce.task.projectanalysis.webhook;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.Map;
26 import java.util.Random;
27 import java.util.function.Supplier;
28 import javax.annotation.Nullable;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.sonar.api.ce.posttask.Branch;
33 import org.sonar.api.ce.posttask.CeTask;
34 import org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester;
35 import org.sonar.api.ce.posttask.Project;
36 import org.sonar.api.ce.posttask.QualityGate;
37 import org.sonar.api.config.Configuration;
38 import org.sonar.api.measures.Metric;
39 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
40 import org.sonar.server.qualitygate.Condition;
41 import org.sonar.server.qualitygate.EvaluatedCondition;
42 import org.sonar.server.qualitygate.EvaluatedQualityGate;
43 import org.sonar.server.webhook.Analysis;
44 import org.sonar.server.webhook.ProjectAnalysis;
45 import org.sonar.server.webhook.WebHooks;
46 import org.sonar.server.webhook.WebhookPayload;
47 import org.sonar.server.webhook.WebhookPayloadFactory;
48
49 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.ArgumentMatchers.eq;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.verify;
55 import static org.mockito.Mockito.when;
56 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newBranchBuilder;
57 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newCeTaskBuilder;
58 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newConditionBuilder;
59 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newProjectBuilder;
60 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newQualityGateBuilder;
61 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newScannerContextBuilder;
62
63 public class WebhookPostTaskTest {
64
65   private final Random random = new Random();
66   private final Configuration configuration = mock(Configuration.class);
67   private final WebhookPayload webhookPayload = mock(WebhookPayload.class);
68   private final WebhookPayloadFactory payloadFactory = mock(WebhookPayloadFactory.class);
69   private final WebHooks webHooks = mock(WebHooks.class);
70   private final ConfigurationRepository configurationRepository = mock(ConfigurationRepository.class);
71   private WebhookPostTask underTest = new WebhookPostTask(payloadFactory, webHooks);
72
73   @Before
74   public void wireMocks() {
75     when(payloadFactory.create(any(ProjectAnalysis.class))).thenReturn(webhookPayload);
76     when(configurationRepository.getConfiguration()).thenReturn(configuration);
77   }
78
79   @Test
80   public void call_webhooks_when_no_analysis_not_qualitygate() {
81     callWebHooks(null, null);
82   }
83
84   @Test
85   public void call_webhooks_with_analysis_and_qualitygate() {
86     QualityGate.Condition condition = newConditionBuilder()
87       .setMetricKey(randomAlphanumeric(96))
88       .setOperator(QualityGate.Operator.values()[random.nextInt(QualityGate.Operator.values().length)])
89       .setErrorThreshold(randomAlphanumeric(22))
90       .setOnLeakPeriod(random.nextBoolean())
91       .build(QualityGate.EvaluationStatus.OK, randomAlphanumeric(33));
92     QualityGate qualityGate = newQualityGateBuilder()
93       .setId(randomAlphanumeric(23))
94       .setName(randomAlphanumeric(66))
95       .setStatus(QualityGate.Status.values()[random.nextInt(QualityGate.Status.values().length)])
96       .add(condition)
97       .build();
98
99     callWebHooks(randomAlphanumeric(40), qualityGate);
100   }
101
102   private void callWebHooks(@Nullable String analysisUUid, @Nullable QualityGate qualityGate) {
103     Project project = newProjectBuilder()
104       .setUuid(randomAlphanumeric(3))
105       .setKey(randomAlphanumeric(4))
106       .setName(randomAlphanumeric(5))
107       .build();
108     CeTask ceTask = newCeTaskBuilder()
109       .setStatus(CeTask.Status.values()[random.nextInt(CeTask.Status.values().length)])
110       .setId(randomAlphanumeric(6))
111       .build();
112     Date date = new Date();
113     Map<String, String> properties = ImmutableMap.of(randomAlphanumeric(17), randomAlphanumeric(18));
114     Branch branch = newBranchBuilder()
115       .setIsMain(random.nextBoolean())
116       .setType(Branch.Type.values()[random.nextInt(Branch.Type.values().length)])
117       .setName(randomAlphanumeric(29))
118       .build();
119
120     PostProjectAnalysisTaskTester.of(underTest)
121       .at(date)
122       .withCeTask(ceTask)
123       .withProject(project)
124       .withBranch(branch)
125       .withQualityGate(qualityGate)
126       .withScannerContext(newScannerContextBuilder()
127         .addProperties(properties)
128         .build())
129       .withAnalysisUuid(analysisUUid)
130       .withQualityGate(qualityGate)
131       .execute();
132
133     ArgumentCaptor<Supplier> supplierCaptor = ArgumentCaptor.forClass(Supplier.class);
134     verify(webHooks)
135       .sendProjectAnalysisUpdate(
136         eq(new WebHooks.Analysis(project.getUuid(),
137           analysisUUid,
138           ceTask.getId())),
139         supplierCaptor.capture());
140
141     assertThat(supplierCaptor.getValue().get()).isSameAs(webhookPayload);
142
143     EvaluatedQualityGate webQualityGate = null;
144     if (qualityGate != null) {
145       QualityGate.Condition condition = qualityGate.getConditions().iterator().next();
146       Condition qgCondition = new Condition(
147         condition.getMetricKey(),
148         Condition.Operator.valueOf(condition.getOperator().name()),
149         condition.getErrorThreshold());
150       webQualityGate = EvaluatedQualityGate.newBuilder()
151         .setQualityGate(new org.sonar.server.qualitygate.QualityGate(qualityGate.getId(), qualityGate.getName(), Collections.singleton(qgCondition)))
152         .setStatus(Metric.Level.valueOf(qualityGate.getStatus().name()))
153         .addCondition(qgCondition, EvaluatedCondition.EvaluationStatus.valueOf(condition.getStatus().name()), condition.getValue())
154         .build();
155     }
156
157     verify(payloadFactory).create(new ProjectAnalysis(
158       new org.sonar.server.webhook.Project(project.getUuid(), project.getKey(), project.getName()),
159       new org.sonar.server.webhook.CeTask(ceTask.getId(), org.sonar.server.webhook.CeTask.Status.valueOf(ceTask.getStatus().name())),
160       analysisUUid == null ? null : new Analysis(analysisUUid, date.getTime()),
161       new org.sonar.server.webhook.Branch(branch.isMain(), branch.getName().get(), org.sonar.server.webhook.Branch.Type.valueOf(branch.getType().name())),
162       webQualityGate,
163       analysisUUid == null ? null : date.getTime(),
164       properties));
165   }
166 }