mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
Implement simple search queries for users, groups and repositories
This commit is contained in:
@@ -12,6 +12,7 @@ import sonia.scm.Manager;
|
||||
import sonia.scm.api.rest.resources.Simple;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -24,18 +25,20 @@ public class CollectionResourceManagerAdapterTest {
|
||||
private Manager<Simple> manager;
|
||||
@Captor
|
||||
private ArgumentCaptor<Comparator<Simple>> comparatorCaptor;
|
||||
@Captor
|
||||
private ArgumentCaptor<Predicate<Simple>> filterCaptor;
|
||||
|
||||
private CollectionResourceManagerAdapter<Simple, HalRepresentation> abstractManagerResource;
|
||||
|
||||
@Before
|
||||
public void captureComparator() {
|
||||
when(manager.getPage(comparatorCaptor.capture(), eq(0), eq(1))).thenReturn(null);
|
||||
when(manager.getPage(filterCaptor.capture(), comparatorCaptor.capture(), eq(0), eq(1))).thenReturn(null);
|
||||
abstractManagerResource = new SimpleManagerResource();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAcceptDefaultSortByParameter() {
|
||||
abstractManagerResource.getAll(0, 1, null, true, r -> null);
|
||||
abstractManagerResource.getAll(0, 1, x -> true, null, true, r -> null);
|
||||
|
||||
Comparator<Simple> comparator = comparatorCaptor.getValue();
|
||||
assertTrue(comparator.compare(new Simple("1", null), new Simple("2", null)) > 0);
|
||||
@@ -43,7 +46,7 @@ public class CollectionResourceManagerAdapterTest {
|
||||
|
||||
@Test
|
||||
public void shouldAcceptValidSortByParameter() {
|
||||
abstractManagerResource.getAll(0, 1, "data", true, r -> null);
|
||||
abstractManagerResource.getAll(0, 1, x -> true, "data", true, r -> null);
|
||||
|
||||
Comparator<Simple> comparator = comparatorCaptor.getValue();
|
||||
assertTrue(comparator.compare(new Simple("", "1"), new Simple("", "2")) > 0);
|
||||
@@ -51,7 +54,7 @@ public class CollectionResourceManagerAdapterTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldFailForIllegalSortByParameter() {
|
||||
abstractManagerResource.getAll(0, 1, "x", true, r -> null);
|
||||
abstractManagerResource.getAll(0, 1, x -> true, "x", true, r -> null);
|
||||
}
|
||||
|
||||
private class SimpleManagerResource extends CollectionResourceManagerAdapter<Simple, HalRepresentation> {
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.PageResult;
|
||||
@@ -30,9 +31,11 @@ import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -67,8 +70,10 @@ public class GroupRootResourceTest {
|
||||
@InjectMocks
|
||||
private PermissionCollectionToDtoMapper permissionCollectionToDtoMapper;
|
||||
|
||||
|
||||
private ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
|
||||
@Captor
|
||||
private ArgumentCaptor<Group> groupCaptor;
|
||||
@Captor
|
||||
private ArgumentCaptor<Predicate<Group>> filterCaptor;
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() {
|
||||
@@ -77,7 +82,7 @@ public class GroupRootResourceTest {
|
||||
doNothing().when(groupManager).modify(groupCaptor.capture());
|
||||
|
||||
Group group = createDummyGroup();
|
||||
when(groupManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(singletonList(group), 1));
|
||||
when(groupManager.getPage(filterCaptor.capture(), any(), eq(0), eq(10))).thenReturn(new PageResult<>(singletonList(group), 1));
|
||||
when(groupManager.get("admin")).thenReturn(group);
|
||||
|
||||
GroupCollectionToDtoMapper groupCollectionToDtoMapper = new GroupCollectionToDtoMapper(groupToDtoMapper, resourceLinks);
|
||||
@@ -317,6 +322,23 @@ public class GroupRootResourceTest {
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/groups/admin\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateFilterForSearch() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupRootResource.GROUPS_PATH_V2 + "?q=One");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
Group group = new Group("xml", "someone");
|
||||
assertTrue(filterCaptor.getValue().test(group));
|
||||
group.setName("nothing");
|
||||
group.setDescription("Someone");
|
||||
assertTrue(filterCaptor.getValue().test(group));
|
||||
group.setDescription("Nobody");
|
||||
assertFalse(filterCaptor.getValue().test(group));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetPermissionLink() throws URISyntaxException, UnsupportedEncodingException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupRootResource.GROUPS_PATH_V2 + "admin");
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.PageResult;
|
||||
@@ -31,6 +32,7 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.stream.Stream.of;
|
||||
@@ -42,6 +44,7 @@ import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_PRECONDITION_FAILED;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyObject;
|
||||
@@ -78,6 +81,8 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
@Mock
|
||||
private ScmPathInfo uriInfo;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Predicate<Repository>> filterCaptor;
|
||||
|
||||
private final URI baseUri = URI.create("/");
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
@@ -150,7 +155,7 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
@Test
|
||||
public void shouldGetAll() throws URISyntaxException, UnsupportedEncodingException {
|
||||
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));
|
||||
when(repositoryManager.getPage(any(), eq(0), eq(10))).thenReturn(singletonPageResult);
|
||||
when(repositoryManager.getPage(any(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
@@ -161,6 +166,22 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
|
||||
assertTrue(response.getContentAsString().contains("\"name\":\"repo\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateFilterForSearch() throws URISyntaxException {
|
||||
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));
|
||||
when(repositoryManager.getPage(filterCaptor.capture(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "?q=Rep");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(filterCaptor.getValue().test(new Repository("x", "git", "all_repos", "x")));
|
||||
assertTrue(filterCaptor.getValue().test(new Repository("x", "git", "x", "repository")));
|
||||
assertFalse(filterCaptor.getValue().test(new Repository("rep", "rep", "x", "x")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleUpdateForNotExistingRepository() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/repository-test-update.json");
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.ContextEntry;
|
||||
@@ -30,6 +31,7 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -72,7 +74,11 @@ public class UserRootResourceTest {
|
||||
@InjectMocks
|
||||
private PermissionCollectionToDtoMapper permissionCollectionToDtoMapper;
|
||||
|
||||
private ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
|
||||
@Captor
|
||||
private ArgumentCaptor<User> userCaptor;
|
||||
@Captor
|
||||
private ArgumentCaptor<Predicate<User>> filterCaptor;
|
||||
|
||||
private User originalUser;
|
||||
|
||||
@Before
|
||||
@@ -333,7 +339,7 @@ public class UserRootResourceTest {
|
||||
@Test
|
||||
public void shouldCreatePageForOnePageOnly() throws URISyntaxException, UnsupportedEncodingException {
|
||||
PageResult<User> singletonPageResult = createSingletonPageResult(1);
|
||||
when(userManager.getPage(any(), eq(0), eq(10))).thenReturn(singletonPageResult);
|
||||
when(userManager.getPage(any(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + UserRootResource.USERS_PATH_V2);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
@@ -349,7 +355,7 @@ public class UserRootResourceTest {
|
||||
@Test
|
||||
public void shouldCreatePageForMultiplePages() throws URISyntaxException, UnsupportedEncodingException {
|
||||
PageResult<User> singletonPageResult = createSingletonPageResult(3);
|
||||
when(userManager.getPage(any(), eq(1), eq(1))).thenReturn(singletonPageResult);
|
||||
when(userManager.getPage(any(), any(), eq(1), eq(1))).thenReturn(singletonPageResult);
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + UserRootResource.USERS_PATH_V2 + "?page=1&pageSize=1");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
@@ -364,6 +370,28 @@ public class UserRootResourceTest {
|
||||
assertTrue(response.getContentAsString().contains("\"last\":{\"href\":\"/v2/users/?page=2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateFilterForSearch() throws URISyntaxException {
|
||||
PageResult<User> singletonPageResult = createSingletonPageResult(1);
|
||||
when(userManager.getPage(filterCaptor.capture(), any(), eq(0), eq(10))).thenReturn(singletonPageResult);
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + UserRootResource.USERS_PATH_V2 + "?q=One");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||
User user = new User("Someone I know");
|
||||
assertTrue(filterCaptor.getValue().test(user));
|
||||
user.setName("nobody");
|
||||
user.setDisplayName("Someone I know");
|
||||
assertTrue(filterCaptor.getValue().test(user));
|
||||
user.setDisplayName("nobody");
|
||||
user.setMail("me@someone.com");
|
||||
assertTrue(filterCaptor.getValue().test(user));
|
||||
user.setMail("me@nowhere.com");
|
||||
assertFalse(filterCaptor.getValue().test(user));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetPermissionLink() throws URISyntaxException, UnsupportedEncodingException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + UserRootResource.USERS_PATH_V2 + "Neo");
|
||||
|
||||
Reference in New Issue
Block a user