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