diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2018-02-14 15:54:21 +0100 |
---|---|---|
committer | Guillaume Jambet <guillaume.jambet@gmail.com> | 2018-03-01 15:21:05 +0100 |
commit | bad30545b5ed235f7778c00baedbbe0e41006049 (patch) | |
tree | f6b9e864dcbe2d0c0d2eca070a73a01d734caeef /server | |
parent | 5caae9b3367cdad3c31d2ed8a7031efd8dfe71c3 (diff) | |
download | sonarqube-bad30545b5ed235f7778c00baedbbe0e41006049.tar.gz sonarqube-bad30545b5ed235f7778c00baedbbe0e41006049.zip |
SONAR-10345 Add IT's for webhooks management
Diffstat (limited to 'server')
4 files changed, 179 insertions, 0 deletions
diff --git a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/Tester.java b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/Tester.java index 9401ef2a907..fe1d5d8236e 100644 --- a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/Tester.java +++ b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/Tester.java @@ -207,6 +207,11 @@ public class Tester extends ExternalResource implements TesterSession { return rootSession.qGates(); } + @Override + public WebhookTester webhooks() { + return rootSession.webhooks(); + } + private static class TesterSessionImpl implements TesterSession { private final WsClient client; @@ -262,5 +267,10 @@ public class Tester extends ExternalResource implements TesterSession { public QGateTester qGates() { return new QGateTester(this); } + + @Override + public WebhookTester webhooks() { + return new WebhookTester(this); + } } } diff --git a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/TesterSession.java b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/TesterSession.java index f9e1b05483d..d0e3afdcbd2 100644 --- a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/TesterSession.java +++ b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/TesterSession.java @@ -41,4 +41,6 @@ public interface TesterSession { QGateTester qGates(); + WebhookTester webhooks(); + } diff --git a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/WebhookTester.java b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/WebhookTester.java new file mode 100644 index 00000000000..5b0382051cf --- /dev/null +++ b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/WebhookTester.java @@ -0,0 +1,120 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarqube.qa.util; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import javax.annotation.Nullable; +import org.sonarqube.ws.Organizations.Organization; +import org.sonarqube.ws.Projects.CreateWsResponse.Project; +import org.sonarqube.ws.Webhooks.CreateWsResponse.Webhook; +import org.sonarqube.ws.Webhooks.Delivery; +import org.sonarqube.ws.client.webhooks.CreateRequest; +import org.sonarqube.ws.client.webhooks.DeleteRequest; +import org.sonarqube.ws.client.webhooks.DeliveriesRequest; +import org.sonarqube.ws.client.webhooks.DeliveryRequest; +import org.sonarqube.ws.client.webhooks.ListRequest; +import org.sonarqube.ws.client.webhooks.WebhooksService; + +import static java.util.Arrays.stream; +import static java.util.Objects.requireNonNull; +import static org.assertj.core.api.Assertions.assertThat; + +public class WebhookTester { + private static final AtomicInteger ID_GENERATOR = new AtomicInteger(); + + private final TesterSession session; + + WebhookTester(TesterSession session) { + this.session = session; + } + + public WebhooksService service() { + return session.wsClient().webhooks(); + } + + public Webhook generate(Consumer<CreateRequest>... populators) { + return generate(null, null, populators); + } + + public Webhook generate(Organization organization, Consumer<CreateRequest>... populators) { + return generate(organization, null, populators); + } + + public Webhook generate(Project project, Consumer<CreateRequest>... populators) { + return generate(null, project, populators); + } + + @SafeVarargs + public final Webhook generate( + @Nullable Organization organization, + @Nullable Project project, + Consumer<CreateRequest>... populators + ) { + int id = ID_GENERATOR.getAndIncrement(); + CreateRequest request = new CreateRequest() + .setName("Webhook " + id) + .setUrl("https://webhook-" + id) + .setProject(project != null ? project.getKey(): null) + .setOrganization(organization != null ? organization.getKey() : null); + stream(populators).forEach(p -> p.accept(request)); + return service().create(request).getWebhook(); + } + + public void deleteAllGlobal() { + service().list(new ListRequest()).getWebhooksList().forEach(p -> + service().delete(new DeleteRequest().setWebhook(p.getKey())) + ); + } + + public List<Delivery> getPersistedDeliveries(Project project) { + DeliveriesRequest deliveriesReq = new DeliveriesRequest().setComponentKey(project.getKey()); + return service().deliveries(deliveriesReq).getDeliveriesList(); + } + + public Delivery getPersistedDeliveryByName(Project project, String webhookName) { + List<Delivery> deliveries = getPersistedDeliveries(project); + Optional<Delivery> delivery = deliveries.stream().filter(d -> d.getName().equals(webhookName)).findFirst(); + assertThat(delivery).isPresent(); + return delivery.get(); + } + + public Delivery getDetailOfPersistedDelivery(Delivery delivery) { + Delivery detail = service().delivery(new DeliveryRequest().setDeliveryId(delivery.getId())).getDelivery(); + return requireNonNull(detail); + } + + public void assertThatPersistedDeliveryIsValid(Delivery delivery, @Nullable Project project, @Nullable String url) { + assertThat(delivery.getId()).isNotEmpty(); + assertThat(delivery.getName()).isNotEmpty(); + assertThat(delivery.hasSuccess()).isTrue(); + assertThat(delivery.getHttpStatus()).isGreaterThanOrEqualTo(200); + assertThat(delivery.getDurationMs()).isGreaterThanOrEqualTo(0); + assertThat(delivery.getAt()).isNotEmpty(); + if (project != null) { + assertThat(delivery.getComponentKey()).isEqualTo(project.getKey()); + } + if (url != null) { + assertThat(delivery.getUrl()).startsWith(url); + } + } +} diff --git a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/pageobjects/WebhooksPage.java b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/pageobjects/WebhooksPage.java index af6c8c6f816..75260c11e11 100644 --- a/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/pageobjects/WebhooksPage.java +++ b/server/sonar-qa-util/src/main/java/org/sonarqube/qa/util/pageobjects/WebhooksPage.java @@ -20,9 +20,13 @@ package org.sonarqube.qa.util.pageobjects; import com.codeborne.selenide.ElementsCollection; +import com.codeborne.selenide.SelenideElement; +import static com.codeborne.selenide.Condition.cssClass; +import static com.codeborne.selenide.Condition.enabled; import static com.codeborne.selenide.Condition.exist; import static com.codeborne.selenide.Condition.text; +import static com.codeborne.selenide.Condition.visible; import static com.codeborne.selenide.Selenide.$; import static com.codeborne.selenide.Selenide.$$; @@ -37,12 +41,55 @@ public class WebhooksPage { return this; } + public WebhooksPage hasNoWebhooks() { + $(".boxed-group").shouldHave(text("No webhook defined")); + return this; + } + public WebhooksPage countWebhooks(Integer number) { getWebhooks().shouldHaveSize(number); return this; } + public WebhooksPage createWebhook(String name, String url) { + $(".js-webhook-create").shouldBe(visible).shouldBe(enabled).shouldNotHave(cssClass("disabled")).click(); + modalShouldBeOpen("Create Webhook"); + $("#webhook-name").shouldBe(visible).sendKeys(name); + $("#webhook-url").shouldBe(visible).sendKeys(url); + $("button[type='submit']").shouldBe(visible).click(); + modalShouldBeClosed(); + return this; + } + + public WebhooksPage createIsDisabled() { + $(".js-webhook-create").shouldBe(visible).shouldHave(cssClass("disabled")).click(); + modalShouldBeClosed(); + return this; + } + + public WebhooksPage deleteWebhook(String webhookName) { + SelenideElement webhook = getWebhook(webhookName); + webhook.$(".dropdown-toggle").shouldBe(visible).click(); + webhook.$(".js-webhook-delete").shouldBe(visible).click(); + modalShouldBeOpen("Delete Webhook"); + $("button.button-red").shouldBe(visible).click(); + modalShouldBeClosed(); + return this; + } + + private static SelenideElement getWebhook(String webhookName) { + return getWebhooks().find(text(webhookName)).should(exist); + } + private static ElementsCollection getWebhooks() { return $$(".boxed-group tbody tr"); } + + private static void modalShouldBeOpen(String title) { + $(".modal-head").shouldBe(visible).shouldHave(text(title)); + } + + private static void modalShouldBeClosed() { + $(".modal-head").shouldNot(exist); + } } |