From 4b4323f13e923b646417f255f650886fe25670f5 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Mon, 27 Jun 2022 15:40:45 +0200 Subject: [PATCH] OZG-1513 add createdBy link to postfachNachricht if nachricht was triggered by client --- .../de/itvsh/goofy/postfach/PostfachMail.java | 4 +-- .../postfach/PostfachMailModelAssembler.java | 8 +++++ .../PostfachMailModelAssemblerTest.java | 30 +++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMail.java b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMail.java index a780e3d688..ed84924b41 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMail.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMail.java @@ -10,11 +10,9 @@ import javax.validation.constraints.Size; import com.fasterxml.jackson.annotation.JsonIgnore; -import de.itvsh.goofy.common.LinkedResource; import de.itvsh.goofy.common.binaryfile.FileId; import de.itvsh.goofy.common.command.CommandBody; import de.itvsh.goofy.common.user.UserId; -import de.itvsh.goofy.common.user.UserProfileController; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; @@ -42,7 +40,7 @@ public class PostfachMail implements CommandBody { private String vorgangId; private ZonedDateTime createdAt; - @LinkedResource(controllerClass = UserProfileController.class) + @JsonIgnore private UserId createdBy; private ZonedDateTime sentAt; diff --git a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailModelAssembler.java b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailModelAssembler.java index a323d38ae2..74734a9518 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailModelAssembler.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/postfach/PostfachMailModelAssembler.java @@ -15,6 +15,7 @@ import org.springframework.stereotype.Component; import de.itvsh.goofy.common.ModelBuilder; import de.itvsh.goofy.common.binaryfile.BinaryFileController; import de.itvsh.goofy.common.command.CommandController.CommandByRelationController; +import de.itvsh.goofy.common.user.UserProfileController; import de.itvsh.goofy.postfach.PostfachMailController.PostfachMailCommandController; @Component @@ -24,10 +25,15 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac public static final String REL_RESEND_POSTFACH_MAIL = "resendPostfachMail"; public static final String REL_ATTACHMENTS = "attachments"; static final String REL_UPLOAD_ATTACHMENT = "uploadAttachment"; + static final String REL_CREATED_BY = "createdBy"; private static final Predicate<PostfachMail> SENT_FAILED = postfachMail -> BooleanUtils.isFalse(postfachMail.getSentSuccessful()); private static final Predicate<PostfachMail> HAS_ATTACHMENTS = nachricht -> !nachricht.getAttachments().isEmpty(); + static final String SYSTEM_USER_IDENTIFIER = "system"; + private static final Predicate<PostfachMail> SENT_BY_CLIENT_USER = postfachNachricht -> !postfachNachricht.getCreatedBy().toString() + .startsWith(SYSTEM_USER_IDENTIFIER); + public CollectionModel<EntityModel<PostfachMail>> toCollectionModel(Stream<PostfachMail> entities, String vorgangId, long vorgangVersion, Optional<String> postfachId) { CollectionModel<EntityModel<PostfachMail>> model = CollectionModel.of(entities.map(this::toModel).toList(), @@ -49,6 +55,8 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac .ifMatch(HAS_ATTACHMENTS) .addLink(linkTo(methodOn(PostfachMailController.class).findAttachments(PostfachNachrichtId.from(postfachMail.getId()))) .withRel(REL_ATTACHMENTS)) + .ifMatch(SENT_BY_CLIENT_USER) + .addLink(linkTo(UserProfileController.class).slash(postfachMail.getCreatedBy()).withRel(REL_CREATED_BY)) .buildModel(); } diff --git a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailModelAssemblerTest.java b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailModelAssemblerTest.java index b3b6db9456..8faa7b4cf6 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailModelAssemblerTest.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailModelAssemblerTest.java @@ -1,8 +1,7 @@ package de.itvsh.goofy.postfach; -import static org.assertj.core.api.Assertions.*; - import java.util.Optional; +import java.util.UUID; import java.util.stream.Stream; import org.junit.jupiter.api.DisplayName; @@ -14,6 +13,9 @@ import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; +import static org.assertj.core.api.Assertions.*; + +import de.itvsh.goofy.common.user.UserId; import de.itvsh.goofy.vorgang.VorgangHeaderTestFactory; class PostfachMailModelAssemblerTest { @@ -78,6 +80,30 @@ class PostfachMailModelAssemblerTest { } } + @DisplayName("created by link") + @Nested + class TestCreatedByLink { + + @Test + void shouldBePresent() { + var link = modelAssembler.toModel(PostfachMailTestFactory.create()).getLink(PostfachMailModelAssembler.REL_CREATED_BY); + + assertThat(link).isPresent().get().extracting(Link::getHref) + .isEqualTo("/api/userProfiles/" + PostfachMailTestFactory.CREATED_BY); + } + + @DisplayName("should not be present if the value of createdBy starts with 'system'") + @Test + void shouldNOTBePresentOnSystemUser() { + var link = modelAssembler + .toModel(PostfachMailTestFactory.createBuilder() + .createdBy(UserId.from(PostfachMailModelAssembler.SYSTEM_USER_IDENTIFIER + UUID.randomUUID().toString())).build()) + .getLink(PostfachMailModelAssembler.REL_CREATED_BY); + + assertThat(link).isNotPresent(); + } + } + } @Nested -- GitLab