3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.webhook;
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;
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;
40 public class WebhookPostTaskTest {
42 private static final long NOW = 1_500_000_000_000L;
45 public LogTester logTester = new LogTester().setLevel(LoggerLevel.DEBUG);
48 public TreeRootHolderRule rootHolder = new TreeRootHolderRule().setRoot(DUMB_PROJECT);
50 private final MapSettings settings = new MapSettings();
51 private final TestWebhookCaller caller = new TestWebhookCaller();
54 public void do_nothing_if_no_webhooks() {
57 assertThat(caller.countSent()).isEqualTo(0);
58 assertThat(logTester.logs(LoggerLevel.DEBUG)).isEmpty();
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"));
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");
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);
86 assertThat(caller.countSent()).isEqualTo(1);
87 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Sent webhook 'First' | url=http://url1 | time=1234ms | status=200");
90 private void execute() {
91 SettingsRepository settingsRepository = new TestSettingsRepository(settings);
92 WebhookPostTask task = new WebhookPostTask(rootHolder, settingsRepository, caller);
94 PostProjectAnalysisTaskTester.of(task)
96 .withCeTask(newCeTaskBuilder()
97 .setStatus(CeTask.Status.SUCCESS)
100 .withProject(newProjectBuilder()
103 .setName("Project One")