mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-16 18:26:16 +01:00
merge
This commit is contained in:
@@ -18,8 +18,11 @@ import java.util.ArrayList;
|
||||
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.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
public class BrowserResultToBrowserResultDtoMapperTest {
|
||||
@@ -60,6 +63,9 @@ public class BrowserResultToBrowserResultDtoMapperTest {
|
||||
fileObject2.setPath("/path/object/2");
|
||||
fileObject2.setDescription("description of file object 2");
|
||||
fileObject2.setDirectory(true);
|
||||
|
||||
when(fileObjectToFileObjectDtoMapper.map(any(FileObject.class), any(NamespaceAndName.class), anyString()))
|
||||
.thenReturn(new FileObjectDto());
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -71,7 +77,7 @@ public class BrowserResultToBrowserResultDtoMapperTest {
|
||||
public void shouldMapAttributesCorrectly() {
|
||||
BrowserResult browserResult = createBrowserResult();
|
||||
|
||||
BrowserResultDto dto = mapper.map(browserResult, new NamespaceAndName("foo", "bar"));
|
||||
BrowserResultDto dto = mapper.map(browserResult, new NamespaceAndName("foo", "bar"), "path");
|
||||
|
||||
assertEqualAttributes(browserResult, dto);
|
||||
}
|
||||
@@ -81,17 +87,25 @@ public class BrowserResultToBrowserResultDtoMapperTest {
|
||||
BrowserResult browserResult = createBrowserResult();
|
||||
NamespaceAndName namespaceAndName = new NamespaceAndName("foo", "bar");
|
||||
|
||||
BrowserResultDto dto = mapper.map(browserResult, namespaceAndName);
|
||||
BrowserResultDto dto = mapper.map(browserResult, namespaceAndName, "path");
|
||||
|
||||
verify(fileObjectToFileObjectDtoMapper).map(fileObject1, namespaceAndName, "Revision");
|
||||
verify(fileObjectToFileObjectDtoMapper).map(fileObject2, namespaceAndName, "Revision");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetLinksCorrectly() {
|
||||
BrowserResult browserResult = createBrowserResult();
|
||||
NamespaceAndName namespaceAndName = new NamespaceAndName("foo", "bar");
|
||||
|
||||
BrowserResultDto dto = mapper.map(browserResult, namespaceAndName, "path");
|
||||
|
||||
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).contains("path");
|
||||
}
|
||||
|
||||
private BrowserResult createBrowserResult() {
|
||||
BrowserResult browserResult = new BrowserResult();
|
||||
browserResult.setTag("Tag");
|
||||
browserResult.setRevision("Revision");
|
||||
browserResult.setBranch("Branch");
|
||||
browserResult.setFiles(createFileObjects());
|
||||
|
||||
return browserResult;
|
||||
@@ -106,8 +120,6 @@ public class BrowserResultToBrowserResultDtoMapperTest {
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ public class ChangesetRootResourceTest {
|
||||
private RepositoryServiceFactory serviceFactory;
|
||||
|
||||
@Mock
|
||||
private RepositoryService service;
|
||||
private RepositoryService repositoryService;
|
||||
|
||||
@Mock
|
||||
private LogCommandBuilder logCommandBuilder;
|
||||
@@ -83,12 +83,12 @@ public class ChangesetRootResourceTest {
|
||||
.of(new RepositoryResource(null, null, null, null, null,
|
||||
MockProvider.of(changesetRootResource), null, null, null, null, null)), null);
|
||||
dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
|
||||
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"));
|
||||
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(repositoryService);
|
||||
when(serviceFactory.create(any(Repository.class))).thenReturn(repositoryService);
|
||||
when(repositoryService.getRepository()).thenReturn(new Repository("repoId", "git", "space", "repo"));
|
||||
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
|
||||
when(service.getLogCommand()).thenReturn(logCommandBuilder);
|
||||
when(repositoryService.getLogCommand()).thenReturn(logCommandBuilder);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
when(subject.isPermitted(any(String.class))).thenReturn(true);
|
||||
|
||||
@@ -89,8 +89,6 @@ public class SourceRootResourceTest {
|
||||
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\":");
|
||||
}
|
||||
|
||||
@@ -107,9 +105,7 @@ public class SourceRootResourceTest {
|
||||
@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!");
|
||||
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.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.junit.After;
|
||||
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.api.rest.AuthorizationExceptionMapper;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.Tag;
|
||||
import sonia.scm.repository.Tags;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
import sonia.scm.repository.api.TagsCommandBuilder;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@Slf4j
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class TagRootResourceTest {
|
||||
|
||||
public static final String TAG_PATH = "space/repo/tags/";
|
||||
public static final String TAG_URL = "/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + TAG_PATH;
|
||||
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 repositoryService;
|
||||
|
||||
@Mock
|
||||
private TagsCommandBuilder tagsCommandBuilder;
|
||||
|
||||
private TagCollectionToDtoMapper tagCollectionToDtoMapper;
|
||||
|
||||
@InjectMocks
|
||||
private TagToTagDtoMapperImpl tagToTagDtoMapper;
|
||||
|
||||
private TagRootResource tagRootResource;
|
||||
|
||||
|
||||
private final Subject subject = mock(Subject.class);
|
||||
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
|
||||
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws Exception {
|
||||
tagCollectionToDtoMapper = new TagCollectionToDtoMapper(resourceLinks, tagToTagDtoMapper);
|
||||
tagRootResource = new TagRootResource(serviceFactory, tagCollectionToDtoMapper, tagToTagDtoMapper);
|
||||
RepositoryRootResource repositoryRootResource = new RepositoryRootResource(MockProvider
|
||||
.of(new RepositoryResource(null, null, null, MockProvider.of(tagRootResource), null,
|
||||
null, null, null, null, null, null)), null);
|
||||
dispatcher.getRegistry().addSingletonResource(repositoryRootResource);
|
||||
when(serviceFactory.create(new NamespaceAndName("space", "repo"))).thenReturn(repositoryService);
|
||||
when(serviceFactory.create(any(Repository.class))).thenReturn(repositoryService);
|
||||
when(repositoryService.getRepository()).thenReturn(new Repository("repoId", "git", "space", "repo"));
|
||||
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
|
||||
when(repositoryService.getTagsCommand()).thenReturn(tagsCommandBuilder);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
when(subject.isPermitted(any(String.class))).thenReturn(true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupContext() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet404OnMissingTag() throws Exception {
|
||||
Tags tags = new Tags();
|
||||
tags.setTags(Lists.emptyList());
|
||||
when(tagsCommandBuilder.getTags()).thenReturn(tags);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(TAG_URL + "not_existing_tag")
|
||||
.accept(VndMediaType.TAG);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(404, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetEmptyTagListOnMissingTags() throws Exception {
|
||||
Tags tags = new Tags();
|
||||
tags.setTags(Lists.emptyList());
|
||||
when(tagsCommandBuilder.getTags()).thenReturn(tags);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(TAG_URL)
|
||||
.accept(VndMediaType.TAG_COLLECTION);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(200, response.getStatus());
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getContentAsString())
|
||||
.isNotBlank()
|
||||
.contains("_links");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet500OnTagCommandError() throws Exception {
|
||||
when(tagsCommandBuilder.getTags()).thenReturn(null);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(TAG_URL + "not_existing_tag")
|
||||
.accept(VndMediaType.TAG);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(500, response.getStatus());
|
||||
|
||||
request = MockHttpRequest
|
||||
.get(TAG_URL)
|
||||
.accept(VndMediaType.TAG_COLLECTION);
|
||||
response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(500, response.getStatus());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldGetTags() throws Exception {
|
||||
Tags tags = new Tags();
|
||||
String tag1 = "v1.0";
|
||||
String revision1 = "revision_1234";
|
||||
String tag2 = "v2.0";
|
||||
String revision2 = "revision_12345";
|
||||
tags.setTags(Lists.newArrayList(new Tag(tag1, revision1), new Tag(tag2, revision2)));
|
||||
when(tagsCommandBuilder.getTags()).thenReturn(tags);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(TAG_URL)
|
||||
.accept(VndMediaType.TAG_COLLECTION);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(200, response.getStatus());
|
||||
log.info("the content: ", response.getContentAsString());
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"name\":\"%s\"", tag1)));
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"revision\":\"%s\"", revision1)));
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"name\":\"%s\"", tag2)));
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"revision\":\"%s\"", revision2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetTag() throws Exception {
|
||||
Tags tags = new Tags();
|
||||
String tag1 = "v1.0";
|
||||
String revision1 = "revision_1234";
|
||||
String tag2 = "v2.0";
|
||||
String revision2 = "revision_12345";
|
||||
tags.setTags(Lists.newArrayList(new Tag(tag1, revision1), new Tag(tag2, revision2)));
|
||||
when(tagsCommandBuilder.getTags()).thenReturn(tags);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(TAG_URL + tag1)
|
||||
.accept(VndMediaType.TAG);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(200, response.getStatus());
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"name\":\"%s\"", tag1)));
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"revision\":\"%s\"", revision1)));
|
||||
|
||||
request = MockHttpRequest
|
||||
.get(TAG_URL + tag2)
|
||||
.accept(VndMediaType.TAG);
|
||||
response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
assertEquals(200, response.getStatus());
|
||||
log.info("the content: ", response.getContentAsString());
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"name\":\"%s\"", tag2)));
|
||||
assertTrue(response.getContentAsString().contains(String.format("\"revision\":\"%s\"", revision2)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user