mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Create resource collection endpoint for get
This commit is contained in:
@@ -17,6 +17,7 @@ public class VndMediaType {
|
|||||||
public static final String REPOSITORY = PREFIX + "repository" + SUFFIX;
|
public static final String REPOSITORY = PREFIX + "repository" + SUFFIX;
|
||||||
public static final String USER_COLLECTION = PREFIX + "userCollection" + SUFFIX;
|
public static final String USER_COLLECTION = PREFIX + "userCollection" + SUFFIX;
|
||||||
public static final String GROUP_COLLECTION = PREFIX + "groupCollection" + SUFFIX;
|
public static final String GROUP_COLLECTION = PREFIX + "groupCollection" + SUFFIX;
|
||||||
|
public static final String REPOSITORY_COLLECTION = PREFIX + "repositoryCollection" + SUFFIX;
|
||||||
|
|
||||||
private VndMediaType() {
|
private VndMediaType() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||||
|
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||||
|
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
import sonia.scm.repository.RepositoryException;
|
||||||
|
import sonia.scm.repository.RepositoryManager;
|
||||||
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.ws.rs.DefaultValue;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
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.Response;
|
||||||
|
|
||||||
|
public class RepositoryCollectionResource {
|
||||||
|
|
||||||
|
private final CollectionResourceManagerAdapter<Repository, RepositoryDto, RepositoryException> adapter;
|
||||||
|
private final RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public RepositoryCollectionResource(RepositoryManager manager, RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper) {
|
||||||
|
this.adapter = new CollectionResourceManagerAdapter<>(manager);
|
||||||
|
this.repositoryCollectionToDtoMapper = repositoryCollectionToDtoMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("")
|
||||||
|
@Produces(VndMediaType.REPOSITORY_COLLECTION)
|
||||||
|
@TypeHint(UserDto[].class)
|
||||||
|
@StatusCodes({
|
||||||
|
@ResponseCode(code = 200, condition = "success"),
|
||||||
|
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
|
||||||
|
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"repository\" privilege"),
|
||||||
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
|
})
|
||||||
|
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) {
|
||||||
|
return adapter.getAll(page, pageSize, sortBy, desc,
|
||||||
|
pageResult -> repositoryCollectionToDtoMapper.map(page, pageSize, pageResult));
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("")
|
||||||
|
public Response create() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import sonia.scm.repository.Repository;
|
||||||
|
import sonia.scm.repository.RepositoryPermissions;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
|
||||||
|
@SuppressWarnings("squid:S3306")
|
||||||
|
public class RepositoryCollectionToDtoMapper extends BasicCollectionToDtoMapper<Repository, RepositoryDto> {
|
||||||
|
|
||||||
|
private final ResourceLinks resourceLinks;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public RepositoryCollectionToDtoMapper(RepositoryToRepositoryDtoMapper repositoryToDtoMapper, ResourceLinks resourceLinks) {
|
||||||
|
super("repositories", repositoryToDtoMapper);
|
||||||
|
this.resourceLinks = resourceLinks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String createCreateLink() {
|
||||||
|
return resourceLinks.repositoryCollection().create();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String createSelfLink() {
|
||||||
|
return resourceLinks.repositoryCollection().self();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
boolean isCreatePermitted() {
|
||||||
|
return RepositoryPermissions.create().isPermitted();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,14 +12,21 @@ public class RepositoryRootResource {
|
|||||||
static final String REPOSITORIES_PATH_V2 = "v2/repositories/";
|
static final String REPOSITORIES_PATH_V2 = "v2/repositories/";
|
||||||
|
|
||||||
private final Provider<RepositoryResource> repositoryResource;
|
private final Provider<RepositoryResource> repositoryResource;
|
||||||
|
private final Provider<RepositoryCollectionResource> repositoryCollectionResource;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RepositoryRootResource(Provider<RepositoryResource> repositoryResource) {
|
public RepositoryRootResource(Provider<RepositoryResource> repositoryResource, Provider<RepositoryCollectionResource> repositoryCollectionResource) {
|
||||||
this.repositoryResource = repositoryResource;
|
this.repositoryResource = repositoryResource;
|
||||||
|
this.repositoryCollectionResource = repositoryCollectionResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Path("{namespace}/{name}")
|
@Path("{namespace}/{name}")
|
||||||
public RepositoryResource getRepositoryResource() {
|
public RepositoryResource getRepositoryResource() {
|
||||||
return repositoryResource.get();
|
return repositoryResource.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Path("")
|
||||||
|
public RepositoryCollectionResource getRepositoryCollectionResource() {
|
||||||
|
return repositoryCollectionResource.get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,6 +125,26 @@ class ResourceLinks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RepositoryCollectionLinks repositoryCollection() {
|
||||||
|
return new RepositoryCollectionLinks(uriInfoStore.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
static class RepositoryCollectionLinks {
|
||||||
|
private final LinkBuilder collectionLinkBuilder;
|
||||||
|
|
||||||
|
RepositoryCollectionLinks(UriInfo uriInfo) {
|
||||||
|
collectionLinkBuilder = new LinkBuilder(uriInfo, RepositoryRootResource.class, RepositoryCollectionResource.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
String self() {
|
||||||
|
return collectionLinkBuilder.method("getRepositoryCollectionResource").parameters().method("getAll").parameters().href();
|
||||||
|
}
|
||||||
|
|
||||||
|
String create() {
|
||||||
|
return collectionLinkBuilder.method("getRepositoryCollectionResource").parameters().method("create").parameters().href();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public TagCollectionLinks tagCollection() {
|
public TagCollectionLinks tagCollection() {
|
||||||
return new TagCollectionLinks(uriInfoStore.get());
|
return new TagCollectionLinks(uriInfoStore.get());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,16 +11,20 @@ import org.junit.Rule;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import sonia.scm.PageResult;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.RepositoryManager;
|
import sonia.scm.repository.RepositoryManager;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
||||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.mockito.MockitoAnnotations.initMocks;
|
import static org.mockito.MockitoAnnotations.initMocks;
|
||||||
|
|
||||||
@@ -49,7 +53,9 @@ public class RepositoryRootResourceTest {
|
|||||||
public void prepareEnvironment() {
|
public void prepareEnvironment() {
|
||||||
initMocks(this);
|
initMocks(this);
|
||||||
RepositoryResource repositoryResource = new RepositoryResource(repositoryToDtoMapper, repositoryManager, null, null, null, null, null);
|
RepositoryResource repositoryResource = new RepositoryResource(repositoryToDtoMapper, repositoryManager, null, null, null, null, null);
|
||||||
RepositoryRootResource repositoryRootResource = new RepositoryRootResource(MockProvider.of(repositoryResource));
|
RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper = new RepositoryCollectionToDtoMapper(repositoryToDtoMapper, resourceLinks);
|
||||||
|
RepositoryCollectionResource repositoryCollectionResource = new RepositoryCollectionResource(repositoryManager, repositoryCollectionToDtoMapper);
|
||||||
|
RepositoryRootResource repositoryRootResource = new RepositoryRootResource(MockProvider.of(repositoryResource), MockProvider.of(repositoryCollectionResource));
|
||||||
dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
|
dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +84,24 @@ public class RepositoryRootResourceTest {
|
|||||||
assertTrue(response.getContentAsString().contains("\"name\":\"repo\""));
|
assertTrue(response.getContentAsString().contains("\"name\":\"repo\""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldGetAll() throws URISyntaxException {
|
||||||
|
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));
|
||||||
|
when(repositoryManager.getPage(any(), eq(0), eq(10))).thenReturn(singletonPageResult);
|
||||||
|
|
||||||
|
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
|
||||||
|
assertEquals(SC_OK, response.getStatus());
|
||||||
|
assertTrue(response.getContentAsString().contains("\"name\":\"repo\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
private PageResult<Repository> createSingletonPageResult(Repository repository) {
|
||||||
|
return new PageResult<>(singletonList(repository), 0);
|
||||||
|
}
|
||||||
|
|
||||||
private Repository mockRepository(String namespace, String name) {
|
private Repository mockRepository(String namespace, String name) {
|
||||||
Repository repository = new Repository();
|
Repository repository = new Repository();
|
||||||
repository.setNamespace(namespace);
|
repository.setNamespace(namespace);
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public class ResourceLinksMock {
|
|||||||
when(resourceLinks.group()).thenReturn(new ResourceLinks.GroupLinks(uriInfo));
|
when(resourceLinks.group()).thenReturn(new ResourceLinks.GroupLinks(uriInfo));
|
||||||
when(resourceLinks.groupCollection()).thenReturn(new ResourceLinks.GroupCollectionLinks(uriInfo));
|
when(resourceLinks.groupCollection()).thenReturn(new ResourceLinks.GroupCollectionLinks(uriInfo));
|
||||||
when(resourceLinks.repository()).thenReturn(new ResourceLinks.RepositoryLinks(uriInfo));
|
when(resourceLinks.repository()).thenReturn(new ResourceLinks.RepositoryLinks(uriInfo));
|
||||||
|
when(resourceLinks.repositoryCollection()).thenReturn(new ResourceLinks.RepositoryCollectionLinks(uriInfo));
|
||||||
when(resourceLinks.tagCollection()).thenReturn(new ResourceLinks.TagCollectionLinks(uriInfo));
|
when(resourceLinks.tagCollection()).thenReturn(new ResourceLinks.TagCollectionLinks(uriInfo));
|
||||||
when(resourceLinks.branchCollection()).thenReturn(new ResourceLinks.BranchCollectionLinks(uriInfo));
|
when(resourceLinks.branchCollection()).thenReturn(new ResourceLinks.BranchCollectionLinks(uriInfo));
|
||||||
when(resourceLinks.changesetCollection()).thenReturn(new ResourceLinks.ChangesetCollectionLinks(uriInfo));
|
when(resourceLinks.changesetCollection()).thenReturn(new ResourceLinks.ChangesetCollectionLinks(uriInfo));
|
||||||
|
|||||||
Reference in New Issue
Block a user