--- /dev/null
+/*
+ * 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.Test;
+import org.sonar.api.issue.Issue;
+import org.sonar.server.user.UserSession;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+
+public class ActionTest {
+
+ @Test
+ public void key_should_not_be_empty() {
+ try {
+ new Action("") {
+ @Override
+ boolean verify(Map<String, Object> properties, List<Issue> issues, UserSession userSession) {
+ return false;
+ }
+ @Override
+ boolean execute(Map<String, Object> properties, Context context) {
+ return false;
+ }
+ };
+ } catch (Exception e) {
+ assertThat(e).hasMessage("Action key must be set").isInstanceOf(IllegalArgumentException.class);
+ }
+ }
+
+ @Test
+ public void key_should_not_be_null() {
+ try {
+ new Action(null) {
+ @Override
+ boolean verify(Map<String, Object> properties, List<Issue> issues, UserSession userSession) {
+ return false;
+ }
+ @Override
+ boolean execute(Map<String, Object> properties, Context context) {
+ return false;
+ }
+ };
+ } catch (Exception e) {
+ assertThat(e).hasMessage("Action key must be set").isInstanceOf(IllegalArgumentException.class);
+ }
+ }
+}
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueQuery;
import org.sonar.api.issue.IssueQueryResult;
+import org.sonar.api.issue.condition.Condition;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.issue.internal.IssueChangeContext;
import org.sonar.api.web.UserRole;
private IssueBulkChangeService service;
- private Action action = mock(Action.class);
private List<Action> actions;
@Before
when(issueQueryResult.issues()).thenReturn(newArrayList((Issue) issue));
actions = newArrayList();
- when(action.key()).thenReturn("assign");
- actions.add(action);
service = new IssueBulkChangeService(finder, issueStorage, issueNotifications, actions);
}
properties.put("issues", "ABCD");
properties.put("actions", "assign");
properties.put("assign.assignee", "fred");
-
- when(action.supports(any(Issue.class))).thenReturn(true);
- when(action.execute(anyMap(), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
- when(action.execute(eq(properties), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
+ actions.add(new MockAction("assign"));
IssueBulkChangeQuery issueBulkChangeQuery = new IssueBulkChangeQuery(properties);
IssueBulkChangeResult result = service.execute(issueBulkChangeQuery, userSession);
properties.put("assign.assignee", "fred");
properties.put("set_severity.severity", "MINOR");
- Action setSeverityAction = mock(Action.class);
- when(setSeverityAction.key()).thenReturn("set_severity");
- actions.add(setSeverityAction);
-
- when(action.supports(any(Issue.class))).thenReturn(true);
- when(action.execute(anyMap(), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
- when(action.execute(eq(properties), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
-
- when(setSeverityAction.supports(any(Issue.class))).thenReturn(true);
- when(setSeverityAction.execute(anyMap(), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
- when(setSeverityAction.execute(eq(properties), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
+ actions.add(new MockAction("set_severity"));
+ actions.add(new MockAction("assign"));
IssueBulkChangeQuery issueBulkChangeQuery = new IssueBulkChangeQuery(properties);
IssueBulkChangeResult result = service.execute(issueBulkChangeQuery, userSession);
properties.put("issues", "ABCD,DEFG");
properties.put("actions", "assign");
properties.put("assign.assignee", "fred");
-
- when(action.supports(any(Issue.class))).thenReturn(true);
- when(action.execute(anyMap(), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
- when(action.execute(eq(properties), any(IssueBulkChangeService.ActionContext.class))).thenReturn(true);
+ actions.add(new MockAction("assign"));
IssueBulkChangeQuery issueBulkChangeQuery = new IssueBulkChangeQuery(properties);
service.execute(issueBulkChangeQuery, userSession);
properties.put("issues", "ABCD");
properties.put("actions", "assign");
properties.put("assign.assignee", "fred");
-
- when(action.supports(any(Issue.class))).thenReturn(false);
+ actions.add(new MockAction("assign", true, true, false));
IssueBulkChangeQuery issueBulkChangeQuery = new IssueBulkChangeQuery(properties);
IssueBulkChangeResult result = service.execute(issueBulkChangeQuery, userSession);
properties.put("issues", "ABCD");
properties.put("actions", "assign");
properties.put("assign.assignee", "fred");
-
- when(action.supports(any(Issue.class))).thenReturn(true);
- when(action.execute(anyMap(), any(IssueBulkChangeService.ActionContext.class))).thenReturn(false);
+ actions.add(new MockAction("assign", true, false, true));
IssueBulkChangeQuery issueBulkChangeQuery = new IssueBulkChangeQuery(properties);
IssueBulkChangeResult result = service.execute(issueBulkChangeQuery, userSession);
properties.put("actions", "assign");
properties.put("assign.assignee", "fred");
+ Action action = mock(Action.class);
+ actions.add(action);
+ when(action.key()).thenReturn("assign");
when(action.supports(any(Issue.class))).thenReturn(true);
doThrow(new RuntimeException("Error")).when(action).execute(anyMap(), any(IssueBulkChangeService.ActionContext.class));
verifyZeroInteractions(issueNotifications);
}
+
+ class MockAction extends Action {
+
+ private boolean verify;
+ private boolean execute;
+
+ public MockAction(String key, boolean verify, boolean execute, final boolean support) {
+ super(key);
+ this.verify = verify;
+ this.execute = execute;
+ setConditions(new Condition() {
+ @Override
+ public boolean matches(Issue issue) {
+ return support;
+ }
+ });
+ }
+
+ public MockAction(String key) {
+ this(key, true, true, true);
+ }
+
+ @Override
+ boolean verify(Map<String, Object> properties, List<Issue> issues, UserSession userSession) {
+ return verify;
+ }
+
+ @Override
+ boolean execute(Map<String, Object> properties, Context context) {
+ return execute;
+ }
+ }
}