mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 10:41:06 +01:00
First step for sub resources
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package sonia.scm.api.rest.resources;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class LinkMapBuilderTest {
|
||||
|
||||
@Path("base")
|
||||
public static class Main {
|
||||
@Path("main/{x}")
|
||||
public Sub sub() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Sub {
|
||||
@Path("sub/{y}/{z}")
|
||||
public Object x() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Path("base")
|
||||
public static class NoParam {
|
||||
@Path("")
|
||||
public Object get() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private UriInfo uriInfo = mock(UriInfo.class);
|
||||
|
||||
@Test
|
||||
public void shouldBuildSimplePath() {
|
||||
LinkMapBuilder builder = new LinkMapBuilder(uriInfo, Main.class);
|
||||
builder
|
||||
.add("link")
|
||||
.method("sub")
|
||||
.parameters("param_x");
|
||||
|
||||
URI actual = builder.getLinkMap().get("link").getHref();
|
||||
assertEquals("http://example.com/base/main/param_x", actual.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBuildPathOverSubResources() {
|
||||
LinkMapBuilder builder = new LinkMapBuilder(uriInfo, Main.class, Sub.class);
|
||||
builder
|
||||
.add("link")
|
||||
.method("sub")
|
||||
.parameters("param_x")
|
||||
.method("x")
|
||||
.parameters("param_y", "param_z");
|
||||
|
||||
URI actual = builder.getLinkMap().get("link").getHref();
|
||||
assertEquals("http://example.com/base/main/param_x/sub/param_y/param_z", actual.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBuildPathWithoutParameters() {
|
||||
LinkMapBuilder builder = new LinkMapBuilder(uriInfo, NoParam.class);
|
||||
builder
|
||||
.add("link")
|
||||
.method("get")
|
||||
.parameters();
|
||||
|
||||
URI actual = builder.getLinkMap().get("link").getHref();
|
||||
assertEquals("http://example.com/base", actual.toString());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void shouldFailForTooManyMethods() {
|
||||
LinkMapBuilder builder = new LinkMapBuilder(uriInfo, Main.class);
|
||||
builder
|
||||
.add("link")
|
||||
.method("sub")
|
||||
.parameters("param_x")
|
||||
.method("x");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setBaseUri() throws URISyntaxException {
|
||||
when(uriInfo.getBaseUri()).thenReturn(new URI("http://example.com/"));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package sonia.scm.api.rest.resources;
|
||||
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.subject.support.SubjectThreadState;
|
||||
import org.apache.shiro.util.ThreadState;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
@@ -10,6 +13,7 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -17,6 +21,8 @@ public class User2UserDtoMapperTest {
|
||||
|
||||
private final User2UserDtoMapper mapper = Mappers.getMapper(User2UserDtoMapper.class);
|
||||
private final UriInfo uriInfo = mock(UriInfo.class);
|
||||
private final Subject subject = mock(Subject.class);
|
||||
private ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||
|
||||
private URI baseUri;
|
||||
|
||||
@@ -24,12 +30,14 @@ public class User2UserDtoMapperTest {
|
||||
public void init() throws URISyntaxException {
|
||||
baseUri = new URI("http://example.com/base/");
|
||||
when(uriInfo.getBaseUri()).thenReturn(baseUri);
|
||||
subjectThreadState.bind();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapLinks() {
|
||||
public void shouldMapLinks_forAdmin() {
|
||||
User user = new User();
|
||||
user.setName("abc");
|
||||
when(subject.hasRole("admin")).thenReturn(true);
|
||||
|
||||
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
||||
|
||||
@@ -39,6 +47,20 @@ public class User2UserDtoMapperTest {
|
||||
assertEquals("expected map with create baseUri", baseUri.resolve("usersnew"), userDto.getLinks().get("create").getHref());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapLinks_forNormalUser() {
|
||||
User user = new User();
|
||||
user.setName("abc");
|
||||
when(subject.hasRole("user")).thenReturn(true);
|
||||
|
||||
UserDto userDto = mapper.userToUserDto(user, uriInfo);
|
||||
|
||||
assertEquals("expected map with self baseUri", baseUri.resolve("usersnew/abc"), userDto.getLinks().get("self").getHref());
|
||||
assertNull("expected map without delete baseUri", userDto.getLinks().get("delete"));
|
||||
assertNull("expected map without update baseUri", userDto.getLinks().get("update"));
|
||||
assertNull("expected map without create baseUri", userDto.getLinks().get("create"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapFields() {
|
||||
User user = new User();
|
||||
|
||||
Reference in New Issue
Block a user