Skip to content
Snippets Groups Projects
Commit 2c68e95a authored by Krzysztof Witukiewicz's avatar Krzysztof Witukiewicz
Browse files

Merge branch 'OZG-7773-set-relation-if-incoming-mails' into 'main'

OZG-7773 setHasNewPostfachNachricht only if there are incoming mails

See merge request !29
parents 4baeb3ed 695dd6cc
No related branches found
No related tags found
1 merge request!29OZG-7773 setHasNewPostfachNachricht only if there are incoming mails
......@@ -25,6 +25,7 @@ package de.ozgcloud.alfa.postfach;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
......@@ -68,6 +69,7 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
private static final String FILE_PATH = "file";
private static final Predicate<PostfachMail> IS_OUTGOING = postfachNachricht -> postfachNachricht.getDirection() == Direction.OUT;
private static final Predicate<PostfachMail> IS_INCOMING = postfachNachricht -> postfachNachricht.getDirection() == Direction.IN;
private static final Predicate<PostfachMail> SENT_FAILED = postfachMail -> Objects.nonNull(postfachMail.getSentAt())
&& BooleanUtils.isFalse(postfachMail.getSentSuccessful());
......@@ -84,11 +86,11 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
public RepresentationModel<EntityModel<PostfachSettings>> toCollectionModel(Stream<PostfachMail> postfachMails, VorgangWithEingang vorgang,
PostfachSettings postfachSettings) {
var model = buildHalRepresentationModel(postfachMails, vorgang, postfachSettings);
var postfachMailsList = postfachMails.toList();
var model = buildHalRepresentationModel(postfachMailsList.stream(), vorgang, postfachSettings);
if (hasServiceKonto(vorgang)) {
addPostfachNachrichtLinks(model, vorgang);
addPostfachNachrichtLinks(model, buildVorgangMailInfo(postfachMailsList, vorgang));
}
return model;
}
......@@ -106,10 +108,18 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
return Optional.ofNullable(vorgang.getHeader()).map(VorgangHead::getServiceKonto).isPresent();
}
void addPostfachNachrichtLinks(RepresentationModel<EntityModel<PostfachSettings>> model, VorgangWithEingang vorgang) {
var vorgangId = vorgang.getId();
VorgangMailInfo buildVorgangMailInfo(List<PostfachMail> postfachMails, VorgangWithEingang vorgang) {
return new VorgangMailInfo(vorgang, hasIncomingMails(postfachMails));
}
boolean hasIncomingMails(List<PostfachMail> postfachMails) {
return postfachMails.stream().anyMatch(IS_INCOMING);
}
void addPostfachNachrichtLinks(RepresentationModel<EntityModel<PostfachSettings>> model, VorgangMailInfo vorgangMailInfo) {
var vorgangId = vorgangMailInfo.vorgang.getId();
if (vorgangController.isEditable(vorgang)) {
if (vorgangController.isEditable(vorgangMailInfo.vorgang)) {
model.add(linkTo(methodOn(PostfachMailCommandByVorgangController.class).sendPostfachMail(vorgangId, null))
.withRel(REL_SEND_POSTFACH_MAIL));
}
......@@ -117,8 +127,11 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
model.add(linkTo(BinaryFileController.class).slash(vorgangId).slash(POSTFACH_NACHRICHT_ATTACHMENT_FIELD).slash(FILE_PATH)
.withRel(REL_UPLOAD_ATTACHMENT));
model.add(linkTo(VorgangController.class).slash(vorgangId).slash(HAS_NEW_POSTFACH_NACHRICHT_FIELD)
.withRel(vorgang.isHasNewPostfachNachricht() ? REL_RESET_NEW_POSTFACH_MAIL : REL_SET_HAS_NEW_POSTFACH_MAIL));
if (vorgangMailInfo.vorgang.isHasNewPostfachNachricht()) {
model.add(linkTo(VorgangController.class).slash(vorgangId).slash(HAS_NEW_POSTFACH_NACHRICHT_FIELD).withRel(REL_RESET_NEW_POSTFACH_MAIL));
} else if (vorgangMailInfo.hasIncomingMails()) {
model.add(linkTo(VorgangController.class).slash(vorgangId).slash(HAS_NEW_POSTFACH_NACHRICHT_FIELD).withRel(REL_SET_HAS_NEW_POSTFACH_MAIL));
}
}
@Override
......@@ -141,4 +154,5 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
() -> Link.of(String.format(userManagerUrlProvider.getUserProfileTemplate(), postfachMail.getCreatedBy()), REL_CREATED_BY));
}
record VorgangMailInfo(VorgangWithEingang vorgang, boolean hasIncomingMails) {}
}
\ No newline at end of file
......@@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
......@@ -34,6 +35,10 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
......@@ -53,6 +58,7 @@ import de.ozgcloud.alfa.common.user.UserId;
import de.ozgcloud.alfa.common.user.UserManagerUrlProvider;
import de.ozgcloud.alfa.postfach.PostfachMail.Direction;
import de.ozgcloud.alfa.postfach.PostfachMailController.PostfachMailCommandByVorgangController;
import de.ozgcloud.alfa.postfach.PostfachMailModelAssembler.VorgangMailInfo;
import de.ozgcloud.alfa.vorgang.ServiceKontoTestFactory;
import de.ozgcloud.alfa.vorgang.VorgangController;
import de.ozgcloud.alfa.vorgang.VorgangHeadTestFactory;
......@@ -291,40 +297,59 @@ class PostfachMailModelAssemblerTest {
@Nested
class TestToCollectionModel {
private final Stream<PostfachMail> mails = Stream.of(PostfachMailTestFactory.create());
private final List<PostfachMail> mails = List.of(PostfachMailTestFactory.create());
private final VorgangWithEingang vorgang = VorgangWithEingangTestFactory.create();
private final PostfachSettings postfachSettings = PostfachSettingsTestFactory.create();
@Mock
private RepresentationModel<EntityModel<PostfachSettings>> model;
@Captor
private ArgumentCaptor<Stream<PostfachMail>> postfachMailsCaptor;
@BeforeEach
void setUpMocks() {
doReturn(model).when(modelAssembler).buildHalRepresentationModel(mails, vorgang, postfachSettings);
doReturn(model).when(modelAssembler).buildHalRepresentationModel(any(), any(), any());
}
@Test
void shouldBuildHalRepresentationModel() {
doReturn(false).when(modelAssembler).hasServiceKonto(vorgang);
callModelAssembler();
verify(modelAssembler).buildHalRepresentationModel(mails, vorgang, postfachSettings);
verify(modelAssembler).buildHalRepresentationModel(postfachMailsCaptor.capture(), same(vorgang), same(postfachSettings));
assertThat(postfachMailsCaptor.getValue()).containsExactlyElementsOf(mails);
}
@Nested
class OnHasServiceKonto {
private final VorgangMailInfo vorgangMailInfo = new VorgangMailInfo(vorgang, true);
@BeforeEach
void init() {
doReturn(vorgangMailInfo).when(modelAssembler).buildVorgangMailInfo(any(), any());
}
@Test
void shouldBuildVorgangMailInfo() {
callModelAssembler();
verify(modelAssembler).buildVorgangMailInfo(mails, vorgang);
}
@Test
void shouldAddPostfachNachrichtLinks() {
doReturn(true).when(modelAssembler).hasServiceKonto(vorgang);
callModelAssembler();
verify(modelAssembler).addPostfachNachrichtLinks(model, vorgang);
verify(modelAssembler).addPostfachNachrichtLinks(model, vorgangMailInfo);
}
}
@Nested
class OnHasNotServiceKonto {
class OnHasNoServiceKonto {
@Test
void shouldAddPostfachNachrichtLinks() {
......@@ -338,13 +363,15 @@ class PostfachMailModelAssemblerTest {
@Test
void shouldReturnModel() {
doReturn(false).when(modelAssembler).hasServiceKonto(vorgang);
var returnedModel = callModelAssembler();
assertThat(returnedModel).isEqualTo(model);
}
private RepresentationModel<EntityModel<PostfachSettings>> callModelAssembler() {
return modelAssembler.toCollectionModel(mails, vorgang, postfachSettings);
return modelAssembler.toCollectionModel(mails.stream(), vorgang, postfachSettings);
}
}
......@@ -410,6 +437,37 @@ class PostfachMailModelAssemblerTest {
}
@Nested
class TestBuildVorgangMailInfo {
private final List<PostfachMail> mails = List.of(PostfachMailTestFactory.create());
private final VorgangWithEingang vorgang = VorgangWithEingangTestFactory.create();
@Test
void shouldCallHasIncomingMails() {
modelAssembler.buildVorgangMailInfo(mails, vorgang);
verify(modelAssembler).hasIncomingMails(mails);
}
@ParameterizedTest
@ValueSource(booleans = { true, false })
void shouldHaveHasIncomingMails(boolean hasIncomingMails) {
doReturn(hasIncomingMails).when(modelAssembler).hasIncomingMails(any());
var vorgangMailInfo = modelAssembler.buildVorgangMailInfo(mails, vorgang);
assertThat(vorgangMailInfo.hasIncomingMails()).isEqualTo(hasIncomingMails);
}
@Test
void shouldHaveVorgang() {
var vorgangMailInfo = modelAssembler.buildVorgangMailInfo(mails, vorgang);
assertThat(vorgangMailInfo.vorgang()).isEqualTo(vorgang);
}
}
@Nested
class TestHasServiceKonto {
......@@ -438,6 +496,33 @@ class PostfachMailModelAssemblerTest {
}
}
@Nested
class TestHasIncomingMails {
@Test
void shouldReturnFalseIfListIsEmpty() {
var hasIncomingMails = modelAssembler.hasIncomingMails(List.of());
assertThat(hasIncomingMails).isFalse();
}
@Test
void shouldReturnFalseIfNoIncomingMails() {
var hasIncomingMails = modelAssembler.hasIncomingMails(List.of(PostfachMailTestFactory.createBuilder().direction(Direction.OUT).build()));
assertThat(hasIncomingMails).isFalse();
}
@Test
void shouldReturnTrueIfIncomingMailsArePresent() {
var hasIncomingMails = modelAssembler.hasIncomingMails(List.of(
PostfachMailTestFactory.createBuilder().direction(Direction.OUT).build(),
PostfachMailTestFactory.createBuilder().direction(Direction.IN).build()));
assertThat(hasIncomingMails).isTrue();
}
}
@Nested
class TestAddPostfachNachrichtLinks {
......@@ -473,9 +558,29 @@ class PostfachMailModelAssemblerTest {
@Nested
class SetHasNewPostfachNachrichtLink {
@Nested
class OnHasNewPostfachNachrichtSet {
@Test
void shouldBePresent() {
callModelAssembler(VorgangWithEingangTestFactory.createBuilder().hasNewPostfachNachricht(false).build());
void shouldNotBePresent() {
callModelAssembler();
var link = model.getLink(PostfachMailModelAssembler.REL_SET_HAS_NEW_POSTFACH_MAIL);
assertThat(link).isNotPresent();
}
private void callModelAssembler() {
modelAssembler.addPostfachNachrichtLinks(model,
new VorgangMailInfo(VorgangWithEingangTestFactory.createBuilder().hasNewPostfachNachricht(true).build(), true));
}
}
@Nested
class OnHasNewPostfachNachrichtUnset {
@Test
void shouldBePresentWhenHasIncomingMails() {
callModelAssembler(true);
var link = model.getLink(PostfachMailModelAssembler.REL_SET_HAS_NEW_POSTFACH_MAIL);
assertThat(link).isPresent().get().extracting(Link::getHref)
......@@ -485,12 +590,18 @@ class PostfachMailModelAssemblerTest {
}
@Test
void shouldNotBePresent() {
callModelAssembler(VorgangWithEingangTestFactory.create());
void shouldNotBePresentWhenHasNoIncomingMails() {
callModelAssembler(false);
var link = model.getLink(PostfachMailModelAssembler.REL_SET_HAS_NEW_POSTFACH_MAIL);
assertThat(link).isNotPresent();
}
private void callModelAssembler(boolean hasIncomingMails) {
modelAssembler.addPostfachNachrichtLinks(model,
new VorgangMailInfo(VorgangWithEingangTestFactory.createBuilder().hasNewPostfachNachricht(false).build(), hasIncomingMails));
}
}
}
@DisplayName("send postfach mail")
......@@ -545,7 +656,7 @@ class PostfachMailModelAssemblerTest {
}
private void callModelAssembler(VorgangWithEingang vorgang) {
modelAssembler.addPostfachNachrichtLinks(model, vorgang);
modelAssembler.addPostfachNachrichtLinks(model, new VorgangMailInfo(vorgang, false));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment