mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
Merge with 2.0.0-m3
sonia.scm.it.RepositoryHookITCase breaks for hg.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package sonia.scm;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ForwardingPushStateDispatcherTest {
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Mock
|
||||
private RequestDispatcher requestDispatcher;
|
||||
|
||||
@Mock
|
||||
private HttpServletResponse response;
|
||||
|
||||
private ForwardingPushStateDispatcher dispatcher = new ForwardingPushStateDispatcher();
|
||||
|
||||
@Test
|
||||
public void testDispatch() throws ServletException, IOException {
|
||||
when(request.getRequestDispatcher("/index.html")).thenReturn(requestDispatcher);
|
||||
|
||||
dispatcher.dispatch(request, response, "/something");
|
||||
|
||||
verify(requestDispatcher).forward(request, response);
|
||||
}
|
||||
|
||||
@Test(expected = IOException.class)
|
||||
public void testWrapServletException() throws ServletException, IOException {
|
||||
when(request.getRequestDispatcher("/index.html")).thenReturn(requestDispatcher);
|
||||
doThrow(ServletException.class).when(requestDispatcher).forward(request, response);
|
||||
|
||||
dispatcher.dispatch(request, response, "/something");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package sonia.scm;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ProxyPushStateDispatcherTest {
|
||||
|
||||
private ProxyPushStateDispatcher dispatcher;
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Mock
|
||||
private HttpServletResponse response;
|
||||
|
||||
@Mock
|
||||
private HttpURLConnection connection;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
dispatcher = new ProxyPushStateDispatcher("http://hitchhiker.com", url -> connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithGetRequest() throws IOException {
|
||||
// configure request mock
|
||||
when(request.getMethod()).thenReturn("GET");
|
||||
when(request.getHeaderNames()).thenReturn(toEnum("Content-Type"));
|
||||
when(request.getHeaders("Content-Type")).thenReturn(toEnum("application/json"));
|
||||
|
||||
// configure proxy url connection mock
|
||||
byte[] data = "hitchhicker".getBytes(Charsets.UTF_8);
|
||||
when(connection.getErrorStream()).thenReturn(null);
|
||||
when(connection.getInputStream()).thenReturn(new ByteArrayInputStream(data));
|
||||
|
||||
Map<String, List<String>> headerFields = new HashMap<>();
|
||||
headerFields.put("Content-Type", Lists.newArrayList("application/yaml"));
|
||||
when(connection.getHeaderFields()).thenReturn(headerFields);
|
||||
when(connection.getResponseCode()).thenReturn(200);
|
||||
when(connection.getContentLength()).thenReturn(data.length);
|
||||
|
||||
// configure response mock
|
||||
DevServletOutputStream output = new DevServletOutputStream();
|
||||
when(response.getOutputStream()).thenReturn(output);
|
||||
|
||||
dispatcher.dispatch(request, response, "/people/trillian");
|
||||
|
||||
// verify connection
|
||||
verify(connection).setRequestMethod("GET");
|
||||
verify(connection).setRequestProperty("Content-Type", "application/json");
|
||||
|
||||
// verify response
|
||||
verify(response).setStatus(200);
|
||||
verify(response).addHeader("Content-Type", "application/yaml");
|
||||
|
||||
assertEquals("hitchhicker", output.stream.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNotFound() throws IOException {
|
||||
// configure request mock
|
||||
when(request.getMethod()).thenReturn("GET");
|
||||
when(request.getHeaderNames()).thenReturn(toEnum("Content-Type"));
|
||||
when(request.getHeaders("Content-Type")).thenReturn(toEnum("application/json"));
|
||||
|
||||
// configure proxy url connection mock
|
||||
byte[] data = "hitchhicker".getBytes(Charsets.UTF_8);
|
||||
when(connection.getErrorStream()).thenReturn(new ByteArrayInputStream(data));
|
||||
|
||||
Map<String, List<String>> headerFields = new HashMap<>();
|
||||
headerFields.put("Content-Type", Lists.newArrayList("application/yaml"));
|
||||
when(connection.getHeaderFields()).thenReturn(headerFields);
|
||||
when(connection.getResponseCode()).thenReturn(404);
|
||||
when(connection.getContentLength()).thenReturn(data.length);
|
||||
|
||||
// configure response mock
|
||||
DevServletOutputStream output = new DevServletOutputStream();
|
||||
when(response.getOutputStream()).thenReturn(output);
|
||||
|
||||
dispatcher.dispatch(request, response, "/people/trillian");
|
||||
|
||||
// verify connection
|
||||
verify(connection).setRequestMethod("GET");
|
||||
verify(connection).setRequestProperty("Content-Type", "application/json");
|
||||
|
||||
// verify response
|
||||
verify(response).setStatus(404);
|
||||
verify(response).addHeader("Content-Type", "application/yaml");
|
||||
|
||||
assertEquals("hitchhicker", output.stream.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithPOSTRequest() throws IOException {
|
||||
// configure request mock
|
||||
when(request.getMethod()).thenReturn("POST");
|
||||
when(request.getHeaderNames()).thenReturn(toEnum());
|
||||
when(request.getInputStream()).thenReturn(new DevServletInputStream("hitchhiker"));
|
||||
when(request.getContentLength()).thenReturn(1);
|
||||
|
||||
// configure proxy url connection mock
|
||||
Map<String, List<String>> headerFields = new HashMap<>();
|
||||
when(connection.getHeaderFields()).thenReturn(headerFields);
|
||||
when(connection.getResponseCode()).thenReturn(204);
|
||||
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
when(connection.getOutputStream()).thenReturn(output);
|
||||
|
||||
dispatcher.dispatch(request, response, "/people/trillian");
|
||||
|
||||
// verify connection
|
||||
verify(connection).setRequestMethod("POST");
|
||||
assertEquals("hitchhiker", output.toString());
|
||||
|
||||
// verify response
|
||||
verify(response).setStatus(204);
|
||||
}
|
||||
|
||||
private Enumeration<String> toEnum(String... values) {
|
||||
Set<String> set = ImmutableSet.copyOf(values);
|
||||
return toEnum(set);
|
||||
}
|
||||
|
||||
private <T> Enumeration<T> toEnum(Collection<T> collection) {
|
||||
return new Vector<>(collection).elements();
|
||||
}
|
||||
|
||||
private class DevServletInputStream extends ServletInputStream {
|
||||
|
||||
private InputStream inputStream;
|
||||
|
||||
private DevServletInputStream(String content) {
|
||||
inputStream = new ByteArrayInputStream(content.getBytes(Charsets.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return inputStream.read();
|
||||
}
|
||||
}
|
||||
|
||||
private class DevServletOutputStream extends ServletOutputStream {
|
||||
|
||||
private ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
stream.write(b);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package sonia.scm;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PushStateDispatcherProviderTest {
|
||||
|
||||
private PushStateDispatcherProvider provider = new PushStateDispatcherProvider();
|
||||
|
||||
@Test
|
||||
public void testGetProxyPushStateWithPropertySet() {
|
||||
System.setProperty(PushStateDispatcherProvider.PROPERTY_TARGET, "http://localhost:9966");
|
||||
PushStateDispatcher dispatcher = provider.get();
|
||||
Assertions.assertThat(dispatcher).isInstanceOf(ProxyPushStateDispatcher.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProxyPushStateWithoutProperty() {
|
||||
PushStateDispatcher dispatcher = provider.get();
|
||||
Assertions.assertThat(dispatcher).isInstanceOf(ForwardingPushStateDispatcher.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupSystemProperty() {
|
||||
System.clearProperty(PushStateDispatcherProvider.PROPERTY_TARGET);
|
||||
}
|
||||
|
||||
}
|
||||
126
scm-webapp/src/test/java/sonia/scm/WebResourceServletTest.java
Normal file
126
scm-webapp/src/test/java/sonia/scm/WebResourceServletTest.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package sonia.scm;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
import sonia.scm.plugin.UberWebResourceLoader;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class WebResourceServletTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Mock
|
||||
private HttpServletResponse response;
|
||||
|
||||
@Mock
|
||||
private PluginLoader pluginLoader;
|
||||
|
||||
@Mock
|
||||
private UberWebResourceLoader webResourceLoader;
|
||||
|
||||
@Mock
|
||||
private PushStateDispatcher pushStateDispatcher;
|
||||
|
||||
private WebResourceServlet servlet;
|
||||
|
||||
@Before
|
||||
public void setUpMocks() {
|
||||
when(pluginLoader.getUberWebResourceLoader()).thenReturn(webResourceLoader);
|
||||
when(request.getContextPath()).thenReturn("/scm");
|
||||
servlet = new WebResourceServlet(pluginLoader, pushStateDispatcher);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPattern() {
|
||||
assertTrue("/some/resource".matches(WebResourceServlet.PATTERN));
|
||||
assertTrue("/favicon.ico".matches(WebResourceServlet.PATTERN));
|
||||
assertTrue("/other.html".matches(WebResourceServlet.PATTERN));
|
||||
assertFalse("/api/v2/repositories".matches(WebResourceServlet.PATTERN));
|
||||
|
||||
// exclude old style ui template servlets
|
||||
assertTrue("/".matches(WebResourceServlet.PATTERN));
|
||||
assertTrue("/index.html".matches(WebResourceServlet.PATTERN));
|
||||
assertTrue("/error.html".matches(WebResourceServlet.PATTERN));
|
||||
assertTrue("/plugins/resources/js/sonia/scm/hg.config-wizard.js".matches(WebResourceServlet.PATTERN));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoGetWithNonExistingResource() throws IOException {
|
||||
when(request.getRequestURI()).thenReturn("/scm/awesome.jpg");
|
||||
servlet.doGet(request, response);
|
||||
verify(pushStateDispatcher).dispatch(request, response, "/awesome.jpg");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDoGet() throws IOException {
|
||||
when(request.getRequestURI()).thenReturn("/scm/README.txt");
|
||||
TestingOutputServletOutputStream output = new TestingOutputServletOutputStream();
|
||||
when(response.getOutputStream()).thenReturn(output);
|
||||
|
||||
File file = temporaryFolder.newFile();
|
||||
Files.write("hello".getBytes(Charsets.UTF_8), file);
|
||||
|
||||
when(webResourceLoader.getResource("/README.txt")).thenReturn(file.toURI().toURL());
|
||||
servlet.doGet(request, response);
|
||||
|
||||
assertEquals("hello", output.buffer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoGetWithError() throws IOException {
|
||||
when(request.getRequestURI()).thenReturn("/scm/README.txt");
|
||||
ServletOutputStream output = mock(ServletOutputStream.class);
|
||||
when(response.getOutputStream()).thenReturn(output);
|
||||
|
||||
File file = temporaryFolder.newFile();
|
||||
assertTrue(file.delete());
|
||||
|
||||
when(webResourceLoader.getResource("/README.txt")).thenReturn(file.toURI().toURL());
|
||||
servlet.doGet(request, response);
|
||||
|
||||
verify(output, never()).write(anyInt());
|
||||
verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoGetWithDispatcherException() throws IOException {
|
||||
when(request.getRequestURI()).thenReturn("/scm/awesome.jpg");
|
||||
doThrow(IOException.class).when(pushStateDispatcher).dispatch(request, response, "/awesome.jpg");
|
||||
servlet.doGet(request, response);
|
||||
verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
private static class TestingOutputServletOutputStream extends ServletOutputStream {
|
||||
|
||||
private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
buffer.write(b);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,13 +24,13 @@ import static org.mockito.Mockito.when;
|
||||
public class AbstractManagerResourceTest {
|
||||
|
||||
@Mock
|
||||
private Manager<Simple, Exception> manager;
|
||||
private Manager<Simple> manager;
|
||||
@Mock
|
||||
private Request request;
|
||||
@Captor
|
||||
private ArgumentCaptor<Comparator<Simple>> comparatorCaptor;
|
||||
|
||||
private AbstractManagerResource<Simple, Exception> abstractManagerResource;
|
||||
private AbstractManagerResource<Simple> abstractManagerResource;
|
||||
|
||||
@Before
|
||||
public void captureComparator() {
|
||||
@@ -60,7 +60,7 @@ public class AbstractManagerResourceTest {
|
||||
}
|
||||
|
||||
|
||||
private class SimpleManagerResource extends AbstractManagerResource<Simple, Exception> {
|
||||
private class SimpleManagerResource extends AbstractManagerResource<Simple> {
|
||||
|
||||
{
|
||||
disableCache = true;
|
||||
|
||||
@@ -11,8 +11,6 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
@@ -20,25 +18,16 @@ import sonia.scm.security.AccessToken;
|
||||
import sonia.scm.security.AccessTokenBuilder;
|
||||
import sonia.scm.security.AccessTokenBuilderFactory;
|
||||
import sonia.scm.security.AccessTokenCookieIssuer;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserException;
|
||||
import sonia.scm.user.UserManager;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SubjectAware(
|
||||
configuration = "classpath:sonia/scm/repository/shiro.ini"
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.subject.support.SubjectThreadState;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.apache.shiro.util.ThreadState;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.repository.BrowserResult;
|
||||
import sonia.scm.repository.FileObject;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
public class BrowserResultToBrowserResultDtoMapperTest {
|
||||
|
||||
private final URI baseUri = URI.create("http://example.com/base/");
|
||||
@SuppressWarnings("unused") // Is injected
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@Mock
|
||||
private FileObjectToFileObjectDtoMapper fileObjectToFileObjectDtoMapper;
|
||||
|
||||
@InjectMocks
|
||||
private BrowserResultToBrowserResultDtoMapper mapper;
|
||||
|
||||
private final Subject subject = mock(Subject.class);
|
||||
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||
|
||||
private FileObject fileObject1 = new FileObject();
|
||||
private FileObject fileObject2 = new FileObject();
|
||||
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
initMocks(this);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
|
||||
fileObject1.setName("FO 1");
|
||||
fileObject1.setLength(100);
|
||||
fileObject1.setLastModified(0L);
|
||||
fileObject1.setPath("/path/object/1");
|
||||
fileObject1.setDescription("description of file object 1");
|
||||
fileObject1.setDirectory(false);
|
||||
|
||||
fileObject2.setName("FO 2");
|
||||
fileObject2.setLength(100);
|
||||
fileObject2.setLastModified(101L);
|
||||
fileObject2.setPath("/path/object/2");
|
||||
fileObject2.setDescription("description of file object 2");
|
||||
fileObject2.setDirectory(true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbind() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapAttributesCorrectly() {
|
||||
BrowserResult browserResult = createBrowserResult();
|
||||
|
||||
BrowserResultDto dto = mapper.map(browserResult, new NamespaceAndName("foo", "bar"));
|
||||
|
||||
assertEqualAttributes(browserResult, dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDelegateToFileObjectsMapper() {
|
||||
BrowserResult browserResult = createBrowserResult();
|
||||
NamespaceAndName namespaceAndName = new NamespaceAndName("foo", "bar");
|
||||
|
||||
BrowserResultDto dto = mapper.map(browserResult, namespaceAndName);
|
||||
|
||||
verify(fileObjectToFileObjectDtoMapper).map(fileObject1, namespaceAndName, "Revision");
|
||||
verify(fileObjectToFileObjectDtoMapper).map(fileObject2, namespaceAndName, "Revision");
|
||||
}
|
||||
|
||||
private BrowserResult createBrowserResult() {
|
||||
BrowserResult browserResult = new BrowserResult();
|
||||
browserResult.setTag("Tag");
|
||||
browserResult.setRevision("Revision");
|
||||
browserResult.setBranch("Branch");
|
||||
browserResult.setFiles(createFileObjects());
|
||||
|
||||
return browserResult;
|
||||
}
|
||||
|
||||
private List<FileObject> createFileObjects() {
|
||||
List<FileObject> fileObjects = new ArrayList<>();
|
||||
|
||||
fileObjects.add(fileObject1);
|
||||
fileObjects.add(fileObject2);
|
||||
return fileObjects;
|
||||
}
|
||||
|
||||
private void assertEqualAttributes(BrowserResult browserResult, BrowserResultDto dto) {
|
||||
assertThat(dto.getTag()).isEqualTo(browserResult.getTag());
|
||||
assertThat(dto.getBranch()).isEqualTo(browserResult.getBranch());
|
||||
assertThat(dto.getRevision()).isEqualTo(browserResult.getRevision());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -95,7 +95,7 @@ public class ChangesetRootResourceTest {
|
||||
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(service);
|
||||
when(serviceFactory.create(any(Repository.class))).thenReturn(service);
|
||||
when(service.getRepository()).thenReturn(new Repository("repoId", "git", "space", "repo"));
|
||||
dispatcher.getProviderFactory().registerProvider(RepositoryNotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
|
||||
when(service.getLogCommand()).thenReturn(logCommandBuilder);
|
||||
subjectThreadState.bind();
|
||||
|
||||
@@ -3,6 +3,7 @@ package sonia.scm.api.v2.resources;
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.io.Resources;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.jboss.resteasy.core.Dispatcher;
|
||||
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||
import org.jboss.resteasy.mock.MockHttpRequest;
|
||||
@@ -21,7 +22,9 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
@SubjectAware(
|
||||
@@ -47,6 +50,13 @@ public class ConfigResourceTest {
|
||||
@InjectMocks
|
||||
private ScmConfigurationToConfigDtoMapperImpl configToDtoMapper;
|
||||
|
||||
public ConfigResourceTest() {
|
||||
// cleanup state that might have been left by other tests
|
||||
ThreadContext.unbindSecurityManager();
|
||||
ThreadContext.unbindSubject();
|
||||
ThreadContext.remove();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() {
|
||||
initMocks(this);
|
||||
|
||||
@@ -108,17 +108,6 @@ public class ContentResourceTest {
|
||||
assertEquals("text/plain", response.getHeaderString("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRecognizeShebangSourceCode() throws Exception {
|
||||
mockContentFromResource("someScript.sh");
|
||||
|
||||
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "someScript.sh");
|
||||
assertEquals(200, response.getStatus());
|
||||
|
||||
assertEquals("PYTHON", response.getHeaderString("Language"));
|
||||
assertEquals("application/x-sh", response.getHeaderString("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleRandomByteFile() throws Exception {
|
||||
mockContentFromResource("JustBytes");
|
||||
@@ -142,6 +131,17 @@ public class ContentResourceTest {
|
||||
assertTrue("stream has to be closed after reading head", stream.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleEmptyFile() throws Exception {
|
||||
mockContent("empty", new byte[]{});
|
||||
|
||||
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "empty");
|
||||
assertEquals(200, response.getStatus());
|
||||
|
||||
assertFalse(response.getHeaders().containsKey("Language"));
|
||||
assertEquals("application/octet-stream", response.getHeaderString("Content-Type"));
|
||||
}
|
||||
|
||||
private void mockContentFromResource(String fileName) throws Exception {
|
||||
URL url = Resources.getResource(fileName);
|
||||
mockContent(fileName, Resources.toByteArray(url));
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.jboss.resteasy.core.Dispatcher;
|
||||
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||
import sonia.scm.api.rest.AlreadyExistsExceptionMapper;
|
||||
import sonia.scm.api.rest.AuthorizationExceptionMapper;
|
||||
import sonia.scm.api.rest.ConcurrentModificationExceptionMapper;
|
||||
|
||||
public class DispatcherMock {
|
||||
public static Dispatcher createDispatcher(Object resource) {
|
||||
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
dispatcher.getRegistry().addSingletonResource(resource);
|
||||
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AlreadyExistsExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(ConcurrentModificationExceptionMapper.class);
|
||||
return dispatcher;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.subject.support.SubjectThreadState;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.apache.shiro.util.ThreadState;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.FileObject;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.SubRepository;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class FileObjectToFileObjectDtoMapperTest {
|
||||
|
||||
private final URI baseUri = URI.create("http://example.com/base/");
|
||||
@SuppressWarnings("unused") // Is injected
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@InjectMocks
|
||||
private FileObjectToFileObjectDtoMapperImpl mapper;
|
||||
|
||||
private final Subject subject = mock(Subject.class);
|
||||
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||
|
||||
private URI expectedBaseUri;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
expectedBaseUri = baseUri.resolve(RepositoryRootResource.REPOSITORIES_PATH_V2 + "/");
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbind() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapAttributesCorrectly() {
|
||||
FileObject fileObject = createFileObject();
|
||||
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
|
||||
|
||||
assertEqualAttributes(fileObject, dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveCorrectSelfLinkForDirectory() {
|
||||
FileObject fileObject = createDirectoryObject();
|
||||
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
|
||||
|
||||
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/sources/revision/foo/bar").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveCorrectContentLink() {
|
||||
FileObject fileObject = createFileObject();
|
||||
fileObject.setDirectory(false);
|
||||
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
|
||||
|
||||
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/content/revision/foo/bar").toString());
|
||||
}
|
||||
|
||||
private FileObject createDirectoryObject() {
|
||||
FileObject fileObject = createFileObject();
|
||||
fileObject.setDirectory(true);
|
||||
return fileObject;
|
||||
}
|
||||
|
||||
private FileObject createFileObject() {
|
||||
FileObject fileObject = new FileObject();
|
||||
fileObject.setName("foo");
|
||||
fileObject.setDescription("bar");
|
||||
fileObject.setPath("foo/bar");
|
||||
fileObject.setDirectory(false);
|
||||
fileObject.setLength(100);
|
||||
fileObject.setLastModified(123L);
|
||||
|
||||
fileObject.setSubRepository(new SubRepository("repo.url"));
|
||||
return fileObject;
|
||||
}
|
||||
|
||||
private void assertEqualAttributes(FileObject fileObject, FileObjectDto dto) {
|
||||
assertThat(dto.getName()).isEqualTo(fileObject.getName());
|
||||
assertThat(dto.getDescription()).isEqualTo(fileObject.getDescription());
|
||||
assertThat(dto.getPath()).isEqualTo(fileObject.getPath());
|
||||
assertThat(dto.isDirectory()).isEqualTo(fileObject.isDirectory());
|
||||
assertThat(dto.getLength()).isEqualTo(fileObject.getLength());
|
||||
assertThat(dto.getLastModified().toEpochMilli()).isEqualTo((long) fileObject.getLastModified());
|
||||
assertThat(dto.getSubRepository().getBrowserUrl()).isEqualTo(fileObject.getSubRepository().getBrowserUrl());
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import org.junit.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import sonia.scm.group.Group;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class GroupDtoToGroupMapperTest {
|
||||
@@ -16,8 +16,10 @@ public class GroupDtoToGroupMapperTest {
|
||||
public void shouldMapAttributes() {
|
||||
GroupDto dto = new GroupDto();
|
||||
dto.setName("group");
|
||||
dto.setLastModified(Instant.ofEpochMilli(1234));
|
||||
Group group = Mappers.getMapper(GroupDtoToGroupMapper.class).map(dto);
|
||||
assertEquals("group", group.getName());
|
||||
assertThat(group.getLastModified()).isEqualTo(dto.getLastModified().toEpochMilli());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.io.Resources;
|
||||
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;
|
||||
@@ -14,8 +13,9 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.PageResult;
|
||||
import sonia.scm.api.rest.JSONContextResolver;
|
||||
import sonia.scm.api.rest.ObjectMapperProvider;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupException;
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
@@ -27,12 +27,15 @@ import java.net.URL;
|
||||
import java.util.Collections;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
import static sonia.scm.api.v2.resources.DispatcherMock.createDispatcher;
|
||||
|
||||
@SubjectAware(
|
||||
username = "trillian",
|
||||
@@ -44,7 +47,7 @@ public class GroupRootResourceTest {
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
private Dispatcher dispatcher;
|
||||
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(URI.create("/"));
|
||||
|
||||
@@ -58,7 +61,7 @@ public class GroupRootResourceTest {
|
||||
private ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws IOException, GroupException {
|
||||
public void prepareEnvironment() throws Exception {
|
||||
initMocks(this);
|
||||
when(groupManager.create(groupCaptor.capture())).thenAnswer(invocation -> invocation.getArguments()[0]);
|
||||
doNothing().when(groupManager).modify(groupCaptor.capture());
|
||||
@@ -72,7 +75,8 @@ public class GroupRootResourceTest {
|
||||
GroupResource groupResource = new GroupResource(groupManager, groupToDtoMapper, dtoToGroupMapper);
|
||||
GroupRootResource groupRootResource = new GroupRootResource(MockProvider.of(groupCollectionResource), MockProvider.of(groupResource));
|
||||
|
||||
dispatcher.getRegistry().addSingletonResource(groupRootResource);
|
||||
dispatcher = createDispatcher(groupRootResource);
|
||||
dispatcher.getProviderFactory().registerProviderInstance(new JSONContextResolver(new ObjectMapperProvider().get()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,10 +122,10 @@ public class GroupRootResourceTest {
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
Group capturedGroup = groupCaptor.getValue();
|
||||
|
||||
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
|
||||
|
||||
Group capturedGroup = groupCaptor.getValue();
|
||||
assertEquals("Updated description", capturedGroup.getDescription());
|
||||
}
|
||||
|
||||
@@ -142,6 +146,40 @@ public class GroupRootResourceTest {
|
||||
assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateShouldFailOnConcurrentModification_oldModificationDate() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/group-test-update-concurrent-modification.json");
|
||||
byte[] groupJson = Resources.toByteArray(url);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.put("/" + GroupRootResource.GROUPS_PATH_V2 + "admin")
|
||||
.contentType(VndMediaType.GROUP)
|
||||
.content(groupJson);
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateShouldFailOnConcurrentModification_unsetModificationDate() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/group-test-update-concurrent-modification_null_date.json");
|
||||
byte[] groupJson = Resources.toByteArray(url);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.put("/" + GroupRootResource.GROUPS_PATH_V2 + "admin")
|
||||
.contentType(VndMediaType.GROUP)
|
||||
.content(groupJson);
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteGroup() throws URISyntaxException {
|
||||
Group group = createDummyGroup();
|
||||
@@ -215,6 +253,7 @@ public class GroupRootResourceTest {
|
||||
group.setName("admin");
|
||||
group.setCreationDate(0L);
|
||||
group.setMembers(Collections.singletonList("user"));
|
||||
group.setLastModified(3600000L);
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,6 @@ package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.io.Resources;
|
||||
import org.apache.shiro.authc.credential.PasswordService;
|
||||
import org.apache.shiro.subject.PrincipalCollection;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.jboss.resteasy.core.Dispatcher;
|
||||
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||
import org.jboss.resteasy.mock.MockHttpRequest;
|
||||
@@ -16,26 +12,19 @@ import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.PageResult;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserException;
|
||||
import sonia.scm.user.UserManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
@SubjectAware(
|
||||
@@ -64,7 +53,7 @@ public class MeResourceTest {
|
||||
private ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws IOException, UserException {
|
||||
public void prepareEnvironment() throws Exception {
|
||||
initMocks(this);
|
||||
createDummyUser("trillian");
|
||||
when(userManager.create(userCaptor.capture())).thenAnswer(invocation -> invocation.getArguments()[0]);
|
||||
|
||||
@@ -14,10 +14,10 @@ import org.apache.shiro.util.ThreadContext;
|
||||
import org.apache.shiro.util.ThreadState;
|
||||
import org.assertj.core.util.Lists;
|
||||
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.jboss.resteasy.spi.HttpRequest;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -27,7 +27,6 @@ import org.junit.jupiter.api.DynamicTest;
|
||||
import org.junit.jupiter.api.TestFactory;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.api.rest.AuthorizationExceptionMapper;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.Permission;
|
||||
import sonia.scm.repository.PermissionType;
|
||||
@@ -55,6 +54,7 @@ import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
import static sonia.scm.api.v2.resources.DispatcherMock.createDispatcher;
|
||||
|
||||
@Slf4j
|
||||
@SubjectAware(
|
||||
@@ -105,7 +105,7 @@ public class PermissionRootResourceTest {
|
||||
.content(PERMISSION_TEST_PAYLOAD)
|
||||
.path(PATH_OF_ONE_PERMISSION);
|
||||
|
||||
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
private Dispatcher dispatcher;
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
@@ -137,13 +137,14 @@ public class PermissionRootResourceTest {
|
||||
permissionRootResource = new PermissionRootResource(permissionDtoToPermissionMapper, permissionToPermissionDtoMapper, permissionCollectionToDtoMapper, resourceLinks, repositoryManager);
|
||||
RepositoryRootResource repositoryRootResource = new RepositoryRootResource(MockProvider
|
||||
.of(new RepositoryResource(null, null, null, null, null, null, null, null, MockProvider.of(permissionRootResource), null)), null);
|
||||
dispatcher = createDispatcher(repositoryRootResource);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
|
||||
dispatcher.getProviderFactory().registerProvider(RepositoryNotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(PermissionNotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(PermissionAlreadyExistsExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbind() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@TestFactory
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.io.Resources;
|
||||
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;
|
||||
@@ -19,7 +18,6 @@ import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.Permission;
|
||||
import sonia.scm.repository.PermissionType;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryException;
|
||||
import sonia.scm.repository.RepositoryIsNotArchivedException;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
@@ -50,6 +48,7 @@ import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
import static sonia.scm.api.v2.resources.DispatcherMock.createDispatcher;
|
||||
|
||||
@SubjectAware(
|
||||
username = "trillian",
|
||||
@@ -58,7 +57,7 @@ import static org.mockito.MockitoAnnotations.initMocks;
|
||||
)
|
||||
public class RepositoryRootResourceTest {
|
||||
|
||||
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
private Dispatcher dispatcher;
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
@@ -84,7 +83,7 @@ public class RepositoryRootResourceTest {
|
||||
RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper = new RepositoryCollectionToDtoMapper(repositoryToDtoMapper, resourceLinks);
|
||||
RepositoryCollectionResource repositoryCollectionResource = new RepositoryCollectionResource(repositoryManager, repositoryCollectionToDtoMapper, dtoToRepositoryMapper, resourceLinks);
|
||||
RepositoryRootResource repositoryRootResource = new RepositoryRootResource(MockProvider.of(repositoryResource), MockProvider.of(repositoryCollectionResource));
|
||||
dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
|
||||
dispatcher = createDispatcher(repositoryRootResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -223,7 +222,7 @@ public class RepositoryRootResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNewRepositoryInCorrectNamespace() throws URISyntaxException, IOException, RepositoryException {
|
||||
public void shouldCreateNewRepositoryInCorrectNamespace() throws Exception {
|
||||
when(repositoryManager.create(any())).thenAnswer(invocation -> {
|
||||
Repository repository = (Repository) invocation.getArguments()[0];
|
||||
repository.setNamespace("otherspace");
|
||||
|
||||
@@ -29,6 +29,8 @@ public class ResourceLinksMock {
|
||||
when(resourceLinks.diff()).thenReturn(new ResourceLinks.DiffLinks(uriInfo));
|
||||
when(resourceLinks.repositoryType()).thenReturn(new ResourceLinks.RepositoryTypeLinks(uriInfo));
|
||||
when(resourceLinks.repositoryTypeCollection()).thenReturn(new ResourceLinks.RepositoryTypeCollectionLinks(uriInfo));
|
||||
when(resourceLinks.uiPluginCollection()).thenReturn(new ResourceLinks.UIPluginCollectionLinks(uriInfo));
|
||||
when(resourceLinks.uiPlugin()).thenReturn(new ResourceLinks.UIPluginLinks(uriInfo));
|
||||
|
||||
return resourceLinks;
|
||||
}
|
||||
|
||||
@@ -135,8 +135,8 @@ public class ResourceLinksTest {
|
||||
|
||||
@Test
|
||||
public void shouldCreateCorrectBranchSourceUrl() {
|
||||
String url = resourceLinks.source().source("space", "name", "revision");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/sources/revision", url);
|
||||
String url = resourceLinks.source().selfWithoutRevision("space", "name");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/sources/", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,13 +147,18 @@ public class ResourceLinksTest {
|
||||
|
||||
@Test
|
||||
public void shouldCreateCorrectSourceCollectionUrl() {
|
||||
String url = resourceLinks.source().self("space", "repo");
|
||||
String url = resourceLinks.source().selfWithoutRevision("space", "repo");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/sources/", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateCorrectSourceUrlWithFilename() {
|
||||
String url = resourceLinks.source().sourceWithPath("foo", "bar", "rev", "file");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "foo/bar/sources/rev/file", url);
|
||||
}
|
||||
@Test
|
||||
public void shouldCreateCorrectPermissionCollectionUrl() {
|
||||
String url = resourceLinks.source().self("space", "repo");
|
||||
String url = resourceLinks.source().selfWithoutRevision("space", "repo");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/sources/", url);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
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.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.repository.BrowserResult;
|
||||
import sonia.scm.repository.FileObject;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.RepositoryNotFoundException;
|
||||
import sonia.scm.repository.RevisionNotFoundException;
|
||||
import sonia.scm.repository.api.BrowseCommandBuilder;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class SourceRootResourceTest {
|
||||
|
||||
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
private final URI baseUri = URI.create("/");
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@Mock
|
||||
private RepositoryServiceFactory serviceFactory;
|
||||
@Mock
|
||||
private RepositoryService service;
|
||||
@Mock
|
||||
private BrowseCommandBuilder browseCommandBuilder;
|
||||
|
||||
@Mock
|
||||
private FileObjectToFileObjectDtoMapper fileObjectToFileObjectDtoMapper;
|
||||
|
||||
@InjectMocks
|
||||
private BrowserResultToBrowserResultDtoMapper browserResultToBrowserResultDtoMapper;
|
||||
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws Exception {
|
||||
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(service);
|
||||
when(service.getBrowseCommand()).thenReturn(browseCommandBuilder);
|
||||
|
||||
FileObjectDto dto = new FileObjectDto();
|
||||
dto.setName("name");
|
||||
dto.setLength(1024);
|
||||
|
||||
when(fileObjectToFileObjectDtoMapper.map(any(FileObject.class), any(NamespaceAndName.class), anyString())).thenReturn(dto);
|
||||
SourceRootResource sourceRootResource = new SourceRootResource(serviceFactory, browserResultToBrowserResultDtoMapper);
|
||||
RepositoryRootResource repositoryRootResource =
|
||||
new RepositoryRootResource(MockProvider.of(new RepositoryResource(null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
MockProvider.of(sourceRootResource),
|
||||
null,
|
||||
null,
|
||||
null)),
|
||||
null);
|
||||
|
||||
dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
|
||||
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSources() throws URISyntaxException, IOException, RevisionNotFoundException {
|
||||
BrowserResult result = createBrowserResult();
|
||||
when(browseCommandBuilder.getBrowserResult()).thenReturn(result);
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/sources");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getContentAsString()).contains("\"revision\":\"revision\"");
|
||||
assertThat(response.getContentAsString()).contains("\"tag\":\"tag\"");
|
||||
assertThat(response.getContentAsString()).contains("\"branch\":\"branch\"");
|
||||
assertThat(response.getContentAsString()).contains("\"files\":");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn404IfRepoNotFound() throws URISyntaxException, RepositoryNotFoundException {
|
||||
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(RepositoryNotFoundException.class);
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "idont/exist/sources");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetResultForSingleFile() throws URISyntaxException, IOException, RevisionNotFoundException {
|
||||
BrowserResult browserResult = new BrowserResult();
|
||||
browserResult.setBranch("abc");
|
||||
browserResult.setRevision("revision");
|
||||
browserResult.setTag("tag");
|
||||
FileObject fileObject = new FileObject();
|
||||
fileObject.setName("File Object!");
|
||||
|
||||
browserResult.setFiles(Arrays.asList(fileObject));
|
||||
|
||||
when(browseCommandBuilder.getBrowserResult()).thenReturn(browserResult);
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/sources/revision/fileabc");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getContentAsString()).contains("\"revision\":\"revision\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet404ForSingleFileIfRepoNotFound() throws URISyntaxException, RepositoryNotFoundException {
|
||||
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(RepositoryNotFoundException.class);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "idont/exist/sources/revision/fileabc");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
private BrowserResult createBrowserResult() {
|
||||
return new BrowserResult("revision", "tag", "branch", createFileObjects());
|
||||
}
|
||||
|
||||
private List<FileObject> createFileObjects() {
|
||||
FileObject fileObject1 = new FileObject();
|
||||
fileObject1.setName("FO 1");
|
||||
fileObject1.setDirectory(false);
|
||||
fileObject1.setDescription("File object 1");
|
||||
fileObject1.setPath("/foo/bar/fo1");
|
||||
fileObject1.setLength(1024L);
|
||||
fileObject1.setLastModified(0L);
|
||||
|
||||
FileObject fileObject2 = new FileObject();
|
||||
fileObject2.setName("FO 2");
|
||||
fileObject2.setDirectory(true);
|
||||
fileObject2.setDescription("File object 2");
|
||||
fileObject2.setPath("/foo/bar/fo2");
|
||||
fileObject2.setLength(4096L);
|
||||
fileObject2.setLastModified(1234L);
|
||||
|
||||
return Arrays.asList(fileObject1, fileObject2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.inject.util.Providers;
|
||||
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.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.plugin.*;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashSet;
|
||||
|
||||
import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
||||
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
||||
import static org.hamcrest.Matchers.equalToIgnoringCase;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class UIRootResourceTest {
|
||||
|
||||
private final Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
|
||||
@Mock
|
||||
private PluginLoader pluginLoader;
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
|
||||
private final URI baseUri = URI.create("/");
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
|
||||
|
||||
@Before
|
||||
public void setUpRestService() {
|
||||
UIPluginDtoMapper mapper = new UIPluginDtoMapper(resourceLinks, request);
|
||||
UIPluginDtoCollectionMapper collectionMapper = new UIPluginDtoCollectionMapper(resourceLinks, mapper);
|
||||
|
||||
UIPluginResource pluginResource = new UIPluginResource(pluginLoader, collectionMapper, mapper);
|
||||
UIRootResource rootResource = new UIRootResource(Providers.of(pluginResource));
|
||||
|
||||
dispatcher.getRegistry().addSingletonResource(rootResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveVndCollectionMediaType() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/v2/ui/plugins");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
String contentType = response.getOutputHeaders().getFirst("Content-Type").toString();
|
||||
assertThat(VndMediaType.UI_PLUGIN_COLLECTION, equalToIgnoringCase(contentType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfPluginNotAvailable() throws URISyntaxException {
|
||||
MockHttpRequest request = MockHttpRequest.get("/v2/ui/plugins/awesome");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_NOT_FOUND, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNotFoundIfPluginHasNoResources() throws URISyntaxException {
|
||||
mockPlugins(mockPlugin("awesome"));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/v2/ui/plugins/awesome");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_NOT_FOUND, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnPlugin() throws URISyntaxException {
|
||||
mockPlugins(mockPlugin("awesome", "Awesome", createPluginResources("my/awesome.bundle.js")));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/v2/ui/plugins/awesome");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("Awesome"));
|
||||
assertTrue(response.getContentAsString().contains("my/awesome.bundle.js"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnPlugins() throws URISyntaxException {
|
||||
mockPlugins(
|
||||
mockPlugin("awesome", "Awesome", createPluginResources("my/awesome.bundle.js")),
|
||||
mockPlugin("special", "Special", createPluginResources("my/special.bundle.js"))
|
||||
);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/v2/ui/plugins");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("Awesome"));
|
||||
assertTrue(response.getContentAsString().contains("my/awesome.bundle.js"));
|
||||
assertTrue(response.getContentAsString().contains("Special"));
|
||||
assertTrue(response.getContentAsString().contains("my/special.bundle.js"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotReturnPluginsWithoutResources() throws URISyntaxException {
|
||||
mockPlugins(
|
||||
mockPlugin("awesome", "Awesome", createPluginResources("my/awesome.bundle.js")),
|
||||
mockPlugin("special")
|
||||
);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/v2/ui/plugins");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains("Awesome"));
|
||||
assertTrue(response.getContentAsString().contains("my/awesome.bundle.js"));
|
||||
assertFalse(response.getContentAsString().contains("Special"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveSelfLink() throws Exception {
|
||||
mockPlugins(mockPlugin("awesome", "Awesome", createPluginResources("my/bundle.js")));
|
||||
|
||||
String uri = "/v2/ui/plugins/awesome";
|
||||
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 shouldHaveBundleWithContextPath() throws Exception {
|
||||
when(request.getContextPath()).thenReturn("/scm");
|
||||
mockPlugins(mockPlugin("awesome", "Awesome", createPluginResources("my/bundle.js")));
|
||||
|
||||
String uri = "/v2/ui/plugins/awesome";
|
||||
MockHttpRequest request = MockHttpRequest.get(uri);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(SC_OK, response.getStatus());
|
||||
|
||||
System.out.println();
|
||||
|
||||
assertTrue(response.getContentAsString().contains("/scm/my/bundle.js"));
|
||||
}
|
||||
|
||||
private void mockPlugins(PluginWrapper... plugins) {
|
||||
when(pluginLoader.getInstalledPlugins()).thenReturn(Lists.newArrayList(plugins));
|
||||
}
|
||||
|
||||
private PluginResources createPluginResources(String... bundles) {
|
||||
HashSet<String> scripts = Sets.newHashSet(bundles);
|
||||
HashSet<String> styles = Sets.newHashSet();
|
||||
return new PluginResources(scripts, styles);
|
||||
}
|
||||
|
||||
private PluginWrapper mockPlugin(String id) {
|
||||
return mockPlugin(id, id, null);
|
||||
}
|
||||
|
||||
private PluginWrapper mockPlugin(String id, String name, PluginResources pluginResources) {
|
||||
PluginWrapper wrapper = mock(PluginWrapper.class);
|
||||
when(wrapper.getId()).thenReturn(id);
|
||||
|
||||
Plugin plugin = mock(Plugin.class);
|
||||
when(wrapper.getPlugin()).thenReturn(plugin);
|
||||
when(plugin.getResources()).thenReturn(pluginResources);
|
||||
|
||||
PluginInformation information = mock(PluginInformation.class);
|
||||
when(plugin.getInformation()).thenReturn(information);
|
||||
when(information.getName()).thenReturn(name);
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.io.Resources;
|
||||
import org.apache.shiro.authc.credential.PasswordService;
|
||||
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;
|
||||
@@ -16,22 +15,26 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.PageResult;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserException;
|
||||
import sonia.scm.user.UserManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
import static sonia.scm.api.v2.resources.DispatcherMock.createDispatcher;
|
||||
|
||||
@SubjectAware(
|
||||
username = "trillian",
|
||||
@@ -43,7 +46,7 @@ public class UserRootResourceTest {
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
private Dispatcher dispatcher;
|
||||
|
||||
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(URI.create("/"));
|
||||
|
||||
@@ -59,7 +62,7 @@ public class UserRootResourceTest {
|
||||
private ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws UserException {
|
||||
public void prepareEnvironment() throws Exception {
|
||||
initMocks(this);
|
||||
User dummyUser = createDummyUser("Neo");
|
||||
when(userManager.create(userCaptor.capture())).thenAnswer(invocation -> invocation.getArguments()[0]);
|
||||
@@ -73,7 +76,7 @@ public class UserRootResourceTest {
|
||||
UserRootResource userRootResource = new UserRootResource(MockProvider.of(userCollectionResource),
|
||||
MockProvider.of(userResource));
|
||||
|
||||
dispatcher.getRegistry().addSingletonResource(userRootResource);
|
||||
dispatcher = createDispatcher(userRootResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -106,7 +109,7 @@ public class UserRootResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNewUserWithEncryptedPassword() throws URISyntaxException, IOException, UserException {
|
||||
public void shouldCreateNewUserWithEncryptedPassword() throws Exception {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/user-test-create.json");
|
||||
byte[] userJson = Resources.toByteArray(url);
|
||||
|
||||
@@ -126,7 +129,7 @@ public class UserRootResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUpdateChangedUserWithEncryptedPassword() throws URISyntaxException, IOException, UserException {
|
||||
public void shouldUpdateChangedUserWithEncryptedPassword() throws Exception {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/user-test-update.json");
|
||||
byte[] userJson = Resources.toByteArray(url);
|
||||
|
||||
@@ -170,7 +173,7 @@ public class UserRootResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteUser() throws URISyntaxException, IOException, UserException {
|
||||
public void shouldDeleteUser() throws Exception {
|
||||
MockHttpRequest request = MockHttpRequest.delete("/" + UserRootResource.USERS_PATH_V2 + "Neo");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
@@ -181,7 +184,7 @@ public class UserRootResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailUpdateForDifferentIds() throws IOException, URISyntaxException, UserException {
|
||||
public void shouldFailUpdateForDifferentIds() throws Exception {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/user-test-update.json");
|
||||
byte[] userJson = Resources.toByteArray(url);
|
||||
createDummyUser("Other");
|
||||
@@ -199,7 +202,7 @@ public class UserRootResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailUpdateForUnknownEntity() throws IOException, URISyntaxException, UserException {
|
||||
public void shouldFailUpdateForUnknownEntity() throws Exception {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/user-test-update.json");
|
||||
byte[] userJson = Resources.toByteArray(url);
|
||||
when(userManager.get("Neo")).thenReturn(null);
|
||||
|
||||
@@ -34,28 +34,24 @@ package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.CharSource;
|
||||
import com.google.common.io.Files;
|
||||
import com.google.common.io.Resources;
|
||||
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
|
||||
import static java.util.Collections.emptyIterator;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -196,7 +192,7 @@ public class CacheConfigurationTestLoader implements CacheConfigurationLoader
|
||||
|
||||
if (moduleConfigurations == null)
|
||||
{
|
||||
urlIterator = Iterators.emptyIterator();
|
||||
urlIterator = emptyIterator();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -51,7 +51,11 @@ import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static sonia.scm.it.IntegrationTestUtil.*;
|
||||
import static sonia.scm.it.IntegrationTestUtil.createAdminClient;
|
||||
import static sonia.scm.it.IntegrationTestUtil.createResource;
|
||||
import static sonia.scm.it.IntegrationTestUtil.getLink;
|
||||
import static sonia.scm.it.IntegrationTestUtil.readJson;
|
||||
import static sonia.scm.it.IntegrationTestUtil.serialize;
|
||||
import static sonia.scm.it.RepositoryITUtil.createRepository;
|
||||
import static sonia.scm.it.RepositoryITUtil.deleteRepository;
|
||||
|
||||
|
||||
@@ -33,7 +33,11 @@ package sonia.scm.it;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import com.sun.jersey.api.client.WebResource;
|
||||
import org.junit.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@@ -36,6 +36,7 @@ package sonia.scm.plugin;
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -52,9 +53,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -143,8 +143,7 @@ public class DefaultUberWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
when(servletContext.getResource("/myresource")).thenReturn(SCM_MANAGER);
|
||||
|
||||
WebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext,
|
||||
new ArrayList<PluginWrapper>());
|
||||
new DefaultUberWebResourceLoader(servletContext, new ArrayList<>());
|
||||
URL resource = resourceLoader.getResource("/myresource");
|
||||
|
||||
assertSame(SCM_MANAGER, resource);
|
||||
@@ -173,6 +172,50 @@ public class DefaultUberWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
containsInAnyOrder(file.toURI().toURL(), BITBUCKET));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForDirectoryFromServletContext() throws IOException {
|
||||
URL url = temp.newFolder().toURI().toURL();
|
||||
when(servletContext.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
WebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext, new ArrayList<>());
|
||||
|
||||
assertNull(resourceLoader.getResource("/myresource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForDirectoryFromPlugin() throws IOException {
|
||||
URL url = temp.newFolder().toURI().toURL();
|
||||
WebResourceLoader loader = mock(WebResourceLoader.class);
|
||||
when(loader.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
PluginWrapper pluginWrapper = mock(PluginWrapper.class);
|
||||
when(pluginWrapper.getWebResourceLoader()).thenReturn(loader);
|
||||
|
||||
WebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext, Lists.newArrayList(pluginWrapper));
|
||||
|
||||
assertNull(resourceLoader.getResource("/myresource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForDirectories() throws IOException {
|
||||
URL url = temp.newFolder().toURI().toURL();
|
||||
when(servletContext.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
WebResourceLoader loader = mock(WebResourceLoader.class);
|
||||
when(loader.getResource("/myresource")).thenReturn(url);
|
||||
|
||||
PluginWrapper pluginWrapper = mock(PluginWrapper.class);
|
||||
when(pluginWrapper.getWebResourceLoader()).thenReturn(loader);
|
||||
|
||||
UberWebResourceLoader resourceLoader =
|
||||
new DefaultUberWebResourceLoader(servletContext, Lists.newArrayList(pluginWrapper));
|
||||
|
||||
List<URL> resources = resourceLoader.getResources("/myresource");
|
||||
Assertions.assertThat(resources).isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
@@ -54,15 +54,18 @@ import java.net.URL;
|
||||
public class PathWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testGetResource() throws IOException
|
||||
{
|
||||
public void testGetNullForDirectories() throws IOException {
|
||||
File directory = temp.newFolder();
|
||||
assertTrue(new File(directory, "awesome").mkdir());
|
||||
|
||||
WebResourceLoader resourceLoader = new PathWebResourceLoader(directory.toPath());
|
||||
assertNull(resourceLoader.getResource("awesome"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetResource() throws IOException {
|
||||
File directory = temp.newFolder();
|
||||
URL url = file(directory, "myresource").toURI().toURL();
|
||||
|
||||
@@ -74,7 +77,4 @@ public class PathWebResourceLoaderTest extends WebResourceLoaderTestBase
|
||||
assertNull(resourceLoader.getResource("other"));
|
||||
}
|
||||
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
@@ -43,7 +43,11 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import sonia.scm.*;
|
||||
import sonia.scm.AlreadyExistsException;
|
||||
import sonia.scm.HandlerEventType;
|
||||
import sonia.scm.Manager;
|
||||
import sonia.scm.ManagerTestBase;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.repository.api.HookContext;
|
||||
@@ -56,9 +60,15 @@ import sonia.scm.security.KeyGenerator;
|
||||
import sonia.scm.store.ConfigurationStoreFactory;
|
||||
import sonia.scm.store.JAXBConfigurationStoreFactory;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.hasProperty;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -83,7 +93,7 @@ import static org.mockito.Mockito.when;
|
||||
password = "secret",
|
||||
configuration = "classpath:sonia/scm/repository/shiro.ini"
|
||||
)
|
||||
public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, RepositoryException> {
|
||||
public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
@@ -96,7 +106,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
private String mockedNamespace = "default_namespace";
|
||||
|
||||
@Test
|
||||
public void testCreate() throws RepositoryException {
|
||||
public void testCreate() throws AlreadyExistsException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
Repository dbRepo = manager.get(heartOfGold.getId());
|
||||
|
||||
@@ -108,18 +118,18 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
username = "unpriv"
|
||||
)
|
||||
@Test(expected = UnauthorizedException.class)
|
||||
public void testCreateWithoutPrivileges() throws RepositoryException {
|
||||
public void testCreateWithoutPrivileges() throws AlreadyExistsException {
|
||||
createTestRepository();
|
||||
}
|
||||
|
||||
@Test(expected = RepositoryAlreadyExistsException.class)
|
||||
public void testCreateExisting() throws RepositoryException {
|
||||
@Test(expected = AlreadyExistsException.class)
|
||||
public void testCreateExisting() throws AlreadyExistsException {
|
||||
createTestRepository();
|
||||
createTestRepository();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() throws RepositoryException {
|
||||
public void testDelete() throws Exception {
|
||||
delete(manager, createTestRepository());
|
||||
}
|
||||
|
||||
@@ -127,24 +137,23 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
username = "unpriv"
|
||||
)
|
||||
@Test(expected = UnauthorizedException.class)
|
||||
public void testDeleteWithoutPrivileges() throws RepositoryException {
|
||||
public void testDeleteWithoutPrivileges() throws Exception {
|
||||
delete(manager, createTestRepository());
|
||||
}
|
||||
|
||||
@Test(expected = RepositoryIsNotArchivedException.class)
|
||||
public void testDeleteNonArchived() throws RepositoryException {
|
||||
public void testDeleteNonArchived() throws Exception {
|
||||
configuration.setEnableRepositoryArchive(true);
|
||||
delete(manager, createTestRepository());
|
||||
}
|
||||
|
||||
@Test(expected = RepositoryNotFoundException.class)
|
||||
public void testDeleteNotFound() throws RepositoryException {
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void testDeleteNotFound() throws NotFoundException {
|
||||
manager.delete(createRepositoryWithId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteWithEnabledArchive()
|
||||
throws RepositoryException {
|
||||
public void testDeleteWithEnabledArchive() throws Exception {
|
||||
Repository repository = createTestRepository();
|
||||
|
||||
repository.setArchived(true);
|
||||
@@ -154,7 +163,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() throws RepositoryException {
|
||||
public void testGet() throws AlreadyExistsException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
String id = heartOfGold.getId();
|
||||
String description = heartOfGold.getDescription();
|
||||
@@ -172,7 +181,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
@SubjectAware(
|
||||
username = "crato"
|
||||
)
|
||||
public void testGetWithoutRequiredPrivileges() throws RepositoryException {
|
||||
public void testGetWithoutRequiredPrivileges() throws AlreadyExistsException {
|
||||
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
|
||||
manager.create(heartOfGold);
|
||||
|
||||
@@ -181,7 +190,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAll() throws RepositoryException {
|
||||
public void testGetAll() throws AlreadyExistsException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
Repository happyVerticalPeopleTransporter = createSecondTestRepository();
|
||||
boolean foundHeart = false;
|
||||
@@ -219,7 +228,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
@SubjectAware(username = "dent")
|
||||
public void testGetAllWithPermissionsForTwoOrThreeRepos() throws RepositoryException {
|
||||
public void testGetAllWithPermissionsForTwoOrThreeRepos() throws AlreadyExistsException {
|
||||
// mock key generator
|
||||
KeyGenerator keyGenerator = mock(KeyGenerator.class);
|
||||
Stack<String> keys = new Stack<>();
|
||||
@@ -260,7 +269,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvents() throws RepositoryException {
|
||||
public void testEvents() throws Exception {
|
||||
RepositoryManager repoManager = createRepositoryManager(false);
|
||||
repoManager.init(contextProvider);
|
||||
TestListener listener = new TestListener();
|
||||
@@ -291,7 +300,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testModify() throws RepositoryException {
|
||||
public void testModify() throws NotFoundException, AlreadyExistsException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
|
||||
heartOfGold.setDescription("prototype ship");
|
||||
@@ -305,7 +314,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "crato")
|
||||
public void testModifyWithoutRequiredPermissions() throws RepositoryException {
|
||||
public void testModifyWithoutRequiredPermissions() throws AlreadyExistsException, NotFoundException {
|
||||
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
|
||||
manager.create(heartOfGold);
|
||||
heartOfGold.setDescription("prototype ship");
|
||||
@@ -314,13 +323,13 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
manager.modify(heartOfGold);
|
||||
}
|
||||
|
||||
@Test(expected = RepositoryNotFoundException.class)
|
||||
public void testModifyNotFound() throws RepositoryException {
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void testModifyNotFound() throws NotFoundException {
|
||||
manager.modify(createRepositoryWithId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefresh() throws RepositoryException {
|
||||
public void testRefresh() throws NotFoundException, AlreadyExistsException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
String description = heartOfGold.getDescription();
|
||||
|
||||
@@ -331,7 +340,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "crato")
|
||||
public void testRefreshWithoutRequiredPermissions() throws RepositoryException {
|
||||
public void testRefreshWithoutRequiredPermissions() throws AlreadyExistsException, NotFoundException {
|
||||
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
|
||||
manager.create(heartOfGold);
|
||||
heartOfGold.setDescription("prototype ship");
|
||||
@@ -341,12 +350,12 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test(expected = RepositoryNotFoundException.class)
|
||||
public void testRefreshNotFound() throws RepositoryException {
|
||||
public void testRefreshNotFound() throws NotFoundException {
|
||||
manager.refresh(createRepositoryWithId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRepositoryHook() throws RepositoryException {
|
||||
public void testRepositoryHook() throws AlreadyExistsException {
|
||||
CountingReceiveHook hook = new CountingReceiveHook();
|
||||
RepositoryManager repoManager = createRepositoryManager(false);
|
||||
|
||||
@@ -375,7 +384,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withoutLeadingSlash() throws RepositoryException {
|
||||
public void getRepositoryFromRequestUri_withoutLeadingSlash() throws AlreadyExistsException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
@@ -386,7 +395,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withLeadingSlash() throws RepositoryException {
|
||||
public void getRepositoryFromRequestUri_withLeadingSlash() throws AlreadyExistsException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
@@ -397,7 +406,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withPartialName() throws RepositoryException {
|
||||
public void getRepositoryFromRequestUri_withPartialName() throws AlreadyExistsException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
@@ -408,7 +417,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withTrailingFilePath() throws RepositoryException {
|
||||
public void getRepositoryFromRequestUri_withTrailingFilePath() throws AlreadyExistsException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
@@ -418,7 +427,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_forNotExistingRepositoryName() throws RepositoryException {
|
||||
public void getRepositoryFromRequestUri_forNotExistingRepositoryName() throws AlreadyExistsException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
@@ -428,7 +437,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_forWrongNamespace() throws RepositoryException {
|
||||
public void getRepositoryFromRequestUri_forWrongNamespace() throws AlreadyExistsException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
@@ -438,14 +447,14 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetNamespace() throws RepositoryException {
|
||||
public void shouldSetNamespace() throws AlreadyExistsException {
|
||||
Repository repository = new Repository(null, "hg", null, "scm");
|
||||
manager.create(repository);
|
||||
assertNotNull(repository.getId());
|
||||
assertNotNull(repository.getNamespace());
|
||||
}
|
||||
|
||||
private void createUriTestRepositories(RepositoryManager m) throws RepositoryException {
|
||||
private void createUriTestRepositories(RepositoryManager m) throws AlreadyExistsException {
|
||||
mockedNamespace = "namespace";
|
||||
createRepository(m, new Repository("1", "hg", "namespace", "scm"));
|
||||
createRepository(m, new Repository("2", "hg", "namespace", "scm-test"));
|
||||
@@ -498,8 +507,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
keyGenerator, repositoryDAO, handlerSet, createRepositoryMatcher(), namespaceStrategy);
|
||||
}
|
||||
|
||||
private void createRepository(RepositoryManager m, Repository repository)
|
||||
throws RepositoryException {
|
||||
private void createRepository(RepositoryManager m, Repository repository) throws AlreadyExistsException {
|
||||
m.create(repository);
|
||||
}
|
||||
|
||||
@@ -526,7 +534,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
return new RepositoryMatcher(Collections.<RepositoryPathMatcher>emptySet());
|
||||
}
|
||||
|
||||
private Repository createRepository(Repository repository) throws RepositoryException {
|
||||
private Repository createRepository(Repository repository) throws AlreadyExistsException {
|
||||
manager.create(repository);
|
||||
assertNotNull(repository.getId());
|
||||
assertNotNull(manager.get(repository.getId()));
|
||||
@@ -541,17 +549,16 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
return repository;
|
||||
}
|
||||
|
||||
private Repository createSecondTestRepository() throws RepositoryException {
|
||||
private Repository createSecondTestRepository() throws AlreadyExistsException {
|
||||
return createRepository(
|
||||
RepositoryTestData.createHappyVerticalPeopleTransporter());
|
||||
}
|
||||
|
||||
private Repository createTestRepository() throws RepositoryException {
|
||||
private Repository createTestRepository() throws AlreadyExistsException {
|
||||
return createRepository(RepositoryTestData.createHeartOfGold());
|
||||
}
|
||||
|
||||
private void delete(Manager<Repository, RepositoryException> manager, Repository repository)
|
||||
throws RepositoryException {
|
||||
private void delete(Manager<Repository> manager, Repository repository) throws NotFoundException {
|
||||
|
||||
String id = repository.getId();
|
||||
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
/***
|
||||
* Copyright (c) 2015, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* https://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.resources;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractResourceManager}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AbstractResourceManagerTest extends ResourceManagerTestBase
|
||||
{
|
||||
|
||||
private DummyResourceManager resourceManager;
|
||||
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
Set<ResourceHandler> resourceHandlers = ImmutableSet.of(resourceHandler);
|
||||
resourceManager = new DummyResourceManager(pluginLoader, resourceHandlers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test {@link AbstractResourceManager#getScriptResources()} in the correct order.
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*
|
||||
* @see <a href="https://goo.gl/ok03l4">Issue 809</a>
|
||||
*/
|
||||
@Test
|
||||
public void testGetScriptResources() throws IOException
|
||||
{
|
||||
appendScriptResources("a/b.js", "z/a.js", "a/a.js");
|
||||
List<String> scripts = resourceManager.getScriptResources();
|
||||
assertThat(scripts, contains("a/a.js", "a/b.js", "z/a.js"));
|
||||
}
|
||||
|
||||
private static class DummyResourceManager extends AbstractResourceManager
|
||||
{
|
||||
|
||||
public DummyResourceManager(PluginLoader pluginLoader, Set<ResourceHandler> resourceHandlers)
|
||||
{
|
||||
super(pluginLoader, resourceHandlers);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void collectResources(Map<ResourceKey, Resource> resourceMap)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
/***
|
||||
* Copyright (c) 2015, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* https://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.resources;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultResourceManager}.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class DefaultResourceManagerTest extends ResourceManagerTestBase {
|
||||
|
||||
private DefaultResourceManager resourceManager;
|
||||
|
||||
/**
|
||||
* Set up {@link DefaultResourceManager} for tests.
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
Set<ResourceHandler> resourceHandlers = ImmutableSet.of(resourceHandler);
|
||||
resourceManager = new DefaultResourceManager(pluginLoader, resourceHandlers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test {@link DefaultResourceManager#getResources(sonia.scm.resources.ResourceType)} method.
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
@Test
|
||||
public void testGetResources() throws IOException
|
||||
{
|
||||
appendScriptResources("a/b.js", "z/a.js", "a/a.js");
|
||||
List<Resource> resources = resourceManager.getResources(ResourceType.SCRIPT);
|
||||
assertEquals(1, resources.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
/***
|
||||
* Copyright (c) 2015, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* https://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
package sonia.scm.resources;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Sets;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.servlet.ServletContext;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.Mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import sonia.scm.plugin.Plugin;
|
||||
import sonia.scm.plugin.PluginCondition;
|
||||
import sonia.scm.plugin.PluginInformation;
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
import sonia.scm.plugin.PluginResources;
|
||||
import sonia.scm.plugin.PluginWrapper;
|
||||
import sonia.scm.plugin.WebResourceLoader;
|
||||
|
||||
/**
|
||||
* Base class for {@link ResourceManager} tests.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public abstract class ResourceManagerTestBase
|
||||
{
|
||||
|
||||
@Mock
|
||||
protected ServletContext servletContext;
|
||||
|
||||
@Mock
|
||||
protected PluginLoader pluginLoader;
|
||||
|
||||
@Mock
|
||||
protected ResourceHandler resourceHandler;
|
||||
|
||||
@Mock
|
||||
protected WebResourceLoader webResourceLoader;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
/**
|
||||
* Append scripts resources to plugin loader.
|
||||
*
|
||||
* @param resources resource names
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
protected void appendScriptResources(String... resources) throws IOException
|
||||
{
|
||||
Set<String> scripts = Sets.newHashSet(resources);
|
||||
Set<String> styles = Sets.newHashSet();
|
||||
Set<String> dependencies = Sets.newHashSet();
|
||||
|
||||
|
||||
Plugin plugin = new Plugin(
|
||||
2,
|
||||
new PluginInformation(),
|
||||
new PluginResources(scripts, styles),
|
||||
new PluginCondition(),
|
||||
false,
|
||||
dependencies
|
||||
);
|
||||
|
||||
Path pluginPath = tempFolder.newFolder().toPath();
|
||||
|
||||
PluginWrapper wrapper = new PluginWrapper(
|
||||
plugin,
|
||||
Thread.currentThread().getContextClassLoader(),
|
||||
webResourceLoader,
|
||||
pluginPath
|
||||
);
|
||||
|
||||
List<PluginWrapper> plugins = ImmutableList.of(wrapper);
|
||||
|
||||
when(pluginLoader.getInstalledPlugins()).thenReturn(plugins);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package sonia.scm.security;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AccessTokenCookieIssuerTest {
|
||||
|
||||
private ScmConfiguration configuration;
|
||||
|
||||
private AccessTokenCookieIssuer issuer;
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
|
||||
@Mock
|
||||
private HttpServletResponse response;
|
||||
|
||||
@Mock
|
||||
private AccessToken accessToken;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<Cookie> cookieArgumentCaptor;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
configuration = new ScmConfiguration();
|
||||
issuer = new AccessTokenCookieIssuer(configuration);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContextPath() {
|
||||
assertContextPath("/scm", "/scm");
|
||||
assertContextPath("/", "/");
|
||||
assertContextPath("", "/");
|
||||
assertContextPath(null, "/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpOnlyShouldBeEnabledIfXsrfProtectionIsDisabled() {
|
||||
configuration.setEnabledXsrfProtection(false);
|
||||
|
||||
Cookie cookie = authenticate();
|
||||
|
||||
assertTrue(cookie.isHttpOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpOnlyShouldBeDisabled() {
|
||||
Cookie cookie = authenticate();
|
||||
|
||||
assertFalse(cookie.isHttpOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secureShouldBeSetIfTheRequestIsSecure() {
|
||||
when(request.isSecure()).thenReturn(true);
|
||||
|
||||
Cookie cookie = authenticate();
|
||||
|
||||
assertTrue(cookie.getSecure());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secureShouldBeDisabledIfTheRequestIsNotSecure() {
|
||||
when(request.isSecure()).thenReturn(false);
|
||||
|
||||
Cookie cookie = authenticate();
|
||||
|
||||
assertFalse(cookie.getSecure());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidate() {
|
||||
issuer.invalidate(request, response);
|
||||
|
||||
verify(response).addCookie(cookieArgumentCaptor.capture());
|
||||
Cookie cookie = cookieArgumentCaptor.getValue();
|
||||
|
||||
assertEquals(0, cookie.getMaxAge());
|
||||
}
|
||||
|
||||
private Cookie authenticate() {
|
||||
when(accessToken.getExpiration()).thenReturn(new Date());
|
||||
|
||||
issuer.authenticate(request, response, accessToken);
|
||||
|
||||
verify(response).addCookie(cookieArgumentCaptor.capture());
|
||||
return cookieArgumentCaptor.getValue();
|
||||
}
|
||||
|
||||
|
||||
private void assertContextPath(String contextPath, String expected) {
|
||||
when(request.getContextPath()).thenReturn(contextPath);
|
||||
assertEquals(expected, issuer.contextPath(request));
|
||||
}
|
||||
}
|
||||
@@ -57,11 +57,15 @@ import sonia.scm.repository.RepositoryTestData;
|
||||
import sonia.scm.user.User;
|
||||
import sonia.scm.user.UserTestData;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.anyObject;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AuthorizationCollector}.
|
||||
|
||||
@@ -36,18 +36,26 @@ import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.collect.Sets;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.hamcrest.Matchers.isEmptyOrNullString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Unit test for {@link JwtAccessTokenBuilder}.
|
||||
@@ -57,6 +65,10 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class JwtAccessTokenBuilderTest {
|
||||
|
||||
{
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Mock
|
||||
private KeyGenerator keyGenerator;
|
||||
|
||||
|
||||
@@ -30,8 +30,7 @@
|
||||
*/
|
||||
package sonia.scm.selenium.page;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import java.util.List;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.JavascriptExecutor;
|
||||
import org.openqa.selenium.NotFoundException;
|
||||
@@ -41,6 +40,8 @@ import org.openqa.selenium.support.FindBy;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Page object for scm-manager's repository creation page.
|
||||
*
|
||||
@@ -130,7 +131,7 @@ public class RepositoriesAddPage extends BasePage<RepositoriesAddPage> {
|
||||
String script = "Sonia.repository.getTypeByName('" + type + "').displayName;";
|
||||
displayName = (String) ((JavascriptExecutor)driver).executeScript(script);
|
||||
}
|
||||
return Objects.firstNonNull(displayName, type);
|
||||
return MoreObjects.firstNonNull(displayName, type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user