import org.sonar.core.resource.ResourceDao;
import org.sonar.core.resource.ResourceDto;
import org.sonar.core.resource.ResourceQuery;
-import org.sonar.core.user.AuthorizationDao;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.user.UserSession;
import javax.annotation.CheckForNull;
+
import java.util.*;
import static com.google.common.collect.Lists.newArrayList;
private final ActionPlanDao actionPlanDao;
private final ActionPlanStatsDao actionPlanStatsDao;
private final ResourceDao resourceDao;
- private final AuthorizationDao authorizationDao;
private final IssueDao issueDao;
private final IssueUpdater issueUpdater;
private final IssueStorage issueStorage;
- public ActionPlanService(ActionPlanDao actionPlanDao, ActionPlanStatsDao actionPlanStatsDao, ResourceDao resourceDao, AuthorizationDao authorizationDao,
+ public ActionPlanService(ActionPlanDao actionPlanDao, ActionPlanStatsDao actionPlanStatsDao, ResourceDao resourceDao,
IssueDao issueDao, IssueUpdater issueUpdater, IssueStorage issueStorage) {
this.actionPlanDao = actionPlanDao;
this.actionPlanStatsDao = actionPlanStatsDao;
this.resourceDao = resourceDao;
- this.authorizationDao = authorizationDao;
this.issueDao = issueDao;
this.issueUpdater = issueUpdater;
this.issueStorage = issueStorage;
public ActionPlan create(ActionPlan actionPlan, UserSession userSession) {
ResourceDto project = findProject(actionPlan.projectKey());
- checkAuthorization(userSession, project, UserRole.ADMIN);
+ checkUserIsProjectAdministrator(project.getKey(), userSession);
actionPlanDao.save(ActionPlanDto.toActionDto(actionPlan, project.getId()));
return actionPlan;
}
public ActionPlan update(ActionPlan actionPlan, UserSession userSession) {
ResourceDto project = findProject(actionPlan.projectKey());
- checkAuthorization(userSession, project, UserRole.ADMIN);
+ checkUserIsProjectAdministrator(project.getKey(), userSession);
actionPlanDao.update(ActionPlanDto.toActionDto(actionPlan, project.getId()));
return actionPlan;
}
public void delete(String actionPlanKey, UserSession userSession) {
ActionPlanDto dto = findActionPlanDto(actionPlanKey);
- checkAuthorization(userSession, dto.getProjectKey(), UserRole.ADMIN);
+ checkUserIsProjectAdministrator(dto.getProjectKey(), userSession);
unplanIssues(dto.toActionPlan(), userSession);
actionPlanDao.delete(actionPlanKey);
}
public ActionPlan setStatus(String actionPlanKey, String status, UserSession userSession) {
ActionPlanDto actionPlanDto = findActionPlanDto(actionPlanKey);
- checkAuthorization(userSession, actionPlanDto.getProjectKey(), UserRole.ADMIN);
+ checkUserIsProjectAdministrator(actionPlanDto.getProjectKey(), userSession);
actionPlanDto.setStatus(status);
actionPlanDto.setCreatedAt(new Date());
if (actionPlanDto == null) {
return null;
}
- checkAuthorization(userSession, actionPlanDto.getProjectKey(), UserRole.USER);
+ checkUserCanAccessProject(actionPlanDto.getProjectKey(), userSession);
return actionPlanDto.toActionPlan();
}
public Collection<ActionPlan> findOpenByProjectKey(String projectKey, UserSession userSession) {
ResourceDto project = findProject(projectKey);
- checkAuthorization(userSession, project, UserRole.USER);
+ checkUserCanAccessProject(project.getKey(), userSession);
List<ActionPlanDto> dtos = actionPlanDao.findOpenByProjectId(project.getId());
List<ActionPlan> plans = toActionPlans(dtos);
public List<ActionPlanStats> findActionPlanStats(String projectKey, UserSession userSession) {
ResourceDto project = findProject(projectKey);
- checkAuthorization(userSession, project, UserRole.USER);
+ checkUserCanAccessProject(project.getKey(), userSession);
List<ActionPlanStatsDto> actionPlanStatsDtos = actionPlanStatsDao.findByProjectId(project.getId());
List<ActionPlanStats> actionPlanStats = newArrayList(Iterables.transform(actionPlanStatsDtos, new Function<ActionPlanStatsDto, ActionPlanStats>() {
private ActionPlanDto findActionPlanDto(String actionPlanKey) {
ActionPlanDto actionPlanDto = actionPlanDao.findByKey(actionPlanKey);
if (actionPlanDto == null) {
- // TODO throw 404
- throw new IllegalArgumentException("Action plan " + actionPlanKey + " has not been found.");
+ throw new NotFoundException("Action plan " + actionPlanKey + " has not been found.");
}
return actionPlanDto;
}
private ResourceDto findProject(String projectKey) {
ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(projectKey));
if (resourceDto == null) {
- // TODO throw 404
- throw new IllegalArgumentException("Project " + projectKey + " does not exists.");
+ throw new NotFoundException("Project " + projectKey + " does not exists.");
}
return resourceDto;
}
- private void checkAuthorization(UserSession userSession, String projectKey, String requiredRole) {
- checkAuthorization(userSession, findProject(projectKey), requiredRole);
+ private void checkUserCanAccessProject(String projectKey, UserSession userSession) {
+ userSession.checkProjectPermission(UserRole.USER, projectKey);
}
- private void checkAuthorization(UserSession userSession, ResourceDto project, String requiredRole) {
- if (!authorizationDao.isAuthorizedComponentKey(project.getKey(), userSession.userId(), requiredRole)) {
- // TODO throw unauthorized
- throw new IllegalStateException("User does not have the required role on the project: " + project.getKey());
- }
+ private void checkUserIsProjectAdministrator(String projectKey, UserSession userSession) {
+ userSession.checkProjectPermission(UserRole.ADMIN, projectKey);
}
}
import static com.google.common.collect.Lists.newArrayList;
/**
+ * Used through ruby code <pre>Internal.issues</pre>
+ *
* All the issue features that are not published to public API.
*
* @since 3.6
<div>
- <% if profiles_administrator? %>
+ <% if is_admin?(@resource.id) %>
<ul style="float: right" class="horizontal">
<li class="marginleft10 add">
<a id="create-link-action_plans"
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.issue.ActionPlan;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueQuery;
import org.sonar.core.resource.ResourceDao;
import org.sonar.core.resource.ResourceDto;
import org.sonar.core.resource.ResourceQuery;
-import org.sonar.core.user.AuthorizationDao;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.MockUserSession;
import org.sonar.server.user.UserSession;
import java.util.Collection;
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.anyInt;
-import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
+@RunWith(MockitoJUnitRunner.class)
public class ActionPlanServiceTest {
- private ActionPlanDao actionPlanDao = mock(ActionPlanDao.class);
- private ActionPlanStatsDao actionPlanStatsDao = mock(ActionPlanStatsDao.class);
- private ResourceDao resourceDao = mock(ResourceDao.class);
- private AuthorizationDao authorizationDao = mock(AuthorizationDao.class);
- private UserSession userSession = mock(UserSession.class);
- private IssueDao issueDao = mock(IssueDao.class);
- private IssueUpdater issueUpdater = mock(IssueUpdater.class);
- private IssueStorage issueStorage = mock(IssueStorage.class);
+ @Mock
+ ActionPlanDao actionPlanDao;
+
+ @Mock
+ ActionPlanStatsDao actionPlanStatsDao;
+
+ @Mock
+ ResourceDao resourceDao;
+
+ @Mock
+ IssueDao issueDao;
+
+ @Mock
+ IssueUpdater issueUpdater;
+
+ @Mock
+ IssueStorage issueStorage;
+
+ String projectKey = "org.sonar.Sample";
+
+ UserSession projectAdministratorUserSession = MockUserSession.create().setLogin("nicolas").setName("Nicolas").addProjectPermissions(UserRole.ADMIN, projectKey);
+ UserSession projectUserSession = MockUserSession.create().setLogin("nicolas").setName("Nicolas").addProjectPermissions(UserRole.USER, projectKey);
+ UserSession unauthorizedUserSession = MockUserSession.create().setLogin("nicolas").setName("Nicolas");
private ActionPlanService actionPlanService;
@Before
public void before() {
- when(userSession.isLoggedIn()).thenReturn(true);
- when(userSession.userId()).thenReturn(10);
- when(authorizationDao.isAuthorizedComponentKey(anyString(), eq(10), anyString())).thenReturn(true);
-
- actionPlanService = new ActionPlanService(actionPlanDao, actionPlanStatsDao, resourceDao, authorizationDao, issueDao, issueUpdater, issueStorage);
+ actionPlanService = new ActionPlanService(actionPlanDao, actionPlanStatsDao, resourceDao, issueDao, issueUpdater, issueStorage);
}
@Test
- public void should_create() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void create() {
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
ActionPlan actionPlan = DefaultActionPlan.create("Long term");
- actionPlanService.create(actionPlan, userSession);
+ actionPlanService.create(actionPlan, projectAdministratorUserSession);
verify(actionPlanDao).save(any(ActionPlanDto.class));
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.ADMIN));
}
@Test
- public void should_create_required_admin_role() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void create_required_admin_role() {
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
ActionPlan actionPlan = DefaultActionPlan.create("Long term");
- when(authorizationDao.isAuthorizedComponentKey(anyString(), eq(10), anyString())).thenReturn(false);
try {
- actionPlanService.create(actionPlan, userSession);
+ actionPlanService.create(actionPlan, unauthorizedUserSession);
fail();
} catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("User does not have the required role on the project: org.sonar.Sample");
+ assertThat(e).isInstanceOf(ForbiddenException.class);
}
- verify(authorizationDao).isAuthorizedComponentKey(eq("org.sonar.Sample"), eq(10), eq(UserRole.ADMIN));
verifyZeroInteractions(actionPlanDao);
}
@Test
- public void should_set_status() {
- when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD"));
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void set_status() {
+ when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD").setProjectKey_unit_test_only(projectKey));
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
- ActionPlan result = actionPlanService.setStatus("ABCD", "CLOSED", userSession);
+ ActionPlan result = actionPlanService.setStatus("ABCD", "CLOSED", projectAdministratorUserSession);
verify(actionPlanDao).update(any(ActionPlanDto.class));
assertThat(result).isNotNull();
assertThat(result.status()).isEqualTo("CLOSED");
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.ADMIN));
}
@Test
- public void should_update() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void update() {
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
ActionPlan actionPlan = DefaultActionPlan.create("Long term");
- actionPlanService.update(actionPlan, userSession);
+ actionPlanService.update(actionPlan, projectAdministratorUserSession);
verify(actionPlanDao).update(any(ActionPlanDto.class));
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.ADMIN));
}
@Test
- public void should_delete() {
- when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD"));
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
- actionPlanService.delete("ABCD", userSession);
+ public void delete() {
+ when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD").setProjectKey_unit_test_only(projectKey));
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
+ actionPlanService.delete("ABCD", projectAdministratorUserSession);
verify(actionPlanDao).delete("ABCD");
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.ADMIN));
}
@Test
- public void should_unplan_all_linked_issues_when_deleting_an_action_plan() {
- when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD"));
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void unplan_all_linked_issues_when_deleting_an_action_plan() {
+ when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD").setProjectKey_unit_test_only(projectKey));
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
IssueDto issueDto = new IssueDto().setId(100L).setStatus(Issue.STATUS_OPEN).setRuleKey_unit_test_only("squid", "s100");
when(issueDao.selectIssues(any(IssueQuery.class))).thenReturn(newArrayList(issueDto));
when(issueUpdater.plan(any(DefaultIssue.class), eq((ActionPlan) null), any(IssueChangeContext.class))).thenReturn(true);
ArgumentCaptor<DefaultIssue> captor = ArgumentCaptor.forClass(DefaultIssue.class);
- actionPlanService.delete("ABCD", userSession);
+ actionPlanService.delete("ABCD", projectAdministratorUserSession);
verify(actionPlanDao).delete("ABCD");
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.ADMIN));
verify(issueUpdater).plan(captor.capture(), eq((ActionPlan) null), any(IssueChangeContext.class));
verify(issueStorage).save(newArrayList(captor.getAllValues()));
}
@Test
- public void should_find_by_key() {
- when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD"));
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void find_by_key() {
+ when(actionPlanDao.findByKey("ABCD")).thenReturn(new ActionPlanDto().setKey("ABCD").setProjectKey_unit_test_only(projectKey));
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
- ActionPlan result = actionPlanService.findByKey("ABCD", userSession);
+ ActionPlan result = actionPlanService.findByKey("ABCD", projectUserSession);
assertThat(result).isNotNull();
assertThat(result.key()).isEqualTo("ABCD");
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.USER));
}
@Test
- public void should_return_null_if_no_action_plan_when_find_by_key() {
+ public void return_null_if_no_action_plan_when_find_by_key() {
when(actionPlanDao.findByKey("ABCD")).thenReturn(null);
- assertThat(actionPlanService.findByKey("ABCD", userSession)).isNull();
+ assertThat(actionPlanService.findByKey("ABCD", projectUserSession)).isNull();
}
@Test
- public void should_find_by_keys() {
+ public void find_by_keys() {
when(actionPlanDao.findByKeys(newArrayList("ABCD"))).thenReturn(newArrayList(new ActionPlanDto().setKey("ABCD")));
Collection<ActionPlan> results = actionPlanService.findByKeys(newArrayList("ABCD"));
assertThat(results).hasSize(1);
}
@Test
- public void should_find_open_by_project_key() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void find_open_by_project_key() {
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
when(actionPlanDao.findOpenByProjectId(1l)).thenReturn(newArrayList(new ActionPlanDto().setKey("ABCD")));
- Collection<ActionPlan> results = actionPlanService.findOpenByProjectKey("org.sonar.Sample", userSession);
+ Collection<ActionPlan> results = actionPlanService.findOpenByProjectKey(projectKey, projectUserSession);
assertThat(results).hasSize(1);
assertThat(results.iterator().next().key()).isEqualTo("ABCD");
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.USER));
}
@Test
- public void should_find_open_by_project_key_required_user_role() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey("org.sonar.Sample").setId(1l));
+ public void find_open_by_project_key_required_user_role() {
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setKey(projectKey).setId(1l));
when(actionPlanDao.findOpenByProjectId(1l)).thenReturn(newArrayList(new ActionPlanDto().setKey("ABCD")));
- when(authorizationDao.isAuthorizedComponentKey(anyString(), eq(10), anyString())).thenReturn(false);
try {
- actionPlanService.findOpenByProjectKey("org.sonar.Sample", userSession);
+ actionPlanService.findOpenByProjectKey(projectKey, unauthorizedUserSession);
fail();
} catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("User does not have the required role on the project: org.sonar.Sample");
+ assertThat(e).isInstanceOf(ForbiddenException.class);
}
- verify(authorizationDao).isAuthorizedComponentKey(eq("org.sonar.Sample"), eq(10), eq(UserRole.USER));
verifyZeroInteractions(actionPlanDao);
}
- @Test(expected = IllegalArgumentException.class)
- public void should_throw_exception_if_project_not_found_when_find_open_by_project_key() {
+ @Test(expected = NotFoundException.class)
+ public void throw_exception_if_project_not_found_when_find_open_by_project_key() {
when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(null);
- actionPlanService.findOpenByProjectKey("<Unkown>", userSession);
+ actionPlanService.findOpenByProjectKey("<Unkown>", projectUserSession);
}
@Test
- public void should_find_action_plan_stats() {
- when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setId(1L).setKey("org.sonar.Sample"));
+ public void find_action_plan_stats() {
+ when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(new ResourceDto().setId(1L).setKey(projectKey));
when(actionPlanStatsDao.findByProjectId(1L)).thenReturn(newArrayList(new ActionPlanStatsDto()));
- Collection<ActionPlanStats> results = actionPlanService.findActionPlanStats("org.sonar.Sample", userSession);
+ Collection<ActionPlanStats> results = actionPlanService.findActionPlanStats(projectKey, projectUserSession);
assertThat(results).hasSize(1);
- verify(authorizationDao).isAuthorizedComponentKey(anyString(), anyInt(), eq(UserRole.USER));
}
- @Test(expected = IllegalArgumentException.class)
- public void should_throw_exception_if_project_not_found_when_find_open_action_plan_stats() {
+ @Test(expected = NotFoundException.class)
+ public void throw_exception_if_project_not_found_when_find_open_action_plan_stats() {
when(resourceDao.getResource(any(ResourceQuery.class))).thenReturn(null);
- actionPlanService.findActionPlanStats("org.sonar.Sample", userSession);
+ actionPlanService.findActionPlanStats(projectKey, projectUserSession);
}
}
}
@Test
- public void should_execute_functions() {
+ public void execute_functions() {
Function function1 = mock(Function.class);
Function function2 = mock(Function.class);
}
@Test
- public void should_modify_issue_when_executing_a_function() {
+ public void modify_issue_when_executing_a_function() {
Function function = new TweetFunction();
UserSession userSession = mock(UserSession.class);
}
@Test
- public void should_inject_project_settings_when_executing_a_function() {
+ public void inject_project_settings_when_executing_a_function() {
Function function = new TweetFunction();
UserSession userSession = mock(UserSession.class);
}
@Test
- public void should_not_execute_function_if_issue_not_found() {
+ public void not_execute_function_if_issue_not_found() {
Function function = mock(Function.class);
IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.<Issue>emptyList());
}
@Test
- public void should_not_execute_function_if_action_not_found() {
+ public void not_execute_function_if_action_not_found() {
Function function = mock(Function.class);
Issue issue = new DefaultIssue().setKey("ABCD");
}
@Test
- public void should_not_execute_function_if_action_is_not_supported() {
+ public void not_execute_function_if_action_is_not_supported() {
Function function = mock(Function.class);
Issue issue = new DefaultIssue().setKey("ABCD");
}
@Test
- public void should_list_available_supported_actions() {
+ 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);
}
@Test
- public void should_list_available_actions_throw_exception_if_issue_not_found() {
+ 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);
}
@Test
- public void should_return_no_action() {
+ 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);
}
@Test
- public void should_get_project_settings(){
+ public void get_project_settings(){
Component project = mock(Component.class);
when(project.key()).thenReturn("struts");
package org.sonar.server.issue;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.issue.ActionPlan;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueQuery;
+import org.sonar.api.issue.action.Action;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.issue.internal.FieldDiffs;
import org.sonar.api.user.User;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
+@RunWith(MockitoJUnitRunner.class)
public class InternalRubyIssueServiceTest {
- InternalRubyIssueService service;
- IssueService issueService = mock(IssueService.class);
+ @Mock
+ IssueService issueService;
+
+ @Mock
IssueCommentService commentService = mock(IssueCommentService.class);
+
+ @Mock
IssueChangelogService changelogService = mock(IssueChangelogService.class);
+
+ @Mock
ActionPlanService actionPlanService = mock(ActionPlanService.class);
+
+ @Mock
ResourceDao resourceDao = mock(ResourceDao.class);
+
+ @Mock
IssueStatsFinder issueStatsFinder = mock(IssueStatsFinder.class);
+
+ @Mock
ActionService actionService = mock(ActionService.class);
+
+ @Mock
IssueFilterService issueFilterService = mock(IssueFilterService.class);
+
+ @Mock
IssueBulkChangeService issueBulkChangeService = mock(IssueBulkChangeService.class);
+ InternalRubyIssueService service;
+
@Before
public void setUp() {
ResourceDto project = new ResourceDto().setKey("org.sonar.Sample");
issueFilterService, issueBulkChangeService);
}
+ @Test
+ public void find_issue_assignees() throws Exception {
+ service.findIssueAssignees(ImmutableMap.<String, Object>of("issues", "ABCD"));
+ verify(issueStatsFinder).findIssueAssignees(any(IssueQuery.class));
+ }
+
+ @Test
+ public void list_transitions_by_issue_key() throws Exception {
+ service.listTransitions("ABCD");
+ verify(issueService).listTransitions(eq("ABCD"), any(UserSession.class));
+ }
+
+ @Test
+ public void list_transitions_by_issue() throws Exception {
+ Issue issue = new DefaultIssue().setKey("ABCD");
+ service.listTransitions(issue);
+ verify(issueService).listTransitions(eq(issue), any(UserSession.class));
+ }
+
+ @Test
+ public void list_status() throws Exception {
+ service.listStatus();
+ verify(issueService).listStatus();
+ }
+
+ @Test
+ public void list_resolutions() throws Exception {
+ assertThat(service.listResolutions()).isEqualTo(Issue.RESOLUTIONS);
+ }
+
@Test
public void list_plugin_actions() {
- service.listPluginActions();
- verify(actionService).listAllActions();
+ Action action = mock(Action.class);
+ when(action.key()).thenReturn("link-to-jira");
+
+ when(actionService.listAllActions()).thenReturn(newArrayList(action));
+
+ assertThat(service.listPluginActions()).containsOnly("link-to-jira");
}
- @Test
- public void should_create_action_plan() {
+ @Test
+ public void do_transition() throws Exception {
+ service.doTransition("ABCD", Issue.STATUS_RESOLVED);
+ verify(issueService).doTransition(eq("ABCD"), eq(Issue.STATUS_RESOLVED), any(UserSession.class));
+ }
+
+ @Test
+ public void create_action_plan() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", "Long term issues");
}
@Test
- public void should_update_action_plan() {
+ public void update_action_plan() {
when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(DefaultActionPlan.create("Long term"));
Map<String, String> parameters = newHashMap();
}
@Test
- public void should_update_action_plan_with_new_project() {
+ public void update_action_plan_with_new_project() {
when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(DefaultActionPlan.create("Long term").setProjectKey("org.sonar.MultiSample"));
Map<String, String> parameters = newHashMap();
}
@Test
- public void should_not_update_action_plan_when_action_plan_is_not_found() {
+ public void not_update_action_plan_when_action_plan_is_not_found() {
when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(null);
Result result = service.updateActionPlan("ABCD", null);
}
@Test
- public void should_delete_action_plan() {
+ public void delete_action_plan() {
when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(DefaultActionPlan.create("Long term"));
Result result = service.deleteActionPlan("ABCD");
}
@Test
- public void should_not_delete_action_plan_if_action_plan_not_found() {
+ public void not_delete_action_plan_if_action_plan_not_found() {
when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(null);
Result result = service.deleteActionPlan("ABCD");
}
@Test
- public void should_close_action_plan() {
+ public void close_action_plan() {
when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(DefaultActionPlan.create("Long term"));
Result result = service.closeActionPlan("ABCD");
}
@Test
- public void should_open_action_plan() {
+ public void open_action_plan() {
when(actionPlanService.findByKey(eq("ABCD"), any(UserSession.class))).thenReturn(DefaultActionPlan.create("Long term"));
Result result = service.openActionPlan("ABCD");
}
@Test
- public void should_get_error_on_action_plan_result_when_no_project() {
+ public void get_error_on_action_plan_result_when_no_project() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_action_plan_result_when_no_name() {
+ public void get_error_on_action_plan_result_when_no_name() {
Map<String, String> parameters = newHashMap();
parameters.put("name", null);
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_action_plan_result_when_name_is_too_long() {
+ public void get_error_on_action_plan_result_when_name_is_too_long() {
Map<String, String> parameters = newHashMap();
parameters.put("name", createLongString(201));
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_action_plan_result_when_description_is_too_long() {
+ public void get_error_on_action_plan_result_when_description_is_too_long() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", createLongString(1001));
}
@Test
- public void should_get_error_on_action_plan_result_when_dead_line_use_wrong_format() {
+ public void get_error_on_action_plan_result_when_dead_line_use_wrong_format() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_action_plan_result_when_dead_line_is_in_the_past() {
+ public void get_error_on_action_plan_result_when_dead_line_is_in_the_past() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_action_plan_result_when_name_is_already_used_for_project() {
+ public void get_error_on_action_plan_result_when_name_is_already_used_for_project() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_action_plan_result_when_project_not_found() {
+ public void get_error_on_action_plan_result_when_project_not_found() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", "Long term issues");
@Test
public void test_changelog_from_issue_key() throws Exception {
- IssueChangelog changelog = new IssueChangelog(Collections.<FieldDiffs> emptyList(), Collections.<User> emptyList());
+ IssueChangelog changelog = new IssueChangelog(Collections.<FieldDiffs>emptyList(), Collections.<User>emptyList());
when(changelogService.changelog(eq("ABCDE"))).thenReturn(changelog);
IssueChangelog result = service.changelog("ABCDE");
public void test_changelog_from_issue() throws Exception {
Issue issue = new DefaultIssue().setKey("ABCDE");
- IssueChangelog changelog = new IssueChangelog(Collections.<FieldDiffs> emptyList(), Collections.<User> emptyList());
+ IssueChangelog changelog = new IssueChangelog(Collections.<FieldDiffs>emptyList(), Collections.<User>emptyList());
when(changelogService.changelog(eq(issue))).thenReturn(changelog);
IssueChangelog result = service.changelog(issue);
}
@Test
- public void should_create_issue_filter() {
+ public void create_issue_filter() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", "Long term issues");
}
@Test
- public void should_update_issue_filter() {
+ public void update_issue_filter() {
Map<String, String> parameters = newHashMap();
parameters.put("id", "10");
parameters.put("name", "Long term");
}
@Test
- public void should_update_data() {
+ public void update_data() {
Map<String, Object> data = newHashMap();
service.updateIssueFilterQuery(10L, data);
verify(issueFilterService).updateFilterQuery(eq(10L), eq(data), any(UserSession.class));
}
@Test
- public void should_delete_issue_filter() {
+ public void delete_issue_filter() {
service.deleteIssueFilter(1L);
verify(issueFilterService).delete(eq(1L), any(UserSession.class));
}
@Test
- public void should_copy_issue_filter() {
+ public void copy_issue_filter() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Copy of Long term");
parameters.put("description", "Copy of Long term issues");
}
@Test
- public void should_get_error_on_create_issue_filter_result_when_no_name() {
+ public void get_error_on_create_issue_filter_result_when_no_name() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "");
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_create_issue_filter_result_when_name_is_too_long() {
+ public void get_error_on_create_issue_filter_result_when_name_is_too_long() {
Map<String, String> parameters = newHashMap();
parameters.put("name", createLongString(101));
parameters.put("description", "Long term issues");
}
@Test
- public void should_get_error_on_create_issue_filter_result_when_description_is_too_long() {
+ public void get_error_on_create_issue_filter_result_when_description_is_too_long() {
Map<String, String> parameters = newHashMap();
parameters.put("name", "Long term");
parameters.put("description", createLongString(4001));
}
@Test
- public void should_get_error_on_create_issue_filter_result_when_id_is_null_on_update() {
+ public void get_error_on_create_issue_filter_result_when_id_is_null_on_update() {
Map<String, String> parameters = newHashMap();
parameters.put("id", null);
parameters.put("name", "Long term");
}
@Test
- public void should_get_error_on_create_issue_filter_result_when_user_is_null_on_update() {
+ public void get_error_on_create_issue_filter_result_when_user_is_null_on_update() {
Map<String, String> parameters = newHashMap();
parameters.put("id", "10");
parameters.put("name", "All Open Issues");
}
@Test
- public void should_get_no_error_on_issue_filter_result_when_id_and_user_are_null_on_copy() {
+ public void get_no_error_on_issue_filter_result_when_id_and_user_are_null_on_copy() {
Map<String, String> parameters = newHashMap();
parameters.put("id", null);
parameters.put("name", "Long term");
}
@Test
- public void should_execute_issue_filter_from_issue_query() {
- service.execute(Maps.<String, Object> newHashMap());
+ public void execute_issue_filter_from_issue_query() {
+ service.execute(Maps.<String, Object>newHashMap());
verify(issueFilterService).execute(any(IssueQuery.class));
}
@Test
- public void should_execute_issue_filter_from_existing_filter() {
+ public void execute_issue_filter_from_existing_filter() {
Map<String, Object> props = newHashMap();
props.put("componentRoots", "struts");
props.put("statuses", "OPEN");
}
@Test
- public void should_serialize_filter_query() {
+ public void serialize_filter_query() {
Map<String, Object> props = newHashMap();
props.put("componentRoots", "struts");
service.serializeFilterQuery(props);
}
@Test
- public void should_deserialize_filter_query() {
+ public void deserialize_filter_query() {
DefaultIssueFilter issueFilter = new DefaultIssueFilter();
service.deserializeFilterQuery(issueFilter);
verify(issueFilterService).deserializeIssueFilterQuery(issueFilter);
}
@Test
- public void should_sanitize_filter_query() {
+ public void sanitize_filter_query() {
Map<String, Object> query = newHashMap();
query.put("statuses", "CLOSED");
query.put("resolved", true);
}
@Test
- public void should_find_user_issue_filters() {
+ public void find_user_issue_filters() {
service.findIssueFiltersForCurrentUser();
verify(issueFilterService).findByUser(any(UserSession.class));
}
@Test
- public void should_find_shared_issue_filters() {
+ public void find_shared_issue_filters() {
service.findSharedFiltersForCurrentUser();
verify(issueFilterService).findSharedFiltersWithoutUserFilters(any(UserSession.class));
}
@Test
- public void should_find_favourite_issue_filters() {
+ public void find_favourite_issue_filters() {
service.findFavouriteIssueFiltersForCurrentUser();
verify(issueFilterService).findFavoriteFilters(any(UserSession.class));
}
@Test
- public void should_toggle_favourite_issue_filter() {
+ public void toggle_favourite_issue_filter() {
service.toggleFavouriteIssueFilter(10L);
verify(issueFilterService).toggleFavouriteIssueFilter(eq(10L), any(UserSession.class));
}
@Test
- public void should_check_if_user_is_authorized_to_see_issue_filter() {
+ public void check_if_user_is_authorized_to_see_issue_filter() {
DefaultIssueFilter issueFilter = new DefaultIssueFilter();
service.isUserAuthorized(issueFilter);
verify(issueFilterService).getLoggedLogin(any(UserSession.class));
}
@Test
- public void should_check_if_user_can_share_issue_filter() {
+ public void check_if_user_can_share_issue_filter() {
service.canUserShareIssueFilter();
verify(issueFilterService).canShareFilter(any(UserSession.class));
}
@Test
- public void should_execute_bulk_change() {
+ public void execute_bulk_change() {
Map<String, Object> params = newHashMap();
params.put("issues", newArrayList("ABCD", "EFGH"));
params.put("actions", newArrayList("do_transition", "assign", "set_severity", "plan"));