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;
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());
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);
}, 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;
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;
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;
@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));
@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");
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);
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);