mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
Move page object creation to PageResult class
This commit is contained in:
@@ -33,14 +33,10 @@
|
|||||||
|
|
||||||
package sonia.scm;
|
package sonia.scm;
|
||||||
|
|
||||||
import sonia.scm.util.Util;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base interface for all manager classes.
|
* Base interface for all manager classes.
|
||||||
*
|
*
|
||||||
@@ -141,14 +137,7 @@ public interface Manager<T extends ModelObject, E extends Exception>
|
|||||||
* empty page result is returned.
|
* empty page result is returned.
|
||||||
*/
|
*/
|
||||||
default PageResult<T> getPage(Comparator<T> comparator, int pageNumber, int pageSize) {
|
default PageResult<T> getPage(Comparator<T> comparator, int pageNumber, int pageSize) {
|
||||||
checkArgument(pageSize > 0, "pageSize must be at least 1");
|
return PageResult.createPage(getAll(comparator), pageNumber, pageSize);
|
||||||
checkArgument(pageNumber >= 0, "pageNumber must be non-negative");
|
|
||||||
|
|
||||||
Collection<T> allEntities = getAll(comparator);
|
|
||||||
|
|
||||||
Collection<T> pagedEntities = Util.createSubCollection(allEntities, pageNumber * pageSize, pageSize);
|
|
||||||
|
|
||||||
return new PageResult<>(pagedEntities, allEntities.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ package sonia.scm;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
import static sonia.scm.util.Util.createSubCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This represents the result of a page request. Contains the results for
|
* This represents the result of a page request. Contains the results for
|
||||||
* the page and the overall count of all elements.
|
* the page and the overall count of all elements.
|
||||||
@@ -12,6 +15,15 @@ public class PageResult<T extends ModelObject> {
|
|||||||
private final Collection<T> entities;
|
private final Collection<T> entities;
|
||||||
private final int overallCount;
|
private final int overallCount;
|
||||||
|
|
||||||
|
public static <T extends ModelObject> PageResult<T> createPage(Collection<T> allEntities, int pageNumber, int pageSize) {
|
||||||
|
checkArgument(pageSize > 0, "pageSize must be at least 1");
|
||||||
|
checkArgument(pageNumber >= 0, "pageNumber must be non-negative");
|
||||||
|
|
||||||
|
Collection<T> pagedEntities = createSubCollection(allEntities, pageNumber * pageSize, pageSize);
|
||||||
|
|
||||||
|
return new PageResult<>(pagedEntities, allEntities.size());
|
||||||
|
}
|
||||||
|
|
||||||
public PageResult(Collection<T> entities, int overallCount) {
|
public PageResult(Collection<T> entities, int overallCount) {
|
||||||
this.entities = entities;
|
this.entities = entities;
|
||||||
this.overallCount = overallCount;
|
this.overallCount = overallCount;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static sonia.scm.PageResult.createPage;
|
||||||
|
|
||||||
public class GroupCollectionToDtoMapperTest {
|
public class GroupCollectionToDtoMapperTest {
|
||||||
|
|
||||||
@@ -68,9 +69,7 @@ public class GroupCollectionToDtoMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateNextPageLink_whenHasMore() {
|
public void shouldCreateNextPageLink_whenHasMore() {
|
||||||
PageResult<Group> intermediate = mockPageResult("nobodies");
|
PageResult<Group> pageResult = createPage(createGroups("nobodies", "bosses"), 0, 1);
|
||||||
PageResult<Group> pageResult = new PageResult<>(intermediate.getEntities(), 2);
|
|
||||||
|
|
||||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
||||||
assertTrue(groupCollectionDto.getLinks().getLinkBy("next").get().getHref().contains("page=2"));
|
assertTrue(groupCollectionDto.getLinks().getLinkBy("next").get().getHref().contains("page=2"));
|
||||||
}
|
}
|
||||||
@@ -113,10 +112,14 @@ public class GroupCollectionToDtoMapperTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private PageResult<Group> mockPageResult(String... groupNames) {
|
private PageResult<Group> mockPageResult(String... groupNames) {
|
||||||
Collection<Group> groups = Arrays.stream(groupNames).map(this::mockGroupWithDto).collect(toList());
|
Collection<Group> groups = createGroups(groupNames);
|
||||||
return new PageResult<>(groups, groups.size());
|
return new PageResult<>(groups, groups.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<Group> createGroups(String... groupNames) {
|
||||||
|
return Arrays.stream(groupNames).map(this::mockGroupWithDto).collect(toList());
|
||||||
|
}
|
||||||
|
|
||||||
private Group mockGroupWithDto(String groupName) {
|
private Group mockGroupWithDto(String groupName) {
|
||||||
Group group = new Group();
|
Group group = new Group();
|
||||||
group.setName(groupName);
|
group.setName(groupName);
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import javax.ws.rs.core.UriInfo;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
import static java.util.stream.Collectors.toList;
|
||||||
@@ -25,6 +24,7 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.mockito.MockitoAnnotations.initMocks;
|
import static org.mockito.MockitoAnnotations.initMocks;
|
||||||
|
import static sonia.scm.PageResult.createPage;
|
||||||
|
|
||||||
public class UserCollectionToDtoMapperTest {
|
public class UserCollectionToDtoMapperTest {
|
||||||
|
|
||||||
@@ -76,8 +76,7 @@ public class UserCollectionToDtoMapperTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateNextPageLink_whenHasMore() {
|
public void shouldCreateNextPageLink_whenHasMore() {
|
||||||
PageResult<User> intermediate = mockPageResult("Hannes");
|
PageResult<User> pageResult = createPage(createUsers("Hannes", "Karl"), 0, 1);
|
||||||
PageResult<User> pageResult = new PageResult<>(intermediate.getEntities(), 2);
|
|
||||||
|
|
||||||
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
||||||
assertTrue(userCollectionDto.getLinks().getLinkBy("next").get().getHref().contains("page=2"));
|
assertTrue(userCollectionDto.getLinks().getLinkBy("next").get().getHref().contains("page=2"));
|
||||||
@@ -121,8 +120,11 @@ public class UserCollectionToDtoMapperTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private PageResult<User> mockPageResult(String... userNames) {
|
private PageResult<User> mockPageResult(String... userNames) {
|
||||||
Collection<User> users = Arrays.stream(userNames).map(this::mockUserWithDto).collect(toList());
|
return createPage(createUsers(userNames), 0, userNames.length);
|
||||||
return new PageResult<>(users, users.size());
|
}
|
||||||
|
|
||||||
|
private List<User> createUsers(String... userNames) {
|
||||||
|
return Arrays.stream(userNames).map(this::mockUserWithDto).collect(toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private User mockUserWithDto(String userName) {
|
private User mockUserWithDto(String userName) {
|
||||||
|
|||||||
Reference in New Issue
Block a user