]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7290 Simplify and rename ActionService to ActionFinder
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 12 Dec 2016 08:47:36 +0000 (09:47 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 13 Dec 2016 15:12:01 +0000 (16:12 +0100)
server/sonar-server/src/main/java/org/sonar/server/issue/ActionFinder.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/issue/IssueCommentService.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/OperationResponseWriter.java
server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/test/java/org/sonar/server/issue/ActionFinderTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java [deleted file]

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 (file)
index 0000000..79476e3
--- /dev/null
@@ -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<String> listAvailableActions(IssueDto issue) {
+    List<String> 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 (file)
index 7105aa1..0000000
+++ /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<String> listAvailableActions(String issueKey) {
-    DbSession session = dbClient.openSession(false);
-    try {
-      return listAvailableActions(issueService.getByKeyForUpdate(session, issueKey).toDefaultIssue());
-    } finally {
-      dbClient.closeSession(session);
-    }
-  }
-
-  public List<String> listAvailableActions(Issue issue) {
-    List<String> 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;
-  }
-
-}
index b39f3c89024c20f95f3f5791e970c0c6e3ae4d13..ddb97357d3f32cd8db596b4ddc491d8a8fc11dfe 100644 (file)
@@ -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");
index b0c26809b18c0f7a20ae0e7696e24994be88e35d..092c9d2f5a7b07f75310c15b5ab7287426af802c 100644 (file)
@@ -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);
   }
index 4123527f2c9467796281279e84b1f848540c9520..bcd8906f3ae4fc891f3dacfb52cc61d61df5347f 100644 (file)
@@ -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.<RuleKey>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
index bd21067dd59b64be57a424bd72ab38810ac800db..606982791a83caf44f0130dd0f8d92e5ea2dfe94 100644 (file)
@@ -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 (file)
index 0000000..f6b1729
--- /dev/null
@@ -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 (file)
index cd042c4..0000000
+++ /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();
-  }
-
-}