From 77b478d6a9edb55dddb80b99e768ef74bff42549 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 18 Jun 2019 08:58:27 +0200 Subject: [PATCH] =?utf8?q?SONAR=E2=88=9212075=20actually=20test=20producti?= =?utf8?q?on=20code=20in=20SourcesWsTest?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit by replacing the content of this test by test of the new SourceWsModule class --- .../platformlevel/PlatformLevel4.java | 23 +----- .../server/source/ws/SourceWsModule.java | 43 +++++++++++ .../server/source/ws/SourceWsModuleTest.java | 38 ++++++++++ .../sonar/server/source/ws/SourcesWsTest.java | 76 +++++-------------- 4 files changed, 104 insertions(+), 76 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/source/ws/SourceWsModule.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index f7081969ecf..d49d50bc62c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -182,16 +182,7 @@ import org.sonar.server.rule.ws.RuleWsSupport; 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; @@ -433,17 +424,7 @@ public class PlatformLevel4 extends PlatformLevel { 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, diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/SourceWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/SourceWsModule.java new file mode 100644 index 00000000000..8f4fc07b0fc --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/SourceWsModule.java @@ -0,0 +1,43 @@ +/* + * 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 + ); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java new file mode 100644 index 00000000000..3037fb81658 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java @@ -0,0 +1,38 @@ +/* + * 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); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java index c657fdc9e03..bc755b51893 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java @@ -19,78 +19,44 @@ */ 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); } } -- 2.39.5