Generate link to branches for repository

This commit is contained in:
René Pfeuffer
2018-07-04 08:37:57 +02:00
parent fb811ef725
commit 7c662ed42a
10 changed files with 92 additions and 5 deletions

View File

@@ -0,0 +1,18 @@
package sonia.scm.api.v2.resources;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
public class BranchCollectionResource {
@GET
@Path("")
public Response getAll(@DefaultValue("0") @QueryParam("page") int page,
@DefaultValue("10") @QueryParam("pageSize") int pageSize,
@QueryParam("sortBy") String sortBy,
@DefaultValue("false") @QueryParam("desc") boolean desc) {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,20 @@
package sonia.scm.api.v2.resources;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.Path;
public class BranchRootResource {
private final Provider<BranchCollectionResource> branchCollectionResource;
@Inject
public BranchRootResource(Provider<BranchCollectionResource> branchCollectionResource) {
this.branchCollectionResource = branchCollectionResource;
}
@Path("")
public BranchCollectionResource getBranchCollectionResource() {
return branchCollectionResource.get();
}
}

View File

@@ -25,13 +25,19 @@ public class RepositoryResource {
private final RepositoryManager manager; private final RepositoryManager manager;
private final SingleResourceManagerAdapter<Repository, RepositoryDto, RepositoryException> adapter; private final SingleResourceManagerAdapter<Repository, RepositoryDto, RepositoryException> adapter;
private final Provider<TagRootResource> tagRootResource; private final Provider<TagRootResource> tagRootResource;
private final Provider<BranchRootResource> branchRootResource;
@Inject @Inject
public RepositoryResource(RepositoryToRepositoryDtoMapper repositoryToDtoMapper, RepositoryManager manager, Provider<TagRootResource> tagRootResource) { public RepositoryResource(
RepositoryToRepositoryDtoMapper repositoryToDtoMapper,
RepositoryManager manager,
Provider<TagRootResource> tagRootResource,
Provider<BranchRootResource> branchRootResource) {
this.manager = manager; this.manager = manager;
this.repositoryToDtoMapper = repositoryToDtoMapper; this.repositoryToDtoMapper = repositoryToDtoMapper;
this.adapter = new SingleResourceManagerAdapter<>(manager); this.adapter = new SingleResourceManagerAdapter<>(manager);
this.tagRootResource = tagRootResource; this.tagRootResource = tagRootResource;
this.branchRootResource = branchRootResource;
} }
@GET @GET
@@ -65,4 +71,9 @@ public class RepositoryResource {
public TagRootResource tags() { public TagRootResource tags() {
return tagRootResource.get(); return tagRootResource.get();
} }
@Path("branches/")
public BranchRootResource branches() {
return branchRootResource.get();
}
} }

View File

@@ -35,6 +35,7 @@ public abstract class RepositoryToRepositoryDtoMapper extends BaseMapper<Reposit
linksBuilder.single(link("update", resourceLinks.repository().update(target.getNamespace(), target.getName()))); linksBuilder.single(link("update", resourceLinks.repository().update(target.getNamespace(), target.getName())));
} }
linksBuilder.single(link("tags", resourceLinks.tagCollection().self(target.getNamespace(), target.getName()))); linksBuilder.single(link("tags", resourceLinks.tagCollection().self(target.getNamespace(), target.getName())));
linksBuilder.single(link("branches", resourceLinks.branchCollection().self(target.getNamespace(), target.getName())));
target.add(linksBuilder.build()); target.add(linksBuilder.build());
} }
} }

View File

@@ -130,14 +130,30 @@ class ResourceLinks {
} }
static class TagCollectionLinks { static class TagCollectionLinks {
private final LinkBuilder repositoryLinkBuilder; private final LinkBuilder tagLinkBuilder;
private TagCollectionLinks(UriInfo uriInfo) { private TagCollectionLinks(UriInfo uriInfo) {
repositoryLinkBuilder = new LinkBuilder(uriInfo, RepositoryRootResource.class, RepositoryResource.class, TagRootResource.class, TagCollectionResource.class); tagLinkBuilder = new LinkBuilder(uriInfo, RepositoryRootResource.class, RepositoryResource.class, TagRootResource.class, TagCollectionResource.class);
} }
String self(String namespace, String name) { String self(String namespace, String name) {
return repositoryLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("tags").parameters().method("getTagCollectionResource").parameters().method("getAll").parameters().href(); return tagLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("tags").parameters().method("getTagCollectionResource").parameters().method("getAll").parameters().href();
}
}
public BranchCollectionLinks branchCollection() {
return new BranchCollectionLinks(uriInfoStore.get());
}
static class BranchCollectionLinks {
private final LinkBuilder branchLinkBuilder;
private BranchCollectionLinks(UriInfo uriInfo) {
branchLinkBuilder = new LinkBuilder(uriInfo, RepositoryRootResource.class, RepositoryResource.class, BranchRootResource.class, BranchCollectionResource.class);
}
String self(String namespace, String name) {
return branchLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("branches").parameters().method("getBranchCollectionResource").parameters().method("getAll").parameters().href();
} }
} }
} }

View File

@@ -48,7 +48,7 @@ public class RepositoryRootResourceTest {
public void prepareEnvironment() { public void prepareEnvironment() {
initMocks(this); initMocks(this);
ResourceLinksMock.initMock(resourceLinks, URI.create("/")); ResourceLinksMock.initMock(resourceLinks, URI.create("/"));
RepositoryResource repositoryResource = new RepositoryResource(repositoryToDtoMapper, repositoryManager, null); RepositoryResource repositoryResource = new RepositoryResource(repositoryToDtoMapper, repositoryManager, null, null);
RepositoryRootResource repositoryRootResource = new RepositoryRootResource(MockProvider.of(repositoryResource)); RepositoryRootResource repositoryRootResource = new RepositoryRootResource(MockProvider.of(repositoryResource));
dispatcher.getRegistry().addSingletonResource(repositoryRootResource); dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
} }

View File

@@ -109,6 +109,14 @@ public class RepositoryToRepositoryDtoMapperTest {
dto.getLinks().getLinkBy("tags").get().getHref()); dto.getLinks().getLinkBy("tags").get().getHref());
} }
@Test
public void shouldCreateBranchesLink() {
RepositoryDto dto = mapper.map(createTestRepository());
assertEquals(
"http://example.com/base/v2/groups/testspace/test/branches/",
dto.getLinks().getLinkBy("branches").get().getHref());
}
private Repository createTestRepository() { private Repository createTestRepository() {
Repository repository = new Repository(); Repository repository = new Repository();
repository.setNamespace("testspace"); repository.setNamespace("testspace");

View File

@@ -28,5 +28,7 @@ public class ResourceLinksMock {
when(resourceLinks.repository().delete(anyString(), anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0] + "/" + invocation.getArguments()[1]); when(resourceLinks.repository().delete(anyString(), anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0] + "/" + invocation.getArguments()[1]);
when(resourceLinks.tagCollection().self(anyString(), anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0] + "/" + invocation.getArguments()[1] + "/tags/"); when(resourceLinks.tagCollection().self(anyString(), anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0] + "/" + invocation.getArguments()[1] + "/tags/");
when(resourceLinks.branchCollection().self(anyString(), anyString())).thenAnswer(invocation -> baseUri + GROUPS_PATH_V2 + invocation.getArguments()[0] + "/" + invocation.getArguments()[1] + "/branches/");
} }
} }

View File

@@ -108,6 +108,12 @@ public class ResourceLinksTest {
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/tags/", url); assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/tags/", url);
} }
@Test
public void shouldCreateCorrectBranchCollectionUrl() {
String url = resourceLinks.branchCollection().self("space", "repo");
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/branches/", url);
}
@Before @Before
public void initUriInfo() { public void initUriInfo() {
initMocks(this); initMocks(this);

View File

@@ -37,6 +37,7 @@ import com.github.sdorra.shiro.ShiroRule;
import com.github.sdorra.shiro.SubjectAware; import com.github.sdorra.shiro.SubjectAware;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.apache.shiro.authz.UnauthorizedException; import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.util.ThreadContext;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
@@ -92,6 +93,10 @@ import static org.mockito.Mockito.when;
) )
public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, RepositoryException> { public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, RepositoryException> {
{
ThreadContext.unbindSecurityManager();
}
@Rule @Rule
public ShiroRule shiro = new ShiroRule(); public ShiroRule shiro = new ShiroRule();