]> source.dussan.org Git - sonarqube.git/commitdiff
Add possibility to set a deprecated key on a param
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Sep 2014 14:57:21 +0000 (16:57 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 Sep 2014 16:22:45 +0000 (18:22 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java
sonar-plugin-api/src/test/java/org/sonar/api/issue/internal/DefaultIssueTest.java
sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java

index d656213f30e1f41ec07e58ea8d4c9266ee40643b..d08e0fdc9232dac1332781d38a84ec0880dc226a 100644 (file)
@@ -32,13 +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.Arrays;
-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}
@@ -251,7 +248,7 @@ public interface WebService extends ServerExtension {
 
   class NewAction {
     private final String key;
-    private String description, since;
+    private String deprecatedKey, description, since;
     private boolean post = false, isInternal = false;
     private RequestHandler handler;
     private Map<String, NewParam> newParams = Maps.newHashMap();
@@ -261,6 +258,11 @@ public interface WebService extends ServerExtension {
       this.key = key;
     }
 
+    public NewAction setDeprecatedKey(@Nullable String s) {
+      this.deprecatedKey = s;
+      return this;
+    }
+
     public NewAction setDescription(@Nullable String s) {
       this.description = s;
       return this;
@@ -324,7 +326,7 @@ public interface WebService extends ServerExtension {
 
   @Immutable
   class Action {
-    private final String key, path, description, since;
+    private final String key, deprecatedKey, path, description, since;
     private final boolean post, isInternal;
     private final RequestHandler handler;
     private final Map<String, Param> params;
@@ -332,6 +334,7 @@ public interface WebService extends ServerExtension {
 
     private Action(Controller controller, NewAction newAction) {
       this.key = newAction.key;
+      this.deprecatedKey = newAction.deprecatedKey;
       this.path = String.format("%s/%s", controller.path(), key);
       this.description = newAction.description;
       this.since = StringUtils.defaultIfBlank(newAction.since, controller.since);
@@ -344,17 +347,21 @@ public interface WebService extends ServerExtension {
       }
       this.handler = newAction.handler;
 
-      ImmutableMap.Builder<String, Param> mapBuilder = ImmutableMap.builder();
+      ImmutableMap.Builder<String, Param> paramsBuilder = ImmutableMap.builder();
       for (NewParam newParam : newAction.newParams.values()) {
-        mapBuilder.put(newParam.key, new Param(newParam));
+        paramsBuilder.put(newParam.key, new Param(newParam));
       }
-      this.params = mapBuilder.build();
+      this.params = paramsBuilder.build();
     }
 
     public String key() {
       return key;
     }
 
+    public String deprecatedKey() {
+      return deprecatedKey;
+    }
+
     public String path() {
       return path;
     }
@@ -434,7 +441,7 @@ public interface WebService extends ServerExtension {
   }
 
   class NewParam {
-    private String key, description, exampleValue, defaultValue;
+    private String key, deprecatedKey, description, exampleValue, defaultValue;
     private boolean required = false;
     private Set<String> possibleValues = null;
 
@@ -442,6 +449,14 @@ public interface WebService extends ServerExtension {
       this.key = key;
     }
 
+    /**
+     * @since 5.0
+     */
+    public NewParam setDeprecatedKey(@Nullable String s) {
+      this.deprecatedKey = s;
+      return this;
+    }
+
     public NewParam setDescription(@Nullable String s) {
       this.description = s;
       return this;
@@ -516,12 +531,13 @@ public interface WebService extends ServerExtension {
 
   @Immutable
   class Param {
-    private final String key, description, exampleValue, defaultValue;
+    private final String key, deprecatedKey, description, exampleValue, defaultValue;
     private final boolean required;
     private final Set<String> possibleValues;
 
     public Param(NewParam newParam) {
       this.key = newParam.key;
+      this.deprecatedKey = newParam.deprecatedKey;
       this.description = newParam.description;
       this.exampleValue = newParam.exampleValue;
       this.defaultValue = newParam.defaultValue;
@@ -533,6 +549,14 @@ public interface WebService extends ServerExtension {
       return key;
     }
 
+    /**
+     * @since 5.0
+     */
+    @CheckForNull
+    public String deprecatedKey() {
+      return deprecatedKey;
+    }
+
     @CheckForNull
     public String description() {
       return description;
index 002612f03c5d0ebf1a00e44281dd2d1a5a60c8e9..26567597a97e2bcabcbf4aabef78f94d27dc9de4 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.api.server.ws.WebService;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+
 import java.util.List;
 import java.util.Set;
 
@@ -61,6 +62,14 @@ public abstract class ValidatingRequest extends Request {
     return value;
   }
 
+//  @CheckForNull
+//  private WebService.Param getDefinition(String key){
+//    WebService.Param definition = action.param(key);
+//    if (definition == null) {
+//      return action.
+//    }
+//  }
+
   @CheckForNull
   @Override
   public List<String> paramAsStrings(String key) {
@@ -100,7 +109,8 @@ public abstract class ValidatingRequest extends Request {
       LoggerFactory.getLogger(getClass()).error(message);
       throw new IllegalArgumentException(message);
     }
-    String value = StringUtils.defaultString(readParam(key), definition.defaultValue());
+    String value = StringUtils.defaultString(readParam(definition.deprecatedKey()), readParam(key));
+    value = StringUtils.defaultString(value, definition.defaultValue());
     if (value == null) {
       return null;
     }
index fe240c6ce89681adf7d7224ed183193a717fb963..4ee5587d843ecd98d16aee3887c59987ab3a8dd4 100644 (file)
@@ -46,6 +46,7 @@ public class DefaultIssueTest {
       .setComponentId(1L)
       .setProjectKey("Sample")
       .setRuleKey(RuleKey.of("squid", "S100"))
+      .setLanguage("xoo")
       .setSeverity("MINOR")
       .setManualSeverity(true)
       .setMessage("a message")
@@ -75,6 +76,7 @@ public class DefaultIssueTest {
     assertThat(issue.componentId()).isEqualTo(1L);
     assertThat(issue.projectKey()).isEqualTo("Sample");
     assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("squid", "S100"));
+    assertThat(issue.language()).isEqualTo("xoo");
     assertThat(issue.severity()).isEqualTo("MINOR");
     assertThat(issue.manualSeverity()).isTrue();
     assertThat(issue.message()).isEqualTo("a message");
index 2c57236b86c430393ab2999f191f906bc77bc826..752b0f819eae302610de58a769636ca7ee54009d 100644 (file)
@@ -88,6 +88,9 @@ public class RequestTest {
 
       action.createParam("has_possible_values").setPossibleValues("foo", "bar");
 
+      action.createParam("new_param").setDeprecatedKey("deprecated_param");
+      action.createParam("new_param_with_default_value").setDeprecatedKey("deprecated_new_param_with_default_value").setDefaultValue("the_default_string");
+
       controller.done();
     }
   }
@@ -209,6 +212,11 @@ public class RequestTest {
     assertThat(request.setParam("a_string", "bar , baz").paramAsStrings("a_string")).containsExactly("bar", "baz");
   }
 
+  @Test
+  public void deprecated_key() throws Exception {
+    assertThat(request.setParam("deprecated_param", "bar").param("new_param")).isEqualTo("bar");
+  }
+
   @Test
   public void fail_if_param_is_not_defined() throws Exception {
     try {