diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-22 19:36:09 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-05-23 16:18:33 +0200 |
commit | ee35d07b1a86819991b5d44a1cc93b1334ba0215 (patch) | |
tree | bd651c111eb187b2a3b1948a1d549afecfa113d7 /sonar-server/src | |
parent | 08ac15573ac5fdbcf071f598dd24c3cd0b2e238b (diff) | |
download | sonarqube-ee35d07b1a86819991b5d44a1cc93b1334ba0215.tar.gz sonarqube-ee35d07b1a86819991b5d44a1cc93b1334ba0215.zip |
SONAR-5338 Create a WS /api/tests/show returning the list of test cases from test plan
Diffstat (limited to 'sonar-server/src')
10 files changed, 260 insertions, 52 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index 8c30773f1b7..de2240db45e 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -487,6 +487,7 @@ class ServerComponents { pico.addSingleton(TestsWs.class); pico.addSingleton(TestsTestableAction.class); pico.addSingleton(TestsPlanAction.class); + pico.addSingleton(TestsShowAction.class); // graphs and perspective related classes pico.addSingleton(TestablePerspectiveLoader.class); diff --git a/sonar-server/src/main/java/org/sonar/server/test/ws/TestsShowAction.java b/sonar-server/src/main/java/org/sonar/server/test/ws/TestsShowAction.java new file mode 100644 index 00000000000..752ff4550a6 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/test/ws/TestsShowAction.java @@ -0,0 +1,85 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.test.ws; + +import com.google.common.io.Resources; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.RequestHandler; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.test.MutableTestPlan; +import org.sonar.api.test.TestCase; +import org.sonar.api.utils.text.JsonWriter; +import org.sonar.api.web.UserRole; +import org.sonar.core.component.SnapshotPerspectives; +import org.sonar.server.user.UserSession; + +public class TestsShowAction implements RequestHandler { + + private static final String KEY = "key"; + + private final SnapshotPerspectives snapshotPerspectives; + + public TestsShowAction(SnapshotPerspectives snapshotPerspectives) { + this.snapshotPerspectives = snapshotPerspectives; + } + + void define(WebService.NewController controller) { + WebService.NewAction action = controller.createAction("show") + .setDescription("Get the list of test cases of a test plan. Require Browse permission on file's project") + .setSince("4.4") + .setResponseExample(Resources.getResource(getClass(), "tests-example-show.json")) + .setHandler(this); + + action + .createParam(KEY) + .setRequired(true) + .setDescription("Test plan key") + .setExampleValue("my_project:/src/test/BarTest.java"); + } + + @Override + public void handle(Request request, Response response) { + String fileKey = request.mandatoryParam(KEY); + UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey); + + MutableTestPlan testPlan = snapshotPerspectives.as(MutableTestPlan.class, fileKey); + JsonWriter json = response.newJsonWriter().beginObject(); + if (testPlan != null) { + writeTests(testPlan, json); + } + json.endObject().close(); + } + + private void writeTests(MutableTestPlan testPlan, JsonWriter json) { + json.name("tests").beginArray(); + for (TestCase testCase : testPlan.testCases()) { + json.beginObject(); + json.prop("name", testCase.name()); + json.prop("status", testCase.status().name()); + json.prop("durationInMs", testCase.durationInMs()); + json.prop("coveredLines", testCase.countCoveredLines()); + json.endObject(); + } + json.endArray(); + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/test/ws/TestsWs.java b/sonar-server/src/main/java/org/sonar/server/test/ws/TestsWs.java index 8d3ba1d215f..93f47a913d3 100644 --- a/sonar-server/src/main/java/org/sonar/server/test/ws/TestsWs.java +++ b/sonar-server/src/main/java/org/sonar/server/test/ws/TestsWs.java @@ -24,10 +24,12 @@ import org.sonar.api.server.ws.WebService; public class TestsWs implements WebService { + private final TestsShowAction showAction; private final TestsTestableAction testableAction; private final TestsPlanAction planAction; - public TestsWs(TestsTestableAction testableAction, TestsPlanAction planAction) { + public TestsWs(TestsShowAction showAction, TestsTestableAction testableAction, TestsPlanAction planAction) { + this.showAction = showAction; this.testableAction = testableAction; this.planAction = planAction; } @@ -38,6 +40,7 @@ public class TestsWs implements WebService { .setSince("4.4") .setDescription("Tests management"); + showAction.define(controller); testableAction.define(controller); planAction.define(controller); diff --git a/sonar-server/src/main/resources/org/sonar/server/test/ws/tests-example-show.json b/sonar-server/src/main/resources/org/sonar/server/test/ws/tests-example-show.json new file mode 100644 index 00000000000..fd6076387e6 --- /dev/null +++ b/sonar-server/src/main/resources/org/sonar/server/test/ws/tests-example-show.json @@ -0,0 +1,16 @@ +{ + "tests": [ + { + "name": "find_by_params", + "status": "OK", + "durationInMs": 10, + "coveredLines" : 89 + }, + { + "name": "find_rules_by_characteristics", + "status": "ERROR", + "durationInMs": 97, + "coveredLines" : 0 + } + ] +} diff --git a/sonar-server/src/test/java/org/sonar/server/test/ws/TestsPlanActionTest.java b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsPlanActionTest.java index 2f623a77071..8e576b47fd9 100644 --- a/sonar-server/src/test/java/org/sonar/server/test/ws/TestsPlanActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsPlanActionTest.java @@ -44,7 +44,7 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class TestsPlanActionTest { - static final String FILE_KEY = "src/test/java/org/foo/BarTest.java"; + static final String TEST_PLAN_KEY = "src/test/java/org/foo/BarTest.java"; @Mock MutableTestPlan testPlan; @@ -54,21 +54,34 @@ public class TestsPlanActionTest { @Before public void setUp() throws Exception { SnapshotPerspectives snapshotPerspectives = mock(SnapshotPerspectives.class); - when(snapshotPerspectives.as(MutableTestPlan.class, FILE_KEY)).thenReturn(testPlan); - tester = new WsTester(new TestsWs(mock(TestsTestableAction.class), new TestsPlanAction(snapshotPerspectives))); + when(snapshotPerspectives.as(MutableTestPlan.class, TEST_PLAN_KEY)).thenReturn(testPlan); + tester = new WsTester(new TestsWs(mock(TestsShowAction.class), mock(TestsTestableAction.class), new TestsPlanAction(snapshotPerspectives))); } @Test public void plan() throws Exception { - MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "SonarQube", FILE_KEY); + MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "SonarQube", TEST_PLAN_KEY); MutableTestCase testCase1 = testCase("org.foo.Bar.java", "src/main/java/org/foo/Bar.java", 10); MutableTestCase testCase2 = testCase("org.foo.File.java", "src/main/java/org/foo/File.java", 3); when(testPlan.testCasesByName("my_test")).thenReturn(newArrayList(testCase1, testCase2)); - WsTester.TestRequest request = tester.newGetRequest("api/tests", "plan").setParam("key", FILE_KEY).setParam("test", "my_test"); - - request.execute().assertJson(getClass(), "plan.json"); + WsTester.TestRequest request = tester.newGetRequest("api/tests", "plan").setParam("key", TEST_PLAN_KEY).setParam("test", "my_test"); + + request.execute().assertJson("{\n" + + " \"files\": [\n" + + " {\n" + + " \"key\": \"org.foo.Bar.java\",\n" + + " \"longName\": \"src/main/java/org/foo/Bar.java\",\n" + + " \"coveredLines\" : 10\n" + + " },\n" + + " {\n" + + " \"key\": \"org.foo.File.java\",\n" + + " \"longName\": \"src/main/java/org/foo/File.java\",\n" + + " \"coveredLines\" : 3\n" + + " }\n" + + " ]\n" + + "}\n"); } private MutableTestCase testCase(String fileKey, String fileLongName, int coveredLines) { diff --git a/sonar-server/src/test/java/org/sonar/server/test/ws/TestsShowActionTest.java b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsShowActionTest.java new file mode 100644 index 00000000000..f3e232350b1 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsShowActionTest.java @@ -0,0 +1,94 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.test.ws; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.sonar.api.test.MutableTestCase; +import org.sonar.api.test.MutableTestPlan; +import org.sonar.api.test.TestCase; +import org.sonar.api.web.UserRole; +import org.sonar.core.component.SnapshotPerspectives; +import org.sonar.server.user.MockUserSession; +import org.sonar.server.ws.WsTester; + +import static com.google.common.collect.Lists.newArrayList; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class TestsShowActionTest { + + static final String TEST_PLAN_KEY = "src/test/java/org/foo/BarTest.java"; + + @Mock + MutableTestPlan testPlan; + + WsTester tester; + + @Before + public void setUp() throws Exception { + SnapshotPerspectives snapshotPerspectives = mock(SnapshotPerspectives.class); + when(snapshotPerspectives.as(MutableTestPlan.class, TEST_PLAN_KEY)).thenReturn(testPlan); + tester = new WsTester(new TestsWs(new TestsShowAction(snapshotPerspectives), mock(TestsTestableAction.class), mock(TestsPlanAction.class))); + } + + @Test + public void show() throws Exception { + MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, "SonarQube", TEST_PLAN_KEY); + + MutableTestCase testCase1 = testCase("test1", TestCase.Status.OK, 10L, 32); + MutableTestCase testCase2 = testCase("test2", TestCase.Status.ERROR, 97L, 21); + when(testPlan.testCases()).thenReturn(newArrayList(testCase1, testCase2)); + + WsTester.TestRequest request = tester.newGetRequest("api/tests", "show").setParam("key", TEST_PLAN_KEY); + + request.execute().assertJson("{\n" + + " \"tests\": [\n" + + " {\n" + + " \"name\": \"test1\",\n" + + " \"status\": \"OK\",\n" + + " \"durationInMs\": 10,\n" + + " \"coveredLines\": 32\n" + + " },\n" + + " {\n" + + " \"name\": \"test2\",\n" + + " \"status\": \"ERROR\",\n" + + " \"durationInMs\": 97,\n" + + " \"coveredLines\": 21\n" + + " }\n" + + " ],\n" + + "}\n"); + } + + private MutableTestCase testCase(String name, TestCase.Status status, Long durationInMs, int coveredLines) { + MutableTestCase testCase = mock(MutableTestCase.class); + when(testCase.name()).thenReturn(name); + when(testCase.status()).thenReturn(status); + when(testCase.durationInMs()).thenReturn(durationInMs); + when(testCase.countCoveredLines()).thenReturn(coveredLines); + return testCase; + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/test/ws/TestsTestableActionTest.java b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsTestableActionTest.java index e34f9ad6d38..2b21d0caf44 100644 --- a/sonar-server/src/test/java/org/sonar/server/test/ws/TestsTestableActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsTestableActionTest.java @@ -52,7 +52,7 @@ public class TestsTestableActionTest { public void setUp() throws Exception { SnapshotPerspectives snapshotPerspectives = mock(SnapshotPerspectives.class); when(snapshotPerspectives.as(MutableTestable.class, FILE_KEY)).thenReturn(testable); - tester = new WsTester(new TestsWs(new TestsTestableAction(snapshotPerspectives), mock(TestsPlanAction.class))); + tester = new WsTester(new TestsWs(mock(TestsShowAction.class), new TestsTestableAction(snapshotPerspectives), mock(TestsPlanAction.class))); } @Test @@ -65,7 +65,32 @@ public class TestsTestableActionTest { WsTester.TestRequest request = tester.newGetRequest("api/tests", "testable").setParam("key", FILE_KEY).setParam("line", "10"); - request.execute().assertJson(getClass(), "testable.json"); + request.execute().assertJson("{\n" + + " \"tests\": [\n" + + " {\n" + + " \"name\": \"test1\",\n" + + " \"status\": \"OK\",\n" + + " \"durationInMs\": 10,\n" + + " \"_ref\": \"1\"\n" + + " },\n" + + " {\n" + + " \"name\": \"test2\",\n" + + " \"status\": \"ERROR\",\n" + + " \"durationInMs\": 97,\n" + + " \"_ref\": \"2\"\n" + + " }\n" + + " ],\n" + + " \"files\": {\n" + + " \"1\": {\n" + + " \"key\": \"org.foo.BarTest.java\",\n" + + " \"longName\": \"src/test/java/org/foo/BarTest.java\"\n" + + " },\n" + + " \"2\": {\n" + + " \"key\": \"org.foo.FileTest.java\",\n" + + " \"longName\": \"src/test/java/org/foo/FileTest.java\"\n" + + " }\n" + + " }\n" + + "}"); } private TestCase testCase(String name, TestCase.Status status, Long durationInMs, String testPlanKey, String testPlanLongName) { diff --git a/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java index 173d6741733..f51d18168d6 100644 --- a/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/test/ws/TestsWsTest.java @@ -36,7 +36,7 @@ public class TestsWsTest { @Before public void setUp() throws Exception { SnapshotPerspectives snapshotPerspectives = mock(SnapshotPerspectives.class); - WsTester tester = new WsTester(new TestsWs(new TestsTestableAction(snapshotPerspectives), new TestsPlanAction(snapshotPerspectives))); + WsTester tester = new WsTester(new TestsWs(new TestsShowAction(snapshotPerspectives), new TestsTestableAction(snapshotPerspectives), new TestsPlanAction(snapshotPerspectives))); controller = tester.controller("api/tests"); } @@ -45,7 +45,18 @@ public class TestsWsTest { assertThat(controller).isNotNull(); assertThat(controller.since()).isEqualTo("4.4"); assertThat(controller.description()).isNotEmpty(); - assertThat(controller.actions()).hasSize(2); + assertThat(controller.actions()).hasSize(3); + } + + @Test + public void define_show_action() throws Exception { + WebService.Action action = controller.action("show"); + assertThat(action).isNotNull(); + assertThat(action.isInternal()).isFalse(); + assertThat(action.isPost()).isFalse(); + assertThat(action.handler()).isNotNull(); + assertThat(action.responseExampleAsString()).isNotEmpty(); + assertThat(action.params()).hasSize(1); } @Test diff --git a/sonar-server/src/test/resources/org/sonar/server/test/ws/TestsPlanActionTest/plan.json b/sonar-server/src/test/resources/org/sonar/server/test/ws/TestsPlanActionTest/plan.json deleted file mode 100644 index be452f5e1b7..00000000000 --- a/sonar-server/src/test/resources/org/sonar/server/test/ws/TestsPlanActionTest/plan.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "files": [ - { - "key": "org.foo.Bar.java", - "longName": "src/main/java/org/foo/Bar.java", - "coveredLines" : 10 - }, - { - "key": "org.foo.File.java", - "longName": "src/main/java/org/foo/File.java", - "coveredLines" : 3 - } - ] -} diff --git a/sonar-server/src/test/resources/org/sonar/server/test/ws/TestsTestableActionTest/testable.json b/sonar-server/src/test/resources/org/sonar/server/test/ws/TestsTestableActionTest/testable.json deleted file mode 100644 index c2ed0fff18f..00000000000 --- a/sonar-server/src/test/resources/org/sonar/server/test/ws/TestsTestableActionTest/testable.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "tests": [ - { - "name": "test1", - "status": "OK", - "durationInMs": 10, - "_ref": "1" - }, - { - "name": "test2", - "status": "ERROR", - "durationInMs": 97, - "_ref": "2" - } - ], - "files": { - "1": { - "key": "org.foo.BarTest.java", - "longName": "src/test/java/org/foo/BarTest.java" - }, - "2": { - "key": "org.foo.FileTest.java", - "longName": "src/test/java/org/foo/FileTest.java" - } - } -} |