From 3a27ef32e1d258a82d457d47bf9aed52390565bf Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 7 Jul 2016 16:53:34 +0200 Subject: [PATCH] SONAR-7804 /api/events creates events only on PROJECT --- .../test/java/it/projectEvent/EventTest.java | 45 +++++++++++++------ .../app/controllers/api/events_controller.rb | 2 +- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/it/it-tests/src/test/java/it/projectEvent/EventTest.java b/it/it-tests/src/test/java/it/projectEvent/EventTest.java index 16b41beca67..554b69f50ef 100644 --- a/it/it-tests/src/test/java/it/projectEvent/EventTest.java +++ b/it/it-tests/src/test/java/it/projectEvent/EventTest.java @@ -32,12 +32,14 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.wsclient.services.Event; import org.sonar.wsclient.services.EventQuery; +import org.sonarqube.ws.client.PostRequest; +import org.sonarqube.ws.client.WsConnector; +import org.sonarqube.ws.client.WsResponse; +import util.ItUtils; import util.QaOnly; import util.selenium.SeleneseTest; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static util.ItUtils.projectDir; @Category(QaOnly.class) @@ -59,9 +61,29 @@ public class EventTest { orchestrator.executeSelenese( Selenese.builder().setHtmlTestsInClasspath("events", "/projectEvent/EventTest/create_event_with_special_character.html", - "/projectEvent/EventTest/no_events_widget_on_dir.html" - ).build() - ); + "/projectEvent/EventTest/no_events_widget_on_dir.html") + .build()); + } + + @Test + public void old_ws_events_does_not_allow_creating_events_on_modules() { + SonarScanner sampleProject = SonarScanner.create(projectDir("shared/xoo-multi-modules-sample")); + orchestrator.executeBuild(sampleProject); + + WsConnector wsConnector = ItUtils.newAdminWsClient(orchestrator).wsConnector(); + WsResponse response = wsConnector.call(newCreateEventRequest("com.sonarsource.it.samples:multi-modules-sample", "bar")); + assertThat(response.code()) + .isEqualTo(200); + + assertThat(wsConnector.call(newCreateEventRequest("com.sonarsource.it.samples:multi-modules-sample:module_a", "bar")).code()) + .isEqualTo(400); + } + + private static PostRequest newCreateEventRequest(String componentKey, String eventName) { + return new PostRequest("/api/events") + .setParam("resource", componentKey) + .setParam("name", eventName) + .setParam("category", "Foo"); } @Test @@ -70,8 +92,7 @@ public class EventTest { new SeleneseTest( Selenese.builder().setHtmlTestsInClasspath("delete-event", - "/projectEvent/EventTest/create_delete_standard_event.html" - ).build()).runOn(orchestrator); + "/projectEvent/EventTest/create_delete_standard_event.html").build()).runOn(orchestrator); } @Test @@ -84,9 +105,7 @@ public class EventTest { orchestrator.executeSelenese( Selenese.builder().setHtmlTestsInClasspath("event-widget", - "/projectEvent/EventTest/show_events_using_filters.html" - ).build() - ); + "/projectEvent/EventTest/show_events_using_filters.html").build()); } /** @@ -104,9 +123,9 @@ public class EventTest { // there should be only 1 "0.1-SNAPSHOT" event and only 1 "0.1" event List events = orchestrator.getServer().getWsClient().findAll(new EventQuery().setResourceKey("sample")); - assertThat(events.size(), is(2)); + assertThat(events.size()).isEqualTo(2); List eventNames = Lists.newArrayList(events.get(0).getName(), events.get(1).getName()); - assertThat(eventNames, hasItems("1.0", "1.0-SNAPSHOT")); + assertThat(eventNames).contains("1.0", "1.0-SNAPSHOT"); } private static void executeAnalysis(String... properties) { diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb index 2f01c8e0ab6..b96323f589c 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb @@ -124,7 +124,7 @@ class Api::EventsController < Api::ApiController def create begin load_resource(:admin, params[:resource]) - raise "Resource must be a root project" unless @resource.scope=='PRJ' + raise "Resource must be a root project" unless @resource.scope=='PRJ' && (@resource.qualifier=='TRK' || @resource.qualifier=='VW' || @resource.qualifier=='DEV') analysis=nil if (params[:dateTime]) -- 2.39.5