Introduce extension point for further protocol implementations

This commit is contained in:
René Pfeuffer
2018-09-13 10:35:10 +02:00
parent 7c76f7a699
commit 145502a7b8
19 changed files with 137 additions and 153 deletions

View File

@@ -8,38 +8,43 @@ import sonia.scm.repository.spi.RepositoryServiceProvider;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.util.IterableUtil.sizeOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class RepositoryServiceTest {
private final RepositoryServiceProvider provider = mock(RepositoryServiceProvider.class);
private final Repository repository = mock(Repository.class);
private final Repository repository = new Repository("", "git", "space", "repo");
@Test
public void shouldReturnProtocolsFromProvider() {
when(provider.getSupportedProtocols()).thenReturn(Collections.singleton(new DummyHttpProtocol(repository)));
public void shouldReturnMatchingProtocolsFromProvider() {
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null, Collections.singleton(new DummyScmProtocolProvider()));
Stream<ScmProtocol> supportedProtocols = repositoryService.getSupportedProtocols();
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null);
Collection<ScmProtocol> supportedProtocols = repositoryService.getSupportedProtocols();
assertThat(sizeOf(supportedProtocols.collect(Collectors.toList()))).isEqualTo(1);
}
assertThat(sizeOf(supportedProtocols)).isEqualTo(1);
@Test
public void shouldFindKnownProtocol() {
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null, Collections.singleton(new DummyScmProtocolProvider()));
HttpScmProtocol protocol = repositoryService.getProtocol(HttpScmProtocol.class);
assertThat(protocol).isNotNull();
}
@Test
public void shouldFailForUnknownProtocol() {
when(provider.getSupportedProtocols()).thenReturn(Collections.emptySet());
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null);
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null, Collections.singleton(new DummyScmProtocolProvider()));
assertThrows(IllegalArgumentException.class, () -> {
repositoryService.getProtocol(HttpScmProtocol.class);
repositoryService.getProtocol(UnknownScmProtocol.class);
});
}
@@ -52,4 +57,18 @@ public class RepositoryServiceTest {
public void serve(HttpServletRequest request, HttpServletResponse response, Repository repository, ServletConfig config) {
}
}
private static class DummyScmProtocolProvider implements ScmProtocolProvider {
@Override
public String getType() {
return "git";
}
@Override
public ScmProtocol get(Repository repository) {
return new DummyHttpProtocol(repository);
}
}
private interface UnknownScmProtocol extends ScmProtocol {}
}

View File

@@ -62,7 +62,12 @@ public class InitializingHttpScmProtocolWrapperTest {
pathInfoStoreProvider = mock(Provider.class);
when(pathInfoStoreProvider.get()).thenReturn(pathInfoStore);
wrapper = new InitializingHttpScmProtocolWrapper(Providers.of(this.delegateServlet), Providers.of(permissionFilter), pathInfoStoreProvider, scmConfiguration) {};
wrapper = new InitializingHttpScmProtocolWrapper(Providers.of(this.delegateServlet), Providers.of(permissionFilter), pathInfoStoreProvider, scmConfiguration) {
@Override
public String getType() {
return "git";
}
};
when(scmConfiguration.getBaseUrl()).thenReturn("http://example.com/scm");
}
@@ -127,6 +132,11 @@ public class InitializingHttpScmProtocolWrapperTest {
verify(delegateServlet, times(2)).service(request, response, REPOSITORY);
}
@Test(expected = IllegalArgumentException.class)
public void shouldFailForIllegalScmType() {
HttpScmProtocol httpScmProtocol = wrapper.get(new Repository("", "other", "space", "name"));
}
private Answer proceedInvocation() {
return invocation -> {
((PermissionFilter.ContinuationServlet) invocation.getArgument(3)).doService();
@@ -135,12 +145,7 @@ public class InitializingHttpScmProtocolWrapperTest {
}
private OngoingStubbing<ScmPathInfo> mockSetPathInfo() {
return when(pathInfoStore.get()).thenReturn(new ScmPathInfo() {
@Override
public URI getApiRestUri() {
return URI.create("http://example.com/scm/api/rest/");
}
});
return when(pathInfoStore.get()).thenReturn(() -> URI.create("http://example.com/scm/api/rest/"));
}
}