import javax.annotation.CheckForNull;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
}
public DefaultIssueFilter find(Long id, UserSession userSession) {
- return findIssueFilterDto(id, userSession).toIssueFilter();
+ return findIssueFilterDto(id, getNotNullLogin(userSession)).toIssueFilter();
}
@CheckForNull
}
public List<DefaultIssueFilter> findByUser(UserSession userSession) {
- if (userSession.isLoggedIn() && userSession.login() != null) {
- return toIssueFilters(issueFilterDao.selectByUser(userSession.login()));
- }
- return Collections.emptyList();
+ return toIssueFilters(issueFilterDao.selectByUser(getNotNullLogin(userSession)));
}
public DefaultIssueFilter save(DefaultIssueFilter issueFilter, UserSession userSession) {
- verifyLoggedIn(userSession);
- issueFilter.setUser(userSession.login());
- validateFilter(issueFilter, userSession);
- return insertIssueFilter(issueFilter, userSession.login());
+ String user = getNotNullLogin(userSession);
+ issueFilter.setUser(user);
+ validateFilter(issueFilter, user);
+ return insertIssueFilter(issueFilter, user);
}
public DefaultIssueFilter update(DefaultIssueFilter issueFilter, UserSession userSession) {
- IssueFilterDto issueFilterDto = findIssueFilterDto(issueFilter.id(), userSession);
- verifyCurrentUserCanModifyFilter(issueFilterDto.toIssueFilter(), userSession);
- validateFilter(issueFilter, userSession);
+ String user = getNotNullLogin(userSession);
+ IssueFilterDto issueFilterDto = findIssueFilterDto(issueFilter.id(), user);
+ verifyCurrentUserCanModifyFilter(issueFilterDto.toIssueFilter(), user);
+ validateFilter(issueFilter, user);
issueFilterDao.update(IssueFilterDto.toIssueFilter(issueFilter));
return issueFilter;
}
public DefaultIssueFilter updateFilterQuery(Long issueFilterId, Map<String, Object> filterQuery, UserSession userSession) {
- IssueFilterDto issueFilterDto = findIssueFilterDto(issueFilterId, userSession);
- verifyCurrentUserCanModifyFilter(issueFilterDto.toIssueFilter(), userSession);
+ String user = getNotNullLogin(userSession);
+ IssueFilterDto issueFilterDto = findIssueFilterDto(issueFilterId, user);
+ verifyCurrentUserCanModifyFilter(issueFilterDto.toIssueFilter(), user);
DefaultIssueFilter issueFilter = issueFilterDto.toIssueFilter();
issueFilter.setData(serializeFilterQuery(filterQuery));
}
public void delete(Long issueFilterId, UserSession userSession) {
- IssueFilterDto issueFilterDto = findIssueFilterDto(issueFilterId, userSession);
- verifyCurrentUserCanModifyFilter(issueFilterDto.toIssueFilter(), userSession);
+ String user = getNotNullLogin(userSession);
+ IssueFilterDto issueFilterDto = findIssueFilterDto(issueFilterId, user);
+ verifyCurrentUserCanModifyFilter(issueFilterDto.toIssueFilter(), user);
deleteFavouriteIssueFilters(issueFilterDto);
issueFilterDao.delete(issueFilterId);
}
public DefaultIssueFilter copy(Long issueFilterIdToCopy, DefaultIssueFilter issueFilter, UserSession userSession) {
- IssueFilterDto issueFilterDtoToCopy = findIssueFilterDto(issueFilterIdToCopy, userSession);
- issueFilter.setUser(userSession.login());
+ String user = getNotNullLogin(userSession);
+ IssueFilterDto issueFilterDtoToCopy = findIssueFilterDto(issueFilterIdToCopy, user);
+ issueFilter.setUser(user);
issueFilter.setData(issueFilterDtoToCopy.getData());
- validateFilter(issueFilter, userSession);
+ validateFilter(issueFilter, user);
- return insertIssueFilter(issueFilter, userSession.login());
+ return insertIssueFilter(issueFilter, user);
}
public List<DefaultIssueFilter> findSharedFilters(UserSession userSession) {
- if (userSession.isLoggedIn() && userSession.login() != null) {
- return toIssueFilters(issueFilterDao.selectSharedWithoutUserFilters(userSession.login()));
- }
- return Collections.emptyList();
+ return toIssueFilters(issueFilterDao.selectSharedWithoutUserFilters(getNotNullLogin(userSession)));
}
public List<DefaultIssueFilter> findFavoriteFilters(UserSession userSession) {
- if (userSession.isLoggedIn() && userSession.login() != null) {
- return toIssueFilters(issueFilterDao.selectByUserWithOnlyFavoriteFilters(userSession.login()));
- }
- return Collections.emptyList();
+ return toIssueFilters(issueFilterDao.selectByUserWithOnlyFavoriteFilters(getNotNullLogin(userSession)));
}
public void toggleFavouriteIssueFilter(Long issueFilterId, UserSession userSession) {
- findIssueFilterDto(issueFilterId, userSession);
- IssueFilterFavouriteDto issueFilterFavouriteDto = findFavouriteIssueFilter(userSession.login(), issueFilterId);
+ String user = getNotNullLogin(userSession);
+ findIssueFilterDto(issueFilterId, user);
+ IssueFilterFavouriteDto issueFilterFavouriteDto = findFavouriteIssueFilter(user, issueFilterId);
if (issueFilterFavouriteDto == null) {
- addFavouriteIssueFilter(issueFilterId, userSession.login());
+ addFavouriteIssueFilter(issueFilterId, user);
} else {
deleteFavouriteIssueFilter(issueFilterFavouriteDto);
}
return issueFilterSerializer.deserialize(issueFilter.data());
}
- private IssueFilterDto findIssueFilterDto(Long id, UserSession userSession) {
- verifyLoggedIn(userSession);
+ private IssueFilterDto findIssueFilterDto(Long id, String user) {
IssueFilterDto issueFilterDto = issueFilterDao.selectById(id);
if (issueFilterDto == null) {
// TODO throw 404
throw new IllegalArgumentException("Filter not found: " + id);
}
- verifyCurrentUserCanReadFilter(issueFilterDto.toIssueFilter(), userSession);
+ verifyCurrentUserCanReadFilter(issueFilterDto.toIssueFilter(), user);
return issueFilterDto;
}
- void verifyLoggedIn(UserSession userSession) {
- if (!userSession.isLoggedIn() && userSession.login() != null) {
+ String getNotNullLogin(UserSession userSession) {
+ String user = userSession.login();
+ if (!userSession.isLoggedIn() && user != null) {
throw new IllegalStateException("User is not logged in");
}
+ return user;
}
- void verifyCurrentUserCanReadFilter(DefaultIssueFilter issueFilter, UserSession userSession) {
- if (!issueFilter.user().equals(userSession.login()) && !issueFilter.shared()) {
+ void verifyCurrentUserCanReadFilter(DefaultIssueFilter issueFilter, String user) {
+ if (!issueFilter.user().equals(user) && !issueFilter.shared()) {
// TODO throw unauthorized
throw new IllegalStateException("User is not authorized to read this filter");
}
}
- private void verifyCurrentUserCanModifyFilter(DefaultIssueFilter issueFilter, UserSession userSession) {
- if (!issueFilter.user().equals(userSession.login()) && !isAdmin(userSession.login())) {
+ private void verifyCurrentUserCanModifyFilter(DefaultIssueFilter issueFilter, String user) {
+ if (!issueFilter.user().equals(user) && !isAdmin(user)) {
// TODO throw unauthorized
throw new IllegalStateException("User is not authorized to modify this filter");
}
}
- private void validateFilter(DefaultIssueFilter issueFilter, UserSession userSession) {
- if (issueFilterDao.selectByNameAndUser(issueFilter.name(), userSession.login(), issueFilter.id()) != null) {
+ private void validateFilter(DefaultIssueFilter issueFilter, String user) {
+ if (issueFilterDao.selectByNameAndUser(issueFilter.name(), user, issueFilter.id()) != null) {
throw new IllegalArgumentException("Name already exists");
}
- if (issueFilter.shared() && issueFilterDao.selectSharedWithoutUserFiltersByName(issueFilter.name(), userSession.login(), issueFilter.id()) != null) {
+ if (issueFilter.shared() && issueFilterDao.selectSharedWithoutUserFiltersByName(issueFilter.name(), user, issueFilter.id()) != null) {
throw new IllegalArgumentException("Other users already share filters with the same name");
}
}
import java.util.Collections;
import java.util.Map;
+import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.eq;
public void should_check_is_user_is_authorized_to_see_issue_filter() {
DefaultIssueFilter issueFilter = new DefaultIssueFilter();
service.isUserAuthorized(issueFilter);
- verify(issueFilterService).verifyLoggedIn(any(UserSession.class));
- verify(issueFilterService).verifyCurrentUserCanReadFilter(eq(issueFilter), any(UserSession.class));
+ verify(issueFilterService).getNotNullLogin(any(UserSession.class));
+ verify(issueFilterService).verifyCurrentUserCanReadFilter(eq(issueFilter), anyString());
}
@Test
public void should_execute_bulk_change() {
- service.executeBulkChange(Maps.<String, Object>newHashMap());
+ Map<String, Object> params = newHashMap();
+ params.put("issues", newArrayList("ABCD", "EFGH"));
+ params.put("assignee", "arthur");
+ service.executeBulkChange(params);
verify(issueBulkChangeService).execute(any(IssueBulkChangeQuery.class), any(UserSession.class));
}
public void should_no_execute_bulk_change_if_unexpected_error() {
doThrow(new RuntimeException("Error")).when(issueBulkChangeService).execute(any(IssueBulkChangeQuery.class), any(UserSession.class));
- Result result = service.executeBulkChange(Maps.<String, Object>newHashMap());
+ Map<String, Object> params = newHashMap();
+ params.put("issues", newArrayList("ABCD", "EFGH"));
+ params.put("assignee", "arthur");
+ Result result = service.executeBulkChange(params);
assertThat(result.ok()).isFalse();
assertThat(((Result.Message) result.errors().get(0)).text()).contains("Error");
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.Test;
+
+import java.util.Collections;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+
+public class IssueBulkChangeQueryTest {
+
+ @Test
+ public void test_build(){
+ IssueBulkChangeQuery issueBulkChangeQuery = IssueBulkChangeQuery.builder().issueKeys(newArrayList("ABCD", "EFGH")).assignee("perceval").build();
+ assertThat(issueBulkChangeQuery.issueKeys()).isNotNull();
+ assertThat(issueBulkChangeQuery.assignee()).isNotNull();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void fail_to_build_if_no_issue(){
+ IssueBulkChangeQuery.builder().assignee("perceval").build();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void fail_to_build_if_issues_are_empty(){
+ IssueBulkChangeQuery.builder().issueKeys(Collections.<String>emptyList()).assignee("perceval").build();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void fail_to_build_if_no_action(){
+ IssueBulkChangeQuery.builder().issueKeys(newArrayList("ABCD", "EFGH")).build();
+ }
+
+}