mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 01:15:44 +01:00
merge
This commit is contained in:
@@ -33,16 +33,10 @@
|
||||
|
||||
package sonia.scm;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
/**
|
||||
* Base interface for all manager classes.
|
||||
*
|
||||
@@ -130,6 +124,8 @@ public interface Manager<T extends ModelObject, E extends Exception>
|
||||
* Returns objects from the store divided into pages with the given page
|
||||
* size for the given page number (zero based) and sorted by the given
|
||||
* {@link java.util.Comparator}.
|
||||
* <p>This default implementation reads all items, first, so you might want to adapt this
|
||||
* whenever reading is expensive!</p>
|
||||
*
|
||||
* @param comparator to sort the returned objects
|
||||
* @param pageNumber the number of the page to be returned (zero based)
|
||||
@@ -141,12 +137,7 @@ public interface Manager<T extends ModelObject, E extends Exception>
|
||||
* empty page result is returned.
|
||||
*/
|
||||
default PageResult<T> getPage(Comparator<T> comparator, int pageNumber, int pageSize) {
|
||||
checkArgument(pageSize > 0, "pageSize must be at least 1");
|
||||
checkArgument(pageNumber >= 0, "pageNumber must be non-negative");
|
||||
|
||||
Collection<T> entities = getAll(comparator, pageNumber * pageSize, pageSize + 1);
|
||||
boolean hasMore = entities.size() > pageSize;
|
||||
return new PageResult<>(Util.createSubCollection(entities, 0, pageSize), hasMore);
|
||||
return PageResult.createPage(getAll(comparator), pageNumber, pageSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,18 +3,30 @@ package sonia.scm;
|
||||
import java.util.Collection;
|
||||
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
|
||||
* the page and a flag whether there are more pages or not.
|
||||
* the page and the overall count of all elements.
|
||||
*/
|
||||
public class PageResult<T extends ModelObject> {
|
||||
|
||||
private final Collection<T> entities;
|
||||
private final boolean hasMore;
|
||||
private final int overallCount;
|
||||
|
||||
public PageResult(Collection<T> entities, boolean hasMore) {
|
||||
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) {
|
||||
this.entities = entities;
|
||||
this.hasMore = hasMore;
|
||||
this.overallCount = overallCount;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,9 +37,9 @@ public class PageResult<T extends ModelObject> {
|
||||
}
|
||||
|
||||
/**
|
||||
* If this is <code>true</code>, there are more pages (that is, more entities).
|
||||
* The overall count of all available elements.
|
||||
*/
|
||||
public boolean hasMore() {
|
||||
return hasMore;
|
||||
public int getOverallCount() {
|
||||
return overallCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,17 @@ import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ManagerTest {
|
||||
|
||||
private int givenItemCount = 0;
|
||||
|
||||
private Manager manager = new Manager() {
|
||||
@Override
|
||||
public void refresh(ModelObject object) throws IOException {
|
||||
@@ -26,12 +28,12 @@ public class ManagerTest {
|
||||
|
||||
@Override
|
||||
public Collection getAll() {
|
||||
return null;
|
||||
return IntStream.range(0, givenItemCount).boxed().collect(toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection getAll(Comparator comparator) {
|
||||
return null;
|
||||
return getAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,13 +43,7 @@ public class ManagerTest {
|
||||
|
||||
@Override
|
||||
public Collection getAll(Comparator comparator, int start, int limit) {
|
||||
if (start == 0 && (limit == 3) || (limit == 5)) {
|
||||
return Arrays.asList(1, 2, 3);
|
||||
} else if (start == 0 && limit == 6) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
return Arrays.asList(3);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,28 +81,37 @@ public class ManagerTest {
|
||||
|
||||
@Test
|
||||
public void getsNoPage() {
|
||||
givenItemCount = 0;
|
||||
PageResult singlePage = manager.getPage(comparator, 0, 5);
|
||||
assertFalse(singlePage.hasMore());
|
||||
assertEquals(0, singlePage.getEntities().size());
|
||||
assertEquals(givenItemCount, singlePage.getOverallCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsSinglePage() {
|
||||
|
||||
public void getsSinglePageWithoutEnoughItems() {
|
||||
givenItemCount = 3;
|
||||
PageResult singlePage = manager.getPage(comparator, 0, 4);
|
||||
assertFalse(singlePage.hasMore());
|
||||
assertEquals(3, singlePage.getEntities().size() );
|
||||
assertEquals(givenItemCount, singlePage.getOverallCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsSinglePageWithExactCountOfItems() {
|
||||
givenItemCount = 3;
|
||||
PageResult singlePage = manager.getPage(comparator, 0, 3);
|
||||
assertEquals(3, singlePage.getEntities().size() );
|
||||
assertEquals(givenItemCount, singlePage.getOverallCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsTwoPages() {
|
||||
|
||||
givenItemCount = 3;
|
||||
PageResult page1 = manager.getPage(comparator, 0, 2);
|
||||
assertTrue(page1.hasMore());
|
||||
assertEquals(2, page1.getEntities().size());
|
||||
assertEquals(givenItemCount, page1.getOverallCount());
|
||||
|
||||
PageResult page2 = manager.getPage(comparator, 1, 2);
|
||||
assertFalse(page2.hasMore());
|
||||
assertEquals(1, page2.getEntities().size());
|
||||
assertEquals(givenItemCount, page2.getOverallCount());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
@@ -12,6 +11,7 @@ import sonia.scm.group.GroupException;
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.GET;
|
||||
|
||||
@@ -11,13 +11,13 @@ import sonia.scm.group.GroupPermissions;
|
||||
import javax.inject.Inject;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.damnhandy.uri.template.UriTemplate.fromTemplate;
|
||||
import static de.otto.edison.hal.Embedded.embeddedBuilder;
|
||||
import static de.otto.edison.hal.Link.link;
|
||||
import static de.otto.edison.hal.Links.linkingTo;
|
||||
import static de.otto.edison.hal.paging.NumberedPaging.zeroBasedNumberedPaging;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static sonia.scm.api.v2.resources.ResourceLinks.groupCollection;
|
||||
|
||||
public class GroupCollectionToDtoMapper {
|
||||
@@ -32,8 +32,8 @@ public class GroupCollectionToDtoMapper {
|
||||
}
|
||||
|
||||
public GroupCollectionDto map(int pageNumber, int pageSize, PageResult<Group> pageResult) {
|
||||
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.hasMore());
|
||||
List<GroupDto> dtos = pageResult.getEntities().stream().map(user -> groupToDtoMapper.map(user)).collect(Collectors.toList());
|
||||
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.getOverallCount());
|
||||
List<GroupDto> dtos = pageResult.getEntities().stream().map(groupToDtoMapper::map).collect(toList());
|
||||
|
||||
GroupCollectionDto groupCollectionDto = new GroupCollectionDto(
|
||||
createLinks(paging),
|
||||
|
||||
@@ -23,12 +23,12 @@ import javax.ws.rs.core.UriInfo;
|
||||
import java.util.Collection;
|
||||
|
||||
@Produces(VndMediaType.GROUP)
|
||||
public class GroupSubResource extends AbstractManagerResource<Group, GroupException> {
|
||||
public class GroupResource extends AbstractManagerResource<Group, GroupException> {
|
||||
|
||||
private final GroupToGroupDtoMapper groupToGroupDtoMapper;
|
||||
|
||||
@Inject
|
||||
public GroupSubResource(GroupManager manager, GroupToGroupDtoMapper groupToGroupDtoMapper) {
|
||||
public GroupResource(GroupManager manager, GroupToGroupDtoMapper groupToGroupDtoMapper) {
|
||||
super(manager);
|
||||
this.groupToGroupDtoMapper = groupToGroupDtoMapper;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
@Path(GroupRootResource.GROUPS_PATH_V2)
|
||||
public class GroupRootResource {
|
||||
|
||||
public static final String GROUPS_PATH_V2 = "v2/groups/";
|
||||
|
||||
private final Provider<GroupCollectionResource> groupCollectionResource;
|
||||
private final Provider<GroupResource> groupResource;
|
||||
|
||||
@Inject
|
||||
public GroupRootResource(Provider<GroupCollectionResource> groupCollectionResource,
|
||||
Provider<GroupResource> groupResource) {
|
||||
this.groupCollectionResource = groupCollectionResource;
|
||||
this.groupResource = groupResource;
|
||||
}
|
||||
|
||||
@Path("")
|
||||
public GroupCollectionResource getGroupCollectionResource() {
|
||||
return groupCollectionResource.get();
|
||||
}
|
||||
|
||||
@Path("{id}")
|
||||
public GroupResource getGroupResource() {
|
||||
return groupResource.get();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import de.otto.edison.hal.Links;
|
||||
import org.mapstruct.AfterMapping;
|
||||
import org.mapstruct.Mapper;
|
||||
@@ -8,6 +7,7 @@ import org.mapstruct.MappingTarget;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupPermissions;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
@Path(GroupV2Resource.GROUPS_PATH_V2)
|
||||
public class GroupV2Resource {
|
||||
|
||||
public static final String GROUPS_PATH_V2 = "v2/groups/";
|
||||
|
||||
private final GroupCollectionResource groupCollectionResource;
|
||||
private final GroupSubResource groupSubResource;
|
||||
|
||||
@Inject
|
||||
public GroupV2Resource(GroupCollectionResource groupCollectionResource, GroupSubResource groupSubResource) {
|
||||
this.groupCollectionResource = groupCollectionResource;
|
||||
this.groupSubResource = groupSubResource;
|
||||
}
|
||||
|
||||
@Path("")
|
||||
public GroupCollectionResource getGroupCollectionResource() {
|
||||
return groupCollectionResource;
|
||||
}
|
||||
|
||||
@Path("{id}")
|
||||
public GroupSubResource getGroupSubResource() {
|
||||
return groupSubResource;
|
||||
}
|
||||
}
|
||||
@@ -15,19 +15,19 @@ class ResourceLinks {
|
||||
private final LinkBuilder groupLinkBuilder;
|
||||
|
||||
private GroupLinks(UriInfo uriInfo) {
|
||||
groupLinkBuilder = new LinkBuilder(uriInfo, GroupV2Resource.class, GroupSubResource.class);
|
||||
groupLinkBuilder = new LinkBuilder(uriInfo, GroupRootResource.class, GroupResource.class);
|
||||
}
|
||||
|
||||
String self(String name) {
|
||||
return groupLinkBuilder.method("getGroupSubResource").parameters(name).method("get").parameters().href();
|
||||
return groupLinkBuilder.method("getGroupResource").parameters(name).method("get").parameters().href();
|
||||
}
|
||||
|
||||
String delete(String name) {
|
||||
return groupLinkBuilder.method("getGroupSubResource").parameters(name).method("delete").parameters().href();
|
||||
return groupLinkBuilder.method("getGroupResource").parameters(name).method("delete").parameters().href();
|
||||
}
|
||||
|
||||
String update(String name) {
|
||||
return groupLinkBuilder.method("getGroupSubResource").parameters(name).method("update").parameters().href();
|
||||
return groupLinkBuilder.method("getGroupResource").parameters(name).method("update").parameters().href();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class ResourceLinks {
|
||||
private final LinkBuilder collectionLinkBuilder;
|
||||
|
||||
private GroupCollectionLinks(UriInfo uriInfo) {
|
||||
collectionLinkBuilder = new LinkBuilder(uriInfo, GroupV2Resource.class, GroupCollectionResource.class);
|
||||
collectionLinkBuilder = new LinkBuilder(uriInfo, GroupRootResource.class, GroupCollectionResource.class);
|
||||
}
|
||||
|
||||
String self() {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
@@ -12,6 +11,7 @@ import sonia.scm.user.UserException;
|
||||
import sonia.scm.user.UserManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
|
||||
@@ -12,13 +12,13 @@ import javax.inject.Inject;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.damnhandy.uri.template.UriTemplate.fromTemplate;
|
||||
import static de.otto.edison.hal.Embedded.embeddedBuilder;
|
||||
import static de.otto.edison.hal.Link.link;
|
||||
import static de.otto.edison.hal.Links.linkingTo;
|
||||
import static de.otto.edison.hal.paging.NumberedPaging.zeroBasedNumberedPaging;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static sonia.scm.api.v2.resources.ResourceLinks.userCollection;
|
||||
|
||||
public class UserCollectionToDtoMapper {
|
||||
@@ -37,8 +37,8 @@ public class UserCollectionToDtoMapper {
|
||||
}
|
||||
|
||||
public UserCollectionDto map(int pageNumber, int pageSize, PageResult<User> pageResult) {
|
||||
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.hasMore());
|
||||
List<UserDto> dtos = pageResult.getEntities().stream().map(userToDtoMapper::map).collect(Collectors.toList());
|
||||
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.getOverallCount());
|
||||
List<UserDto> dtos = pageResult.getEntities().stream().map(userToDtoMapper::map).collect(toList());
|
||||
|
||||
UserCollectionDto userCollectionDto = new UserCollectionDto(
|
||||
createLinks(uriInfoStore.get(), paging),
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import org.apache.shiro.authc.credential.PasswordService;
|
||||
import org.mapstruct.Context;
|
||||
import org.mapstruct.Mapper;
|
||||
@@ -9,6 +8,8 @@ import org.mapstruct.Mappings;
|
||||
import org.mapstruct.Named;
|
||||
import sonia.scm.user.User;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static sonia.scm.api.rest.resources.UserResource.DUMMY_PASSWORT;
|
||||
|
||||
@Mapper
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
@@ -10,6 +9,7 @@ import sonia.scm.user.UserException;
|
||||
import sonia.scm.user.UserManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import de.otto.edison.hal.Links;
|
||||
import org.mapstruct.AfterMapping;
|
||||
import org.mapstruct.Mapper;
|
||||
@@ -9,6 +8,8 @@ import sonia.scm.api.rest.resources.UserResource;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserPermissions;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static de.otto.edison.hal.Link.link;
|
||||
import static de.otto.edison.hal.Links.linkingTo;
|
||||
import static sonia.scm.api.v2.resources.ResourceLinks.user;
|
||||
|
||||
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static sonia.scm.PageResult.createPage;
|
||||
|
||||
public class GroupCollectionToDtoMapperTest {
|
||||
|
||||
@@ -41,7 +42,7 @@ public class GroupCollectionToDtoMapperTest {
|
||||
public void init() throws URISyntaxException {
|
||||
uriInfoStore.set(uriInfo);
|
||||
URI baseUri = new URI("http://example.com/base/");
|
||||
expectedBaseUri = baseUri.resolve(GroupV2Resource.GROUPS_PATH_V2 + "/");
|
||||
expectedBaseUri = baseUri.resolve(GroupRootResource.GROUPS_PATH_V2 + "/");
|
||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
@@ -54,35 +55,35 @@ public class GroupCollectionToDtoMapperTest {
|
||||
|
||||
@Test
|
||||
public void shouldSetPageNumber() {
|
||||
PageResult<Group> pageResult = mockPageResult(true, "nobodies");
|
||||
PageResult<Group> pageResult = mockPageResult("nobodies");
|
||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertEquals(1, groupCollectionDto.getPage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveSelfLink() {
|
||||
PageResult<Group> pageResult = mockPageResult(true, "nobodies");
|
||||
PageResult<Group> pageResult = mockPageResult("nobodies");
|
||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertTrue(groupCollectionDto.getLinks().getLinkBy("self").get().getHref().startsWith(expectedBaseUri.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNextPageLink_whenHasMore() {
|
||||
PageResult<Group> pageResult = mockPageResult(true, "nobodies");
|
||||
PageResult<Group> pageResult = createPage(createGroups("nobodies", "bosses"), 0, 1);
|
||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertTrue(groupCollectionDto.getLinks().getLinkBy("next").get().getHref().contains("page=2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotCreateNextPageLink_whenNoMore() {
|
||||
PageResult<Group> pageResult = mockPageResult(false, "nobodies");
|
||||
PageResult<Group> pageResult = mockPageResult("nobodies");
|
||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertFalse(groupCollectionDto.getLinks().stream().anyMatch(link -> link.getHref().contains("page=2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveCreateLink_whenHasPermission() {
|
||||
PageResult<Group> pageResult = mockPageResult(false, "nobodies");
|
||||
PageResult<Group> pageResult = mockPageResult("nobodies");
|
||||
when(subject.isPermitted("group:create")).thenReturn(true);
|
||||
|
||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
||||
@@ -92,7 +93,7 @@ public class GroupCollectionToDtoMapperTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotHaveCreateLink_whenHasNoPermission() {
|
||||
PageResult<Group> pageResult = mockPageResult(false, "nobodies");
|
||||
PageResult<Group> pageResult = mockPageResult("nobodies");
|
||||
when(subject.isPermitted("group:create")).thenReturn(false);
|
||||
|
||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 1, pageResult);
|
||||
@@ -102,7 +103,7 @@ public class GroupCollectionToDtoMapperTest {
|
||||
|
||||
@Test
|
||||
public void shouldMapGroups() {
|
||||
PageResult<Group> pageResult = mockPageResult(false, "nobodies", "bosses");
|
||||
PageResult<Group> pageResult = mockPageResult("nobodies", "bosses");
|
||||
GroupCollectionDto groupCollectionDto = mapper.map(1, 2, pageResult);
|
||||
List<HalRepresentation> groups = groupCollectionDto.getEmbedded().getItemsBy("groups");
|
||||
assertEquals(2, groups.size());
|
||||
@@ -110,9 +111,13 @@ public class GroupCollectionToDtoMapperTest {
|
||||
assertEquals("bosses", ((GroupDto) groups.get(1)).getName());
|
||||
}
|
||||
|
||||
private PageResult<Group> mockPageResult(boolean hasMore, String... groupNames) {
|
||||
Collection<Group> groups = Arrays.stream(groupNames).map(this::mockGroupWithDto).collect(toList());
|
||||
return new PageResult<>(groups, hasMore);
|
||||
private PageResult<Group> mockPageResult(String... groupNames) {
|
||||
Collection<Group> groups = createGroups(groupNames);
|
||||
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) {
|
||||
|
||||
@@ -38,45 +38,42 @@ import static org.mockito.MockitoAnnotations.initMocks;
|
||||
password = "secret",
|
||||
configuration = "classpath:sonia/scm/repository/shiro.ini"
|
||||
)
|
||||
public class GroupV2ResourceTest {
|
||||
public class GroupRootResourceTest {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
|
||||
@Mock
|
||||
private GroupManager groupManager;
|
||||
@Mock
|
||||
private UriInfo uriInfo;
|
||||
@Mock
|
||||
private UriInfoStore uriInfoStore;
|
||||
@InjectMocks
|
||||
GroupDtoToGroupMapperImpl dtoToGroupMapper;
|
||||
@InjectMocks
|
||||
GroupToGroupDtoMapperImpl groupToDtoMapper;
|
||||
@InjectMocks
|
||||
GroupCollectionToDtoMapper groupCollectionToDtoMapper;
|
||||
|
||||
@Mock
|
||||
private GroupManager groupManager;
|
||||
@InjectMocks
|
||||
private GroupDtoToGroupMapperImpl dtoToGroupMapper;
|
||||
@InjectMocks
|
||||
private GroupToGroupDtoMapperImpl groupToDtoMapper;
|
||||
@InjectMocks
|
||||
private GroupCollectionToDtoMapper groupCollectionToDtoMapper;
|
||||
|
||||
ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
|
||||
private ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws IOException, GroupException {
|
||||
initMocks(this);
|
||||
doNothing().when(groupManager).create(groupCaptor.capture());
|
||||
|
||||
Group group = new Group();
|
||||
group.setName("admin");
|
||||
group.setCreationDate(0L);
|
||||
group.setMembers(Collections.singletonList("user"));
|
||||
Group group = createDummyGroup();
|
||||
when(groupManager.get("admin")).thenReturn(group);
|
||||
|
||||
GroupCollectionResource groupCollectionResource = new GroupCollectionResource(groupManager, dtoToGroupMapper, groupToDtoMapper, groupCollectionToDtoMapper);
|
||||
GroupSubResource groupSubResource = new GroupSubResource(groupManager, groupToDtoMapper);
|
||||
GroupV2Resource groupV2Resource = new GroupV2Resource(groupCollectionResource, groupSubResource);
|
||||
GroupResource groupResource = new GroupResource(groupManager, groupToDtoMapper);
|
||||
GroupRootResource groupRootResource = new GroupRootResource(MockProvider.of(groupCollectionResource), MockProvider.of(groupResource));
|
||||
|
||||
dispatcher.getRegistry().addSingletonResource(groupV2Resource);
|
||||
dispatcher.getRegistry().addSingletonResource(groupRootResource);
|
||||
|
||||
when(uriInfo.getBaseUri()).thenReturn(URI.create("/"));
|
||||
when(uriInfoStore.get()).thenReturn(uriInfo);
|
||||
@@ -84,7 +81,7 @@ public class GroupV2ResourceTest {
|
||||
|
||||
@Test
|
||||
public void shouldGetNotFoundForNotExistentGroup() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupV2Resource.GROUPS_PATH_V2 + "nosuchgroup");
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupRootResource.GROUPS_PATH_V2 + "nosuchgroup");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
@@ -95,7 +92,7 @@ public class GroupV2ResourceTest {
|
||||
@Test
|
||||
@SubjectAware(username = "unpriv")
|
||||
public void shouldGetNotAuthorizedForWrongUser() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupV2Resource.GROUPS_PATH_V2 + "admin");
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupRootResource.GROUPS_PATH_V2 + "admin");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
@@ -105,13 +102,10 @@ public class GroupV2ResourceTest {
|
||||
|
||||
@Test
|
||||
public void shouldGetGroup() throws URISyntaxException {
|
||||
Group group = new Group();
|
||||
group.setName("admin");
|
||||
group.setCreationDate(0L);
|
||||
group.setMembers(Collections.singletonList("user"));
|
||||
Group group = createDummyGroup();
|
||||
when(groupManager.get("admin")).thenReturn(group);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupV2Resource.GROUPS_PATH_V2 + "admin");
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + GroupRootResource.GROUPS_PATH_V2 + "admin");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
@@ -129,7 +123,7 @@ public class GroupV2ResourceTest {
|
||||
byte[] groupJson = Resources.toByteArray(url);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.post("/" + GroupV2Resource.GROUPS_PATH_V2)
|
||||
.post("/" + GroupRootResource.GROUPS_PATH_V2)
|
||||
.contentType(VndMediaType.GROUP)
|
||||
.content(groupJson);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
@@ -142,4 +136,12 @@ public class GroupV2ResourceTest {
|
||||
assertEquals(2, createdGroup.getMembers().size());
|
||||
assertEquals("user1", createdGroup.getMembers().get(0));
|
||||
}
|
||||
|
||||
private Group createDummyGroup() {
|
||||
Group group = new Group();
|
||||
group.setName("admin");
|
||||
group.setCreationDate(0L);
|
||||
group.setMembers(Collections.singletonList("user"));
|
||||
return group;
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class GroupToGroupDtoMapperTest {
|
||||
public void init() throws URISyntaxException {
|
||||
initMocks(this);
|
||||
URI baseUri = new URI("http://example.com/base/");
|
||||
expectedBaseUri = baseUri.resolve(GroupV2Resource.GROUPS_PATH_V2 + "/");
|
||||
expectedBaseUri = baseUri.resolve(GroupRootResource.GROUPS_PATH_V2 + "/");
|
||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
||||
when(uriInfoStore.get()).thenReturn(uriInfo);
|
||||
subjectThreadState.bind();
|
||||
|
||||
@@ -52,31 +52,31 @@ public class ResourceLinksTest {
|
||||
@Test
|
||||
public void shouldCreateCorrectGroupSelfUrl() {
|
||||
String url = group(uriInfo).self("nobodies");
|
||||
assertEquals(BASE_URL + GroupV2Resource.GROUPS_PATH_V2 + "nobodies", url);
|
||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateCorrectGroupDeleteUrl() {
|
||||
String url = group(uriInfo).delete("nobodies");
|
||||
assertEquals(BASE_URL + GroupV2Resource.GROUPS_PATH_V2 + "nobodies", url);
|
||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateCorrectGroupUpdateUrl() {
|
||||
String url = group(uriInfo).update("nobodies");
|
||||
assertEquals(BASE_URL + GroupV2Resource.GROUPS_PATH_V2 + "nobodies", url);
|
||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void shouldCreateCorrectGroupCreateUrl() {
|
||||
// String url = ResourceLinks.groupCollection(uriInfo).create();
|
||||
// assertEquals(BASE_URL + GroupV2Resource.GROUPS_PATH_V2, url);
|
||||
// assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2, url);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void shouldCreateCorrectGroupCollectionUrl() {
|
||||
// String url = ResourceLinks.groupCollection(uriInfo).self();
|
||||
// assertEquals(BASE_URL + GroupV2Resource.GROUPS_PATH_V2, url);
|
||||
// assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2, url);
|
||||
// }
|
||||
|
||||
@Before
|
||||
|
||||
@@ -16,7 +16,6 @@ import javax.ws.rs.core.UriInfo;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
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.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
import static sonia.scm.PageResult.createPage;
|
||||
|
||||
public class UserCollectionToDtoMapperTest {
|
||||
|
||||
@@ -62,35 +62,36 @@ public class UserCollectionToDtoMapperTest {
|
||||
|
||||
@Test
|
||||
public void shouldSetPageNumber() {
|
||||
PageResult<User> pageResult = mockPageResult(true, "Hannes");
|
||||
PageResult<User> pageResult = mockPageResult("Hannes");
|
||||
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertEquals(1, userCollectionDto.getPage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveSelfLink() {
|
||||
PageResult<User> pageResult = mockPageResult(true, "Hannes");
|
||||
PageResult<User> pageResult = mockPageResult("Hannes");
|
||||
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertTrue(userCollectionDto.getLinks().getLinkBy("self").get().getHref().startsWith(expectedBaseUri.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNextPageLink_whenHasMore() {
|
||||
PageResult<User> pageResult = mockPageResult(true, "Hannes");
|
||||
PageResult<User> pageResult = createPage(createUsers("Hannes", "Karl"), 0, 1);
|
||||
|
||||
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertTrue(userCollectionDto.getLinks().getLinkBy("next").get().getHref().contains("page=2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotCreateNextPageLink_whenNoMore() {
|
||||
PageResult<User> pageResult = mockPageResult(false, "Hannes");
|
||||
PageResult<User> pageResult = mockPageResult("Hannes");
|
||||
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
||||
assertFalse(userCollectionDto.getLinks().stream().anyMatch(link -> link.getHref().contains("page=2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveCreateLink_whenHasPermission() {
|
||||
PageResult<User> pageResult = mockPageResult(false, "Hannes");
|
||||
PageResult<User> pageResult = mockPageResult("Hannes");
|
||||
when(subject.isPermitted("user:create")).thenReturn(true);
|
||||
|
||||
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
||||
@@ -100,7 +101,7 @@ public class UserCollectionToDtoMapperTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotHaveCreateLink_whenHasNoPermission() {
|
||||
PageResult<User> pageResult = mockPageResult(false, "Hannes");
|
||||
PageResult<User> pageResult = mockPageResult("Hannes");
|
||||
when(subject.isPermitted("user:create")).thenReturn(false);
|
||||
|
||||
UserCollectionDto userCollectionDto = mapper.map(1, 1, pageResult);
|
||||
@@ -110,7 +111,7 @@ public class UserCollectionToDtoMapperTest {
|
||||
|
||||
@Test
|
||||
public void shouldMapUsers() {
|
||||
PageResult<User> pageResult = mockPageResult(false, "Hannes", "Wurst");
|
||||
PageResult<User> pageResult = mockPageResult("Hannes", "Wurst");
|
||||
UserCollectionDto userCollectionDto = mapper.map(1, 2, pageResult);
|
||||
List<HalRepresentation> users = userCollectionDto.getEmbedded().getItemsBy("users");
|
||||
assertEquals(2, users.size());
|
||||
@@ -118,9 +119,12 @@ public class UserCollectionToDtoMapperTest {
|
||||
assertEquals("Wurst", ((UserDto) users.get(1)).getName());
|
||||
}
|
||||
|
||||
private PageResult<User> mockPageResult(boolean hasMore, String... userNames) {
|
||||
Collection<User> users = Arrays.stream(userNames).map(this::mockUserWithDto).collect(toList());
|
||||
return new PageResult<>(users, hasMore);
|
||||
private PageResult<User> mockPageResult(String... userNames) {
|
||||
return createPage(createUsers(userNames), 0, userNames.length);
|
||||
}
|
||||
|
||||
private List<User> createUsers(String... userNames) {
|
||||
return Arrays.stream(userNames).map(this::mockUserWithDto).collect(toList());
|
||||
}
|
||||
|
||||
private User mockUserWithDto(String userName) {
|
||||
|
||||
@@ -26,8 +26,8 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -70,7 +70,7 @@ public class UserRootResourceTest {
|
||||
public void prepareEnvironment() throws IOException, UserException {
|
||||
initMocks(this);
|
||||
User dummyUser = createDummyUser();
|
||||
when(userManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(Collections.singletonList(dummyUser), true));
|
||||
when(userManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(singletonList(dummyUser), 1));
|
||||
when(userManager.get("Neo")).thenReturn(dummyUser);
|
||||
doNothing().when(userManager).create(userCaptor.capture());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user