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