From b869f970c10e33f45f3b3ac28e2c30c80e7a1962 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 3 Jun 2013 12:32:42 +0200 Subject: [PATCH] SONAR-4315 Add projectSettings in Action API --- pom.xml | 1 - .../org/sonar/api/issue/action/Function.java | 3 + .../org/sonar/server/issue/ActionService.java | 34 +++++++-- .../sonar/server/issue/ActionServiceTest.java | 71 +++++++++++-------- 4 files changed, 75 insertions(+), 34 deletions(-) diff --git a/pom.xml b/pom.xml index 73cac67243d..54b1a54c98f 100644 --- a/pom.xml +++ b/pom.xml @@ -266,7 +266,6 @@ org.codehaus.sonar sonar-packaging-maven-plugin - 1.6 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 70401abc17d..a213670ec47 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 @@ -19,6 +19,7 @@ */ package org.sonar.api.issue.action; +import org.sonar.api.config.Settings; import org.sonar.api.issue.Issue; import javax.annotation.Nullable; @@ -33,6 +34,8 @@ public interface Function { interface Context { Issue issue(); + Settings projectSettings(); + Context setAttribute(String key, @Nullable String value); Context addComment(@Nullable String text); 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 301d0d2bf15..b8a4ac85fbe 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 @@ -25,6 +25,8 @@ import com.google.common.base.Predicate; import com.google.common.base.Strings; import com.google.common.collect.Iterables; import org.sonar.api.ServerComponent; +import org.sonar.api.component.Component; +import org.sonar.api.config.Settings; import org.sonar.api.issue.Issue; import org.sonar.api.issue.IssueQuery; import org.sonar.api.issue.IssueQueryResult; @@ -35,6 +37,8 @@ import org.sonar.api.issue.internal.IssueChangeContext; import org.sonar.api.web.UserRole; import org.sonar.core.issue.IssueUpdater; import org.sonar.core.issue.db.IssueStorage; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; import org.sonar.server.user.UserSession; import javax.annotation.CheckForNull; @@ -51,17 +55,21 @@ public class ActionService implements ServerComponent { private final DefaultIssueFinder finder; private final IssueStorage issueStorage; private final IssueUpdater updater; + private final Settings settings; + private final PropertiesDao propertiesDao; private final List actions; - public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, List actions) { + public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao, List actions) { this.finder = finder; this.issueStorage = issueStorage; this.updater = updater; + this.settings = settings; + this.propertiesDao = propertiesDao; this.actions = actions; } - public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater) { - this(finder, issueStorage, updater, Collections.emptyList()); + public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao) { + this(finder, issueStorage, updater, settings, propertiesDao, Collections.emptyList()); } public List listAvailableActions(final Issue issue) { @@ -101,7 +109,7 @@ public class ActionService implements ServerComponent { } IssueChangeContext changeContext = IssueChangeContext.createUser(new Date(), userSession.login()); - FunctionContext functionContext = new FunctionContext(updater, issue, changeContext); + FunctionContext functionContext = new FunctionContext(issue, updater, changeContext, getProjectSettings(queryResult.component(issue))); for (Function function : action.functions()) { function.execute(functionContext); } @@ -124,16 +132,27 @@ public class ActionService implements ServerComponent { }, null); } + public Settings getProjectSettings(Component project) { + Settings projectSettings = new Settings(settings); + List properties = propertiesDao.selectProjectProperties(project.key()); + for (PropertyDto dto : properties) { + projectSettings.setProperty(dto.getKey(), dto.getValue()); + } + return projectSettings; + } + static class FunctionContext implements Function.Context { private final DefaultIssue issue; private final IssueUpdater updater; private final IssueChangeContext changeContext; + private final Settings projectSettings; - FunctionContext(IssueUpdater updater, DefaultIssue issue, IssueChangeContext changeContext) { + FunctionContext(DefaultIssue issue, IssueUpdater updater, IssueChangeContext changeContext, Settings projectSettings) { this.updater = updater; this.issue = issue; this.changeContext = changeContext; + this.projectSettings = projectSettings; } @Override @@ -141,6 +160,11 @@ public class ActionService implements ServerComponent { return issue; } + @Override + public Settings projectSettings() { + return projectSettings; + } + @Override public Function.Context setAttribute(String key, @Nullable String value) { updater.setAttribute(issue, key, value, changeContext); 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 index 7ef368d9f95..c2d8eaa0400 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java @@ -20,7 +20,10 @@ package org.sonar.server.issue; +import org.junit.Before; import org.junit.Test; +import org.sonar.api.component.Component; +import org.sonar.api.config.Settings; import org.sonar.api.issue.Issue; import org.sonar.api.issue.IssueQuery; import org.sonar.api.issue.IssueQueryResult; @@ -32,9 +35,11 @@ import org.sonar.api.issue.internal.IssueChangeContext; import org.sonar.core.issue.DefaultIssueQueryResult; import org.sonar.core.issue.IssueUpdater; import org.sonar.core.issue.db.IssueStorage; +import org.sonar.core.properties.PropertiesDao; import org.sonar.server.user.UserSession; import java.util.Collections; +import java.util.List; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; @@ -45,10 +50,25 @@ import static org.mockito.Mockito.*; public class ActionServiceTest { + private DefaultIssueFinder finder; + private IssueStorage issueStorage; + private IssueUpdater updater; + private PropertiesDao propertiesDao; + private Settings settings; + + private List actions; private ActionService actionService; - private DefaultIssueFinder finder = mock(DefaultIssueFinder.class); - private IssueStorage issueStorage = mock(IssueStorage.class); - private IssueUpdater updater = mock(IssueUpdater.class); + + @Before + public void before(){ + finder = mock(DefaultIssueFinder.class); + issueStorage = mock(IssueStorage.class); + updater = mock(IssueUpdater.class); + propertiesDao = mock(PropertiesDao.class); + settings = new Settings(); + actions = newArrayList(); + actionService = new ActionService(finder, issueStorage, updater, settings, propertiesDao, actions); + } @Test public void should_execute_functions() { @@ -56,12 +76,15 @@ public class ActionServiceTest { Function function2 = mock(Function.class); Issue issue = new DefaultIssue().setKey("ABCD"); - IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue)); + IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class); + when(issueQueryResult.first()).thenReturn(issue); + + Component project = mock(Component.class); + when(issueQueryResult.component(issue)).thenReturn(project); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); - Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function1, function2).build(); + actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function1, function2).build()); - actionService = new ActionService(finder, issueStorage, updater, newArrayList(action)); assertThat(actionService.execute("ABCD", "link-to-jira", mock(UserSession.class))).isNotNull(); verify(function1).execute(any(Function.Context.class)); @@ -77,12 +100,14 @@ public class ActionServiceTest { when(userSession.login()).thenReturn("arthur"); DefaultIssue issue = new DefaultIssue().setKey("ABCD"); - IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList((Issue) issue)); - when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); + IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class); + when(issueQueryResult.first()).thenReturn(issue); + Component project = mock(Component.class); + when(issueQueryResult.component(issue)).thenReturn(project); - Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build(); + when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); - actionService = new ActionService(finder, issueStorage, updater, newArrayList(action)); + actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build()); assertThat(actionService.execute("ABCD", "link-to-jira", userSession)).isNotNull(); verify(updater).addComment(eq(issue), eq("New tweet on issue ABCD"), any(IssueChangeContext.class)); @@ -96,10 +121,7 @@ public class ActionServiceTest { IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.emptyList()); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); - Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build(); - - actionService = new ActionService(finder, issueStorage, updater, newArrayList(action)); - + actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build()); try { actionService.execute("ABCD", "link-to-jira", mock(UserSession.class)); } catch (Exception e){ @@ -116,9 +138,7 @@ public class ActionServiceTest { IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue)); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); - Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build(); - - actionService = new ActionService(finder, issueStorage, updater, newArrayList(action)); + actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build()); try { actionService.execute("ABCD", "tweet", mock(UserSession.class)); } catch (Exception e){ @@ -135,9 +155,7 @@ public class ActionServiceTest { IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue)); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); - Action action = Action.builder("link-to-jira").conditions(new NeverMatch()).functions(function).build(); - - actionService = new ActionService(finder, issueStorage, updater, newArrayList(action)); + actions.add(Action.builder("link-to-jira").conditions(new NeverMatch()).functions(function).build()); try { actionService.execute("ABCD", "link-to-jira", mock(UserSession.class)); } catch (Exception e){ @@ -153,9 +171,9 @@ public class ActionServiceTest { when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); Action action1 = Action.builder("link-to-jira").conditions(new AlwaysMatch()).build(); + actions.add(action1); Action action2 = Action.builder("tweet").conditions(new NeverMatch()).build(); - - actionService = new ActionService(finder, issueStorage, updater, newArrayList(action1, action2)); + actions.add(action2); assertThat(actionService.listAvailableActions("ABCD")).containsOnly(action1); } @@ -164,11 +182,8 @@ public class ActionServiceTest { IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.emptyList()); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); - Action action1 = Action.builder("link-to-jira").conditions(new AlwaysMatch()).build(); - Action action2 = Action.builder("tweet").conditions(new NeverMatch()).build(); - - actionService = new ActionService(finder, issueStorage, updater, newArrayList(action1, action2)); - + actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).build()); + actions.add(Action.builder("tweet").conditions(new NeverMatch()).build()); try { actionService.listAvailableActions("ABCD"); fail(); @@ -183,7 +198,7 @@ public class ActionServiceTest { IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue)); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); - actionService = new ActionService(finder, issueStorage, updater); + actionService = new ActionService(finder, issueStorage, updater, settings, propertiesDao); assertThat(actionService.listAvailableActions("ABCD")).isEmpty(); } -- 2.39.5