]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4315 Inject project settings when executing an action
authorJulien Lancelot <julien.lancelot@gmail.com>
Mon, 17 Jun 2013 08:20:31 +0000 (10:20 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Mon, 17 Jun 2013 09:37:21 +0000 (11:37 +0200)
sonar-server/src/main/java/org/sonar/server/issue/ActionService.java
sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java

index 48df6ff62d79bf2ad6ec7e29dc17a0963dc62b53..cc94316bb116493a724d5d94ed9ba1224df4d1b3 100644 (file)
@@ -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);
     }
index 9624fe12cfd43507fa7bfde852d6ad135ef7cae3..be2161dfbdce9e667f89a4bd7f500f19ecb13f63 100644 (file)
@@ -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<PropertyDto> 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) {