Added content link to FileObjectDto

This commit is contained in:
Philipp Czora
2018-08-16 15:20:20 +02:00
parent e792be5c76
commit edac761d67
5 changed files with 65 additions and 14 deletions

View File

@@ -17,8 +17,8 @@ public class FileObjectDto extends HalRepresentation {
private boolean directory;
private String description;
private int length;
// TODO: What about subrepos?
private Instant lastModified;
private SubRepositoryDto subRepository;
@Override
@SuppressWarnings("squid:S1185") // We want to have this method available in this package

View File

@@ -7,9 +7,12 @@ import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.SubRepository;
import javax.inject.Inject;
import static de.otto.edison.hal.Link.link;
@Mapper
public abstract class FileObjectMapper extends BaseMapper<FileObject, FileObjectDto> {
@@ -18,8 +21,13 @@ public abstract class FileObjectMapper extends BaseMapper<FileObject, FileObject
protected abstract FileObjectDto map(FileObject fileObject, @Context NamespaceAndName namespaceAndName, @Context String revision);
abstract SubRepositoryDto mapSubrepository(SubRepository subRepository);
@AfterMapping
void addLinks(FileObject fileObject, @MappingTarget FileObjectDto dto, @Context NamespaceAndName namespaceAndName, @Context String revision) {
dto.add(Links.linkingTo().self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, fileObject.getName())).build());
dto.add(Links.linkingTo()
.self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, fileObject.getName()))
.single(link("content", resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, fileObject.getName())))
.build());
}
}

View File

@@ -305,6 +305,10 @@ class ResourceLinks {
public String sourceWithPath(String namespace, String name, String revision, String path) {
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) {
return sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("content").parameters().method("get").parameters(revision, path).href();
}
}
public PermissionCollectionLinks permissionCollection() {

View File

@@ -0,0 +1,14 @@
package sonia.scm.api.v2.resources;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class SubRepositoryDto {
private String repositoryUrl;
private String browserUrl;
private String revision;
}

View File

@@ -10,24 +10,32 @@ 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 static org.junit.Assert.assertEquals;
import java.net.URI;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.Silent.class)
public class FileObjectMapperTest {
private final URI baseUri = URI.create("http://example.com/base/");
@SuppressWarnings("unused") // Is injected
private final ResourceLinks resourceLinks = ResourceLinksMock.createMock(baseUri);
@InjectMocks
private FileObjectMapperImpl mapper;
private final Subject subject = mock(Subject.class);
private final ThreadState subjectThreadState = new SubjectThreadState(subject);
private URI expectedBaseUri;
@Before
public void init() {
initMocks(this);
expectedBaseUri = baseUri.resolve(RepositoryRootResource.REPOSITORIES_PATH_V2 + "/");
subjectThreadState.bind();
ThreadContext.bind(subject);
}
@@ -36,11 +44,26 @@ public class FileObjectMapperTest {
@Test
public void shouldMapAttributesCorrectly() {
FileObject fileObject = createFileObject();
FileObjectDto dto = mapper.map(fileObject);
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
assertEqualAttributes(fileObject, dto);
}
@Test
public void shouldHaveCorrectSelfLink() {
FileObject fileObject = createFileObject();
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").toString());
}
@Test
public void shouldHaveCorrectContentLink() {
FileObject fileObject = createFileObject();
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
assertThat(dto.getLinks().getLinkBy("content").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/content/revision/foo").toString());
}
private FileObject createFileObject() {
FileObject fileObject = new FileObject();
@@ -50,16 +73,18 @@ public class FileObjectMapperTest {
fileObject.setDirectory(false);
fileObject.setLength(100);
fileObject.setLastModified(123L);
fileObject.setSubRepository(new SubRepository("repo.url"));
return fileObject;
}
//TODO: subrepo
private void assertEqualAttributes(FileObject fileObject, FileObjectDto dto) {
assertEquals(fileObject.getName(), dto.getName());
assertEquals(fileObject.getDescription(), dto.getDescription());
assertEquals(fileObject.getPath(), dto.getPath());
assertEquals(fileObject.isDirectory(), dto.isDirectory());
assertEquals(fileObject.getLength(), dto.getLength());
assertEquals((long)fileObject.getLastModified(), dto.getLastModified().toEpochMilli());
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());
}
}