Remove tags and branches from browse result and embed files

This commit is contained in:
René Pfeuffer
2018-09-05 15:17:49 +02:00
parent d7e319a856
commit e79041140f
7 changed files with 19 additions and 46 deletions

View File

@@ -97,7 +97,7 @@ public class RepositoryAccessITCase {
.then() .then()
.statusCode(HttpStatus.SC_OK) .statusCode(HttpStatus.SC_OK)
.extract() .extract()
.path("files.find{it.name=='a.txt'}._links.self.href"); .path("_embedded.files.find{it.name=='a.txt'}._links.self.href");
given() given()
.when() .when()
@@ -112,7 +112,7 @@ public class RepositoryAccessITCase {
.then() .then()
.statusCode(HttpStatus.SC_OK) .statusCode(HttpStatus.SC_OK)
.extract() .extract()
.path("files.find{it.name=='subfolder'}._links.self.href"); .path("_embedded.files.find{it.name=='subfolder'}._links.self.href");
String selfOfSubfolderUrl = given() String selfOfSubfolderUrl = given()
.when() .when()
.get(subfolderSourceUrl) .get(subfolderSourceUrl)
@@ -127,7 +127,7 @@ public class RepositoryAccessITCase {
.then() .then()
.statusCode(HttpStatus.SC_OK) .statusCode(HttpStatus.SC_OK)
.extract() .extract()
.path("files[0]._links.self.href"); .path("_embedded.files[0]._links.self.href");
given() given()
.when() .when()
.get(subfolderContentUrl) .get(subfolderContentUrl)

View File

@@ -6,18 +6,13 @@ import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import java.util.Iterator;
import java.util.List; import java.util.List;
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
public class BrowserResultDto extends HalRepresentation implements Iterable<FileObjectDto> { public class BrowserResultDto extends HalRepresentation {
private String revision; private String revision;
private String tag;
private String branch;
// REVIEW files nicht embedded?
private List<FileObjectDto> files;
@Override @Override
@SuppressWarnings("squid:S1185") // We want to have this method available in this package @SuppressWarnings("squid:S1185") // We want to have this method available in this package
@@ -25,16 +20,7 @@ public class BrowserResultDto extends HalRepresentation implements Iterable<File
return super.add(links); return super.add(links);
} }
// REVIEW return null? public void setFiles(List<FileObjectDto> files) {
@Override this.withEmbedded("files", files);
public Iterator<FileObjectDto> iterator() {
Iterator<FileObjectDto> it = null;
if (files != null)
{
it = files.iterator();
}
return it;
} }
} }

View File

@@ -20,8 +20,6 @@ public class BrowserResultToBrowserResultDtoMapper {
public BrowserResultDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName, String path) { public BrowserResultDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName, String path) {
BrowserResultDto browserResultDto = new BrowserResultDto(); BrowserResultDto browserResultDto = new BrowserResultDto();
browserResultDto.setTag(browserResult.getTag());
browserResultDto.setBranch(browserResult.getBranch());
browserResultDto.setRevision(browserResult.getRevision()); browserResultDto.setRevision(browserResult.getRevision());
List<FileObjectDto> fileObjectDtoList = new ArrayList<>(); List<FileObjectDto> fileObjectDtoList = new ArrayList<>();
@@ -48,6 +46,4 @@ public class BrowserResultToBrowserResultDtoMapper {
dto.add(Links.linkingTo().self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), browserResult.getRevision(), path)).build()); dto.add(Links.linkingTo().self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), browserResult.getRevision(), path)).build());
} }
} }
} }

View File

@@ -10,7 +10,6 @@ import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.SubRepository; import sonia.scm.repository.SubRepository;
import javax.inject.Inject; import javax.inject.Inject;
import java.net.URI;
@Mapper @Mapper
public abstract class FileObjectToFileObjectDtoMapper extends BaseMapper<FileObject, FileObjectDto> { public abstract class FileObjectToFileObjectDtoMapper extends BaseMapper<FileObject, FileObjectDto> {
@@ -27,19 +26,14 @@ public abstract class FileObjectToFileObjectDtoMapper extends BaseMapper<FileObj
String path = removeFirstSlash(fileObject.getPath()); String path = removeFirstSlash(fileObject.getPath());
Links.Builder links = Links.linkingTo(); Links.Builder links = Links.linkingTo();
if (dto.isDirectory()) { if (dto.isDirectory()) {
links.self(addPath(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, ""), path)); links.self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, path));
} else { } else {
links.self(addPath(resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, ""), path)); links.self(resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, path));
} }
dto.add(links.build()); dto.add(links.build());
} }
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
private String addPath(String sourceWithPath, String path) {
return URI.create(sourceWithPath).resolve(path).toASCIIString();
}
private String removeFirstSlash(String source) { private String removeFirstSlash(String source) {
return source.startsWith("/") ? source.substring(1) : source; return source.startsWith("/") ? source.substring(1) : source;
} }

View File

@@ -331,15 +331,16 @@ class ResourceLinks {
} }
public String sourceWithPath(String namespace, String name, String revision, String path) { public String sourceWithPath(String namespace, String name, String revision, String path) {
if (revision == null) { return addPath(sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("sources").parameters().method("get").parameters(revision, "").href(), path);
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("sources").parameters().method("get").parameters(null, path).href();
} else {
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("sources").parameters().method("get").parameters(revision, path).href();
}
} }
public String content(String namespace, String name, String revision, String path) { public String content(String namespace, String name, String revision, String path) {
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("content").parameters().method("get").parameters(revision, path).href(); return addPath(sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("content").parameters().method("get").parameters(revision, "").href(), path);
}
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
private String addPath(String sourceWithPath, String path) {
return URI.create(sourceWithPath).resolve(path).toASCIIString();
} }
} }
public PermissionLinks permission() { public PermissionLinks permission() {

View File

@@ -18,8 +18,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks; import static org.mockito.MockitoAnnotations.initMocks;
public class BrowserResultToBrowserResultDtoMapperTest { public class BrowserResultToBrowserResultDtoMapperTest {
@@ -60,6 +62,8 @@ public class BrowserResultToBrowserResultDtoMapperTest {
fileObject2.setPath("/path/object/2"); fileObject2.setPath("/path/object/2");
fileObject2.setDescription("description of file object 2"); fileObject2.setDescription("description of file object 2");
fileObject2.setDirectory(true); fileObject2.setDirectory(true);
when(fileObjectToFileObjectDtoMapper.map(any(), any(), any())).thenReturn(new FileObjectDto());
} }
@After @After
@@ -99,9 +103,7 @@ public class BrowserResultToBrowserResultDtoMapperTest {
private BrowserResult createBrowserResult() { private BrowserResult createBrowserResult() {
BrowserResult browserResult = new BrowserResult(); BrowserResult browserResult = new BrowserResult();
browserResult.setTag("Tag");
browserResult.setRevision("Revision"); browserResult.setRevision("Revision");
browserResult.setBranch("Branch");
browserResult.setFiles(createFileObjects()); browserResult.setFiles(createFileObjects());
return browserResult; return browserResult;
@@ -116,8 +118,6 @@ public class BrowserResultToBrowserResultDtoMapperTest {
} }
private void assertEqualAttributes(BrowserResult browserResult, BrowserResultDto dto) { 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()); assertThat(dto.getRevision()).isEqualTo(browserResult.getRevision());
} }

View File

@@ -90,8 +90,6 @@ public class SourceRootResourceTest {
dispatcher.invoke(request, response); dispatcher.invoke(request, response);
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContentAsString()).contains("\"revision\":\"revision\""); assertThat(response.getContentAsString()).contains("\"revision\":\"revision\"");
assertThat(response.getContentAsString()).contains("\"tag\":\"tag\"");
assertThat(response.getContentAsString()).contains("\"branch\":\"branch\"");
assertThat(response.getContentAsString()).contains("\"files\":"); assertThat(response.getContentAsString()).contains("\"files\":");
} }
@@ -108,9 +106,7 @@ public class SourceRootResourceTest {
@Test @Test
public void shouldGetResultForSingleFile() throws URISyntaxException, IOException, RevisionNotFoundException { public void shouldGetResultForSingleFile() throws URISyntaxException, IOException, RevisionNotFoundException {
BrowserResult browserResult = new BrowserResult(); BrowserResult browserResult = new BrowserResult();
browserResult.setBranch("abc");
browserResult.setRevision("revision"); browserResult.setRevision("revision");
browserResult.setTag("tag");
FileObject fileObject = new FileObject(); FileObject fileObject = new FileObject();
fileObject.setName("File Object!"); fileObject.setName("File Object!");