merge with branch feature/repositories-ui

This commit is contained in:
Sebastian Sdorra
2018-08-07 16:34:26 +02:00
93 changed files with 3954 additions and 275 deletions

View File

@@ -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("repositoryTypes", 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/repositoryTypes/", self.get().getHref());
}
}

View File

@@ -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());
}
}

View File

@@ -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/repositoryTypes/hk",
dto.getLinks().getLinkBy("self").get().getHref()
);
}
}

View File

@@ -25,6 +25,9 @@ public class ResourceLinksMock {
when(resourceLinks.sourceCollection()).thenReturn(new ResourceLinks.SourceCollectionLinks(uriInfo));
when(resourceLinks.permissionCollection()).thenReturn(new ResourceLinks.PermissionCollectionLinks(uriInfo));
when(resourceLinks.config()).thenReturn(new ResourceLinks.ConfigLinks(uriInfo));
when(resourceLinks.repositoryType()).thenReturn(new ResourceLinks.RepositoryTypeLinks(uriInfo));
when(resourceLinks.repositoryTypeCollection()).thenReturn(new ResourceLinks.RepositoryTypeCollectionLinks(uriInfo));
return resourceLinks;
}
}

View File

@@ -65,7 +65,7 @@ import java.util.Collection;
*/
public final class IntegrationTestUtil
{
public static final Person AUTHOR = new Person("SCM Administrator", "scmadmin@scm-manager.org");
/** Field description */
@@ -73,10 +73,10 @@ public final class IntegrationTestUtil
/** Field description */
public static final String ADMIN_USERNAME = "scmadmin";
/** scm-manager base url */
public static final String BASE_URL = "http://localhost:8081/scm/";
/** scm-manager base url for the rest api */
public static final String REST_BASE_URL = BASE_URL.concat("api/rest/v2/");
@@ -163,7 +163,7 @@ public final class IntegrationTestUtil
{
return URI.create(REST_BASE_URL).resolve(url);
}
public static String readJson(String jsonFileName) {
URL url = Resources.getResource(jsonFileName);
try {

View File

@@ -17,8 +17,16 @@ public class DefaultNamespaceStrategyTest {
@Test
@SubjectAware(username = "trillian", password = "secret")
public void testNamespaceStrategy() {
assertEquals("trillian", namespaceStrategy.getNamespace());
public void testNamespaceStrategyWithoutPreset() {
assertEquals("trillian", namespaceStrategy.createNamespace(new Repository()));
}
@Test
@SubjectAware(username = "trillian", password = "secret")
public void testNamespaceStrategyWithPreset() {
Repository repository = new Repository();
repository.setNamespace("awesome");
assertEquals("awesome", namespaceStrategy.createNamespace(repository));
}
}

View File

@@ -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);

View File

@@ -36,10 +36,12 @@ 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;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import sonia.scm.*;
import sonia.scm.config.ScmConfiguration;
@@ -57,7 +59,14 @@ import sonia.scm.store.JAXBConfigurationStoreFactory;
import java.util.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -66,7 +75,7 @@ import static org.mockito.Mockito.when;
/**
* Unit tests for {@link DefaultRepositoryManager}.
*
*
* @author Sebastian Sdorra
*/
@SubjectAware(
@@ -300,7 +309,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
manager.create(heartOfGold);
heartOfGold.setDescription("prototype ship");
thrown.expect(UnauthorizedException.class);
manager.modify(heartOfGold);
}
@@ -326,7 +335,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
manager.create(heartOfGold);
heartOfGold.setDescription("prototype ship");
thrown.expect(UnauthorizedException.class);
manager.refresh(heartOfGold);
}
@@ -449,7 +458,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
}
//~--- methods --------------------------------------------------------------
@Override
protected DefaultRepositoryManager createManager() {
return createRepositoryManager(false);
@@ -465,14 +474,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());
}
});
@@ -483,12 +492,12 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
configuration.setEnableRepositoryArchive(archiveEnabled);
NamespaceStrategy namespaceStrategy = mock(NamespaceStrategy.class);
when(namespaceStrategy.getNamespace()).thenAnswer(invocation -> mockedNamespace);
when(namespaceStrategy.createNamespace(Mockito.any(Repository.class))).thenAnswer(invocation -> mockedNamespace);
return new DefaultRepositoryManager(configuration, contextProvider,
keyGenerator, repositoryDAO, handlerSet, createRepositoryMatcher(), namespaceStrategy);
}
private void createRepository(RepositoryManager m, Repository repository)
throws RepositoryException {
m.create(repository);
@@ -503,7 +512,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
return new HookContextFactory(ppu).createContext(provider, repository);
}
private void assertRepositoriesEquals(Repository repo, Repository other) {
assertEquals(repo.getId(), other.getId());
assertEquals(repo.getName(), other.getName());
@@ -512,10 +521,10 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
assertEquals(repo.getCreationDate(), other.getCreationDate());
assertEquals(repo.getLastModified(), other.getLastModified());
}
private RepositoryMatcher createRepositoryMatcher() {
return new RepositoryMatcher(Collections.<RepositoryPathMatcher>emptySet());
}
}
private Repository createRepository(Repository repository) throws RepositoryException {
manager.create(repository);