From: Julien Lancelot Date: Mon, 12 Dec 2016 08:47:36 +0000 (+0100) Subject: SONAR-7290 Simplify and rename ActionService to ActionFinder X-Git-Tag: 6.3-RC1~784 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4f4c4b67f15d9f61bb6e04f57e60db62d326af8b;p=sonarqube.git SONAR-7290 Simplify and rename ActionService to ActionFinder --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ActionFinder.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ActionFinder.java new file mode 100644 index 00000000000..79476e3f3b0 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ActionFinder.java @@ -0,0 +1,61 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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 java.util.List; +import org.sonar.db.issue.IssueDto; +import org.sonar.server.user.UserSession; + +import static com.google.common.collect.Lists.newArrayList; +import static org.sonar.api.web.UserRole.ISSUE_ADMIN; + +/** + * @since 3.6 + */ +public class ActionFinder { + + private final UserSession userSession; + + public ActionFinder(UserSession userSession) { + this.userSession = userSession; + } + + public List listAvailableActions(IssueDto issue) { + List availableActions = newArrayList(); + String login = userSession.getLogin(); + if (login != null) { + availableActions.add("comment"); + if (issue.getResolution() == null) { + availableActions.add("assign"); + availableActions.add("set_tags"); + availableActions.add("set_type"); + if (!login.equals(issue.getAssignee())) { + availableActions.add("assign_to_me"); + } + String projectUuid = issue.getProjectUuid(); + if (projectUuid != null && userSession.hasComponentUuidPermission(ISSUE_ADMIN, projectUuid)) { + availableActions.add("set_severity"); + } + } + } + return availableActions; + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java deleted file mode 100644 index 7105aa104f7..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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 java.util.List; -import org.sonar.api.issue.Issue; -import org.sonar.api.server.ServerSide; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.server.user.UserSession; - -import static com.google.common.collect.Lists.newArrayList; -import static org.sonar.api.web.UserRole.ISSUE_ADMIN; - -/** - * @since 3.6 - */ -@ServerSide -public class ActionService { - - private final DbClient dbClient; - private final UserSession userSession; - private final IssueService issueService; - - public ActionService(DbClient dbClient, UserSession userSession, IssueService issueService) { - this.dbClient = dbClient; - this.userSession = userSession; - this.issueService = issueService; - } - - public List listAvailableActions(String issueKey) { - DbSession session = dbClient.openSession(false); - try { - return listAvailableActions(issueService.getByKeyForUpdate(session, issueKey).toDefaultIssue()); - } finally { - dbClient.closeSession(session); - } - } - - public List listAvailableActions(Issue issue) { - List availableActions = newArrayList(); - String login = userSession.getLogin(); - if (login != null) { - availableActions.add("comment"); - if (issue.resolution() == null) { - availableActions.add("assign"); - availableActions.add("set_tags"); - availableActions.add("set_type"); - if (!login.equals(issue.assignee())) { - availableActions.add("assign_to_me"); - } - String projectUuid = issue.projectUuid(); - if (projectUuid != null && userSession.hasComponentUuidPermission(ISSUE_ADMIN, projectUuid)) { - availableActions.add("set_severity"); - } - } - } - return availableActions; - } - -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java index b39f3c89024..ddb97357d3f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java @@ -144,10 +144,6 @@ public class IssueCommentService { return comment; } - public boolean canEditOrDelete(IssueChangeDto dto) { - return userSession.isLoggedIn() && userSession.getLogin().equals(dto.getUserLogin()); - } - private void verifyLoggedIn(UserSession userSession) { if (!userSession.isLoggedIn()) { throw new UnauthorizedException("User is not logged in"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/OperationResponseWriter.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/OperationResponseWriter.java index b0c26809b18..092c9d2f5a7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/OperationResponseWriter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/OperationResponseWriter.java @@ -37,12 +37,12 @@ public class OperationResponseWriter { this.format = format; } - public void write(String issueKey, Request request, Response response) throws Exception { + public void write(String issueKey, Request request, Response response) { SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector( ALL_ADDITIONAL_FIELDS, singletonList(issueKey)); SearchResponseData data = loader.load(collector, null); - Issues.Operation responseBody = this.format.formatOperation(data); + Issues.Operation responseBody = format.formatOperation(data); WsUtils.writeProtobuf(responseBody, request, response); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java index 4123527f2c9..bcd8906f3ae 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java @@ -37,9 +37,9 @@ import org.sonar.db.issue.IssueChangeDto; import org.sonar.db.issue.IssueDto; import org.sonar.db.protobuf.DbIssues; import org.sonar.server.es.Facets; -import org.sonar.server.issue.ActionService; -import org.sonar.server.issue.IssueCommentService; +import org.sonar.server.issue.ActionFinder; import org.sonar.server.issue.TransitionService; +import org.sonar.server.user.UserSession; import org.sonarqube.ws.client.issue.IssuesWsParameters; import static com.google.common.collect.Lists.newArrayList; @@ -54,15 +54,15 @@ import static org.sonar.server.issue.ws.SearchAdditionalField.USERS; */ public class SearchResponseLoader { + private final UserSession userSession; private final DbClient dbClient; - private final ActionService actionService; - private final IssueCommentService commentService; + private final ActionFinder actionService; private final TransitionService transitionService; - public SearchResponseLoader(DbClient dbClient, ActionService actionService, IssueCommentService commentService, TransitionService transitionService) { + public SearchResponseLoader(UserSession userSession, DbClient dbClient, ActionFinder actionService, TransitionService transitionService) { + this.userSession = userSession; this.dbClient = dbClient; this.actionService = actionService; - this.commentService = commentService; this.transitionService = transitionService; } @@ -101,13 +101,17 @@ public class SearchResponseLoader { result.setComments(comments); for (IssueChangeDto comment : comments) { collector.add(USERS, comment.getUserLogin()); - if (commentService.canEditOrDelete(comment)) { + if (canEditOrDelete(comment)) { result.addUpdatableComment(comment.getKey()); } } } } + private boolean canEditOrDelete(IssueChangeDto dto) { + return userSession.isLoggedIn() && userSession.getLogin().equals(dto.getUserLogin()); + } + private void loadRules(Collector collector, DbSession dbSession, SearchResponseData result) { if (collector.contains(RULES)) { result.setRules(dbClient.ruleDao().selectByKeys(dbSession, collector.get(RULES))); @@ -131,7 +135,7 @@ public class SearchResponseLoader { for (IssueDto dto : result.getIssues()) { // so that IssueDto can be used. if (collector.contains(ACTIONS)) { - result.addActions(dto.getKey(), actionService.listAvailableActions(dto.toDefaultIssue())); + result.addActions(dto.getKey(), actionService.listAvailableActions(dto)); } if (collector.contains(TRANSITIONS)) { // TODO workflow and action engines must not depend on org.sonar.api.issue.Issue but on a generic interface diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index bd21067dd59..606982791a8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -57,7 +57,7 @@ import org.sonar.server.email.ws.EmailsWsModule; import org.sonar.server.es.IndexCreator; import org.sonar.server.es.IndexDefinitions; import org.sonar.server.event.NewAlerts; -import org.sonar.server.issue.ActionService; +import org.sonar.server.issue.ActionFinder; import org.sonar.server.issue.AddTagsAction; import org.sonar.server.issue.AssignAction; import org.sonar.server.issue.CommentAction; @@ -404,7 +404,7 @@ public class PlatformLevel4 extends PlatformLevel { IssueCommentService.class, InternalRubyIssueService.class, IssueChangelogService.class, - ActionService.class, + ActionFinder.class, IssueBulkChangeService.class, WsResponseCommonFormat.class, IssueWsModule.class, diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ActionFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ActionFinderTest.java new file mode 100644 index 00000000000..f6b172913c8 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ActionFinderTest.java @@ -0,0 +1,86 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.issue.IssueDto; +import org.sonar.server.tester.UserSessionRule; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.issue.Issue.RESOLUTION_FIXED; +import static org.sonar.api.web.UserRole.ISSUE_ADMIN; +import static org.sonar.db.component.ComponentTesting.newFileDto; +import static org.sonar.db.component.ComponentTesting.newProjectDto; +import static org.sonar.db.rule.RuleTesting.newXooX1; +import static org.sonar.server.issue.IssueTesting.newDto; + +public class ActionFinderTest { + + static final String PROJECT_KEY = "PROJECT_KEY"; + static final String PROJECT_UUID = "PROJECT_UUID"; + + static final String ISSUE_KEY = "ISSUE_KEY"; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public UserSessionRule userSession = UserSessionRule.standalone().login("arthur"); + + private ComponentDto project = newProjectDto(PROJECT_UUID).setKey(PROJECT_KEY); + private IssueDto issue = newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY); + + private ActionFinder underTest = new ActionFinder(userSession); + + @Test + public void return_provided_actions_without_set_severity_when_not_issue_admin() { + assertThat(underTest.listAvailableActions(issue)).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me"); + } + + @Test + public void return_provided_actions_with_set_severity_when_issue_admin() { + userSession.addProjectUuidPermissions(ISSUE_ADMIN, PROJECT_UUID); + assertThat(underTest.listAvailableActions(issue)).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me", "set_severity"); + } + + @Test + public void return_no_actions_when_not_logged() { + userSession.anonymous(); + assertThat(underTest.listAvailableActions(issue)).isEmpty(); + } + + @Test + public void doest_not_return_assign_to_me_action_when_issue_already_assigned_to_user() { + + userSession.login("julien"); + IssueDto issue = newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setAssignee("julien"); + assertThat(underTest.listAvailableActions(issue)).doesNotContain("assign_to_me"); + } + + @Test + public void return_only_comment_action_when_issue_has_a_resolution() { + IssueDto issue = newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setResolution(RESOLUTION_FIXED); + assertThat(underTest.listAvailableActions(issue)).containsOnly("comment"); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java deleted file mode 100644 index cd042c45c3d..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.issue.IssueDto; -import org.sonar.server.tester.UserSessionRule; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.api.issue.Issue.RESOLUTION_FIXED; -import static org.sonar.api.web.UserRole.ISSUE_ADMIN; -import static org.sonar.db.component.ComponentTesting.newFileDto; -import static org.sonar.db.component.ComponentTesting.newProjectDto; -import static org.sonar.db.rule.RuleTesting.newXooX1; - -public class ActionServiceTest { - - static final String PROJECT_KEY = "PROJECT_KEY"; - static final String PROJECT_UUID = "PROJECT_UUID"; - - static final String ISSUE_KEY = "ISSUE_KEY"; - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Rule - public UserSessionRule userSession = UserSessionRule.standalone().login("arthur"); - - DbClient dbClient = mock(DbClient.class); - DbSession session = mock(DbSession.class); - - IssueService issueService = mock(IssueService.class); - ActionService underTest; - - ComponentDto project; - IssueDto issue; - - @Before - public void before() { - when(dbClient.openSession(false)).thenReturn(session); - - project = newProjectDto(PROJECT_UUID).setKey(PROJECT_KEY); - issue = IssueTesting.newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY); - - underTest = new ActionService(dbClient, userSession, issueService); - } - - @Test - public void return_provided_actions_without_set_severity_when_not_issue_admin() { - assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me"); - } - - @Test - public void return_provided_actions_with_set_severity_when_issue_admin() { - userSession.addProjectUuidPermissions(ISSUE_ADMIN, PROJECT_UUID); - assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me", "set_severity"); - } - - @Test - public void return_no_actions_when_not_logged() { - userSession.anonymous(); - assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).isEmpty(); - } - - @Test - public void doest_not_return_assign_to_me_action_when_issue_already_assigned_to_user() { - userSession.login("julien"); - IssueDto issue = IssueTesting.newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setAssignee("julien"); - assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).doesNotContain("assign_to_me"); - } - - @Test - public void return_only_comment_action_when_issue_has_a_resolution() { - IssueDto issue = IssueTesting.newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setResolution(RESOLUTION_FIXED); - assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).containsOnly("comment"); - } - - @Test - public void return_actions_by_issue_key() { - when(issueService.getByKeyForUpdate(session, ISSUE_KEY)).thenReturn(issue); - assertThat(underTest.listAvailableActions(ISSUE_KEY)).isNotEmpty(); - } - -}