From 2e004c7c6d77ffcb2b352754568bcf8a0cb21b03 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Sun, 2 Jun 2013 22:33:26 +0200 Subject: [PATCH] SONAR-4315 Improve ActionService --- .../org/sonar/api/issue/action/Function.java | 6 -- .../condition/HasIssuePropertyCondition.java | 48 ++++++++++++++ .../workflow/function/CommentFunction.java | 2 +- .../HasIssuePropertyConditionTest.java | 63 +++++++++++++++++++ .../org/sonar/server/issue/ActionService.java | 15 +---- .../issue/InternalRubyIssueService.java | 5 +- .../app/controllers/api/issues_controller.rb | 2 +- .../sonar/server/issue/ActionServiceTest.java | 42 +++++++++++++ 8 files changed, 160 insertions(+), 23 deletions(-) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/issue/condition/HasIssuePropertyCondition.java create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/issue/condition/HasIssuePropertyConditionTest.java create mode 100644 sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Function.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Function.java index 2bb6ab8ddc9..70401abc17d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Function.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Function.java @@ -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 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 index 00000000000..d6386e9ed8b --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/condition/HasIssuePropertyCondition.java @@ -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)); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java index a312e6061d7..697e9b5f2a0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/workflow/function/CommentFunction.java @@ -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 index 00000000000..e4dbfd396f4 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/issue/condition/HasIssuePropertyConditionTest.java @@ -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(""); + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java b/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java index e343f7b0333..1f6bd2e87cc 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java @@ -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 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 parameters; private final IssueUpdater updater; private final IssueChangeContext changeContext; - FunctionContext(IssueUpdater updater, DefaultIssue issue, Map 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 parameters() { - return parameters; - } - @Override public Function.Context setAttribute(String key, @Nullable String value) { updater.setAttribute(issue, key, value, changeContext); diff --git a/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java b/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java index f418fe65cf7..8588fd26eae 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java @@ -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 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 listActions(String issueKey){ diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb index b5b77a18a2a..4cde1b10929 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb @@ -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 index 00000000000..c5a796232fa --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java @@ -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(){ + + } +} -- 2.39.5