]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4421 Improve issues bulk change errors handling
authorJulien Lancelot <julien.lancelot@gmail.com>
Thu, 4 Jul 2013 12:28:18 +0000 (14:28 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Thu, 4 Jul 2013 12:28:18 +0000 (14:28 +0200)
sonar-server/src/main/java/org/sonar/server/issue/IssueBulkChangeService.java
sonar-server/src/test/java/org/sonar/server/issue/IssueBulkChangeServiceTest.java

index cc23710a2c662124ea83e07f3e65f5040f61b4bc..71c72db08a15ae4923f4722d96a2ee0828e223e0 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.issue.internal.IssueChangeContext;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.issue.IssueNotifications;
 import org.sonar.core.issue.db.IssueStorage;
+import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.user.UserSession;
 
 import javax.annotation.CheckForNull;
@@ -59,7 +60,7 @@ public class IssueBulkChangeService {
 
   public IssueBulkChangeResult execute(IssueBulkChangeQuery issueBulkChangeQuery, UserSession userSession) {
     LOG.debug("BulkChangeQuery : {}", issueBulkChangeQuery);
-    verifyLoggedIn(userSession);
+    userSession.checkLoggedIn();
 
     IssueBulkChangeResult result = new IssueBulkChangeResult();
     IssueQueryResult issueQueryResult = issueFinder.find(IssueQuery.builder().issueKeys(issueBulkChangeQuery.issues()).pageSize(-1).requiredRole(UserRole.USER).build());
@@ -68,7 +69,7 @@ public class IssueBulkChangeService {
     for (String actionName : issueBulkChangeQuery.actions()) {
       Action action = getAction(actionName);
       if (action == null) {
-        throw new IllegalArgumentException("The action : '"+ actionName + "' is unknown");
+        throw new BadRequestException("The action : '"+ actionName + "' is unknown");
       }
       action.verify(issueBulkChangeQuery.properties(actionName), issues, userSession);
       bulkActions.add(action);
@@ -107,13 +108,6 @@ public class IssueBulkChangeService {
     }, null);
   }
 
-  private void verifyLoggedIn(UserSession userSession) {
-    if (!userSession.isLoggedIn()) {
-      // must be logged
-      throw new IllegalStateException("User is not logged in");
-    }
-  }
-
   static class ActionContext implements Action.Context {
     private final Issue issue;
     private final IssueChangeContext changeContext;
index 0d42dc13c8c92a9ee976b2f85bf8fd143d933546..09bc9e1fd2654fb249e84152932cbc165e640567 100644 (file)
@@ -31,6 +31,9 @@ import org.sonar.api.issue.internal.IssueChangeContext;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.issue.IssueNotifications;
 import org.sonar.core.issue.db.IssueStorage;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.user.MockUserSession;
 import org.sonar.server.user.UserSession;
 
 import java.util.List;
@@ -51,7 +54,7 @@ public class IssueBulkChangeServiceTest {
   private IssueNotifications issueNotifications = mock(IssueNotifications.class);
 
   private IssueQueryResult issueQueryResult = mock(IssueQueryResult.class);
-  private UserSession userSession = mock(UserSession.class);
+  private UserSession userSession = MockUserSession.create().setLogin("john").setUserId(10);
   private DefaultIssue issue = new DefaultIssue().setKey("ABCD");
 
   private IssueBulkChangeService service;
@@ -61,10 +64,6 @@ public class IssueBulkChangeServiceTest {
 
   @Before
   public void before() {
-    when(userSession.isLoggedIn()).thenReturn(true);
-    when(userSession.userId()).thenReturn(10);
-    when(userSession.login()).thenReturn("fred");
-
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
     when(issueQueryResult.issues()).thenReturn(newArrayList((Issue) issue));
 
@@ -208,7 +207,7 @@ public class IssueBulkChangeServiceTest {
 
   @Test
   public void should_fail_if_user_not_logged() {
-    when(userSession.isLoggedIn()).thenReturn(false);
+    userSession = MockUserSession.create().setLogin(null);
 
     Map<String, Object> properties = newHashMap();
     properties.put("issues", "ABCD");
@@ -219,7 +218,7 @@ public class IssueBulkChangeServiceTest {
       service.execute(issueBulkChangeQuery, userSession);
       fail();
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("User is not logged in");
+      assertThat(e).isInstanceOf(UnauthorizedException.class);
     }
     verifyZeroInteractions(issueStorage);
     verifyZeroInteractions(issueNotifications);
@@ -236,7 +235,7 @@ public class IssueBulkChangeServiceTest {
       service.execute(issueBulkChangeQuery, userSession);
       fail();
     } catch (Exception e) {
-      assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The action : 'unknown' is unknown");
+      assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("The action : 'unknown' is unknown");
     }
     verifyZeroInteractions(issueStorage);
     verifyZeroInteractions(issueNotifications);