/**
* Defines a web service. Note that contrary to the deprecated {@link org.sonar.api.web.Webservice}
* the ws is fully implemented in Java and does not require any Ruby on Rails code.
- *
+ * <p/>
* <p/>
* The classes implementing this extension point must be declared in {@link org.sonar.api.SonarPlugin#getExtensions()}.
- *
+ * <p/>
* <h3>How to use</h3>
* <pre>
* public class HelloWs implements WebService {
}
class NewParam {
- private String key, description;
+ private String key, description, exampleValue;
+ private boolean required = false;
+ private String[] possibleValues = null;
private NewParam(String key) {
this.key = key;
return this;
}
+ /**
+ * Is the parameter required or optional ? Default value is false (optional).
+ * @since 4.4
+ */
+ public NewParam setRequired(boolean b) {
+ this.required = b;
+ return this;
+ }
+
+ /**
+ * @since 4.4
+ */
+ public NewParam setExampleValue(@Nullable String s) {
+ this.exampleValue = s;
+ return this;
+ }
+
+ /**
+ * Exhaustive list of possible values when it makes sense, for example
+ * list of severities.
+ * @since 4.4
+ */
+ public NewParam setPossibleValues(@Nullable String... s) {
+ this.possibleValues = s;
+ return this;
+ }
+
@Override
public String toString() {
return key;
@Immutable
class Param {
- private final String key, description;
+ private final String key, description, exampleValue;
+ private final boolean required;
+ private final String[] possibleValues;
public Param(NewParam newParam) {
this.key = newParam.key;
this.description = newParam.description;
+ this.exampleValue = newParam.exampleValue;
+ this.required = newParam.required;
+ this.possibleValues = (newParam.possibleValues == null ? new String[0] : newParam.possibleValues);
}
public String key() {
return description;
}
+ /**
+ * @since 4.4
+ */
+ @CheckForNull
+ public String exampleValue() {
+ return exampleValue;
+ }
+
+ /**
+ * Is the parameter required or optional ?
+ * @since 4.4
+ */
+ public boolean isRequired() {
+ return required;
+ }
+
+ /**
+ * @since 4.4
+ */
+ public String[] possibleValues() {
+ return possibleValues;
+ }
+
@Override
public String toString() {
return key;
<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>
<profile>
<id>js-tests</id>
<activation>
- <property><name>!skipTests</name></property>
+ <property>
+ <name>!skipTests</name>
+ </property>
</activation>
<build>
<plugins>
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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) {
+
+ }
+}
--- /dev/null
+/*
+ * 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) {
+
+ }
+}
--- /dev/null
+/*
+ * 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;
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;
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;
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;
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;
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;
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;
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;
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;
));
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
));
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
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
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)
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))
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;
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;
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;
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;
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;
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;
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;
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;
--- /dev/null
+/*
+ * 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();
+ }
+}
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;
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;
--- /dev/null
+/*
+ * 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);
+ }
+}
+++ /dev/null
-/*
- * 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.api.server.ws;
-
-import org.apache.commons.io.Charsets;
-import org.apache.commons.io.IOUtils;
-import org.skyscreamer.jsonassert.JSONAssert;
-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);
- }
-}