import org.sonar.server.rule.ws.RulesWs;
import org.sonar.server.rule.ws.TagsAction;
import org.sonar.server.setting.ws.SettingsWsModule;
-import org.sonar.server.source.HtmlSourceDecorator;
-import org.sonar.server.source.SourceService;
-import org.sonar.server.source.ws.HashAction;
-import org.sonar.server.source.ws.IndexAction;
-import org.sonar.server.source.ws.IssueSnippetsAction;
-import org.sonar.server.source.ws.LinesAction;
-import org.sonar.server.source.ws.LinesJsonWriter;
-import org.sonar.server.source.ws.RawAction;
-import org.sonar.server.source.ws.ScmAction;
-import org.sonar.server.source.ws.SourcesWs;
+import org.sonar.server.source.ws.SourceWsModule;
import org.sonar.server.startup.LogServerId;
import org.sonar.server.telemetry.TelemetryClient;
import org.sonar.server.telemetry.TelemetryDaemon;
DebtRulesXMLImporter.class,
// source
- HtmlSourceDecorator.class,
- LinesJsonWriter.class,
- SourceService.class,
- SourcesWs.class,
- org.sonar.server.source.ws.ShowAction.class,
- IssueSnippetsAction.class,
- LinesAction.class,
- HashAction.class,
- RawAction.class,
- IndexAction.class,
- ScmAction.class,
+ SourceWsModule.class,
// Duplications
DuplicationsParser.class,
--- /dev/null
+/*
+ * 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.source.ws;
+
+import org.sonar.core.platform.Module;
+import org.sonar.server.source.HtmlSourceDecorator;
+import org.sonar.server.source.SourceService;
+
+public class SourceWsModule extends Module {
+ @Override
+ protected void configureModule() {
+ add(
+ HtmlSourceDecorator.class,
+ LinesJsonWriter.class,
+ SourceService.class,
+ SourcesWs.class,
+ ShowAction.class,
+ IssueSnippetsAction.class,
+ LinesAction.class,
+ HashAction.class,
+ RawAction.class,
+ IndexAction.class,
+ ScmAction.class
+ );
+ }
+}
--- /dev/null
+/*
+ * 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.source.ws;
+
+import org.junit.Test;
+import org.sonar.core.platform.ComponentContainer;
+import org.sonar.server.ws.WsAction;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SourceWsModuleTest {
+ private SourceWsModule underTest = new SourceWsModule();
+
+ @Test
+ public void verify_count_of_actions() {
+ ComponentContainer container = new ComponentContainer();
+ underTest.configure(container);
+ assertThat(container.getPicoContainer().getComponentAdapters(WsAction.class)).hasSize(7);
+ }
+
+}
*/
package org.sonar.server.source.ws;
+import java.util.Random;
+import java.util.stream.IntStream;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.server.ws.WebService;
-import org.sonar.db.DbClient;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.ws.ComponentViewerJsonWriter;
-import org.sonar.server.issue.IssueFinder;
-import org.sonar.server.source.SourceService;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
public class SourcesWsTest {
@Rule
public UserSessionRule userSessionRule = UserSessionRule.standalone();
- ShowAction showAction = new ShowAction(mock(SourceService.class), mock(DbClient.class), userSessionRule, mock(ComponentFinder.class));
- RawAction rawAction = new RawAction(mock(DbClient.class), mock(SourceService.class), userSessionRule, mock(ComponentFinder.class));
- LinesAction linesAction = new LinesAction(mock(ComponentFinder.class), mock(DbClient.class), mock(SourceService.class), mock(LinesJsonWriter.class), userSessionRule);
- HashAction hashAction = new HashAction(mock(DbClient.class), userSessionRule, mock(ComponentFinder.class));
- IssueSnippetsAction issueSnippetsAction = new IssueSnippetsAction(mock(SourceService.class), mock(DbClient.class), mock(IssueFinder.class),
- mock(LinesJsonWriter.class), mock(ComponentViewerJsonWriter.class));
- WsTester tester = new WsTester(new SourcesWs(showAction, rawAction, linesAction, hashAction, issueSnippetsAction));
-
@Test
public void define_ws() {
- WebService.Controller controller = tester.controller("api/sources");
+ SourcesWsAction[] actions = IntStream.range(0, 1 + new Random().nextInt(10))
+ .mapToObj(i -> {
+ SourcesWsAction wsAction = mock(SourcesWsAction.class);
+ doAnswer(invocation -> {
+ WebService.NewController controller = invocation.getArgument(0);
+ controller.createAction("action_" + i)
+ .setHandler(wsAction);
+ return null;
+ }).when(wsAction).define(any(WebService.NewController.class));
+ return wsAction;
+ })
+ .toArray(SourcesWsAction[]::new);
+
+ WsTester underTest = new WsTester(new SourcesWs(actions));
+
+ WebService.Controller controller = underTest.controller("api/sources");
assertThat(controller).isNotNull();
assertThat(controller.since()).isEqualTo("4.2");
assertThat(controller.description()).isNotEmpty();
- assertThat(controller.actions()).hasSize(5);
-
- WebService.Action show = controller.action("show");
- assertThat(show).isNotNull();
- assertThat(show.handler()).isSameAs(showAction);
- assertThat(show.since()).isEqualTo("4.4");
- assertThat(show.isInternal()).isFalse();
- assertThat(show.responseExampleAsString()).isNotEmpty();
- assertThat(show.params()).hasSize(3);
-
- WebService.Action raw = controller.action("raw");
- assertThat(raw).isNotNull();
- assertThat(raw.handler()).isSameAs(rawAction);
- assertThat(raw.since()).isEqualTo("5.0");
- assertThat(raw.isInternal()).isFalse();
- assertThat(raw.responseExampleAsString()).isNotEmpty();
- assertThat(raw.params()).hasSize(3);
-
- WebService.Action lines = controller.action("lines");
- assertThat(lines).isNotNull();
- assertThat(lines.handler()).isSameAs(linesAction);
- assertThat(lines.since()).isEqualTo("5.0");
- assertThat(lines.isInternal()).isTrue();
- assertThat(lines.responseExampleAsString()).isNotEmpty();
- assertThat(lines.params()).hasSize(6);
-
- WebService.Action hash = controller.action("hash");
- assertThat(hash).isNotNull();
- assertThat(hash.handler()).isSameAs(hashAction);
- assertThat(hash.since()).isEqualTo("5.0");
- assertThat(hash.isInternal()).isTrue();
- assertThat(hash.responseExampleAsString()).isNotEmpty();
- assertThat(hash.params()).hasSize(1);
-
- WebService.Action issueSnippets = controller.action("issue_snippets");
- assertThat(issueSnippets).isNotNull();
- assertThat(issueSnippets.handler()).isSameAs(issueSnippetsAction);
- assertThat(issueSnippets.since()).isEqualTo("7.8");
- assertThat(issueSnippets.isInternal()).isTrue();
- assertThat(issueSnippets.responseExampleAsString()).isNotEmpty();
- assertThat(issueSnippets.params()).hasSize(1);
+ assertThat(controller.actions()).hasSize(actions.length);
}
}