From: Julien Lancelot Date: Mon, 17 Jun 2013 08:20:31 +0000 (+0200) Subject: SONAR-4315 Inject project settings when executing an action X-Git-Tag: 3.7~476 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7a5138aeda1f2bceaea9c078936e418fc333eb44;p=sonarqube.git SONAR-4315 Inject project settings when executing an action --- 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 48df6ff62d7..cc94316bb11 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 @@ -97,7 +97,8 @@ public class ActionService implements ServerComponent { } IssueChangeContext changeContext = IssueChangeContext.createUser(new Date(), userSession.login()); - FunctionContext functionContext = new FunctionContext(issue, updater, changeContext, getProjectSettings(queryResult.component(issue))); + Component project = queryResult.project(issue); + FunctionContext functionContext = new FunctionContext(issue, updater, changeContext, getProjectSettings(project)); for (Function function : action.functions()) { function.execute(functionContext); } 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 9624fe12cfd..be2161dfbdc 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 @@ -36,9 +36,11 @@ 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.core.properties.PropertyDto; 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; @@ -77,8 +79,7 @@ public class ActionServiceTest { IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class); when(issueQueryResult.first()).thenReturn(issue); - Component project = mock(Component.class); - when(issueQueryResult.component(issue)).thenReturn(project); + when(issueQueryResult.project(issue)).thenReturn(mock(Component.class)); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function1, function2); @@ -100,8 +101,7 @@ public class ActionServiceTest { DefaultIssue issue = new DefaultIssue().setKey("ABCD"); IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class); when(issueQueryResult.first()).thenReturn(issue); - Component project = mock(Component.class); - when(issueQueryResult.component(issue)).thenReturn(project); + when(issueQueryResult.project(issue)).thenReturn(mock(Component.class)); when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); @@ -112,6 +112,28 @@ public class ActionServiceTest { verify(updater).setAttribute(eq(issue), eq("tweet"), eq("tweet sent"), any(IssueChangeContext.class)); } + @Test + public void should_inject_project_settings_when_executing_a_function() { + Function function = new TweetFunction(); + + UserSession userSession = mock(UserSession.class); + when(userSession.login()).thenReturn("arthur"); + + DefaultIssue issue = new DefaultIssue().setKey("ABCD"); + IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class); + when(issueQueryResult.first()).thenReturn(issue); + Component project = mock(Component.class); + when(project.key()).thenReturn("struts"); + when(issueQueryResult.project(issue)).thenReturn(project); + + when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult); + + actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function); + assertThat(actionService.execute("ABCD", "link-to-jira", userSession)).isNotNull(); + + verify(propertiesDao).selectProjectProperties(eq("struts")); + } + @Test public void should_not_execute_function_if_issue_not_found() { Function function = mock(Function.class); @@ -200,6 +222,24 @@ public class ActionServiceTest { assertThat(actionService.listAvailableActions("ABCD")).isEmpty(); } + @Test + public void should_get_project_settings(){ + Component project = mock(Component.class); + when(project.key()).thenReturn("struts"); + + // Global property + settings.appendProperty("sonar.core.version", "3.6"); + + // Project property + List projectProperties = newArrayList(new PropertyDto().setKey("sonar.jira.project.key").setValue("STRUTS")); + when(propertiesDao.selectProjectProperties("struts")).thenReturn(projectProperties); + + Settings result = actionService.getProjectSettings(project); + assertThat(result).isNotNull(); + assertThat(result.hasKey("sonar.core.version")).isTrue(); + assertThat(result.hasKey("sonar.jira.project.key")).isTrue(); + } + public class AlwaysMatch implements Condition { @Override public boolean matches(Issue issue) {