]> source.dussan.org Git - sonarqube.git/blob
ac8224ce0f6a4f677fe3b932b682195288e9ba5c
[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.io.IOException;
23 import java.util.Date;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.ce.posttask.CeTask;
27 import org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester;
28 import org.sonar.api.config.MapSettings;
29 import org.sonar.api.utils.log.LogTester;
30 import org.sonar.api.utils.log.LoggerLevel;
31 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
32 import org.sonar.server.computation.task.projectanalysis.component.TestSettingsRepository;
33 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newCeTaskBuilder;
37 import static org.sonar.api.ce.posttask.PostProjectAnalysisTaskTester.newProjectBuilder;
38 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.DUMB_PROJECT;
39
40 public class WebhookPostTaskTest {
41
42   private static final long NOW = 1_500_000_000_000L;
43
44   @Rule
45   public LogTester logTester = new LogTester().setLevel(LoggerLevel.DEBUG);
46
47   @Rule
48   public TreeRootHolderRule rootHolder = new TreeRootHolderRule().setRoot(DUMB_PROJECT);
49
50   private final MapSettings settings = new MapSettings();
51   private final TestWebhookCaller caller = new TestWebhookCaller();
52
53   @Test
54   public void do_nothing_if_no_webhooks() {
55     execute();
56
57     assertThat(caller.countSent()).isEqualTo(0);
58     assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
59   }
60
61   @Test
62   public void send_global_webhooks() {
63     settings.setProperty("sonar.webhooks.global", "1,2");
64     settings.setProperty("sonar.webhooks.global.1.name", "First");
65     settings.setProperty("sonar.webhooks.global.1.url", "http://url1");
66     settings.setProperty("sonar.webhooks.global.2.name", "Second");
67     settings.setProperty("sonar.webhooks.global.2.url", "http://url2");
68     caller.enqueueSuccess(NOW, 200, 1_234);
69     caller.enqueueFailure(NOW, new IOException("Fail to connect"));
70
71     execute();
72
73     assertThat(caller.countSent()).isEqualTo(2);
74     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Sent webhook 'First' | url=http://url1 | time=1234ms | status=200");
75     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Failed to send webhook 'Second' | url=http://url2 | message=Fail to connect");
76   }
77   @Test
78   public void send_project_webhooks() {
79     settings.setProperty("sonar.webhooks.project", "1");
80     settings.setProperty("sonar.webhooks.project.1.name", "First");
81     settings.setProperty("sonar.webhooks.project.1.url", "http://url1");
82     caller.enqueueSuccess(NOW, 200, 1_234);
83
84     execute();
85
86     assertThat(caller.countSent()).isEqualTo(1);
87     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Sent webhook 'First' | url=http://url1 | time=1234ms | status=200");
88   }
89
90   private void execute() {
91     SettingsRepository settingsRepository = new TestSettingsRepository(settings);
92     WebhookPostTask task = new WebhookPostTask(rootHolder, settingsRepository, caller);
93
94     PostProjectAnalysisTaskTester.of(task)
95       .at(new Date())
96       .withCeTask(newCeTaskBuilder()
97         .setStatus(CeTask.Status.SUCCESS)
98         .setId("#1")
99         .build())
100       .withProject(newProjectBuilder()
101         .setUuid("P1_UUID")
102         .setKey("P1")
103         .setName("Project One")
104         .build())
105       .execute();
106   }
107 }