mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 08:25:44 +01:00
Move create link to collection resource
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import de.otto.edison.hal.Embedded;
|
||||||
|
import de.otto.edison.hal.Links;
|
||||||
|
import de.otto.edison.hal.paging.NumberedPaging;
|
||||||
|
import de.otto.edison.hal.paging.PagingRel;
|
||||||
|
import sonia.scm.PageResult;
|
||||||
|
import sonia.scm.user.User;
|
||||||
|
import sonia.scm.user.UserPermissions;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class UserCollection2DtoMapper {
|
||||||
|
|
||||||
|
private final User2UserDtoMapper userToDtoMapper;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public UserCollection2DtoMapper(User2UserDtoMapper userToDtoMapper) {
|
||||||
|
this.userToDtoMapper = userToDtoMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserCollectionDto userCollectionToUserDto(UriInfo uriInfo, int pageNumber, int pageSize, PageResult<User> pageResult) {
|
||||||
|
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.hasMore());
|
||||||
|
List<UserDto> dtos = pageResult.getEntities().stream().map(user -> userToDtoMapper.userToUserDto(user, uriInfo)).collect(Collectors.toList());
|
||||||
|
|
||||||
|
UserCollectionDto userCollectionDto = new UserCollectionDto(
|
||||||
|
createLinks(uriInfo, paging),
|
||||||
|
embedDtos(dtos)
|
||||||
|
);
|
||||||
|
userCollectionDto.setPage(pageNumber);
|
||||||
|
return userCollectionDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Links createLinks(UriInfo uriInfo, NumberedPaging page) {
|
||||||
|
LinkBuilder collectionLinkBuilder = new LinkBuilder(uriInfo, UserV2Resource.class, UserCollectionResource.class);
|
||||||
|
String baseUrl = collectionLinkBuilder.method("getUserCollectionResource").parameters().method("create").parameters().href();
|
||||||
|
|
||||||
|
Links.Builder linksBuilder = linkingTo()
|
||||||
|
.with(page.links(
|
||||||
|
fromTemplate(baseUrl + "{?page,pageSize}"),
|
||||||
|
EnumSet.allOf(PagingRel.class)));
|
||||||
|
if (UserPermissions.create().isPermitted()) {
|
||||||
|
linksBuilder
|
||||||
|
.single(link("create", collectionLinkBuilder. method("getUserCollectionResource").parameters().method("create").parameters().href()));
|
||||||
|
}
|
||||||
|
return linksBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Embedded embedDtos(List<UserDto> dtos) {
|
||||||
|
return embeddedBuilder()
|
||||||
|
.with("users", dtos)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +1,17 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import de.otto.edison.hal.Embedded;
|
||||||
import de.otto.edison.hal.HalRepresentation;
|
import de.otto.edison.hal.HalRepresentation;
|
||||||
import de.otto.edison.hal.paging.NumberedPaging;
|
import de.otto.edison.hal.Links;
|
||||||
import de.otto.edison.hal.paging.PagingRel;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.util.EnumSet;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static com.damnhandy.uri.template.UriTemplate.fromTemplate;
|
|
||||||
import static de.otto.edison.hal.Embedded.embeddedBuilder;
|
|
||||||
import static de.otto.edison.hal.Links.linkingTo;
|
|
||||||
|
|
||||||
|
@Data
|
||||||
public class UserCollectionDto extends HalRepresentation {
|
public class UserCollectionDto extends HalRepresentation {
|
||||||
public UserCollectionDto(String baseUrl, NumberedPaging page, List<UserDto> users) {
|
|
||||||
super(
|
private int page;
|
||||||
linkingTo()
|
private int pageTotal;
|
||||||
.with(page.links(
|
|
||||||
fromTemplate(baseUrl + "{?page,pageSize}"),
|
public UserCollectionDto(Links links, Embedded embedded) {
|
||||||
EnumSet.allOf(PagingRel.class)))
|
super(links, embedded);
|
||||||
.build(),
|
|
||||||
embeddedBuilder()
|
|
||||||
.with("users", users)
|
|
||||||
.build()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Singleton;
|
|
||||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||||
import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
|
import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
|
||||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||||
@@ -17,12 +16,7 @@ import javax.ws.rs.*;
|
|||||||
import javax.ws.rs.core.*;
|
import javax.ws.rs.core.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static de.otto.edison.hal.paging.NumberedPaging.zeroBasedNumberedPaging;
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
@Produces(VndMediaType.USER)
|
@Produces(VndMediaType.USER)
|
||||||
public class UserCollectionResource extends AbstractManagerResource<User, UserException> {
|
public class UserCollectionResource extends AbstractManagerResource<User, UserException> {
|
||||||
public static final int DEFAULT_PAGE_SIZE = 10;
|
public static final int DEFAULT_PAGE_SIZE = 10;
|
||||||
@@ -55,19 +49,15 @@ public class UserCollectionResource extends AbstractManagerResource<User, UserEx
|
|||||||
@ResponseCode(code = 403, condition = "forbidden, the current user has no admin privileges"),
|
@ResponseCode(code = 403, condition = "forbidden, the current user has no admin privileges"),
|
||||||
@ResponseCode(code = 500, condition = "internal server error")
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
})
|
})
|
||||||
public Response getAll(@Context Request request, @Context UriInfo uriInfo, @DefaultValue("0")
|
public Response getAll(@Context Request request, @Context UriInfo uriInfo,
|
||||||
@QueryParam("page") int page, @DefaultValue("" + DEFAULT_PAGE_SIZE)
|
@DefaultValue("0") @QueryParam("page") int page,
|
||||||
@QueryParam("pageSize") int pageSize, @QueryParam("sortby") String sortby,
|
@DefaultValue("" + DEFAULT_PAGE_SIZE) @QueryParam("pageSize") int pageSize,
|
||||||
|
@QueryParam("sortby") String sortby,
|
||||||
@DefaultValue("false")
|
@DefaultValue("false")
|
||||||
@QueryParam("desc") boolean desc) {
|
@QueryParam("desc") boolean desc) {
|
||||||
PageResult<User> pageResult = fetchPage(sortby, desc, page, pageSize);
|
PageResult<User> pageResult = fetchPage(sortby, desc, page, pageSize);
|
||||||
|
|
||||||
LinkBuilder collectionLinkBuilder = new LinkBuilder(uriInfo, UserV2Resource.class, UserCollectionResource.class);
|
return Response.ok(new UserCollection2DtoMapper(userToDtoMapper).userCollectionToUserDto(uriInfo, page, pageSize, pageResult)).build();
|
||||||
String baseUrl = collectionLinkBuilder.method("getUserCollectionResource").parameters().method("create").parameters().href();
|
|
||||||
|
|
||||||
List<UserDto> dtos = pageResult.getEntities().stream().map(user -> userToDtoMapper.userToUserDto(user, uriInfo)).collect(Collectors.toList());
|
|
||||||
|
|
||||||
return Response.ok(new UserCollectionDto(baseUrl, zeroBasedNumberedPaging(page, pageSize, pageResult.hasMore()), dtos)).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Singleton;
|
|
||||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||||
@@ -18,7 +17,6 @@ import javax.ws.rs.core.*;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
|
||||||
@Singleton
|
|
||||||
@Produces(VndMediaType.USER)
|
@Produces(VndMediaType.USER)
|
||||||
public class UserSubResource extends AbstractManagerResource<User, UserException> {
|
public class UserSubResource extends AbstractManagerResource<User, UserException> {
|
||||||
private final UserDto2UserMapper dtoToUserMapper;
|
private final UserDto2UserMapper dtoToUserMapper;
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Singleton;
|
|
||||||
|
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
|
|
||||||
@Singleton
|
|
||||||
@Path(UserV2Resource.USERS_PATH_V2)
|
@Path(UserV2Resource.USERS_PATH_V2)
|
||||||
public class UserV2Resource {
|
public class UserV2Resource {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import de.otto.edison.hal.HalRepresentation;
|
||||||
|
import org.apache.shiro.subject.Subject;
|
||||||
|
import org.apache.shiro.subject.support.SubjectThreadState;
|
||||||
|
import org.apache.shiro.util.ThreadContext;
|
||||||
|
import org.apache.shiro.util.ThreadState;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import sonia.scm.PageResult;
|
||||||
|
import sonia.scm.user.User;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public class UserCollection2DtoMapperTest {
|
||||||
|
|
||||||
|
private final UriInfo uriInfo = mock(UriInfo.class);
|
||||||
|
private final User2UserDtoMapper userToDtoMapper = mock(User2UserDtoMapper.class);
|
||||||
|
private final Subject subject = mock(Subject.class);
|
||||||
|
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||||
|
|
||||||
|
private final UserCollection2DtoMapper mapper = new UserCollection2DtoMapper(userToDtoMapper);
|
||||||
|
|
||||||
|
private URI expectedBaseUri;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() throws URISyntaxException {
|
||||||
|
URI baseUri = new URI("http://example.com/base/");
|
||||||
|
expectedBaseUri = baseUri.resolve(UserV2Resource.USERS_PATH_V2 + "/");
|
||||||
|
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
||||||
|
subjectThreadState.bind();
|
||||||
|
ThreadContext.bind(subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSetPageNumber() {
|
||||||
|
PageResult<User> pageResult = mockPageResult(true, "Hannes");
|
||||||
|
UserCollectionDto userCollectionDto = mapper.userCollectionToUserDto(uriInfo, 1, 1, pageResult);
|
||||||
|
assertEquals(1, userCollectionDto.getPage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHaveSelfLink() {
|
||||||
|
PageResult<User> pageResult = mockPageResult(true, "Hannes");
|
||||||
|
UserCollectionDto userCollectionDto = mapper.userCollectionToUserDto(uriInfo, 1, 1, pageResult);
|
||||||
|
assertTrue(userCollectionDto.getLinks().getLinkBy("self").get().getHref().startsWith(expectedBaseUri.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateNextPageLink_whenHasMore() {
|
||||||
|
PageResult<User> pageResult = mockPageResult(true, "Hannes");
|
||||||
|
UserCollectionDto userCollectionDto = mapper.userCollectionToUserDto(uriInfo, 1, 1, pageResult);
|
||||||
|
assertTrue(userCollectionDto.getLinks().getLinkBy("next").get().getHref().contains("page=2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotCreateNextPageLink_whenNoMore() {
|
||||||
|
PageResult<User> pageResult = mockPageResult(false, "Hannes");
|
||||||
|
UserCollectionDto userCollectionDto = mapper.userCollectionToUserDto(uriInfo, 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");
|
||||||
|
when(subject.isPermitted("user:create")).thenReturn(true);
|
||||||
|
|
||||||
|
UserCollectionDto userCollectionDto = mapper.userCollectionToUserDto(uriInfo, 1, 1, pageResult);
|
||||||
|
|
||||||
|
assertTrue(userCollectionDto.getLinks().getLinkBy("create").isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotHaveCreateLink_whenHasNoPermission() {
|
||||||
|
PageResult<User> pageResult = mockPageResult(false, "Hannes");
|
||||||
|
when(subject.isPermitted("user:create")).thenReturn(false);
|
||||||
|
|
||||||
|
UserCollectionDto userCollectionDto = mapper.userCollectionToUserDto(uriInfo, 1, 1, pageResult);
|
||||||
|
|
||||||
|
assertFalse(userCollectionDto.getLinks().getLinkBy("create").isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldMapUsers() {
|
||||||
|
PageResult<User> pageResult = mockPageResult(false, "Hannes", "Wurst");
|
||||||
|
UserCollectionDto userCollectionDto = mapper.userCollectionToUserDto(uriInfo, 1, 2, pageResult);
|
||||||
|
List<HalRepresentation> users = userCollectionDto.getEmbedded().getItemsBy("users");
|
||||||
|
assertEquals(2, users.size());
|
||||||
|
assertEquals("Hannes", ((UserDto) users.get(0)).getName());
|
||||||
|
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 User mockUserWithDto(String userName) {
|
||||||
|
User user = new User();
|
||||||
|
user.setName(userName);
|
||||||
|
when(userToDtoMapper.userToUserDto(user, uriInfo)).thenReturn(createUserDto(user));
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserDto createUserDto(User user) {
|
||||||
|
UserDto dto = new UserDto();
|
||||||
|
dto.setName(user.getName());
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import org.jboss.resteasy.mock.MockHttpResponse;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import sonia.scm.PageResult;
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
import sonia.scm.user.UserManager;
|
import sonia.scm.user.UserManager;
|
||||||
|
|
||||||
@@ -17,6 +18,8 @@ import java.net.URISyntaxException;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -35,7 +38,7 @@ public class UserV2ResourceTest {
|
|||||||
@Before
|
@Before
|
||||||
public void prepareEnvironment() {
|
public void prepareEnvironment() {
|
||||||
UserManager userManager = mock(UserManager.class);
|
UserManager userManager = mock(UserManager.class);
|
||||||
when(userManager.getAll()).thenReturn(Collections.singletonList(createDummyUser()));
|
when(userManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(Collections.singletonList(createDummyUser()), true));
|
||||||
|
|
||||||
UserDto2UserMapperImpl dtoToUserMapper = new UserDto2UserMapperImpl();
|
UserDto2UserMapperImpl dtoToUserMapper = new UserDto2UserMapperImpl();
|
||||||
User2UserDtoMapperImpl userToDtoMapper = new User2UserDtoMapperImpl();
|
User2UserDtoMapperImpl userToDtoMapper = new User2UserDtoMapperImpl();
|
||||||
|
|||||||
Reference in New Issue
Block a user