mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Replace static access to ResourceLinks with injection
This commit is contained in:
@@ -18,25 +18,24 @@ import javax.ws.rs.POST;
|
|||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.Context;
|
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.group;
|
|
||||||
|
|
||||||
public class GroupCollectionResource {
|
public class GroupCollectionResource {
|
||||||
|
|
||||||
private static final int DEFAULT_PAGE_SIZE = 10;
|
private static final int DEFAULT_PAGE_SIZE = 10;
|
||||||
private final GroupDtoToGroupMapper dtoToGroupMapper;
|
private final GroupDtoToGroupMapper dtoToGroupMapper;
|
||||||
private final GroupCollectionToDtoMapper groupCollectionToDtoMapper;
|
private final GroupCollectionToDtoMapper groupCollectionToDtoMapper;
|
||||||
|
private final ResourceLinks resourceLinks;
|
||||||
|
|
||||||
private final ResourceManagerAdapter<Group, GroupDto, GroupException> adapter;
|
private final ResourceManagerAdapter<Group, GroupDto, GroupException> adapter;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public GroupCollectionResource(GroupManager manager, GroupDtoToGroupMapper dtoToGroupMapper, GroupCollectionToDtoMapper groupCollectionToDtoMapper) {
|
public GroupCollectionResource(GroupManager manager, GroupDtoToGroupMapper dtoToGroupMapper, GroupCollectionToDtoMapper groupCollectionToDtoMapper, ResourceLinks resourceLinks) {
|
||||||
this.dtoToGroupMapper = dtoToGroupMapper;
|
this.dtoToGroupMapper = dtoToGroupMapper;
|
||||||
this.groupCollectionToDtoMapper = groupCollectionToDtoMapper;
|
this.groupCollectionToDtoMapper = groupCollectionToDtoMapper;
|
||||||
|
this.resourceLinks = resourceLinks;
|
||||||
this.adapter = new ResourceManagerAdapter<>(manager);
|
this.adapter = new ResourceManagerAdapter<>(manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,9 +84,9 @@ public class GroupCollectionResource {
|
|||||||
})
|
})
|
||||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||||
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created group"))
|
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created group"))
|
||||||
public Response create(@Context UriInfo uriInfo, GroupDto groupDto) throws IOException, GroupException {
|
public Response create(GroupDto groupDto) throws IOException, GroupException {
|
||||||
return adapter.create(groupDto,
|
return adapter.create(groupDto,
|
||||||
() -> dtoToGroupMapper.map(groupDto),
|
() -> dtoToGroupMapper.map(groupDto),
|
||||||
group -> group(uriInfo).self(group.getName()));
|
group -> resourceLinks.group().self(group.getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,26 +5,24 @@ import sonia.scm.group.GroupPermissions;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.groupCollection;
|
|
||||||
|
|
||||||
public class GroupCollectionToDtoMapper extends BasicCollectionToDtoMapper<Group, GroupDto> {
|
public class GroupCollectionToDtoMapper extends BasicCollectionToDtoMapper<Group, GroupDto> {
|
||||||
|
|
||||||
private final UriInfoStore uriInfoStore;
|
private final ResourceLinks resourceLinks;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public GroupCollectionToDtoMapper(GroupToGroupDtoMapper groupToDtoMapper, UriInfoStore uriInfoStore) {
|
public GroupCollectionToDtoMapper(GroupToGroupDtoMapper groupToDtoMapper, ResourceLinks resourceLinks) {
|
||||||
super("groups", groupToDtoMapper);
|
super("groups", groupToDtoMapper);
|
||||||
this.uriInfoStore = uriInfoStore;
|
this.resourceLinks = resourceLinks;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String createCreateLink() {
|
String createCreateLink() {
|
||||||
return groupCollection(uriInfoStore.get()).create();
|
return resourceLinks.groupCollection().create();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String createSelfLink() {
|
String createSelfLink() {
|
||||||
return groupCollection(uriInfoStore.get()).self();
|
return resourceLinks.groupCollection().self();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import static de.otto.edison.hal.Link.link;
|
import static de.otto.edison.hal.Link.link;
|
||||||
import static de.otto.edison.hal.Links.linkingTo;
|
import static de.otto.edison.hal.Links.linkingTo;
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.group;
|
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.user;
|
|
||||||
|
|
||||||
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
||||||
@SuppressWarnings("squid:S3306")
|
@SuppressWarnings("squid:S3306")
|
||||||
@@ -22,16 +20,16 @@ import static sonia.scm.api.v2.resources.ResourceLinks.user;
|
|||||||
public abstract class GroupToGroupDtoMapper extends BaseMapper<Group, GroupDto> {
|
public abstract class GroupToGroupDtoMapper extends BaseMapper<Group, GroupDto> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private UriInfoStore uriInfoStore;
|
private ResourceLinks resourceLinks;
|
||||||
|
|
||||||
@AfterMapping
|
@AfterMapping
|
||||||
void appendLinks(Group group, @MappingTarget GroupDto target) {
|
void appendLinks(Group group, @MappingTarget GroupDto target) {
|
||||||
Links.Builder linksBuilder = linkingTo().self(group(uriInfoStore.get()).self(target.getName()));
|
Links.Builder linksBuilder = linkingTo().self(resourceLinks.group().self(target.getName()));
|
||||||
if (GroupPermissions.delete(group).isPermitted()) {
|
if (GroupPermissions.delete(group).isPermitted()) {
|
||||||
linksBuilder.single(link("delete", group(uriInfoStore.get()).delete(target.getName())));
|
linksBuilder.single(link("delete", resourceLinks.group().delete(target.getName())));
|
||||||
}
|
}
|
||||||
if (GroupPermissions.modify(group).isPermitted()) {
|
if (GroupPermissions.modify(group).isPermitted()) {
|
||||||
linksBuilder.single(link("update", group(uriInfoStore.get()).update(target.getName())));
|
linksBuilder.single(link("update", resourceLinks.group().update(target.getName())));
|
||||||
}
|
}
|
||||||
target.add(linksBuilder.build());
|
target.add(linksBuilder.build());
|
||||||
}
|
}
|
||||||
@@ -43,7 +41,7 @@ public abstract class GroupToGroupDtoMapper extends BaseMapper<Group, GroupDto>
|
|||||||
}
|
}
|
||||||
|
|
||||||
private MemberDto createMember(String name) {
|
private MemberDto createMember(String name) {
|
||||||
Links.Builder linksBuilder = linkingTo().self(user(uriInfoStore.get()).self(name));
|
Links.Builder linksBuilder = linkingTo().self(resourceLinks.user().self(name));
|
||||||
MemberDto memberDto = new MemberDto(name);
|
MemberDto memberDto = new MemberDto(name);
|
||||||
memberDto.add(linksBuilder.build());
|
memberDto.add(linksBuilder.build());
|
||||||
return memberDto;
|
return memberDto;
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
import javax.ws.rs.core.UriInfo;
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
|
||||||
class ResourceLinks {
|
class ResourceLinks {
|
||||||
|
|
||||||
private ResourceLinks() {
|
private final UriInfoStore uriInfoStore;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
ResourceLinks(UriInfoStore uriInfoStore) {
|
||||||
|
this.uriInfoStore = uriInfoStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
static GroupLinks group(UriInfo uriInfo) {
|
|
||||||
return new GroupLinks(uriInfo);
|
GroupLinks group() {
|
||||||
|
return new GroupLinks(uriInfoStore.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GroupLinks {
|
static class GroupLinks {
|
||||||
@@ -31,8 +37,8 @@ class ResourceLinks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static GroupCollectionLinks groupCollection(UriInfo uriInfo) {
|
GroupCollectionLinks groupCollection() {
|
||||||
return new GroupCollectionLinks(uriInfo);
|
return new GroupCollectionLinks(uriInfoStore.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GroupCollectionLinks {
|
static class GroupCollectionLinks {
|
||||||
@@ -51,8 +57,8 @@ class ResourceLinks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static UserLinks user(UriInfo uriInfo) {
|
UserLinks user() {
|
||||||
return new UserLinks(uriInfo);
|
return new UserLinks(uriInfoStore.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
static class UserLinks {
|
static class UserLinks {
|
||||||
@@ -75,8 +81,8 @@ class ResourceLinks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static UserCollectionLinks userCollection(UriInfo uriInfo) {
|
UserCollectionLinks userCollection() {
|
||||||
return new UserCollectionLinks(uriInfo);
|
return new UserCollectionLinks(uriInfoStore.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
static class UserCollectionLinks {
|
static class UserCollectionLinks {
|
||||||
|
|||||||
@@ -18,27 +18,25 @@ import javax.ws.rs.POST;
|
|||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.Context;
|
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.user;
|
|
||||||
|
|
||||||
public class UserCollectionResource {
|
public class UserCollectionResource {
|
||||||
|
|
||||||
private static final int DEFAULT_PAGE_SIZE = 10;
|
private static final int DEFAULT_PAGE_SIZE = 10;
|
||||||
private final UserDtoToUserMapper dtoToUserMapper;
|
private final UserDtoToUserMapper dtoToUserMapper;
|
||||||
private final UserCollectionToDtoMapper userCollectionToDtoMapper;
|
private final UserCollectionToDtoMapper userCollectionToDtoMapper;
|
||||||
|
private final ResourceLinks resourceLinks;
|
||||||
|
|
||||||
private final ResourceManagerAdapter<User, UserDto, UserException> adapter;
|
private final ResourceManagerAdapter<User, UserDto, UserException> adapter;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public UserCollectionResource(UserManager manager, UserDtoToUserMapper dtoToUserMapper,
|
public UserCollectionResource(UserManager manager, UserDtoToUserMapper dtoToUserMapper,
|
||||||
UserCollectionToDtoMapper userCollectionToDtoMapper) {
|
UserCollectionToDtoMapper userCollectionToDtoMapper, ResourceLinks resourceLinks) {
|
||||||
this.dtoToUserMapper = dtoToUserMapper;
|
this.dtoToUserMapper = dtoToUserMapper;
|
||||||
this.userCollectionToDtoMapper = userCollectionToDtoMapper;
|
this.userCollectionToDtoMapper = userCollectionToDtoMapper;
|
||||||
this.adapter = new ResourceManagerAdapter<>(manager);
|
this.adapter = new ResourceManagerAdapter<>(manager);
|
||||||
|
this.resourceLinks = resourceLinks;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,9 +86,9 @@ public class UserCollectionResource {
|
|||||||
})
|
})
|
||||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||||
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created user"))
|
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created user"))
|
||||||
public Response create(@Context UriInfo uriInfo, UserDto userDto) throws IOException, UserException {
|
public Response create(UserDto userDto) throws IOException, UserException {
|
||||||
return adapter.create(userDto,
|
return adapter.create(userDto,
|
||||||
() -> dtoToUserMapper.map(userDto, ""),
|
() -> dtoToUserMapper.map(userDto, ""),
|
||||||
user -> user(uriInfo).self(user.getName()));
|
user -> resourceLinks.user().self(user.getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,28 +5,26 @@ import sonia.scm.user.UserPermissions;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.userCollection;
|
|
||||||
|
|
||||||
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
||||||
@SuppressWarnings("squid:S3306")
|
@SuppressWarnings("squid:S3306")
|
||||||
public class UserCollectionToDtoMapper extends BasicCollectionToDtoMapper<User, UserDto> {
|
public class UserCollectionToDtoMapper extends BasicCollectionToDtoMapper<User, UserDto> {
|
||||||
|
|
||||||
private final UriInfoStore uriInfoStore;
|
private final ResourceLinks resourceLinks;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public UserCollectionToDtoMapper(UserToUserDtoMapper userToDtoMapper, UriInfoStore uriInfoStore) {
|
public UserCollectionToDtoMapper(UserToUserDtoMapper userToDtoMapper, ResourceLinks resourceLinks) {
|
||||||
super("users", userToDtoMapper);
|
super("users", userToDtoMapper);
|
||||||
this.uriInfoStore = uriInfoStore;
|
this.resourceLinks = resourceLinks;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String createCreateLink() {
|
String createCreateLink() {
|
||||||
return userCollection(uriInfoStore.get()).create();
|
return resourceLinks.userCollection().create();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String createSelfLink() {
|
String createSelfLink() {
|
||||||
return userCollection(uriInfoStore.get()).self();
|
return resourceLinks.userCollection().self();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
import static de.otto.edison.hal.Link.link;
|
import static de.otto.edison.hal.Link.link;
|
||||||
import static de.otto.edison.hal.Links.linkingTo;
|
import static de.otto.edison.hal.Links.linkingTo;
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.user;
|
|
||||||
|
|
||||||
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
||||||
@SuppressWarnings("squid:S3306")
|
@SuppressWarnings("squid:S3306")
|
||||||
@@ -20,7 +19,7 @@ import static sonia.scm.api.v2.resources.ResourceLinks.user;
|
|||||||
public abstract class UserToUserDtoMapper extends BaseMapper<User, UserDto> {
|
public abstract class UserToUserDtoMapper extends BaseMapper<User, UserDto> {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private UriInfoStore uriInfoStore;
|
private ResourceLinks resourceLinks;
|
||||||
|
|
||||||
@AfterMapping
|
@AfterMapping
|
||||||
void removePassword(@MappingTarget UserDto target) {
|
void removePassword(@MappingTarget UserDto target) {
|
||||||
@@ -29,13 +28,14 @@ public abstract class UserToUserDtoMapper extends BaseMapper<User, UserDto> {
|
|||||||
|
|
||||||
@AfterMapping
|
@AfterMapping
|
||||||
void appendLinks(User user, @MappingTarget UserDto target) {
|
void appendLinks(User user, @MappingTarget UserDto target) {
|
||||||
Links.Builder linksBuilder = linkingTo().self(user(uriInfoStore.get()).self(target.getName()));
|
Links.Builder linksBuilder = linkingTo().self(resourceLinks.user().self(target.getName()));
|
||||||
if (UserPermissions.delete(user).isPermitted()) {
|
if (UserPermissions.delete(user).isPermitted()) {
|
||||||
linksBuilder.single(link("delete", user(uriInfoStore.get()).delete(target.getName())));
|
linksBuilder.single(link("delete", resourceLinks.user().delete(target.getName())));
|
||||||
}
|
}
|
||||||
if (UserPermissions.modify(user).isPermitted()) {
|
if (UserPermissions.modify(user).isPermitted()) {
|
||||||
linksBuilder.single(link("update", user(uriInfoStore.get()).update(target.getName())));
|
linksBuilder.single(link("update", resourceLinks.user().update(target.getName())));
|
||||||
}
|
}
|
||||||
target.add(linksBuilder.build());
|
target.add(linksBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,11 +30,12 @@ public class GroupCollectionToDtoMapperTest {
|
|||||||
|
|
||||||
private final UriInfo uriInfo = mock(UriInfo.class);
|
private final UriInfo uriInfo = mock(UriInfo.class);
|
||||||
private final UriInfoStore uriInfoStore = new UriInfoStore();
|
private final UriInfoStore uriInfoStore = new UriInfoStore();
|
||||||
|
private final ResourceLinks resourceLinks = new ResourceLinks(uriInfoStore);
|
||||||
private final GroupToGroupDtoMapper groupToDtoMapper = mock(GroupToGroupDtoMapper.class);
|
private final GroupToGroupDtoMapper groupToDtoMapper = mock(GroupToGroupDtoMapper.class);
|
||||||
private final Subject subject = mock(Subject.class);
|
private final Subject subject = mock(Subject.class);
|
||||||
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||||
|
|
||||||
private final GroupCollectionToDtoMapper mapper = new GroupCollectionToDtoMapper(groupToDtoMapper, uriInfoStore);
|
private final GroupCollectionToDtoMapper mapper = new GroupCollectionToDtoMapper(groupToDtoMapper, resourceLinks);
|
||||||
|
|
||||||
private URI expectedBaseUri;
|
private URI expectedBaseUri;
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,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 org.mockito.Answers;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
@@ -20,7 +21,6 @@ import sonia.scm.group.GroupManager;
|
|||||||
import sonia.scm.web.VndMediaType;
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
@@ -28,7 +28,9 @@ import java.net.URL;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.doNothing;
|
import static org.mockito.Mockito.doNothing;
|
||||||
@@ -47,10 +49,8 @@ public class GroupRootResourceTest {
|
|||||||
|
|
||||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||||
|
|
||||||
@Mock
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private UriInfo uriInfo;
|
private ResourceLinks resourceLinks;
|
||||||
@Mock
|
|
||||||
private UriInfoStore uriInfoStore;
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private GroupManager groupManager;
|
private GroupManager groupManager;
|
||||||
@@ -70,15 +70,14 @@ public class GroupRootResourceTest {
|
|||||||
when(groupManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(singletonList(group), 1));
|
when(groupManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(singletonList(group), 1));
|
||||||
when(groupManager.get("admin")).thenReturn(group);
|
when(groupManager.get("admin")).thenReturn(group);
|
||||||
|
|
||||||
GroupCollectionToDtoMapper groupCollectionToDtoMapper = new GroupCollectionToDtoMapper(groupToDtoMapper, uriInfoStore);
|
ResourceLinksMock.initMock(resourceLinks, URI.create("/"));
|
||||||
GroupCollectionResource groupCollectionResource = new GroupCollectionResource(groupManager, dtoToGroupMapper, groupCollectionToDtoMapper);
|
|
||||||
|
GroupCollectionToDtoMapper groupCollectionToDtoMapper = new GroupCollectionToDtoMapper(groupToDtoMapper, resourceLinks);
|
||||||
|
GroupCollectionResource groupCollectionResource = new GroupCollectionResource(groupManager, dtoToGroupMapper, groupCollectionToDtoMapper, resourceLinks);
|
||||||
GroupResource groupResource = new GroupResource(groupManager, groupToDtoMapper, dtoToGroupMapper);
|
GroupResource groupResource = new GroupResource(groupManager, groupToDtoMapper, dtoToGroupMapper);
|
||||||
GroupRootResource groupRootResource = new GroupRootResource(MockProvider.of(groupCollectionResource), MockProvider.of(groupResource));
|
GroupRootResource groupRootResource = new GroupRootResource(MockProvider.of(groupCollectionResource), MockProvider.of(groupResource));
|
||||||
|
|
||||||
dispatcher.getRegistry().addSingletonResource(groupRootResource);
|
dispatcher.getRegistry().addSingletonResource(groupRootResource);
|
||||||
|
|
||||||
when(uriInfo.getBaseUri()).thenReturn(URI.create("/"));
|
|
||||||
when(uriInfoStore.get()).thenReturn(uriInfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import org.apache.shiro.util.ThreadState;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Answers;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import sonia.scm.group.Group;
|
import sonia.scm.group.Group;
|
||||||
|
|
||||||
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.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
@@ -24,10 +24,8 @@ import static org.mockito.MockitoAnnotations.initMocks;
|
|||||||
|
|
||||||
public class GroupToGroupDtoMapperTest {
|
public class GroupToGroupDtoMapperTest {
|
||||||
|
|
||||||
@Mock
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private UriInfo uriInfo;
|
private ResourceLinks resourceLinks;
|
||||||
@Mock
|
|
||||||
private UriInfoStore uriInfoStore;
|
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private GroupToGroupDtoMapperImpl mapper;
|
private GroupToGroupDtoMapperImpl mapper;
|
||||||
@@ -42,9 +40,10 @@ public class GroupToGroupDtoMapperTest {
|
|||||||
initMocks(this);
|
initMocks(this);
|
||||||
URI baseUri = new URI("http://example.com/base/");
|
URI baseUri = new URI("http://example.com/base/");
|
||||||
expectedBaseUri = baseUri.resolve(GroupRootResource.GROUPS_PATH_V2 + "/");
|
expectedBaseUri = baseUri.resolve(GroupRootResource.GROUPS_PATH_V2 + "/");
|
||||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
|
||||||
when(uriInfoStore.get()).thenReturn(uriInfo);
|
|
||||||
subjectThreadState.bind();
|
subjectThreadState.bind();
|
||||||
|
|
||||||
|
ResourceLinksMock.initMock(resourceLinks, baseUri);
|
||||||
|
|
||||||
ThreadContext.bind(subject);
|
ThreadContext.bind(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static sonia.scm.api.v2.resources.GroupRootResource.GROUPS_PATH_V2;
|
||||||
|
import static sonia.scm.api.v2.resources.UserRootResource.USERS_PATH_V2;
|
||||||
|
|
||||||
|
public class ResourceLinksMock {
|
||||||
|
public static void initMock(ResourceLinks resourceLinks, URI baseUri) {
|
||||||
|
when(resourceLinks.user().self(anyString())).thenAnswer(invocation -> baseUri + USERS_PATH_V2 + invocation.getArguments()[0]);
|
||||||
|
when(resourceLinks.user().update(anyString())).thenAnswer(invocation -> baseUri + USERS_PATH_V2 + invocation.getArguments()[0]);
|
||||||
|
when(resourceLinks.user().delete(anyString())).thenAnswer(invocation -> baseUri + USERS_PATH_V2 + invocation.getArguments()[0]);
|
||||||
|
|
||||||
|
when(resourceLinks.userCollection().self()).thenAnswer(invocation -> baseUri + USERS_PATH_V2);
|
||||||
|
when(resourceLinks.userCollection().create()).thenAnswer(invocation -> baseUri + USERS_PATH_V2);
|
||||||
|
|
||||||
|
when(resourceLinks.group().self(anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0]);
|
||||||
|
when(resourceLinks.group().update(anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0]);
|
||||||
|
when(resourceLinks.group().delete(anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0]);
|
||||||
|
|
||||||
|
when(resourceLinks.groupCollection().self()).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2);
|
||||||
|
when(resourceLinks.groupCollection().create()).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,85 +2,92 @@ package sonia.scm.api.v2.resources;
|
|||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
|
||||||
import javax.ws.rs.core.UriInfo;
|
import javax.ws.rs.core.UriInfo;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.group;
|
import static org.mockito.MockitoAnnotations.initMocks;
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.user;
|
|
||||||
import static sonia.scm.api.v2.resources.ResourceLinks.userCollection;
|
|
||||||
|
|
||||||
public class ResourceLinksTest {
|
public class ResourceLinksTest {
|
||||||
|
|
||||||
private static final String BASE_URL = "http://example.com/";
|
private static final String BASE_URL = "http://example.com/";
|
||||||
|
|
||||||
private UriInfo uriInfo = mock(UriInfo.class);
|
@Mock
|
||||||
|
private UriInfoStore uriInfoStore;
|
||||||
|
@Mock
|
||||||
|
private UriInfo uriInfo;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private ResourceLinks resourceLinks;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectUserSelfUrl() {
|
public void shouldCreateCorrectUserSelfUrl() {
|
||||||
String url = user(uriInfo).self("ich");
|
String url = resourceLinks.user().self("ich");
|
||||||
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2 + "ich", url);
|
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2 + "ich", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectUserDeleteUrl() {
|
public void shouldCreateCorrectUserDeleteUrl() {
|
||||||
String url = user(uriInfo).delete("ich");
|
String url = resourceLinks.user().delete("ich");
|
||||||
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2 + "ich", url);
|
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2 + "ich", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectUserUpdateUrl() {
|
public void shouldCreateCorrectUserUpdateUrl() {
|
||||||
String url = user(uriInfo).update("ich");
|
String url = resourceLinks.user().update("ich");
|
||||||
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2 + "ich", url);
|
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2 + "ich", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectUserCreateUrl() {
|
public void shouldCreateCorrectUserCreateUrl() {
|
||||||
String url = userCollection(uriInfo).create();
|
String url = resourceLinks.userCollection().create();
|
||||||
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2, url);
|
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectUserCollectionUrl() {
|
public void shouldCreateCorrectUserCollectionUrl() {
|
||||||
String url = userCollection(uriInfo).self();
|
String url = resourceLinks.userCollection().self();
|
||||||
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2, url);
|
assertEquals(BASE_URL + UserRootResource.USERS_PATH_V2, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectGroupSelfUrl() {
|
public void shouldCreateCorrectGroupSelfUrl() {
|
||||||
String url = group(uriInfo).self("nobodies");
|
String url = resourceLinks.group().self("nobodies");
|
||||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectGroupDeleteUrl() {
|
public void shouldCreateCorrectGroupDeleteUrl() {
|
||||||
String url = group(uriInfo).delete("nobodies");
|
String url = resourceLinks.group().delete("nobodies");
|
||||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectGroupUpdateUrl() {
|
public void shouldCreateCorrectGroupUpdateUrl() {
|
||||||
String url = group(uriInfo).update("nobodies");
|
String url = resourceLinks.group().update("nobodies");
|
||||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2 + "nobodies", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectGroupCreateUrl() {
|
public void shouldCreateCorrectGroupCreateUrl() {
|
||||||
String url = ResourceLinks.groupCollection(uriInfo).create();
|
String url = resourceLinks.groupCollection().create();
|
||||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2, url);
|
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateCorrectGroupCollectionUrl() {
|
public void shouldCreateCorrectGroupCollectionUrl() {
|
||||||
String url = ResourceLinks.groupCollection(uriInfo).self();
|
String url = resourceLinks.groupCollection().self();
|
||||||
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2, url);
|
assertEquals(BASE_URL + GroupRootResource.GROUPS_PATH_V2, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initUriInfo() {
|
public void initUriInfo() {
|
||||||
|
initMocks(this);
|
||||||
|
when(uriInfoStore.get()).thenReturn(uriInfo);
|
||||||
when(uriInfo.getBaseUri()).thenReturn(URI.create(BASE_URL));
|
when(uriInfo.getBaseUri()).thenReturn(URI.create(BASE_URL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ import org.apache.shiro.util.ThreadContext;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Answers;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import sonia.scm.PageResult;
|
import sonia.scm.PageResult;
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
|
|
||||||
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;
|
||||||
@@ -28,10 +28,8 @@ import static sonia.scm.PageResult.createPage;
|
|||||||
|
|
||||||
public class UserCollectionToDtoMapperTest {
|
public class UserCollectionToDtoMapperTest {
|
||||||
|
|
||||||
@Mock
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private UriInfo uriInfo;
|
private ResourceLinks resourceLinks;
|
||||||
@Mock
|
|
||||||
private UriInfoStore uriInfoStore;
|
|
||||||
@Mock
|
@Mock
|
||||||
private UserToUserDtoMapper userToDtoMapper;
|
private UserToUserDtoMapper userToDtoMapper;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -49,8 +47,7 @@ public class UserCollectionToDtoMapperTest {
|
|||||||
initMocks(this);
|
initMocks(this);
|
||||||
URI baseUri = new URI("http://example.com/base/");
|
URI baseUri = new URI("http://example.com/base/");
|
||||||
expectedBaseUri = baseUri.resolve(UserRootResource.USERS_PATH_V2 + "/");
|
expectedBaseUri = baseUri.resolve(UserRootResource.USERS_PATH_V2 + "/");
|
||||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
ResourceLinksMock.initMock(resourceLinks, baseUri);
|
||||||
when(uriInfoStore.get()).thenReturn(uriInfo);
|
|
||||||
subjectThreadState.bind();
|
subjectThreadState.bind();
|
||||||
ThreadContext.bind(subject);
|
ThreadContext.bind(subject);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,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 org.mockito.Answers;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
@@ -21,7 +22,6 @@ import sonia.scm.user.UserManager;
|
|||||||
import sonia.scm.web.VndMediaType;
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
@@ -51,10 +51,8 @@ public class UserRootResourceTest {
|
|||||||
|
|
||||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||||
|
|
||||||
@Mock
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private UriInfo uriInfo;
|
private ResourceLinks resourceLinks;
|
||||||
@Mock
|
|
||||||
private UriInfoStore uriInfoStore;
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private PasswordService passwordService;
|
private PasswordService passwordService;
|
||||||
@@ -75,16 +73,16 @@ public class UserRootResourceTest {
|
|||||||
doNothing().when(userManager).modify(userCaptor.capture());
|
doNothing().when(userManager).modify(userCaptor.capture());
|
||||||
doNothing().when(userManager).delete(userCaptor.capture());
|
doNothing().when(userManager).delete(userCaptor.capture());
|
||||||
|
|
||||||
UserCollectionToDtoMapper userCollectionToDtoMapper = new UserCollectionToDtoMapper(userToDtoMapper, uriInfoStore);
|
ResourceLinksMock.initMock(resourceLinks, URI.create("/"));
|
||||||
|
|
||||||
|
UserCollectionToDtoMapper userCollectionToDtoMapper = new UserCollectionToDtoMapper(userToDtoMapper, resourceLinks);
|
||||||
UserCollectionResource userCollectionResource = new UserCollectionResource(userManager, dtoToUserMapper,
|
UserCollectionResource userCollectionResource = new UserCollectionResource(userManager, dtoToUserMapper,
|
||||||
userCollectionToDtoMapper);
|
userCollectionToDtoMapper, resourceLinks);
|
||||||
UserResource userResource = new UserResource(dtoToUserMapper, userToDtoMapper, userManager);
|
UserResource userResource = new UserResource(dtoToUserMapper, userToDtoMapper, userManager);
|
||||||
UserRootResource userRootResource = new UserRootResource(MockProvider.of(userCollectionResource),
|
UserRootResource userRootResource = new UserRootResource(MockProvider.of(userCollectionResource),
|
||||||
MockProvider.of(userResource));
|
MockProvider.of(userResource));
|
||||||
|
|
||||||
dispatcher.getRegistry().addSingletonResource(userRootResource);
|
dispatcher.getRegistry().addSingletonResource(userRootResource);
|
||||||
when(uriInfo.getBaseUri()).thenReturn(URI.create("/"));
|
|
||||||
when(uriInfoStore.get()).thenReturn(uriInfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ import org.apache.shiro.util.ThreadState;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Answers;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import sonia.scm.api.rest.resources.UserResource;
|
import sonia.scm.api.rest.resources.UserResource;
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
|
|
||||||
import javax.ws.rs.core.UriInfo;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@@ -25,10 +25,8 @@ import static org.mockito.MockitoAnnotations.initMocks;
|
|||||||
|
|
||||||
public class UserToUserDtoMapperTest {
|
public class UserToUserDtoMapperTest {
|
||||||
|
|
||||||
@Mock
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private UriInfo uriInfo;
|
private ResourceLinks resourceLinks;
|
||||||
@Mock
|
|
||||||
private UriInfoStore uriInfoStore;
|
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private UserToUserDtoMapperImpl mapper;
|
private UserToUserDtoMapperImpl mapper;
|
||||||
@@ -43,9 +41,8 @@ public class UserToUserDtoMapperTest {
|
|||||||
initMocks(this);
|
initMocks(this);
|
||||||
URI baseUri = new URI("http://example.com/base/");
|
URI baseUri = new URI("http://example.com/base/");
|
||||||
expectedBaseUri = baseUri.resolve(UserRootResource.USERS_PATH_V2 + "/");
|
expectedBaseUri = baseUri.resolve(UserRootResource.USERS_PATH_V2 + "/");
|
||||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
|
||||||
when(uriInfoStore.get()).thenReturn(uriInfo);
|
|
||||||
subjectThreadState.bind();
|
subjectThreadState.bind();
|
||||||
|
ResourceLinksMock.initMock(resourceLinks, baseUri);
|
||||||
ThreadContext.bind(subject);
|
ThreadContext.bind(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user