From 314a594674f6708eeb8cc9d15e013a3fa5697b7d Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 21 Nov 2019 11:03:48 +0100 Subject: [PATCH] SONAR-12735 drop ws api/updatecenter/upload --- .../updatecenter/ws/UpdateCenterWsModule.java | 1 - .../server/updatecenter/ws/UploadAction.java | 78 --------- .../ws/UpdateCenterWsModuleTest.java | 2 +- .../updatecenter/ws/UploadActionTest.java | 160 ------------------ 4 files changed, 1 insertion(+), 240 deletions(-) delete mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UploadAction.java delete mode 100644 server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UploadActionTest.java diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModule.java index a9af13ec956..f3b3858617c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModule.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModule.java @@ -25,7 +25,6 @@ public class UpdateCenterWsModule extends Module { @Override protected void configureModule() { add( - UploadAction.class, InstalledPluginsAction.class, UpdateCenterWs.class diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UploadAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UploadAction.java deleted file mode 100644 index c30ccacb222..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/updatecenter/ws/UploadAction.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 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.updatecenter.ws; - -import java.io.File; -import java.io.InputStream; -import java.nio.file.Files; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.server.platform.ServerFileSystem; -import org.sonar.server.user.UserSession; - -import static com.google.common.base.Preconditions.checkArgument; -import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; -import static org.apache.commons.io.IOUtils.closeQuietly; -import static org.sonar.api.server.ws.Request.Part; - -public class UploadAction implements UpdateCenterWsAction { - - public static final String PARAM_FILE = "file"; - - private final UserSession userSession; - private final File downloadDir; - - public UploadAction(UserSession userSession, ServerFileSystem fileSystem) { - this.userSession = userSession; - this.downloadDir = fileSystem.getDownloadedPluginsDir(); - } - - @Override - public void define(WebService.NewController context) { - WebService.NewAction action = context.createAction("upload") - .setDescription("Upload a plugin.
Requires 'Administer System' permission.") - .setSince("6.0") - .setPost(true) - .setInternal(true) - .setHandler(this); - - action.createParam(PARAM_FILE) - .setDescription("The jar file of the plugin to install") - .setRequired(true); - } - - @Override - public void handle(Request request, Response response) throws Exception { - userSession.checkIsSystemAdministrator(); - - Part part = request.mandatoryParamAsPart(PARAM_FILE); - String fileName = part.getFileName(); - checkArgument(fileName.endsWith(".jar"), "Only jar file is allowed"); - InputStream inputStream = part.getInputStream(); - try { - File destPlugin = new File(downloadDir, fileName); - Files.copy(inputStream, destPlugin.toPath(), REPLACE_EXISTING); - response.noContent(); - } finally { - closeQuietly(inputStream); - } - } -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModuleTest.java index 11bdedf7ab5..74b70188437 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UpdateCenterWsModuleTest.java @@ -30,7 +30,7 @@ public class UpdateCenterWsModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new UpdateCenterWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 3); + assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UploadActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UploadActionTest.java deleted file mode 100644 index 6601f40de4f..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/updatecenter/ws/UploadActionTest.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2019 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.updatecenter.ws; - -import java.io.File; -import java.io.InputStream; -import java.nio.channels.ClosedChannelException; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.server.ws.WebService; -import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.platform.ServerFileSystem; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.TestResponse; -import org.sonar.server.ws.WsActionTester; - -import static java.nio.file.Files.newInputStream; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.test.ExceptionCauseMatcher.hasType; - -public class UploadActionTest { - - private static final String PLUGIN_NAME = "plugin.jar"; - - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - private ServerFileSystem fileSystem = mock(ServerFileSystem.class); - private File pluginDirectory; - private File plugin = new File(getClass().getResource("UploadActionTest/plugin.jar").getFile()); - private WsActionTester wsTester; - - @Before - public void setUp() throws Exception { - pluginDirectory = folder.newFolder(); - when(fileSystem.getDownloadedPluginsDir()).thenReturn(pluginDirectory); - wsTester = new WsActionTester(new UploadAction(userSession, fileSystem)); - } - - @Test - public void define_upload_action() { - WebService.Action action = wsTester.getDef(); - - assertThat(action).isNotNull(); - assertThat(action.key()).isEqualTo("upload"); - assertThat(action.handler()).isNotNull(); - assertThat(action.isInternal()).isTrue(); - assertThat(action.isPost()).isTrue(); - assertThat(action.params()).hasSize(1); - } - - @Test - public void upload_plugin() throws Exception { - logInAsSystemAdministrator(); - - TestResponse response = call(newInputStream(plugin.toPath()), PLUGIN_NAME); - - assertThat(response.getStatus()).isEqualTo(204); - assertPluginIsUploaded(PLUGIN_NAME); - } - - @Test - public void erase_existing_plugin_if_already_exists() throws Exception { - logInAsSystemAdministrator(); - - File plugin1 = new File(getClass().getResource("UploadActionTest/plugin.jar").getFile()); - call(newInputStream(plugin1.toPath()), PLUGIN_NAME); - - File plugin2 = new File(getClass().getResource("UploadActionTest/anotherPlugin.jar").getFile()); - call(newInputStream(plugin2.toPath()), PLUGIN_NAME); - - File result = new File(pluginDirectory, PLUGIN_NAME); - assertThat(result.exists()).isTrue(); - assertThat(result.length()).isNotEqualTo(plugin1.length()).isEqualTo(plugin2.length()); - } - - @Test - public void fail_when_plugin_extension_is_not_jar() throws Exception { - logInAsSystemAdministrator(); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Only jar file is allowed"); - call(newInputStream(plugin.toPath()), "plugin.zip"); - } - - @Test - public void fail_when_no_files_param() { - logInAsSystemAdministrator(); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The 'file' parameter is missing"); - wsTester.newRequest().execute(); - } - - @Test - public void input_stream_should_be_closed() throws Exception { - logInAsSystemAdministrator(); - - InputStream inputStream = newInputStream(plugin.toPath()); - call(inputStream, PLUGIN_NAME); - - // As the same InputStream is used, it will fail as it should have been called during the first execution of the WS - expectedException.expectCause(hasType(ClosedChannelException.class)); - call(inputStream, PLUGIN_NAME); - } - - @Test - public void throw_ForbiddenException_if_not_system_administrator() throws Exception { - userSession.logIn().setNonSystemAdministrator(); - - expectedException.expect(ForbiddenException.class); - expectedException.expectMessage("Insufficient privileges"); - - call(newInputStream(plugin.toPath()), PLUGIN_NAME); - } - - private TestResponse call(InputStream inputStream, String fileName) { - return wsTester.newRequest() - .setPart("file", inputStream, fileName) - .execute(); - } - - private void logInAsSystemAdministrator() { - userSession.logIn().setSystemAdministrator(); - } - - private void assertPluginIsUploaded(String pluginName) { - File result = new File(pluginDirectory, pluginName); - assertThat(result.exists()).isTrue(); - } - -} -- 2.39.5