mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 17:05:43 +01:00
Append self links to parsed diff results
This commit is contained in:
@@ -2,6 +2,7 @@ package sonia.scm.api.v2.resources;
|
||||
|
||||
|
||||
import com.google.inject.util.Providers;
|
||||
import de.otto.edison.hal.Links;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.subject.support.SubjectThreadState;
|
||||
@@ -13,6 +14,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.NotFoundException;
|
||||
@@ -20,6 +22,8 @@ import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.api.DiffCommandBuilder;
|
||||
import sonia.scm.repository.api.DiffFormat;
|
||||
import sonia.scm.repository.api.DiffResult;
|
||||
import sonia.scm.repository.api.DiffResultCommandBuilder;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
import sonia.scm.util.CRLFInjectionException;
|
||||
@@ -34,7 +38,6 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -45,6 +48,7 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
|
||||
public static final String DIFF_PATH = "space/repo/diff/";
|
||||
public static final String DIFF_URL = "/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + DIFF_PATH;
|
||||
public static final Repository REPOSITORY = new Repository("repoId", "git", "space", "repo");
|
||||
|
||||
private RestDispatcher dispatcher = new RestDispatcher();
|
||||
|
||||
@@ -54,8 +58,13 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
@Mock
|
||||
private RepositoryService service;
|
||||
|
||||
@Mock
|
||||
@Mock(answer = Answers.RETURNS_SELF)
|
||||
private DiffCommandBuilder diffCommandBuilder;
|
||||
@Mock(answer = Answers.RETURNS_SELF)
|
||||
private DiffResultCommandBuilder diffResultCommandBuilder;
|
||||
|
||||
@Mock
|
||||
private DiffResultToDiffResultDtoMapper diffResultToDiffResultDtoMapper;
|
||||
|
||||
private DiffRootResource diffRootResource;
|
||||
|
||||
@@ -66,15 +75,16 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() {
|
||||
diffRootResource = new DiffRootResource(serviceFactory);
|
||||
diffRootResource = new DiffRootResource(serviceFactory, diffResultToDiffResultDtoMapper);
|
||||
super.diffRootResource = Providers.of(diffRootResource);
|
||||
dispatcher.addSingletonResource(getRepositoryRootResource());
|
||||
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(service.getRepository()).thenReturn(REPOSITORY);
|
||||
ExceptionWithContextToErrorDtoMapperImpl mapper = new ExceptionWithContextToErrorDtoMapperImpl();
|
||||
dispatcher.registerException(CRLFInjectionException.class, Response.Status.BAD_REQUEST);
|
||||
when(service.getDiffCommand()).thenReturn(diffCommandBuilder);
|
||||
when(service.getDiffResultCommand()).thenReturn(diffResultCommandBuilder);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
when(subject.isPermitted(any(String.class))).thenReturn(true);
|
||||
@@ -87,8 +97,6 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldGetDiffs() throws Exception {
|
||||
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setFormat(any())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.retrieveContent()).thenReturn(output -> {});
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(DIFF_URL + "revision")
|
||||
@@ -106,6 +114,25 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
.contains(expectedValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetParsedDiffs() throws Exception {
|
||||
DiffResult diffResult = mock(DiffResult.class);
|
||||
when(diffResultCommandBuilder.getDiffResult()).thenReturn(diffResult);
|
||||
when(diffResultToDiffResultDtoMapper.mapForRevision(REPOSITORY, diffResult, "revision"))
|
||||
.thenReturn(new DiffResultDto(Links.linkingTo().self("http://self").build()));
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(DIFF_URL + "revision/parsed")
|
||||
.accept(VndMediaType.DIFF_PARSED);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertThat(response.getStatus())
|
||||
.isEqualTo(200);
|
||||
assertThat(response.getContentAsString())
|
||||
.contains("\"self\":{\"href\":\"http://self\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet404OnMissingRepository() throws URISyntaxException {
|
||||
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(new NotFoundException("Text", "x"));
|
||||
@@ -119,8 +146,6 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldGet404OnMissingRevision() throws Exception {
|
||||
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setFormat(any())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.retrieveContent()).thenThrow(new NotFoundException("Text", "x"));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
@@ -135,8 +160,6 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldGet400OnCrlfInjection() throws Exception {
|
||||
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setFormat(any())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.retrieveContent()).thenThrow(new NotFoundException("Text", "x"));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
@@ -149,8 +172,6 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldGet400OnUnknownFormat() throws Exception {
|
||||
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setFormat(any())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.retrieveContent()).thenThrow(new NotFoundException("Test", "test"));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
@@ -163,8 +184,6 @@ public class DiffResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldAcceptDiffFormats() throws Exception {
|
||||
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setFormat(any())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.retrieveContent()).thenReturn(output -> {});
|
||||
|
||||
Arrays.stream(DiffFormat.values()).map(DiffFormat::name).forEach(
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Link;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.api.DiffFile;
|
||||
import sonia.scm.repository.api.DiffLine;
|
||||
import sonia.scm.repository.api.DiffResult;
|
||||
@@ -10,8 +12,10 @@ import sonia.scm.repository.api.Hunk;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import static java.net.URI.create;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -19,21 +23,14 @@ import static org.mockito.Mockito.when;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class DiffResultToDiffResultDtoMapperTest {
|
||||
|
||||
private static final Repository REPOSITORY = new Repository("1", "git", "space", "X");
|
||||
|
||||
ResourceLinks resourceLinks = ResourceLinksMock.createMock(create("/scm/api/v2"));
|
||||
DiffResultToDiffResultDtoMapper mapper = new DiffResultToDiffResultDtoMapper(resourceLinks);
|
||||
|
||||
@Test
|
||||
void shouldMapDiffResult() {
|
||||
DiffResult result = result(
|
||||
addedFile("A.java", "abc"),
|
||||
modifiedFile("B.ts", "def", "abc",
|
||||
hunk("@@ -3,4 1,2 @@", 1, 2, 3, 4,
|
||||
insertedLine("a", 1),
|
||||
modifiedLine("b", 2),
|
||||
deletedLine("c", 3)
|
||||
)
|
||||
),
|
||||
deletedFile("C.go", "ghi")
|
||||
);
|
||||
|
||||
DiffResultDto dto = DiffResultToDiffResultDtoMapper.INSTANCE.map(result);
|
||||
DiffResultDto dto = mapper.mapForRevision(REPOSITORY, createResult(), "123");
|
||||
|
||||
List<DiffResultDto.FileDto> files = dto.getFiles();
|
||||
assertAddedFile(files.get(0), "A.java", "abc", "java");
|
||||
@@ -49,6 +46,44 @@ class DiffResultToDiffResultDtoMapperTest {
|
||||
assertDeletedLine(changes.get(2), "c", 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateSelfLinkForRevision() {
|
||||
DiffResultDto dto = mapper.mapForRevision(REPOSITORY, createResult(), "123");
|
||||
|
||||
Optional<Link> selfLink = dto.getLinks().getLinkBy("self");
|
||||
assertThat(selfLink)
|
||||
.isPresent()
|
||||
.get()
|
||||
.extracting("href")
|
||||
.contains("/scm/api/v2/repositories/space/X/diff/123/parsed");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateSelfLinkForIncoming() {
|
||||
DiffResultDto dto = mapper.mapForIncoming(REPOSITORY, createResult(), "feature/some", "master");
|
||||
|
||||
Optional<Link> selfLink = dto.getLinks().getLinkBy("self");
|
||||
assertThat(selfLink)
|
||||
.isPresent()
|
||||
.get()
|
||||
.extracting("href")
|
||||
.contains("/scm/api/v2/repositories/space/X/incoming/feature%2Fsome/master/diff/parsed");
|
||||
}
|
||||
|
||||
private DiffResult createResult() {
|
||||
return result(
|
||||
addedFile("A.java", "abc"),
|
||||
modifiedFile("B.ts", "def", "abc",
|
||||
hunk("@@ -3,4 1,2 @@", 1, 2, 3, 4,
|
||||
insertedLine("a", 1),
|
||||
modifiedLine("b", 2),
|
||||
deletedLine("c", 3)
|
||||
)
|
||||
),
|
||||
deletedFile("C.go", "ghi")
|
||||
);
|
||||
}
|
||||
|
||||
public void assertInsertedLine(DiffResultDto.ChangeDto change, String content, int lineNumber) {
|
||||
assertThat(change.getContent()).isEqualTo(content);
|
||||
assertThat(change.getLineNumber()).isEqualTo(lineNumber);
|
||||
|
||||
@@ -2,6 +2,7 @@ package sonia.scm.api.v2.resources;
|
||||
|
||||
|
||||
import com.google.inject.util.Providers;
|
||||
import de.otto.edison.hal.Links;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.subject.support.SubjectThreadState;
|
||||
@@ -24,6 +25,8 @@ import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.api.DiffCommandBuilder;
|
||||
import sonia.scm.repository.api.DiffResult;
|
||||
import sonia.scm.repository.api.DiffResultCommandBuilder;
|
||||
import sonia.scm.repository.api.LogCommandBuilder;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
@@ -45,6 +48,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static sonia.scm.repository.api.DiffFormat.NATIVE;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
@Slf4j
|
||||
@@ -54,6 +58,7 @@ public class IncomingRootResourceTest extends RepositoryTestBase {
|
||||
public static final String INCOMING_PATH = "space/repo/incoming/";
|
||||
public static final String INCOMING_CHANGESETS_URL = "/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + INCOMING_PATH;
|
||||
public static final String INCOMING_DIFF_URL = "/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + INCOMING_PATH;
|
||||
public static final Repository REPOSITORY = new Repository("repoId", "git", "space", "repo");
|
||||
|
||||
private RestDispatcher dispatcher = new RestDispatcher();
|
||||
|
||||
@@ -71,7 +76,11 @@ public class IncomingRootResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Mock
|
||||
private DiffCommandBuilder diffCommandBuilder;
|
||||
@Mock
|
||||
private DiffResultCommandBuilder diffResultCommandBuilder;
|
||||
|
||||
@Mock
|
||||
private DiffResultToDiffResultDtoMapper diffResultToDiffResultDtoMapper;
|
||||
|
||||
private IncomingChangesetCollectionToDtoMapper incomingChangesetCollectionToDtoMapper;
|
||||
|
||||
@@ -88,14 +97,15 @@ public class IncomingRootResourceTest extends RepositoryTestBase {
|
||||
@Before
|
||||
public void prepareEnvironment() {
|
||||
incomingChangesetCollectionToDtoMapper = new IncomingChangesetCollectionToDtoMapper(changesetToChangesetDtoMapper, resourceLinks);
|
||||
incomingRootResource = new IncomingRootResource(serviceFactory, incomingChangesetCollectionToDtoMapper);
|
||||
incomingRootResource = new IncomingRootResource(serviceFactory, incomingChangesetCollectionToDtoMapper, diffResultToDiffResultDtoMapper);
|
||||
super.incomingRootResource = Providers.of(incomingRootResource);
|
||||
dispatcher.addSingletonResource(getRepositoryRootResource());
|
||||
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"));
|
||||
when(serviceFactory.create(REPOSITORY)).thenReturn(repositoryService);
|
||||
when(repositoryService.getRepository()).thenReturn(REPOSITORY);
|
||||
when(repositoryService.getLogCommand()).thenReturn(logCommandBuilder);
|
||||
when(repositoryService.getDiffCommand()).thenReturn(diffCommandBuilder);
|
||||
when(repositoryService.getDiffResultCommand()).thenReturn(diffResultCommandBuilder);
|
||||
dispatcher.registerException(CRLFInjectionException.class, Response.Status.BAD_REQUEST);
|
||||
subjectThreadState.bind();
|
||||
ThreadContext.bind(subject);
|
||||
@@ -170,9 +180,9 @@ public class IncomingRootResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldGetDiffs() throws Exception {
|
||||
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setAncestorChangeset(anyString())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setFormat(any())).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setRevision("src_changeset_id")).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setAncestorChangeset("target_changeset_id")).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.setFormat(NATIVE)).thenReturn(diffCommandBuilder);
|
||||
when(diffCommandBuilder.retrieveContent()).thenReturn(output -> {});
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(INCOMING_DIFF_URL + "src_changeset_id/target_changeset_id/diff")
|
||||
@@ -190,6 +200,28 @@ public class IncomingRootResourceTest extends RepositoryTestBase {
|
||||
.contains(expectedValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetParsedDiffs() throws Exception {
|
||||
when(diffResultCommandBuilder.setRevision("src_changeset_id")).thenReturn(diffResultCommandBuilder);
|
||||
when(diffResultCommandBuilder.setAncestorChangeset("target_changeset_id")).thenReturn(diffResultCommandBuilder);
|
||||
DiffResult diffResult = mock(DiffResult.class);
|
||||
when(diffResultCommandBuilder.getDiffResult()).thenReturn(diffResult);
|
||||
when(diffResultToDiffResultDtoMapper.mapForIncoming(REPOSITORY, diffResult, "src_changeset_id", "target_changeset_id"))
|
||||
.thenReturn(new DiffResultDto(Links.linkingTo().self("http://self").build()));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.get(INCOMING_DIFF_URL + "src_changeset_id/target_changeset_id/diff/parsed")
|
||||
.accept(VndMediaType.DIFF_PARSED);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertThat(response.getStatus())
|
||||
.isEqualTo(200);
|
||||
assertThat(response.getContentAsString())
|
||||
.contains("\"self\":{\"href\":\"http://self\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGet404OnMissingRepository() throws URISyntaxException {
|
||||
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(new NotFoundException("Text", "x"));
|
||||
|
||||
Reference in New Issue
Block a user