diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-05-20 23:01:12 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-05-20 23:01:12 +0200 |
commit | 57ec7e177f89ea5290184040ff9ed6ba74f5331f (patch) | |
tree | ed92735c256d0cbffc44af971bf547f77de2d283 /sonar-plugin-api | |
parent | 6af08cecc609d8cfd7e59338398a37cc954828b7 (diff) | |
download | sonarqube-57ec7e177f89ea5290184040ff9ed6ba74f5331f.tar.gz sonarqube-57ec7e177f89ea5290184040ff9ed6ba74f5331f.zip |
SONAR-5007 add tests
Diffstat (limited to 'sonar-plugin-api')
6 files changed, 215 insertions, 125 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java index 9d969a68ce7..8cb158e87eb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java @@ -22,28 +22,15 @@ package org.sonar.api.server.ws; import com.google.common.base.Splitter; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; -import org.slf4j.LoggerFactory; import javax.annotation.CheckForNull; -import javax.annotation.Nullable; import java.util.List; -import java.util.Set; /** * @since 4.2 */ public abstract class Request { - private WebService.Action action; - - protected void setAction(WebService.Action action) { - this.action = action; - } - - public WebService.Action action() { - return action; - } - /** * Returns the name of the HTTP method with which this request was made. Possible * values are GET and POST. Others are not supported. @@ -105,75 +92,16 @@ public abstract class Request { return values; } - @CheckForNull - public String param(String key) { - return param(key, true); - } - - @CheckForNull - String param(String key, boolean validateValue) { - WebService.Param definition = action.param(key); - String value = readParamOrDefaultValue(key, definition); - if (value != null && validateValue) { - validate(value, definition); - } - return value; - } - - @CheckForNull public List<String> paramAsStrings(String key) { - WebService.Param definition = action.param(key); - String value = readParamOrDefaultValue(key, definition); - if (value == null) { - return null; - } - List<String> values = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value)); - for (String s : values) { - validate(s, definition); - } - return values; - } - - @CheckForNull - public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) { - WebService.Param definition = action.param(key); - String value = readParamOrDefaultValue(key, definition); - if (value == null) { - return null; - } - Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value); - List<E> result = Lists.newArrayList(); - for (String s : values) { - validate(s, definition); - result.add(Enum.valueOf(enumClass, s)); - } - return result; - } - - @CheckForNull - private String readParamOrDefaultValue(String key, @Nullable WebService.Param definition) { - if (definition == null) { - String message = String.format("BUG - parameter '%s' is undefined for action '%s'", key, action.key()); - LoggerFactory.getLogger(getClass()).error(message); - throw new IllegalArgumentException(message); - } - String value = StringUtils.defaultString(readParam(key), definition.defaultValue()); + String value = param(key); if (value == null) { return null; } - return value; + return Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value)); } @CheckForNull - protected abstract String readParam(String key); - - private void validate(String value, WebService.Param definition) { - Set<String> possibleValues = definition.possibleValues(); - if (possibleValues != null && !possibleValues.contains(value)) { - throw new IllegalArgumentException(String.format( - "Value of parameter '%s' (%s) must be one of: %s", definition.key(), value, possibleValues)); - } - } + public abstract String param(String key); /** * @deprecated to be dropped in 4.4. Default values are declared in ws metadata @@ -238,4 +166,18 @@ public abstract class Request { String s = param(key); return s == null ? null : Enum.valueOf(enumClass, s); } + + @CheckForNull + public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) { + String value = param(key); + if (value == null) { + return null; + } + Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value); + List<E> result = Lists.newArrayList(); + for (String s : values) { + result.add(Enum.valueOf(enumClass, s)); + } + return result; + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java index 9508fd1d179..5e464a4200f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java @@ -32,12 +32,10 @@ import org.sonar.api.ServerExtension; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; + import java.io.IOException; import java.net.URL; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; /** * Defines a web service. Note that contrary to the deprecated {@link org.sonar.api.web.Webservice} @@ -467,27 +465,33 @@ public interface WebService extends ServerExtension { /** * Exhaustive list of possible values when it makes sense, for example * list of severities. - * <p/> - * Note that the parameter supports values with type Iterable, for example : - * <pre> - * setPossibleValues(Arrays.asList("one", "two"), "three", "four"); - * </pre> * * @since 4.4 */ public NewParam setPossibleValues(@Nullable Object... values) { + return setPossibleValues(values == null ? (Collection) null : Arrays.asList(values)); + } + + /** + * @since 4.4 + */ + public NewParam setBooleanPossibleValues() { + return setPossibleValues("true", "false"); + } + + /** + * Exhaustive list of possible values when it makes sense, for example + * list of severities. + * + * @since 4.4 + */ + public NewParam setPossibleValues(@Nullable Collection values) { if (values == null) { this.possibleValues = null; } else { this.possibleValues = Sets.newLinkedHashSet(); for (Object value : values) { - if (value instanceof Iterable) { - for (Object o : (Iterable) value) { - this.possibleValues.add(o.toString()); - } - } else { - this.possibleValues.add(value.toString()); - } + this.possibleValues.add(value.toString()); } } return this; @@ -496,43 +500,11 @@ public interface WebService extends ServerExtension { /** * @since 4.4 */ - public NewParam setBooleanPossibleValues() { - return setPossibleValues("true", "false"); - } - - /** - * @since 4.4 - */ public NewParam setDefaultValue(@Nullable String s) { this.defaultValue = s; return this; } - @CheckForNull - public String description() { - return description; - } - - @CheckForNull - public String exampleValue() { - return exampleValue; - } - - @CheckForNull - public String defaultValue() { - return defaultValue; - } - - @CheckForNull - public boolean isRequired() { - return required; - } - - @CheckForNull - public Set<String> possibleValues() { - return possibleValues; - } - @Override public String toString() { return key; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java new file mode 100644 index 00000000000..c7fa2a29267 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java @@ -0,0 +1,54 @@ +/* + * 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.internal; + +import com.google.common.collect.Maps; +import org.sonar.api.server.ws.Request; + +import javax.annotation.Nullable; +import java.util.Map; + +/** + * Fake implementation of {@link org.sonar.api.server.ws.Request} used + * for testing. Call the method {@link #setParam(String, String)} to + * emulate some parameter values. + */ +public class SimpleGetRequest extends Request { + + private final Map<String, String> params = Maps.newHashMap(); + + @Override + public String method() { + return "GET"; + } + + @Override + public String param(String key) { + return params.get(key); + } + + public SimpleGetRequest setParam(String key, @Nullable String value) { + if (value != null) { + params.put(key, value); + } + return this; + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java new file mode 100644 index 00000000000..002612f03c5 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java @@ -0,0 +1,120 @@ +/* + * 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.internal; + +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; +import org.slf4j.LoggerFactory; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.WebService; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import java.util.List; +import java.util.Set; + +/** + * @since 4.2 + */ +public abstract class ValidatingRequest extends Request { + + private WebService.Action action; + + public void setAction(WebService.Action action) { + this.action = action; + } + + public WebService.Action action() { + return action; + } + + @CheckForNull + public String param(String key) { + return param(key, true); + } + + @CheckForNull + private String param(String key, boolean validateValue) { + WebService.Param definition = action.param(key); + String value = readParamOrDefaultValue(key, definition); + if (value != null && validateValue) { + validate(value, definition); + } + return value; + } + + @CheckForNull + @Override + public List<String> paramAsStrings(String key) { + WebService.Param definition = action.param(key); + String value = readParamOrDefaultValue(key, definition); + if (value == null) { + return null; + } + List<String> values = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(value)); + for (String s : values) { + validate(s, definition); + } + return values; + } + + @CheckForNull + @Override + public <E extends Enum<E>> List<E> paramAsEnums(String key, Class<E> enumClass) { + WebService.Param definition = action.param(key); + String value = readParamOrDefaultValue(key, definition); + if (value == null) { + return null; + } + Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value); + List<E> result = Lists.newArrayList(); + for (String s : values) { + validate(s, definition); + result.add(Enum.valueOf(enumClass, s)); + } + return result; + } + + @CheckForNull + private String readParamOrDefaultValue(String key, @Nullable WebService.Param definition) { + if (definition == null) { + String message = String.format("BUG - parameter '%s' is undefined for action '%s'", key, action.key()); + LoggerFactory.getLogger(getClass()).error(message); + throw new IllegalArgumentException(message); + } + String value = StringUtils.defaultString(readParam(key), definition.defaultValue()); + if (value == null) { + return null; + } + return value; + } + + @CheckForNull + protected abstract String readParam(String key); + + private void validate(String value, WebService.Param definition) { + Set<String> possibleValues = definition.possibleValues(); + if (possibleValues != null && !possibleValues.contains(value)) { + throw new IllegalArgumentException(String.format( + "Value of parameter '%s' (%s) must be one of: %s", definition.key(), value, possibleValues)); + } + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java index 76421b05c18..9d2926460f6 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java @@ -23,6 +23,7 @@ import com.google.common.collect.Maps; import org.junit.Before; import org.junit.Test; import org.sonar.api.rule.RuleStatus; +import org.sonar.api.server.ws.internal.ValidatingRequest; import javax.annotation.Nullable; import java.util.Map; @@ -33,7 +34,7 @@ import static org.mockito.Mockito.mock; public class RequestTest { - private static class SimpleRequest extends Request { + private static class SimpleRequest extends ValidatingRequest { private final Map<String, String> params = Maps.newHashMap(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java index ccebd01cd4b..4dab17f0641 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java @@ -256,7 +256,7 @@ public class WebServiceTest { NewController newController = context.createController("api/rule"); NewAction create = newController.createAction("create").setHandler(mock(RequestHandler.class)); create.createParam("key").setDescription("Key of the new rule"); - create.createParam("severity").setDefaultValue("MAJOR"); + create.createParam("severity").setDefaultValue("MAJOR").setPossibleValues("INFO", "MAJOR", "BLOCKER"); newController.done(); } }.define(context); @@ -271,6 +271,7 @@ public class WebServiceTest { assertThat(action.param("severity").key()).isEqualTo("severity"); assertThat(action.param("severity").description()).isNull(); assertThat(action.param("severity").defaultValue()).isEqualTo("MAJOR"); + assertThat(action.param("severity").possibleValues()).containsOnly("INFO", "MAJOR", "BLOCKER"); } @Test |