mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
implemented rest endpoint for repository types
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import de.otto.edison.hal.Embedded;
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import de.otto.edison.hal.Link;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.RepositoryType;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RepositoryTypeCollectionToDtoMapperTest {
|
||||
|
||||
private final URI baseUri = URI.create("https://scm-manager.org/scm/");
|
||||
|
||||
@SuppressWarnings("unused") // Is injected
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@InjectMocks
|
||||
private RepositoryTypeToRepositoryTypeDtoMapperImpl mapper;
|
||||
|
||||
private List<RepositoryType> types = Lists.newArrayList(
|
||||
new RepositoryType("hk", "Hitchhiker", Sets.newHashSet()),
|
||||
new RepositoryType("hog", "Heart of Gold", Sets.newHashSet())
|
||||
);
|
||||
|
||||
private RepositoryTypeCollectionToDtoMapper collectionMapper;
|
||||
|
||||
@Before
|
||||
public void setUpEnvironment() {
|
||||
collectionMapper = new RepositoryTypeCollectionToDtoMapper(mapper, resourceLinks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveEmbeddedDtos() {
|
||||
HalRepresentation mappedTypes = collectionMapper.map(types);
|
||||
Embedded embedded = mappedTypes.getEmbedded();
|
||||
List<RepositoryTypeDto> embeddedTypes = embedded.getItemsBy("repository-types", RepositoryTypeDto.class);
|
||||
assertEquals("hk", embeddedTypes.get(0).getName());
|
||||
assertEquals("hog", embeddedTypes.get(1).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveSelfLink() {
|
||||
HalRepresentation mappedTypes = collectionMapper.map(types);
|
||||
Optional<Link> self = mappedTypes.getLinks().getLinkBy("self");
|
||||
assertEquals("https://scm-manager.org/scm/v2/repository-types/", self.get().getHref());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.jboss.resteasy.core.Dispatcher;
|
||||
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||
import org.jboss.resteasy.mock.MockHttpRequest;
|
||||
import org.jboss.resteasy.mock.MockHttpResponse;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.RepositoryType;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RepositoryTypeRootResourceTest {
|
||||
|
||||
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
|
||||
@Mock
|
||||
private RepositoryManager repositoryManager;
|
||||
|
||||
private final URI baseUri = URI.create("/");
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@InjectMocks
|
||||
private RepositoryTypeToRepositoryTypeDtoMapperImpl mapper;
|
||||
|
||||
private List<RepositoryType> types = Lists.newArrayList(
|
||||
new RepositoryType("hk", "Hitchhiker", Sets.newHashSet()),
|
||||
new RepositoryType("hog", "Heart of Gold", Sets.newHashSet())
|
||||
);
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() {
|
||||
when(repositoryManager.getConfiguredTypes()).thenReturn(types);
|
||||
|
||||
RepositoryTypeCollectionToDtoMapper collectionMapper = new RepositoryTypeCollectionToDtoMapper(mapper, resourceLinks);
|
||||
RepositoryTypeCollectionResource collectionResource = new RepositoryTypeCollectionResource(repositoryManager, collectionMapper);
|
||||
RepositoryTypeResource resource = new RepositoryTypeResource(repositoryManager, mapper);
|
||||
RepositoryTypeRootResource rootResource = new RepositoryTypeRootResource(MockProvider.of(collectionResource), MockProvider.of(resource));
|
||||
dispatcher.getRegistry().addSingletonResource(rootResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveCollectionVndMediaType() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryTypeRootResource.PATH);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
String contentType = response.getOutputHeaders().getFirst("Content-Type").toString();
|
||||
assertThat(VndMediaType.REPOSITORY_TYPE_COLLECTION, equalToIgnoringCase(contentType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveCollectionSelfLink() throws URISyntaxException {
|
||||
String uri = "/" + RepositoryTypeRootResource.PATH;
|
||||
MockHttpRequest request = MockHttpRequest.get(uri);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"" + uri + "\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveEmbeddedRepositoryTypes() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryTypeRootResource.PATH);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("Hitchhiker"));
|
||||
assertTrue(response.getContentAsString().contains("Heart of Gold"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveVndMediaType() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryTypeRootResource.PATH + "hk");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
String contentType = response.getOutputHeaders().getFirst("Content-Type").toString();
|
||||
assertThat(VndMediaType.REPOSITORY_TYPE, equalToIgnoringCase(contentType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldContainAttributes() throws URISyntaxException {
|
||||
String uri = "/" + RepositoryTypeRootResource.PATH + "hk";
|
||||
MockHttpRequest request = MockHttpRequest.get(uri);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("hk"));
|
||||
assertTrue(response.getContentAsString().contains("Hitchhiker"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveSelfLink() throws URISyntaxException {
|
||||
String uri = "/" + RepositoryTypeRootResource.PATH + "hk";
|
||||
MockHttpRequest request = MockHttpRequest.get(uri);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"" + uri + "\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn404OnUnknownTypes() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryTypeRootResource.PATH + "git");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_NOT_FOUND, response.getStatus());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.RepositoryType;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RepositoryTypeToRepositoryTypeDtoMapperTest {
|
||||
|
||||
private final URI baseUri = URI.create("https://scm-manager.org/scm/");
|
||||
|
||||
@SuppressWarnings("unused") // Is injected
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@InjectMocks
|
||||
private RepositoryTypeToRepositoryTypeDtoMapperImpl mapper;
|
||||
|
||||
private RepositoryType type = new RepositoryType("hk", "Hitchhiker", Sets.newHashSet());
|
||||
|
||||
@Test
|
||||
public void shouldMapSimpleProperties() {
|
||||
RepositoryTypeDto dto = mapper.map(type);
|
||||
assertEquals("hk", dto.getName());
|
||||
assertEquals("Hitchhiker", dto.getDisplayName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAppendSelfLink() {
|
||||
RepositoryTypeDto dto = mapper.map(type);
|
||||
assertEquals(
|
||||
"https://scm-manager.org/scm/v2/repository-types/hk",
|
||||
dto.getLinks().getLinkBy("self").get().getHref()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,9 @@ public class ResourceLinksMock {
|
||||
when(resourceLinks.changesetCollection()).thenReturn(new ResourceLinks.ChangesetCollectionLinks(uriInfo));
|
||||
when(resourceLinks.sourceCollection()).thenReturn(new ResourceLinks.SourceCollectionLinks(uriInfo));
|
||||
when(resourceLinks.permissionCollection()).thenReturn(new ResourceLinks.PermissionCollectionLinks(uriInfo));
|
||||
when(resourceLinks.repositoryType()).thenReturn(new ResourceLinks.RepositoryTypeLinks(uriInfo));
|
||||
when(resourceLinks.repositoryTypeCollection()).thenReturn(new ResourceLinks.RepositoryTypeCollectionLinks(uriInfo));
|
||||
|
||||
return resourceLinks;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import sonia.scm.Type;
|
||||
import sonia.scm.api.rest.JSONContextResolver;
|
||||
import sonia.scm.api.rest.ObjectMapperProvider;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.RepositoryType;
|
||||
import sonia.scm.repository.client.api.ClientCommand;
|
||||
import sonia.scm.repository.client.api.RepositoryClient;
|
||||
import sonia.scm.user.User;
|
||||
@@ -140,7 +141,7 @@ public final class IntegrationTestUtil
|
||||
assertEquals("scmadmin", user.getName());
|
||||
assertTrue(user.isAdmin());
|
||||
|
||||
Collection<Type> types = state.getRepositoryTypes();
|
||||
Collection<RepositoryType> types = state.getRepositoryTypes();
|
||||
|
||||
assertNotNull(types);
|
||||
assertFalse(types.isEmpty());
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.junit.Test;
|
||||
|
||||
import sonia.scm.ScmState;
|
||||
import sonia.scm.Type;
|
||||
import sonia.scm.repository.RepositoryType;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserTestData;
|
||||
|
||||
@@ -204,7 +205,7 @@ public class UserITCase extends AbstractAdminITCaseBase
|
||||
assertEquals("scmadmin", user.getName());
|
||||
assertTrue(user.isAdmin());
|
||||
|
||||
Collection<Type> types = state.getRepositoryTypes();
|
||||
Collection<RepositoryType> types = state.getRepositoryTypes();
|
||||
|
||||
assertNotNull(types);
|
||||
assertFalse(types.isEmpty());
|
||||
|
||||
@@ -32,6 +32,7 @@ package sonia.scm.repository;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.inject.Provider;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.AuthenticationException;
|
||||
@@ -109,7 +110,7 @@ public class DefaultRepositoryManagerPerfTest {
|
||||
*/
|
||||
@Before
|
||||
public void setUpObjectUnderTest(){
|
||||
when(repositoryHandler.getType()).thenReturn(new Type(REPOSITORY_TYPE, REPOSITORY_TYPE));
|
||||
when(repositoryHandler.getType()).thenReturn(new RepositoryType(REPOSITORY_TYPE, REPOSITORY_TYPE, Sets.newHashSet()));
|
||||
Set<RepositoryHandler> handlerSet = ImmutableSet.of(repositoryHandler);
|
||||
RepositoryMatcher repositoryMatcher = new RepositoryMatcher(Collections.<RepositoryPathMatcher>emptySet());
|
||||
NamespaceStrategy namespaceStrategy = mock(NamespaceStrategy.class);
|
||||
|
||||
@@ -36,6 +36,7 @@ import com.github.legman.Subscribe;
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.apache.shiro.authz.UnauthorizedException;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -482,14 +483,14 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
handlerSet.add(new DummyRepositoryHandler(factory));
|
||||
handlerSet.add(new DummyRepositoryHandler(factory) {
|
||||
@Override
|
||||
public Type getType() {
|
||||
return new Type("hg", "Mercurial");
|
||||
public RepositoryType getType() {
|
||||
return new RepositoryType("hg", "Mercurial", Sets.newHashSet());
|
||||
}
|
||||
});
|
||||
handlerSet.add(new DummyRepositoryHandler(factory) {
|
||||
@Override
|
||||
public Type getType() {
|
||||
return new Type("git", "Git");
|
||||
public RepositoryType getType() {
|
||||
return new RepositoryType("git", "Git", Sets.newHashSet());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user