diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-04-25 20:42:57 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-04-25 20:43:08 +0200 |
commit | 097437f96c9d57375e7537da53a5a95bc096655a (patch) | |
tree | 2e7b8a7cdbaddb43b9a7c29f2bef9ed0e10c5638 /sonar-server | |
parent | f3209b446356db78f90a102890d453f0c487f047 (diff) | |
download | sonarqube-097437f96c9d57375e7537da53a5a95bc096655a.tar.gz sonarqube-097437f96c9d57375e7537da53a5a95bc096655a.zip |
Complete WebService with new param metadata
Diffstat (limited to 'sonar-server')
25 files changed, 546 insertions, 30 deletions
diff --git a/sonar-server/pom.xml b/sonar-server/pom.xml index afe7cbc6274..335705fda92 100644 --- a/sonar-server/pom.xml +++ b/sonar-server/pom.xml @@ -191,8 +191,13 @@ <scope>test</scope> </dependency> <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>sonar-testing-harness</artifactId> + <groupId>org.skyscreamer</groupId> + <artifactId>jsonassert</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-all</artifactId> <scope>test</scope> </dependency> <dependency> @@ -356,7 +361,9 @@ <profile> <id>js-tests</id> <activation> - <property><name>!skipTests</name></property> + <property> + <name>!skipTests</name> + </property> </activation> <build> <plugins> diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java new file mode 100644 index 00000000000..f00af95d68e --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java @@ -0,0 +1,35 @@ +/* + * 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.rule2; + +import org.sonar.api.rule.RuleKey; + +import javax.annotation.CheckForNull; + +/** + * @since 4.4 + */ +public class RuleService { + + @CheckForNull + public Rule getByKey(RuleKey key) { + return null; + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ws/RulesWebService.java b/sonar-server/src/main/java/org/sonar/server/rule2/ws/RulesWebService.java new file mode 100644 index 00000000000..7b54490d79e --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ws/RulesWebService.java @@ -0,0 +1,46 @@ +/* + * 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.rule2.ws; + +import org.sonar.api.server.ws.WebService; + +public class RulesWebService implements WebService { + + private final SearchAction search; + private final ShowAction show; + + public RulesWebService(SearchAction search, ShowAction show) { + this.search = search; + this.show = show; + } + + @Override + public void define(Context context) { + NewController controller = context + .createController("api/rules2") + .setDescription("Coding rules") + .setSince("4.4"); + + search.define(controller); + show.define(controller); + + controller.done(); + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ws/SearchAction.java b/sonar-server/src/main/java/org/sonar/server/rule2/ws/SearchAction.java new file mode 100644 index 00000000000..21f659a63f6 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ws/SearchAction.java @@ -0,0 +1,66 @@ +/* + * 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.rule2.ws; + +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.server.rule2.RuleService; + +/** + * @since 4.4 + */ +public class SearchAction implements RequestHandler { + + private final RuleService service; + + public SearchAction(RuleService service) { + this.service = service; + } + + void define(WebService.NewController controller) { + WebService.NewAction action = controller + .createAction("search") + .setDescription("Returns a collection of relevant rules matching a specified query") + .setSince("4.4") + .setHandler(this); + + action + .createParam("q") + .setDescription("UTF-8 search query") + .setExampleValue("null pointer"); + + action + .createParam("qProfile") + .setDescription("Key of Quality profile") + .setExampleValue("java:Sonar way"); + + action + .createParam("activation") + .setDescription("Only if 'qProfile' is set. Possible values are: true | false | all") + .setExampleValue("java:Sonar way"); + } + + @Override + public void handle(Request request, Response response) { + + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ws/ShowAction.java b/sonar-server/src/main/java/org/sonar/server/rule2/ws/ShowAction.java new file mode 100644 index 00000000000..b844aad181f --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ws/ShowAction.java @@ -0,0 +1,57 @@ +/* + * 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.rule2.ws; + +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.server.rule2.RuleService; + +/** + * @since 4.4 + */ +public class ShowAction implements RequestHandler { + + private final RuleService service; + + public ShowAction(RuleService service) { + this.service = service; + } + + void define(WebService.NewController controller) { + WebService.NewAction action = controller + .createAction("show") + .setDescription("Returns detailed information about a rule") + .setSince("4.4") + .setHandler(this); + + action + .createParam("key") + .setDescription("Rule key") + .setRequired(true) + .setExampleValue("javascript:EmptyBlock"); + } + + @Override + public void handle(Request request, Response response) { + + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ws/package-info.java b/sonar-server/src/main/java/org/sonar/server/rule2/ws/package-info.java new file mode 100644 index 00000000000..8fcf3f3f2f5 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ws/package-info.java @@ -0,0 +1,23 @@ +/* + * 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.server.rule2.ws; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-server/src/test/java/org/sonar/server/issue/filter/IssueFilterWsTest.java b/sonar-server/src/test/java/org/sonar/server/issue/filter/IssueFilterWsTest.java index 87e1fef48e1..8757a9855a2 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/filter/IssueFilterWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/filter/IssueFilterWsTest.java @@ -21,7 +21,7 @@ package org.sonar.server.issue.filter; import org.junit.Test; import org.sonar.api.server.ws.WebService; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.core.issue.DefaultIssueFilter; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.MockUserSession; diff --git a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java index 9f21f8646cc..01ddbfbabd9 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssueShowWsHandlerTest.java @@ -38,7 +38,7 @@ import org.sonar.api.issue.internal.FieldDiffs; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.Rule; import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.api.user.User; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.Duration; diff --git a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssuesWsTest.java b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssuesWsTest.java index ba6ec3fd9fb..9fd33aa4b2d 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/ws/IssuesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/ws/IssuesWsTest.java @@ -21,7 +21,7 @@ package org.sonar.server.issue.ws; import org.junit.Test; import org.sonar.api.server.ws.WebService; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java index 84590153905..9a6f5641c30 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java @@ -21,7 +21,7 @@ package org.sonar.server.platform.ws; import org.junit.Test; import org.sonar.api.config.Settings; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.api.utils.System2; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.platform.Platform; diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/BatchWsTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/BatchWsTest.java index d20d346180d..fbc92faa24a 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/BatchWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/plugins/BatchWsTest.java @@ -27,7 +27,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.platform.Server; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import java.io.File; import java.io.IOException; diff --git a/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java index 1e6baad3415..4771a585710 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java @@ -31,7 +31,7 @@ import org.mockito.stubbing.Answer; import org.sonar.api.i18n.I18n; import org.sonar.api.measures.Metric; import org.sonar.api.measures.Metric.ValueType; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.core.timemachine.Periods; import org.sonar.server.qualitygate.QualityGates; diff --git a/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java b/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java index 1953a8490e5..ef9108c1a8e 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java @@ -28,7 +28,6 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.server.ws.WebService.Action; import org.sonar.api.server.ws.WebService.Controller; -import org.sonar.api.server.ws.WsTester; import org.sonar.core.qualitygate.db.ProjectQgateAssociation; import org.sonar.core.qualitygate.db.ProjectQgateAssociationQuery; import org.sonar.core.qualitygate.db.QualityGateConditionDto; @@ -37,6 +36,7 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.qualitygate.QgateProjectFinder; import org.sonar.server.qualitygate.QgateProjectFinder.Association; import org.sonar.server.qualitygate.QualityGates; +import org.sonar.server.ws.WsTester; import java.util.List; @@ -253,7 +253,7 @@ public class QualityGatesWsTest { )); when(qGates.currentUserHasWritePermission()).thenReturn(false); tester.newRequest("list").execute().assertJson( - "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}]}"); + "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}]}"); } @Test @@ -266,7 +266,7 @@ public class QualityGatesWsTest { )); when(qGates.getDefault()).thenReturn(defaultQgate); tester.newRequest("list").execute().assertJson( - "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}],'default':42}"); + "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}],'default':42}"); } @Test @@ -282,14 +282,15 @@ public class QualityGatesWsTest { long gateId = 12345L; when(qGates.get(gateId)).thenReturn(new QualityGateDto().setId(gateId).setName("Golden")); when(qGates.listConditions(gateId)).thenReturn(ImmutableList.of( - new QualityGateConditionDto().setId(1L).setMetricKey("ncloc").setOperator("GT").setErrorThreshold("10000"), - new QualityGateConditionDto().setId(2L).setMetricKey("new_coverage").setOperator("LT").setWarningThreshold("90").setPeriod(3) + new QualityGateConditionDto().setId(1L).setMetricKey("ncloc").setOperator("GT").setErrorThreshold("10000"), + new QualityGateConditionDto().setId(2L).setMetricKey("new_coverage").setOperator("LT").setWarningThreshold("90").setPeriod(3) )); tester.newRequest("show").setParam("id", Long.toString(gateId)).execute().assertJson( "{'id':12345,'name':'Golden','conditions':[" + "{'id':1,'metric':'ncloc','op':'GT','error':'10000'}," + "{'id':2,'metric':'new_coverage','op':'LT','warning':'90','period':3}" - + "]}"); + + "]}" + ); } @Test @@ -298,14 +299,15 @@ public class QualityGatesWsTest { String gateName = "Golden"; when(qGates.get(gateName)).thenReturn(new QualityGateDto().setId(qGateId).setName(gateName)); when(qGates.listConditions(qGateId)).thenReturn(ImmutableList.of( - new QualityGateConditionDto().setId(1L).setMetricKey("ncloc").setOperator("GT").setErrorThreshold("10000"), - new QualityGateConditionDto().setId(2L).setMetricKey("new_coverage").setOperator("LT").setWarningThreshold("90").setPeriod(3) + new QualityGateConditionDto().setId(1L).setMetricKey("ncloc").setOperator("GT").setErrorThreshold("10000"), + new QualityGateConditionDto().setId(2L).setMetricKey("new_coverage").setOperator("LT").setWarningThreshold("90").setPeriod(3) )); tester.newRequest("show").setParam("name", gateName).execute().assertJson( "{'id':12345,'name':'Golden','conditions':[" + "{'id':1,'metric':'ncloc','op':'GT','error':'10000'}," + "{'id':2,'metric':'new_coverage','op':'LT','warning':'90','period':3}" - + "]}"); + + "]}" + ); } @Test(expected = BadRequestException.class) @@ -375,8 +377,8 @@ public class QualityGatesWsTest { List<ProjectQgateAssociation> projects = ImmutableList.of( new ProjectQgateAssociation().setId(42L).setName("Project One").setMember(false), new ProjectQgateAssociation().setId(24L).setName("Project Two").setMember(true) - ); - when(assoc.projects()).thenReturn(projects ); + ); + when(assoc.projects()).thenReturn(projects); when(projectFinder.find(any(ProjectQgateAssociationQuery.class))).thenReturn(assoc); tester.newRequest("search") .setParam("gateId", Long.toString(gateId)) diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupWsHandlerTest.java index bd5e43be632..2c947e16bc4 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupWsHandlerTest.java @@ -25,7 +25,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.server.qualityprofile.QProfileBackup; import org.sonar.server.qualityprofile.QProfileResult; diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index 74501b9af40..9b93e8e1cdb 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -26,7 +26,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.server.ws.WebService; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import static org.fest.assertions.Assertions.assertThat; diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/AddTagsWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/AddTagsWsHandlerTest.java index a6d58d76799..31c91202065 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/AddTagsWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/AddTagsWsHandlerTest.java @@ -30,7 +30,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.rule.RuleKey; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.core.permission.GlobalPermissions; import org.sonar.server.rule.Rule; import org.sonar.server.rule.Rules; diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/RemoveTagsWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/RemoveTagsWsHandlerTest.java index 8eaf6fded06..957424eb283 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/RemoveTagsWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/RemoveTagsWsHandlerTest.java @@ -28,7 +28,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.rule.RuleKey; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.core.permission.GlobalPermissions; import org.sonar.server.rule.Rule; import org.sonar.server.rule.Rules; diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleSearchWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleSearchWsHandlerTest.java index 566d12b1bcc..1100762fd2c 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleSearchWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleSearchWsHandlerTest.java @@ -32,7 +32,7 @@ import org.sonar.api.resources.Languages; import org.sonar.api.rule.RuleKey; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.server.paging.PagedResult; import org.sonar.server.paging.PagingResult; import org.sonar.server.rule.Rule; diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java index 2719ad27c74..f44b71fbd00 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleShowWsHandlerTest.java @@ -31,7 +31,7 @@ import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleFinder; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.api.utils.DateUtils; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.rule.Rule; diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleTagsWsTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleTagsWsTest.java index 9290508cc9a..7674f66b0a5 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleTagsWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleTagsWsTest.java @@ -26,7 +26,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.server.ws.WebService; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.core.rule.RuleTagDto; import org.sonar.server.rule.RuleTags; diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWsTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWsTest.java index a8e0e867578..a513d509ff4 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWsTest.java @@ -25,7 +25,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.server.ws.WebService; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import static org.fest.assertions.Assertions.assertThat; diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/ws/RulesWebServiceTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/ws/RulesWebServiceTest.java new file mode 100644 index 00000000000..c61a550609e --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/rule2/ws/RulesWebServiceTest.java @@ -0,0 +1,48 @@ +/* + * 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.rule2.ws; + +import org.junit.Test; +import org.sonar.api.server.ws.WebService; +import org.sonar.server.rule2.RuleService; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class RulesWebServiceTest { + + @Test + public void define() throws Exception { + RuleService service = mock(RuleService.class); + SearchAction search = new SearchAction(service); + ShowAction show = new ShowAction(service); + RulesWebService ws = new RulesWebService(search, show); + + WebService.Context context = new WebService.Context(); + ws.define(context); + + WebService.Controller controller = context.controller("api/rules2"); + assertThat(controller).isNotNull(); + assertThat(controller.since()).isEqualTo("4.4"); + assertThat(controller.actions()).hasSize(2); + assertThat(controller.action("search")).isNotNull(); + assertThat(controller.action("show")).isNotNull(); + } +} diff --git a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java index f64ec6a8d0a..537e1403974 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesShowWsHandlerTest.java @@ -25,7 +25,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.SourceService; diff --git a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java index 30ca4d9be55..b24da55479b 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java @@ -22,7 +22,7 @@ package org.sonar.server.source.ws; import org.junit.Test; import org.sonar.api.server.ws.WebService; -import org.sonar.api.server.ws.WsTester; +import org.sonar.server.ws.WsTester; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; diff --git a/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java b/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java new file mode 100644 index 00000000000..890699710d1 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java @@ -0,0 +1,232 @@ +/* + * 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.ws; + +import org.apache.commons.io.Charsets; +import org.apache.commons.io.IOUtils; +import org.skyscreamer.jsonassert.JSONAssert; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.text.JsonWriter; +import org.sonar.api.utils.text.XmlWriter; + +import javax.annotation.CheckForNull; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +/** + * @since 4.2 + */ +public class WsTester { + + public static class TestRequest extends Request { + + private final WebService.Controller controller; + private final WebService.Action action; + private String method = "GET"; + private Map<String, String> params = new HashMap<String, String>(); + + private TestRequest(WebService.Controller controller, WebService.Action action) { + this.controller = controller; + this.action = action; + } + + @Override + public WebService.Action action() { + return action; + } + + @Override + public String method() { + return method; + } + + public TestRequest setMethod(String s) { + this.method = s; + return this; + } + + public TestRequest setParams(Map<String, String> m) { + this.params = m; + return this; + } + + public TestRequest setParam(String key, @CheckForNull String value) { + if (value != null) { + params.put(key, value); + } + return this; + } + + @Override + @CheckForNull + public String param(String key) { + return params.get(key); + } + + public Result execute() { + TestResponse response = new TestResponse(); + action.handler().handle(this, response); + return new Result(response); + } + } + + public static class TestResponse implements Response { + + public class TestStream implements Response.Stream { + private String mediaType; + private int status; + + @CheckForNull + public String mediaType() { + return mediaType; + } + + public int status() { + return status; + } + + @Override + public Response.Stream setMediaType(String s) { + this.mediaType = s; + return this; + } + + @Override + public Response.Stream setStatus(int i) { + this.status = i; + return this; + } + + @Override + public OutputStream output() { + return output; + } + } + + private final ByteArrayOutputStream output = new ByteArrayOutputStream(); + + @Override + public JsonWriter newJsonWriter() { + return JsonWriter.of(new OutputStreamWriter(output, Charsets.UTF_8)); + } + + @Override + public XmlWriter newXmlWriter() { + return XmlWriter.of(new OutputStreamWriter(output, Charsets.UTF_8)); + } + + @Override + public Stream stream() { + return new TestStream(); + } + + + @Override + public Response noContent() { + IOUtils.closeQuietly(output); + return this; + } + } + + + public static class Result { + private final TestResponse response; + + private Result(TestResponse response) { + this.response = response; + } + + public Result assertNoContent() { + //FIXME + return this; + } + + public String outputAsString() { + return new String(response.output.toByteArray(), Charsets.UTF_8); + } + + public Result assertJson(String expectedJson) throws Exception { + String json = outputAsString(); + JSONAssert.assertEquals(expectedJson, json, true); + return this; + } + + /** + * Compares JSON response with JSON file available in classpath. For example if class + * is org.foo.BarTest and filename is index.json, then file must be located + * at src/test/resources/org/foo/BarTest/index.json. + * + * @param clazz the test class + * @param jsonResourceFilename name of the file containing the expected JSON + */ + public Result assertJson(Class clazz, String expectedJsonFilename) throws Exception { + String path = clazz.getSimpleName() + "/" + expectedJsonFilename; + URL url = clazz.getResource(path); + if (url == null) { + throw new IllegalStateException("Cannot find " + path); + } + String json = outputAsString(); + JSONAssert.assertEquals(IOUtils.toString(url), json, true); + return this; + } + } + + private final WebService.Context context = new WebService.Context(); + + public WsTester(WebService... webServices) { + for (WebService webService : webServices) { + webService.define(context); + } + } + + public WebService.Context context() { + return context; + } + + @CheckForNull + public WebService.Controller controller(String path) { + return context.controller(path); + } + + public TestRequest newRequest(String actionKey) { + if (context.controllers().size() != 1) { + throw new IllegalStateException("The method newRequest(String) requires to define one, and only one, controller"); + } + WebService.Controller controller = context.controllers().get(0); + WebService.Action action = controller.action(actionKey); + if (action == null) { + throw new IllegalArgumentException("Action not found: " + actionKey); + } + return new TestRequest(controller, action); + } + + public TestRequest newRequest(String controllerPath, String actionKey) { + WebService.Controller controller = context.controller(controllerPath); + WebService.Action action = controller.action(actionKey); + return new TestRequest(controller, action); + } +} |