]> source.dussan.org Git - sonarqube.git/commitdiff
Check that the value of a pram is in possible values (if defined)
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 30 Apr 2014 11:45:45 +0000 (13:45 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 30 Apr 2014 11:45:45 +0000 (13:45 +0200)
sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java

index 27349e9e5780847a80bb2d8180c5d12aaddb098a..f566c4d75c88563e4ddf1d741b42efa4e7b23363 100644 (file)
@@ -128,8 +128,13 @@ public class WebServiceEngine implements ServerComponent, Startable {
         if (paramDef == null) {
           throw new BadRequestException(String.format("Parameter '%s' is undefined for action '%s'", key, action.key()));
         }
+        String value = StringUtils.defaultString(super.param(key), paramDef.defaultValue());
 
-        return StringUtils.defaultString(super.param(key), paramDef.defaultValue());
+        List<String> possibleValues = paramDef.possibleValues();
+        if (value != null && possibleValues != null && !possibleValues.contains(value)) {
+          throw new BadRequestException(String.format("Value of parameter '%s' can only be one of %s", key, possibleValues));
+        }
+        return value;
       }
     };
   }
@@ -143,15 +148,15 @@ public class WebServiceEngine implements ServerComponent, Startable {
     for (Message message : e.errors()) {
       messages.add(message(message.text(), message.l10nKey(), message.l10nParams()));
     }
-    sendErrors(response, e.httpCode(), messages.toArray(new String[0]));
+    sendErrors(response, e.httpCode(), messages.toArray(new String[messages.size()]));
   }
 
   @CheckForNull
-  private String message(@Nullable String message, @Nullable  String l10nKey, @Nullable Object[] l10nParams){
+  private String message(@Nullable String message, @Nullable String l10nKey, Object... l10nParams) {
     if (l10nKey != null) {
       return i18n.message(Locale.getDefault(), l10nKey, message, l10nParams);
     } else {
-     return message;
+      return message;
     }
   }
 
index 03baa5579a07e7ddbca9083230da649b04b6114e..4434f4705d61999b73f2b6284148fbc4c652bb12 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.server.exceptions.ServerException;
 import org.sonar.server.plugins.MimeTypes;
 
 import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 
 import java.io.IOException;
 import java.util.HashMap;
@@ -69,7 +70,7 @@ public class WebServiceEngineTest {
       return this;
     }
 
-    public SimpleRequest setParam(String key, @CheckForNull String value) {
+    public SimpleRequest setParam(String key, @Nullable String value) {
       if (value != null) {
         params.put(key, value);
       }
@@ -195,6 +196,28 @@ public class WebServiceEngineTest {
     assertThat(response.stream().outputAsString()).isEqualTo("Hello World by Marcel");
   }
 
+  @Test
+  public void param_value_is_in_possible_values() throws Exception {
+    InternalRequest request = new SimpleRequest()
+      .setParam("message", "Hello World")
+      .setParam("format", "json");
+    ServletResponse response = new ServletResponse();
+    engine.execute(request, response, "api/system", "print");
+
+    assertThat(response.stream().outputAsString()).isEqualTo("Hello World by -");
+  }
+
+  @Test
+  public void param_value_is_not_in_possible_values() throws Exception {
+    InternalRequest request = new SimpleRequest()
+      .setParam("message", "Hello World")
+      .setParam("format", "html");
+    ServletResponse response = new ServletResponse();
+    engine.execute(request, response, "api/system", "print");
+
+    assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[{\"msg\":\"Value of parameter 'format' can only be one of [json, xml]\"}]}");
+  }
+
   @Test
   public void internal_error() throws Exception {
     InternalRequest request = new SimpleRequest();
@@ -361,17 +384,19 @@ public class WebServiceEngineTest {
       NewAction print = newController.createAction("print");
       print.createParam("message").setDescription("required message").setRequired(true);
       print.createParam("author").setDescription("optional author").setDefaultValue("-");
+      print.createParam("format").setDescription("optional format").setPossibleValues("json", "xml");
       print.setHandler(new RequestHandler() {
-          @Override
-          public void handle(Request request, Response response) {
-            try {
-              IOUtils.write(
-                request.mandatoryParam("message") + " by " + request.param("author", "nobody"), response.stream().output());
-            } catch (IOException e) {
-              throw new IllegalStateException(e);
-            }
+        @Override
+        public void handle(Request request, Response response) {
+          try {
+            request.param("format");
+            IOUtils.write(
+              request.mandatoryParam("message") + " by " + request.param("author", "nobody"), response.stream().output());
+          } catch (IOException e) {
+            throw new IllegalStateException(e);
           }
-        });
+        }
+      });
       newController.done();
     }
   }