1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 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.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;
import org.sonar.api.issue.action.Actions;
import org.sonar.api.issue.action.Function;
import org.sonar.api.issue.condition.Condition;
import org.sonar.api.issue.internal.DefaultIssue;
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.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;
import static org.fest.assertions.Fail.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
public class ActionServiceTest {
DefaultIssueFinder finder;
IssueStorage issueStorage;
IssueUpdater updater;
PropertiesDao propertiesDao;
Settings settings;
Actions actions;
ActionService actionService;
@Before
public void before() {
finder = mock(DefaultIssueFinder.class);
issueStorage = mock(IssueStorage.class);
updater = mock(IssueUpdater.class);
propertiesDao = mock(PropertiesDao.class);
settings = new Settings();
actions = new Actions();
actionService = new ActionService(finder, issueStorage, updater, settings, propertiesDao, actions);
}
@Test
public void execute_functions() {
Function function1 = mock(Function.class);
Function function2 = mock(Function.class);
Issue issue = new DefaultIssue().setKey("ABCD");
IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class);
when(issueQueryResult.first()).thenReturn(issue);
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);
assertThat(actionService.execute("ABCD", "link-to-jira", mock(UserSession.class))).isNotNull();
verify(function1).execute(any(Function.Context.class));
verify(function2).execute(any(Function.Context.class));
verifyNoMoreInteractions(function1, function2);
}
@Test
public void modify_issue_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);
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(function);
assertThat(actionService.execute("ABCD", "link-to-jira", userSession)).isNotNull();
verify(updater).addComment(eq(issue), eq("New tweet on issue ABCD"), any(IssueChangeContext.class));
verify(updater).setAttribute(eq(issue), eq("tweet"), eq("tweet sent"), any(IssueChangeContext.class));
}
@Test
public void 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 not_execute_function_if_issue_not_found() {
Function function = mock(Function.class);
IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.<Issue>emptyList());
when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function);
try {
actionService.execute("ABCD", "link-to-jira", mock(UserSession.class));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("No issue found");
}
verifyZeroInteractions(function);
}
@Test
public void not_execute_function_if_action_not_found() {
Function function = mock(Function.class);
Issue issue = new DefaultIssue().setKey("ABCD");
IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
actions.add("link-to-jira").setConditions(new AlwaysMatch()).setFunctions(function);
try {
actionService.execute("ABCD", "tweet", mock(UserSession.class));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Action is not found : tweet");
}
verifyZeroInteractions(function);
}
@Test
public void not_execute_function_if_action_is_not_supported() {
Function function = mock(Function.class);
Issue issue = new DefaultIssue().setKey("ABCD");
IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
actions.add("link-to-jira").setConditions(new NeverMatch()).setFunctions(function);
try {
actionService.execute("ABCD", "link-to-jira", mock(UserSession.class));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("A condition is not respected");
}
verifyZeroInteractions(function);
}
@Test
public void list_available_supported_actions() {
Issue issue = new DefaultIssue().setKey("ABCD");
IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
actions.add("link-to-jira").setConditions(new AlwaysMatch());
actions.add("tweet").setConditions(new NeverMatch());
assertThat(actionService.listAvailableActions("ABCD")).hasSize(1);
}
@Test
public void list_available_actions_throw_exception_if_issue_not_found() {
IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.<Issue>emptyList());
when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
actions.add("link-to-jira").setConditions(new AlwaysMatch());
actions.add("tweet").setConditions(new NeverMatch());
try {
actionService.listAvailableActions("ABCD");
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("No issue found");
}
}
@Test
public void return_no_action() {
Issue issue = new DefaultIssue().setKey("ABCD");
IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
assertThat(actionService.listAvailableActions("ABCD")).isEmpty();
}
@Test
public void 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();
}
@Test
public void list_all_actions() {
actions.add("link-to-jira").setConditions(new AlwaysMatch());
assertThat(actionService.listAllActions()).hasSize(1);
}
public class AlwaysMatch implements Condition {
@Override
public boolean matches(Issue issue) {
return true;
}
}
public class NeverMatch implements Condition {
@Override
public boolean matches(Issue issue) {
return false;
}
}
public class TweetFunction implements Function {
@Override
public void execute(Context context) {
context.addComment("New tweet on issue " + context.issue().key());
context.setAttribute("tweet", "tweet sent");
}
}
}
|