diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2022-01-19 14:49:03 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-02-18 15:48:03 +0000 |
commit | dd5c24bc96a10a3b81bd1c94d40b936ba451cd90 (patch) | |
tree | ef8b6bed3f3ebd7ebb2f4221202f7481bbf5280e /server/sonar-webserver-pushapi/src | |
parent | 44ec99de4e139b08a1eac054aad07ea1e8176d33 (diff) | |
download | sonarqube-dd5c24bc96a10a3b81bd1c94d40b936ba451cd90.tar.gz sonarqube-dd5c24bc96a10a3b81bd1c94d40b936ba451cd90.zip |
SONAR-15934 introduced new module sonar-webserver-pushapi and added required dependencies
Diffstat (limited to 'server/sonar-webserver-pushapi/src')
7 files changed, 337 insertions, 0 deletions
diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushAction.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushAction.java new file mode 100644 index 00000000000..405145b77ac --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushAction.java @@ -0,0 +1,27 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonar.server.pushapi; + +import org.sonar.server.ws.WsAction; + +public interface ServerPushAction extends WsAction { + //marker interface + +} diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWs.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWs.java new file mode 100644 index 00000000000..3ac5dadb628 --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWs.java @@ -0,0 +1,43 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonar.server.pushapi; + +import java.util.List; +import org.sonar.api.server.ws.WebService; + +public class ServerPushWs implements WebService { + private final List<ServerPushAction> actions; + + public ServerPushWs(List<ServerPushAction> actions) { + this.actions = actions; + } + + @Override + public void define(Context context) { + NewController controller = context + .createController("api/push") + .setSince("9.4") + .setDescription("Endpoints supporting server side events."); + + actions.forEach(action -> action.define(controller)); + + controller.done(); + } +} diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWsModule.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWsModule.java new file mode 100644 index 00000000000..f82da6a534d --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWsModule.java @@ -0,0 +1,34 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonar.server.pushapi; + +import org.sonar.core.platform.Module; +import org.sonar.server.pushapi.sonarlint.SonarLintPushAction; + +public class ServerPushWsModule extends Module { + + @Override + protected void configureModule() { + add( + ServerPushWs.class, + + SonarLintPushAction.class); + } +} diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintPushAction.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintPushAction.java new file mode 100644 index 00000000000..3c73eefd492 --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/sonarlint/SonarLintPushAction.java @@ -0,0 +1,69 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonar.server.pushapi.sonarlint; + +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.server.pushapi.ServerPushAction; + +public class SonarLintPushAction implements ServerPushAction { + + private static final Logger LOGGER = Loggers.get(SonarLintPushAction.class); + + private static final String PROJECT_PARAM_KEY = "projectKeys"; + private static final String LANGUAGE_PARAM_KEY = "languages"; + + @Override + public void define(WebService.NewController controller) { + WebService.NewAction action = controller + .createAction("sonarlint_events") + .setInternal(true) + .setDescription("Endpoint for listening to server side events. Currently it notifies listener about change to activation of a rule") + .setSince("9.4") + .setHandler(this); + + action + .createParam(PROJECT_PARAM_KEY) + .setDescription("Comma-separated list of projects keys for which events will be delivered") + .setRequired(true) + .setExampleValue("example-project-key,example-project-key2"); + + action + .createParam(LANGUAGE_PARAM_KEY) + .setDescription("Comma-separated list of languages for which events will be delivered") + .setRequired(true) + .setExampleValue("java,cobol"); + } + + @Override + public void handle(Request request, Response response) { + String projectKeys = request.getParam(PROJECT_PARAM_KEY).getValue(); + String languages = request.getParam(LANGUAGE_PARAM_KEY).getValue(); + + //to remove later + LOGGER.debug(projectKeys != null ? projectKeys : ""); + LOGGER.debug(languages != null ? languages : ""); + + response.noContent(); + } +} diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsModuleTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsModuleTest.java new file mode 100644 index 00000000000..696cb6fa9b1 --- /dev/null +++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsModuleTest.java @@ -0,0 +1,39 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonar.server.pushapi; + +import org.junit.Test; +import org.sonar.core.platform.ComponentContainer; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ServerPushWsModuleTest { + + + @Test + public void verify_count_of_added_components() { + ComponentContainer container = new ComponentContainer(); + + new ServerPushWsModule().configure(container); + + assertThat(container.size()).isPositive(); + } +} + diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsTest.java new file mode 100644 index 00000000000..184044a2ea4 --- /dev/null +++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/ServerPushWsTest.java @@ -0,0 +1,61 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonar.server.pushapi; + +import java.util.Arrays; +import org.junit.Test; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ServerPushWsTest { + + private DummyServerPushAction dummyServerPushAction = new DummyServerPushAction(); + private ServerPushWs underTest = new ServerPushWs(Arrays.asList(dummyServerPushAction)); + + @Test + public void define_ws() { + WebService.Context context = new WebService.Context(); + + underTest.define(context); + + WebService.Controller controller = context.controller("api/push"); + assertThat(controller).isNotNull(); + assertThat(controller.path()).isEqualTo("api/push"); + assertThat(controller.since()).isEqualTo("9.4"); + assertThat(controller.description()).isNotEmpty(); + assertThat(controller.actions()).isNotEmpty(); + } + + private static class DummyServerPushAction implements ServerPushAction { + + @Override + public void define(WebService.NewController context) { + context.createAction("foo").setHandler(this); + } + + @Override + public void handle(Request request, Response response) { + + } + } +} diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintPushActionTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintPushActionTest.java new file mode 100644 index 00000000000..87a7203a40a --- /dev/null +++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/sonarlint/SonarLintPushActionTest.java @@ -0,0 +1,64 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonar.server.pushapi.sonarlint; + +import org.junit.Test; +import org.sonar.api.server.ws.WebService; +import org.sonar.server.ws.TestRequest; +import org.sonar.server.ws.TestResponse; +import org.sonar.server.ws.WsActionTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.tuple; + +public class SonarLintPushActionTest { + + private final WsActionTester ws = new WsActionTester(new SonarLintPushAction()); + + @Test + public void defineTest() { + WebService.Action def = ws.getDef(); + + assertThat(def.since()).isEqualTo("9.4"); + assertThat(def.isInternal()).isTrue(); + assertThat(def.params()) + .extracting(WebService.Param::key, WebService.Param::isRequired) + .containsExactlyInAnyOrder(tuple("languages", true), tuple("projectKeys", true)); + } + + @Test + public void handle_returnsNoResponseWhenParamsProvided() { + TestResponse response = ws.newRequest() + .setParam("projectKeys", "project1,project2") + .setParam("languages", "java") + .execute(); + + assertThat(response.getStatus()).isEqualTo(204); + } + + @Test + public void handle_whenParamsNotProvided_throwException() { + TestRequest testRequest = ws.newRequest(); + assertThatThrownBy(testRequest::execute) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("The 'projectKeys' parameter is missing"); + } +} |