import org.apache.commons.lang.StringUtils;
import org.picocontainer.Startable;
import org.sonar.api.server.ServerSide;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.platform.ServerFileSystem;
/**
try {
File input = new File(batchDir, filename);
if (!input.exists() || !FileUtils.directoryContains(batchDir, input)) {
- throw new IllegalArgumentException("Bad filename: " + filename);
+ throw new NotFoundException("Bad filename: " + filename);
}
return input;
-
} catch (IOException e) {
throw new IllegalStateException("Can get file " + filename, e);
}
*/
package org.sonar.server.batch;
-import java.io.IOException;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
import org.sonar.api.server.ws.WebService;
+import static java.util.Arrays.stream;
+
public class BatchWs implements WebService {
public static final String API_ENDPOINT = "batch";
- private final BatchIndex batchIndex;
private final BatchWsAction[] actions;
- public BatchWs(BatchIndex batchIndex, BatchWsAction... actions) {
- this.batchIndex = batchIndex;
+ public BatchWs(BatchWsAction... actions) {
this.actions = actions;
}
NewController controller = context.createController(API_ENDPOINT)
.setSince("4.4")
.setDescription("Get JAR files and referentials for batch");
-
- defineIndexAction(controller);
- defineFileAction(controller);
- for (BatchWsAction action : actions) {
- action.define(controller);
- }
-
+ stream(actions).forEach(action -> action.define(controller));
controller.done();
}
- private void defineIndexAction(NewController controller) {
- controller.createAction("index")
- .setInternal(true)
- .setSince("4.4")
- .setDescription("List the JAR files to be downloaded by scanners")
- .setHandler((request, response) -> {
- try {
- response.stream().setMediaType("text/plain");
- IOUtils.write(batchIndex.getIndex(), response.stream().output());
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- })
- .setResponseExample(getClass().getResource("batch-index-example.txt"));
- }
-
- private void defineFileAction(NewController controller) {
- NewAction action = controller.createAction("file")
- .setInternal(true)
- .setSince("4.4")
- .setDescription("Download a JAR file listed in the index (see batch/index)")
- .setResponseExample(getClass().getResource("batch-file-example.txt"))
- .setHandler((request, response) -> {
- String filename = request.mandatoryParam("name");
- try {
- response.stream().setMediaType("application/java-archive");
- FileUtils.copyFile(batchIndex.getFile(filename), response.stream().output());
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- });
- action
- .createParam("name")
- .setDescription("File name")
- .setExampleValue("batch-library-2.3.jar");
- }
}
ProjectDataLoader.class,
IssuesAction.class,
UsersAction.class,
+ IndexAction.class,
+ FileAction.class,
BatchWs.class);
}
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.batch;
+
+import java.io.File;
+import java.io.IOException;
+import org.apache.commons.io.FileUtils;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+
+public class FileAction implements BatchWsAction {
+
+ private final BatchIndex batchIndex;
+
+ public FileAction(BatchIndex batchIndex) {
+ this.batchIndex = batchIndex;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ WebService.NewAction action = context.createAction("file")
+ .setInternal(true)
+ .setSince("4.4")
+ .setDescription("Download a JAR file listed in the index (see batch/index)")
+ .setResponseExample(getClass().getResource("batch-file-example.txt"))
+ .setHandler(this);
+ action
+ .createParam("name")
+ .setDescription("File name")
+ .setExampleValue("batch-library-2.3.jar");
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ String filename = request.mandatoryParam("name");
+ try {
+ response.stream().setMediaType("application/java-archive");
+ File file = batchIndex.getFile(filename);
+ FileUtils.copyFile(file, response.stream().output());
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.batch;
+
+import java.io.IOException;
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+
+import static com.google.common.base.Preconditions.checkState;
+
+public class IndexAction implements BatchWsAction {
+
+ private final BatchIndex batchIndex;
+
+ public IndexAction(BatchIndex batchIndex) {
+ this.batchIndex = batchIndex;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ context.createAction("index")
+ .setInternal(true)
+ .setSince("4.4")
+ .setDescription("List the JAR files to be downloaded by scanners")
+ .setHandler(this)
+ .setResponseExample(getClass().getResource("index-example.txt"));
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ try {
+ response.stream().setMediaType("text/plain");
+ String index = batchIndex.getIndex();
+ checkState(index != null, "No available files");
+ IOUtils.write(index, response.stream().output());
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+}
+++ /dev/null
-sonar-batch-4.4.jar|2d7cbec208114970ea419ce963775f68
-sonar-batch-library-2.3.jar|86f577369ec914ae079411803cebc7d2
--- /dev/null
+sonar-batch-4.4.jar|2d7cbec208114970ea419ce963775f68
+sonar-batch-library-2.3.jar|86f577369ec914ae079411803cebc7d2
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.platform.ServerFileSystem;
import static org.assertj.core.api.Assertions.assertThat;
*/
@Test
public void check_location_of_file() {
- thrown.expect(IllegalArgumentException.class);
+ thrown.expect(NotFoundException.class);
thrown.expectMessage("Bad filename: ../sonar-batch.jar");
BatchIndex batchIndex = new BatchIndex(fs);
@Test
public void file_does_not_exist() {
- thrown.expect(IllegalArgumentException.class);
+ thrown.expect(NotFoundException.class);
thrown.expectMessage("Bad filename: other.jar");
BatchIndex batchIndex = new BatchIndex(fs);
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new BatchWsModule().configure(container);
- assertThat(container.size()).isEqualTo(8);
+ assertThat(container.size()).isEqualTo(10);
}
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.batch;
-
-import java.io.File;
-import org.apache.commons.io.FileUtils;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.db.DbClient;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.issue.index.IssueIndex;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public class BatchWsTest {
- @Rule
- public UserSessionRule userSessionRule = UserSessionRule.standalone();
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Mock
- BatchIndex batchIndex;
-
- WsTester tester;
-
- @Before
- public void before() {
- tester = new WsTester(new BatchWs(batchIndex,
- new ProjectAction(mock(ProjectDataLoader.class)),
- new IssuesAction(mock(DbClient.class), mock(IssueIndex.class), userSessionRule, mock(ComponentFinder.class))));
- }
-
- @Test
- public void download_index() throws Exception {
- when(batchIndex.getIndex()).thenReturn("sonar-batch.jar|acbd18db4cc2f85cedef654fccc4a4d8");
-
- String index = tester.newGetRequest("batch", "index").execute().outputAsString();
- assertThat(index).isEqualTo("sonar-batch.jar|acbd18db4cc2f85cedef654fccc4a4d8");
- }
-
- @Test
- public void download_file() throws Exception {
- String filename = "sonar-batch.jar";
-
- File file = temp.newFile(filename);
- FileUtils.writeStringToFile(file, "foo");
- when(batchIndex.getFile(filename)).thenReturn(file);
-
- String jar = tester.newGetRequest("batch", "file").setParam("name", filename).execute().outputAsString();
- assertThat(jar).isEqualTo("foo");
- }
-
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.batch;
+
+import java.io.File;
+import org.apache.commons.io.FileUtils;
+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.NotFoundException;
+import org.sonar.server.platform.ServerFileSystem;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.apache.commons.io.FileUtils.writeStringToFile;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class FileActionTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private ServerFileSystem serverFileSystem = mock(ServerFileSystem.class);
+ private BatchIndex batchIndex = new BatchIndex(serverFileSystem);
+
+ private File batchDir;
+
+ private WsActionTester tester = new WsActionTester(new FileAction(batchIndex));
+
+ @Before
+ public void setUp() throws Exception {
+ File homeDir = temp.newFolder();
+ when(serverFileSystem.getHomeDir()).thenReturn(homeDir);
+ batchDir = new File(homeDir, "lib/scanner");
+ FileUtils.forceMkdir(batchDir);
+ }
+
+ @Test
+ public void download_file() throws Exception {
+ writeStringToFile(new File(batchDir, "sonar-batch.jar"), "foo");
+ writeStringToFile(new File(batchDir, "other.jar"), "bar");
+ batchIndex.start();
+
+ String jar = tester.newRequest().setParam("name", "sonar-batch.jar").execute().getInput();
+
+ assertThat(jar).isEqualTo("foo");
+ }
+
+ @Test
+ public void throw_NotFoundException_when_file_does_not_exist() throws Exception {
+ writeStringToFile(new File(batchDir, "sonar-batch.jar"), "foo");
+ batchIndex.start();
+
+ thrown.expect(NotFoundException.class);
+ thrown.expectMessage("Bad filename: unknown");
+ tester.newRequest().setParam("name", "unknown").execute();
+ }
+
+ @Test
+ public void throw_IAE_when_no_name_parameter() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("The 'name' parameter is missing");
+ tester.newRequest().execute();
+ }
+
+ @Test
+ public void test_definition() throws Exception {
+ WebService.Action definition = tester.getDef();
+ assertThat(definition.isInternal()).isTrue();
+ assertThat(definition.isPost()).isFalse();
+ assertThat(definition.responseExampleAsString()).isNotEmpty();
+ assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("name");
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.batch;
+
+import java.io.File;
+import org.apache.commons.io.FileUtils;
+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.platform.ServerFileSystem;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.apache.commons.io.FileUtils.writeStringToFile;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class IndexActionTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private ServerFileSystem serverFileSystem = mock(ServerFileSystem.class);
+ private BatchIndex batchIndex = new BatchIndex(serverFileSystem);
+
+ private File batchDir;
+
+ private WsActionTester tester = new WsActionTester(new IndexAction(batchIndex));
+
+ @Before
+ public void setUp() throws Exception {
+ File homeDir = temp.newFolder();
+ when(serverFileSystem.getHomeDir()).thenReturn(homeDir);
+ batchDir = new File(homeDir, "lib/scanner");
+ FileUtils.forceMkdir(batchDir);
+ }
+
+ @Test
+ public void get_index() throws Exception {
+ writeStringToFile(new File(batchDir, "sonar-batch.jar"), "something");
+ batchIndex.start();
+
+ String index = tester.newRequest().execute().getInput();
+
+ assertThat(index).startsWith("sonar-batch.jar|");
+ }
+
+ @Test
+ public void throw_ISE_when_no_file() throws Exception {
+ thrown.expect(IllegalStateException.class);
+ tester.newRequest().execute();
+ }
+
+ @Test
+ public void test_definition() throws Exception {
+ WebService.Action definition = tester.getDef();
+ assertThat(definition.isInternal()).isTrue();
+ assertThat(definition.isPost()).isFalse();
+ assertThat(definition.responseExampleAsString()).isNotEmpty();
+ assertThat(definition.params()).isEmpty();
+ }
+}
public void before() {
IssueIndex issueIndex = new IssueIndex(es.client(), system2, userSessionRule, new AuthorizationTypeSupport(userSessionRule));
IssuesAction issuesAction = new IssuesAction(db.getDbClient(), issueIndex, userSessionRule, new ComponentFinder(db.getDbClient()));
- tester = new WsTester(new BatchWs(new BatchIndex(fs), issuesAction));
+ tester = new WsTester(new BatchWs(issuesAction));
}
@Test
import org.sonar.scanner.protocol.input.ScannerInput.User;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.UnauthorizedException;
-import org.sonar.server.platform.ServerFileSystem;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
public class UsersActionTest {
public void before() {
userIndex = new UserIndex(es.client());
usersAction = new UsersAction(userIndex, userSessionRule);
- tester = new WsTester(new BatchWs(new BatchIndex(mock(ServerFileSystem.class)), usersAction));
+ tester = new WsTester(new BatchWs(usersAction));
}
@Test