]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR−12075 actually test production code in SourcesWsTest
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 18 Jun 2019 06:58:27 +0000 (08:58 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 28 Jun 2019 06:45:39 +0000 (08:45 +0200)
by replacing the content of this test by test of the new SourceWsModule class

server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/SourceWsModule.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/source/ws/SourceWsModuleTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java

index f7081969ecf1738029ac1a01ddf3d18b9fe71976..d49d50bc62cd395b23cfc72641fcb15e9b9b616a 100644 (file)
@@ -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 (file)
index 0000000..8f4fc07
--- /dev/null
@@ -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 (file)
index 0000000..3037fb8
--- /dev/null
@@ -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);
+  }
+
+}
index c657fdc9e0333cc767a32081d6b51faafce1ceec..bc755b518931e0324e012eb9e133a9a27269c088 100644 (file)
  */
 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);
   }
 }