Skip to content
Snippets Groups Projects
Commit 38d8be8f authored by Felix Reichenbach's avatar Felix Reichenbach
Browse files

OZG-7615 add documentation link

parent 2f5e4cf5
No related branches found
No related tags found
1 merge request!6Ozg 7615 leitfaden link
package de.ozgcloud.admin;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = DocumentationProperties.DOCUMENTATION_PROPERTIES_PREFIX)
public class DocumentationProperties {
static final String DOCUMENTATION_PROPERTIES_PREFIX = "ozgcloud.user-assistance.documentation";
private String url;
}
...@@ -23,8 +23,7 @@ ...@@ -23,8 +23,7 @@
*/ */
package de.ozgcloud.admin; package de.ozgcloud.admin;
import java.util.ArrayList; import java.util.Objects;
import java.util.List;
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties; import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties;
import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.EntityModel;
...@@ -40,24 +39,19 @@ import lombok.RequiredArgsConstructor; ...@@ -40,24 +39,19 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
public class RootModelAssembler implements RepresentationModelAssembler<Root, EntityModel<Root>> { public class RootModelAssembler implements RepresentationModelAssembler<Root, EntityModel<Root>> {
static final String REL_CONFIGURATION = "configuration"; static final String REL_CONFIGURATION = "configuration";
static final String REL_DOCUMENTATIONS = "documentations";
private final RepositoryRestProperties restProperties; private final RepositoryRestProperties restProperties;
private final CurrentUserService currentUserService; private final CurrentUserService currentUserService;
private final DocumentationProperties documentationProperties;
@Override @Override
public EntityModel<Root> toModel(Root root) { public EntityModel<Root> toModel(Root root) {
List<Link> links = buildRootModelLinks(); var rootModel = EntityModel.of(root);
return EntityModel.of(root, links); rootModel.add(WebMvcLinkBuilder.linkTo(RootController.class).withSelfRel());
} rootModel.addIf(currentUserService.hasConfigurationPermission(), this::buildConfigLink);
rootModel.addIf(Objects.nonNull(documentationProperties.getUrl()), () -> Link.of(documentationProperties.getUrl(), REL_DOCUMENTATIONS));
List<Link> buildRootModelLinks() { return rootModel;
List<Link> links = new ArrayList<>();
var rootLinkBuilder = WebMvcLinkBuilder.linkTo(RootController.class);
links.add(rootLinkBuilder.withSelfRel());
if (currentUserService.hasConfigurationPermission()) {
links.add(buildConfigLink());
}
return links;
} }
private Link buildConfigLink() { private Link buildConfigLink() {
......
...@@ -27,8 +27,6 @@ import static de.ozgcloud.admin.RootModelAssembler.*; ...@@ -27,8 +27,6 @@ import static de.ozgcloud.admin.RootModelAssembler.*;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
...@@ -37,8 +35,11 @@ import org.mockito.InjectMocks; ...@@ -37,8 +35,11 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties; import org.springframework.boot.autoconfigure.data.rest.RepositoryRestProperties;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link; import org.springframework.hateoas.Link;
import com.thedeanda.lorem.LoremIpsum;
import de.ozgcloud.admin.common.user.CurrentUserService; import de.ozgcloud.admin.common.user.CurrentUserService;
class RootModelAssemblerTest { class RootModelAssemblerTest {
...@@ -53,42 +54,35 @@ class RootModelAssemblerTest { ...@@ -53,42 +54,35 @@ class RootModelAssemblerTest {
private RepositoryRestProperties restProperties; private RepositoryRestProperties restProperties;
@Mock @Mock
private CurrentUserService currentUserService; private CurrentUserService currentUserService;
@Mock
private DocumentationProperties documentationProperties;
@DisplayName("Entity Model") @DisplayName("Entity Model")
@Nested @Nested
class TestEntityModel { class TestToModel {
private final List<Link> links = List.of(Link.of(RootController.PATH)); private final Root root = RootTestFactory.create();
@BeforeEach
void beforeEach() {
doReturn(links).when(modelAssembler).buildRootModelLinks();
}
@Test @Test
void shouldHaveRoot() { void shouldHaveRootContent() {
var givenRoot = RootTestFactory.create(); var resultRoot = modelAssembler.toModel(root).getContent();
var resultRoot = modelAssembler.toModel(givenRoot).getContent();
assertThat(resultRoot).isEqualTo(givenRoot); assertThat(resultRoot).isEqualTo(root);
} }
@Test @Test
void shouldHaveLinks() { void shouldHaveSelfLink() {
var modelLinks = modelAssembler.toModel(RootTestFactory.create()).getLinks(); var model = modelAssembler.toModel(root);
assertThat(modelLinks).containsAll(links); assertThat(model.getLink(IanaLinkRelations.SELF)).get().extracting(Link::getHref).isEqualTo(RootController.PATH);
}
} }
@DisplayName("Root Model Links")
@Nested @Nested
class TestBuildRootModelLinks { class TestConfigurationLink {
@Test @Test
void shouldCheckConfigurationPermission() { void shouldCallHasConfigurationPermission() {
modelAssembler.buildRootModelLinks(); modelAssembler.toModel(root);
verify(currentUserService).hasConfigurationPermission(); verify(currentUserService).hasConfigurationPermission();
} }
...@@ -104,16 +98,14 @@ class RootModelAssemblerTest { ...@@ -104,16 +98,14 @@ class RootModelAssemblerTest {
@Test @Test
void shouldHaveHrefToConfiguration() { void shouldHaveHrefToConfiguration() {
var links = modelAssembler.buildRootModelLinks(); var model = modelAssembler.toModel(root);
assertThat(links).containsExactly( assertThat(model.getLink(REL_CONFIGURATION)).get().extracting(Link::getHref).isEqualTo(BASE_PATH);
Link.of(RootController.PATH),
Link.of(BASE_PATH, REL_CONFIGURATION));
} }
} }
@Nested @Nested
class TestOnNotHasConfigurationPermission { class TestOnHasNotConfigurationPermission {
@BeforeEach @BeforeEach
void hasNotConfigurationPermission() { void hasNotConfigurationPermission() {
...@@ -121,14 +113,57 @@ class RootModelAssemblerTest { ...@@ -121,14 +113,57 @@ class RootModelAssemblerTest {
} }
@Test @Test
void shouldHaveOnlySelfLink() { void shouldNotHaveConfigurationLink() {
var links = modelAssembler.buildRootModelLinks(); var model = modelAssembler.toModel(root);
assertThat(links).containsExactly( assertThat(model.getLink(REL_CONFIGURATION)).isEmpty();
Link.of(RootController.PATH)); }
} }
} }
@Nested
class TestDocumentationLink {
@Test
void shouldGetDocumentationUrl() {
modelAssembler.toModel(root);
verify(documentationProperties).getUrl();
} }
@Nested
class TestOnDocumentationUrlGiven {
private final String documentationUrl = LoremIpsum.getInstance().getUrl();
@BeforeEach
void mock() {
when(documentationProperties.getUrl()).thenReturn(documentationUrl);
}
@Test
void shouldHaveDocumentationLink() {
var model = modelAssembler.toModel(root);
assertThat(model.getLink(REL_DOCUMENTATIONS)).get().extracting(Link::getHref).isEqualTo(documentationUrl);
}
}
@Nested
class TestOnDocumentationUrlNotGiven {
@BeforeEach
void mock() {
when(documentationProperties.getUrl()).thenReturn(null);
}
@Test
void shouldNotHaveDocumentationLink() {
var model = modelAssembler.toModel(root);
assertThat(model.getLink(REL_DOCUMENTATIONS)).isEmpty();
}
}
}
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment