]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4315 Improve ActionService
authorJulien Lancelot <julien.lancelot@gmail.com>
Sun, 2 Jun 2013 20:33:26 +0000 (22:33 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Sun, 2 Jun 2013 20:33:26 +0000 (22:33 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Function.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/condition/HasIssuePropertyCondition.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java
sonar-plugin-api/src/test/java/org/sonar/api/issue/condition/HasIssuePropertyConditionTest.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/issue/ActionService.java
sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb
sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java [new file with mode: 0644]

index 2bb6ab8ddc9e9884e8f46b95cbbdefe4064f6dea..70401abc17da7c549f034cb8c476d253a559d2d8 100644 (file)
@@ -21,11 +21,8 @@ package org.sonar.api.issue.action;
 
 import org.sonar.api.issue.Issue;
 
-import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
-import java.util.Map;
-
 /**
  * @since 3.6
  */
@@ -36,9 +33,6 @@ public interface Function {
   interface Context {
     Issue issue();
 
-    @CheckForNull
-    Map<String, String> parameters();
-
     Context setAttribute(String key, @Nullable String value);
 
     Context addComment(@Nullable String text);
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/condition/HasIssuePropertyCondition.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/condition/HasIssuePropertyCondition.java
new file mode 100644 (file)
index 0000000..d6386e9
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 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.issue.condition;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.sonar.api.issue.Issue;
+
+/**
+ * @since 3.1
+ */
+@Beta
+public final class HasIssuePropertyCondition implements Condition {
+
+  private final String propertyKey;
+
+  public HasIssuePropertyCondition(String propertyKey) {
+    Preconditions.checkArgument(!Strings.isNullOrEmpty(propertyKey));
+    this.propertyKey = propertyKey;
+  }
+
+  public String getPropertyKey() {
+    return propertyKey;
+  }
+
+  @Override
+  public boolean matches(Issue issue) {
+    return !Strings.isNullOrEmpty(issue.attributes().get(propertyKey));
+  }
+}
index a312e6061d7d4eb99b71dda5c3f746455f52b0c3..697e9b5f2a0a6ce27e4f1275af625bf40f291a04 100644 (file)
@@ -30,6 +30,6 @@ public final class CommentFunction implements Function {
 
   @Override
   public void execute(Context context) {
-    context.addComment(context.parameters().get("text"));
+    context.addComment("New comment!");
   }
 }
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/issue/condition/HasIssuePropertyConditionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/issue/condition/HasIssuePropertyConditionTest.java
new file mode 100644 (file)
index 0000000..e4dbfd3
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 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.issue.condition;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.issue.internal.DefaultIssue;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class HasIssuePropertyConditionTest {
+
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
+  DefaultIssue issue = new DefaultIssue();
+
+  @Test
+  public void should_match() {
+    HasIssuePropertyCondition condition = new HasIssuePropertyCondition("foo");
+
+    assertThat(condition.matches(issue)).isFalse();
+    assertThat(condition.matches(issue.setAttribute("foo", ""))).isFalse();
+    assertThat(condition.matches(issue.setAttribute("foo", "bar"))).isTrue();
+  }
+
+  @Test
+  public void should_get_property_key() {
+    HasIssuePropertyCondition condition = new HasIssuePropertyCondition("foo");
+    assertThat(condition.getPropertyKey()).isEqualTo("foo");
+  }
+
+  @Test
+  public void shoul_fail_if_null_property() {
+    thrown.expect(IllegalArgumentException.class);
+    new HasIssuePropertyCondition(null);
+  }
+
+  @Test
+  public void should_fail_if_empty_property() {
+    thrown.expect(IllegalArgumentException.class);
+    new HasIssuePropertyCondition("");
+  }
+
+}
index e343f7b03339993b58603be9ca77ca138835be40..1f6bd2e87cc39fc8b642b27053fd4d191cdbd67f 100644 (file)
@@ -38,10 +38,8 @@ import org.sonar.core.issue.db.IssueStorage;
 import org.sonar.server.user.UserSession;
 
 import javax.annotation.Nullable;
-
 import java.util.Date;
 import java.util.List;
-import java.util.Map;
 
 import static com.google.common.collect.Lists.newArrayList;
 
@@ -71,7 +69,7 @@ public class ActionService implements ServerComponent {
     }));
   }
 
-  public Issue execute(String issueKey, String actionKey, UserSession userSession, Map<String, String> parameters) {
+  public Issue execute(String issueKey, String actionKey, UserSession userSession) {
     Preconditions.checkArgument(!Strings.isNullOrEmpty(actionKey), "Missing action");
 
     IssueQueryResult queryResult = loadIssue(issueKey);
@@ -89,7 +87,7 @@ public class ActionService implements ServerComponent {
     }
 
     IssueChangeContext changeContext = IssueChangeContext.createUser(new Date(), userSession.login());
-    FunctionContext functionContext = new FunctionContext(updater, issue, parameters, changeContext);
+    FunctionContext functionContext = new FunctionContext(updater, issue, changeContext);
     for (Function function : action.functions()) {
       function.execute(functionContext);
     }
@@ -105,14 +103,12 @@ public class ActionService implements ServerComponent {
   static class FunctionContext implements Function.Context {
 
     private final DefaultIssue issue;
-    private final Map<String, String> parameters;
     private final IssueUpdater updater;
     private final IssueChangeContext changeContext;
 
-    FunctionContext(IssueUpdater updater, DefaultIssue issue, Map<String, String> parameters, IssueChangeContext changeContext) {
+    FunctionContext(IssueUpdater updater, DefaultIssue issue, IssueChangeContext changeContext) {
       this.updater = updater;
       this.issue = issue;
-      this.parameters = parameters;
       this.changeContext = changeContext;
     }
 
@@ -121,11 +117,6 @@ public class ActionService implements ServerComponent {
       return issue;
     }
 
-    @Override
-    public Map<String, String> parameters() {
-      return parameters;
-    }
-
     @Override
     public Function.Context setAttribute(String key, @Nullable String value) {
       updater.setAttribute(issue, key, value, changeContext);
index f418fe65cf748e18713f365d2f602bb92b1275e8..8588fd26eae567218c0776b8408fb772f44f5e3d 100644 (file)
@@ -44,7 +44,6 @@ import org.sonar.server.user.UserSession;
 import org.sonar.server.util.RubyUtils;
 
 import javax.annotation.Nullable;
-
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -306,8 +305,8 @@ public class InternalRubyIssueService implements ServerComponent {
     return result;
   }
 
-  public Issue executeAction(String issueKey, String actionKey, Map<String, String> parameters) {
-    return actionService.execute(issueKey, actionKey, UserSession.get(), parameters);
+  public Issue executeAction(String issueKey, String actionKey) {
+    return actionService.execute(issueKey, actionKey, UserSession.get());
   }
 
   public List<Action> listActions(String issueKey){
index b5b77a18a2a305f93a54b6239ae275e63e53face..4cde1b109293313ad1a02e66ab2bd1bb4be675d3 100644 (file)
@@ -254,7 +254,7 @@ class Api::IssuesController < Api::ApiController
     verify_post_request
     require_parameters :issue, :actionKey
 
-    issue = Internal.issues.executeAction(params[:issue], params[:actionKey], params)
+    issue = Internal.issues.executeAction(params[:issue], params[:actionKey])
     if issue
       render :json => jsonp({
                                 :issue => Issue.to_hash(issue)
diff --git a/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java b/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java
new file mode 100644 (file)
index 0000000..c5a7962
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 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.issue;
+
+import org.junit.Before;
+import org.sonar.core.issue.IssueUpdater;
+import org.sonar.core.issue.db.IssueStorage;
+
+import static org.mockito.Mockito.mock;
+
+public class ActionServiceTest {
+
+  private ActionService actionService;
+
+  private DefaultIssueFinder finder = mock(DefaultIssueFinder.class);
+  private IssueStorage issueStorage = mock(IssueStorage.class);
+  private IssueUpdater updater = mock(IssueUpdater.class);
+  private DefaultActions actions = mock(DefaultActions.class);
+
+  @Before
+  public void before(){
+
+  }
+}