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

OZG-7773 OZG-8079 setHasNewPostfachNachricht only if there are incoming mails

parent 4baeb3ed
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; ...@@ -25,6 +25,7 @@ package de.ozgcloud.alfa.postfach;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate; import java.util.function.Predicate;
...@@ -68,6 +69,7 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac ...@@ -68,6 +69,7 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
private static final String FILE_PATH = "file"; 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_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()) private static final Predicate<PostfachMail> SENT_FAILED = postfachMail -> Objects.nonNull(postfachMail.getSentAt())
&& BooleanUtils.isFalse(postfachMail.getSentSuccessful()); && BooleanUtils.isFalse(postfachMail.getSentSuccessful());
...@@ -84,11 +86,11 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac ...@@ -84,11 +86,11 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
public RepresentationModel<EntityModel<PostfachSettings>> toCollectionModel(Stream<PostfachMail> postfachMails, VorgangWithEingang vorgang, public RepresentationModel<EntityModel<PostfachSettings>> toCollectionModel(Stream<PostfachMail> postfachMails, VorgangWithEingang vorgang,
PostfachSettings postfachSettings) { PostfachSettings postfachSettings) {
var postfachMailsList = postfachMails.toList();
var model = buildHalRepresentationModel(postfachMails, vorgang, postfachSettings); var model = buildHalRepresentationModel(postfachMailsList.stream(), vorgang, postfachSettings);
if (hasServiceKonto(vorgang)) { if (hasServiceKonto(vorgang)) {
addPostfachNachrichtLinks(model, vorgang); addPostfachNachrichtLinks(model, getVorgangInfo(postfachMailsList, vorgang));
} }
return model; return model;
} }
...@@ -106,10 +108,20 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac ...@@ -106,10 +108,20 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
return Optional.ofNullable(vorgang.getHeader()).map(VorgangHead::getServiceKonto).isPresent(); return Optional.ofNullable(vorgang.getHeader()).map(VorgangHead::getServiceKonto).isPresent();
} }
void addPostfachNachrichtLinks(RepresentationModel<EntityModel<PostfachSettings>> model, VorgangWithEingang vorgang) { record VorgangInfo(VorgangWithEingang vorgang, boolean hasIncomingMails) {}
var vorgangId = vorgang.getId();
VorgangInfo getVorgangInfo(List<PostfachMail> postfachMails, VorgangWithEingang vorgang) {
return new VorgangInfo(vorgang, hasIncomingMails(postfachMails));
}
if (vorgangController.isEditable(vorgang)) { boolean hasIncomingMails(List<PostfachMail> postfachMails) {
return postfachMails.stream().anyMatch(IS_INCOMING);
}
void addPostfachNachrichtLinks(RepresentationModel<EntityModel<PostfachSettings>> model, VorgangInfo vorgangInfo) {
var vorgangId = vorgangInfo.vorgang.getId();
if (vorgangController.isEditable(vorgangInfo.vorgang)) {
model.add(linkTo(methodOn(PostfachMailCommandByVorgangController.class).sendPostfachMail(vorgangId, null)) model.add(linkTo(methodOn(PostfachMailCommandByVorgangController.class).sendPostfachMail(vorgangId, null))
.withRel(REL_SEND_POSTFACH_MAIL)); .withRel(REL_SEND_POSTFACH_MAIL));
} }
...@@ -117,8 +129,11 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac ...@@ -117,8 +129,11 @@ class PostfachMailModelAssembler implements RepresentationModelAssembler<Postfac
model.add(linkTo(BinaryFileController.class).slash(vorgangId).slash(POSTFACH_NACHRICHT_ATTACHMENT_FIELD).slash(FILE_PATH) model.add(linkTo(BinaryFileController.class).slash(vorgangId).slash(POSTFACH_NACHRICHT_ATTACHMENT_FIELD).slash(FILE_PATH)
.withRel(REL_UPLOAD_ATTACHMENT)); .withRel(REL_UPLOAD_ATTACHMENT));
model.add(linkTo(VorgangController.class).slash(vorgangId).slash(HAS_NEW_POSTFACH_NACHRICHT_FIELD) if (vorgangInfo.vorgang.isHasNewPostfachNachricht()) {
.withRel(vorgang.isHasNewPostfachNachricht() ? REL_RESET_NEW_POSTFACH_MAIL : REL_SET_HAS_NEW_POSTFACH_MAIL)); model.add(linkTo(VorgangController.class).slash(vorgangId).slash(HAS_NEW_POSTFACH_NACHRICHT_FIELD).withRel(REL_RESET_NEW_POSTFACH_MAIL));
} else if (vorgangInfo.hasIncomingMails()) {
model.add(linkTo(VorgangController.class).slash(vorgangId).slash(HAS_NEW_POSTFACH_NACHRICHT_FIELD).withRel(REL_SET_HAS_NEW_POSTFACH_MAIL));
}
} }
@Override @Override
......
...@@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.*; ...@@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -34,6 +35,10 @@ import org.junit.jupiter.api.BeforeEach; ...@@ -34,6 +35,10 @@ 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;
import org.junit.jupiter.api.Test; 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.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
...@@ -53,6 +58,7 @@ import de.ozgcloud.alfa.common.user.UserId; ...@@ -53,6 +58,7 @@ import de.ozgcloud.alfa.common.user.UserId;
import de.ozgcloud.alfa.common.user.UserManagerUrlProvider; import de.ozgcloud.alfa.common.user.UserManagerUrlProvider;
import de.ozgcloud.alfa.postfach.PostfachMail.Direction; import de.ozgcloud.alfa.postfach.PostfachMail.Direction;
import de.ozgcloud.alfa.postfach.PostfachMailController.PostfachMailCommandByVorgangController; import de.ozgcloud.alfa.postfach.PostfachMailController.PostfachMailCommandByVorgangController;
import de.ozgcloud.alfa.postfach.PostfachMailModelAssembler.VorgangInfo;
import de.ozgcloud.alfa.vorgang.ServiceKontoTestFactory; import de.ozgcloud.alfa.vorgang.ServiceKontoTestFactory;
import de.ozgcloud.alfa.vorgang.VorgangController; import de.ozgcloud.alfa.vorgang.VorgangController;
import de.ozgcloud.alfa.vorgang.VorgangHeadTestFactory; import de.ozgcloud.alfa.vorgang.VorgangHeadTestFactory;
...@@ -291,40 +297,59 @@ class PostfachMailModelAssemblerTest { ...@@ -291,40 +297,59 @@ class PostfachMailModelAssemblerTest {
@Nested @Nested
class TestToCollectionModel { 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 VorgangWithEingang vorgang = VorgangWithEingangTestFactory.create();
private final PostfachSettings postfachSettings = PostfachSettingsTestFactory.create(); private final PostfachSettings postfachSettings = PostfachSettingsTestFactory.create();
@Mock @Mock
private RepresentationModel<EntityModel<PostfachSettings>> model; private RepresentationModel<EntityModel<PostfachSettings>> model;
@Captor
private ArgumentCaptor<Stream<PostfachMail>> postfachMailsCaptor;
@BeforeEach @BeforeEach
void setUpMocks() { void setUpMocks() {
doReturn(model).when(modelAssembler).buildHalRepresentationModel(mails, vorgang, postfachSettings); doReturn(model).when(modelAssembler).buildHalRepresentationModel(any(), any(), any());
} }
@Test @Test
void shouldBuildHalRepresentationModel() { void shouldBuildHalRepresentationModel() {
doReturn(false).when(modelAssembler).hasServiceKonto(vorgang);
callModelAssembler(); callModelAssembler();
verify(modelAssembler).buildHalRepresentationModel(mails, vorgang, postfachSettings); verify(modelAssembler).buildHalRepresentationModel(postfachMailsCaptor.capture(), same(vorgang), same(postfachSettings));
assertThat(postfachMailsCaptor.getValue()).containsExactlyElementsOf(mails);
} }
@Nested @Nested
class OnHasServiceKonto { class OnHasServiceKonto {
private final VorgangInfo vorgangInfo = new VorgangInfo(vorgang, true);
@BeforeEach
void init() {
doReturn(vorgangInfo).when(modelAssembler).getVorgangInfo(any(), any());
}
@Test
void shouldGetVorgangInfo() {
callModelAssembler();
verify(modelAssembler).getVorgangInfo(mails, vorgang);
}
@Test @Test
void shouldAddPostfachNachrichtLinks() { void shouldAddPostfachNachrichtLinks() {
doReturn(true).when(modelAssembler).hasServiceKonto(vorgang); doReturn(true).when(modelAssembler).hasServiceKonto(vorgang);
callModelAssembler(); callModelAssembler();
verify(modelAssembler).addPostfachNachrichtLinks(model, vorgang); verify(modelAssembler).addPostfachNachrichtLinks(model, vorgangInfo);
} }
} }
@Nested @Nested
class OnHasNotServiceKonto { class OnHasNoServiceKonto {
@Test @Test
void shouldAddPostfachNachrichtLinks() { void shouldAddPostfachNachrichtLinks() {
...@@ -338,13 +363,15 @@ class PostfachMailModelAssemblerTest { ...@@ -338,13 +363,15 @@ class PostfachMailModelAssemblerTest {
@Test @Test
void shouldReturnModel() { void shouldReturnModel() {
doReturn(false).when(modelAssembler).hasServiceKonto(vorgang);
var returnedModel = callModelAssembler(); var returnedModel = callModelAssembler();
assertThat(returnedModel).isEqualTo(model); assertThat(returnedModel).isEqualTo(model);
} }
private RepresentationModel<EntityModel<PostfachSettings>> callModelAssembler() { private RepresentationModel<EntityModel<PostfachSettings>> callModelAssembler() {
return modelAssembler.toCollectionModel(mails, vorgang, postfachSettings); return modelAssembler.toCollectionModel(mails.stream(), vorgang, postfachSettings);
} }
} }
...@@ -410,6 +437,37 @@ class PostfachMailModelAssemblerTest { ...@@ -410,6 +437,37 @@ class PostfachMailModelAssemblerTest {
} }
@Nested
class TestGetVorgangInfo {
private final List<PostfachMail> mails = List.of(PostfachMailTestFactory.create());
private final VorgangWithEingang vorgang = VorgangWithEingangTestFactory.create();
@Test
void shouldCallHasIncomingMails() {
modelAssembler.getVorgangInfo(mails, vorgang);
verify(modelAssembler).hasIncomingMails(mails);
}
@ParameterizedTest
@ValueSource(booleans = { true, false })
void shouldHaveHasIncomingMails(boolean hasIncomingMails) {
doReturn(hasIncomingMails).when(modelAssembler).hasIncomingMails(any());
var vorgangInfo = modelAssembler.getVorgangInfo(mails, vorgang);
assertThat(vorgangInfo.hasIncomingMails()).isEqualTo(hasIncomingMails);
}
@Test
void shouldHaveVorgang() {
var vorgangInfo = modelAssembler.getVorgangInfo(mails, vorgang);
assertThat(vorgangInfo.vorgang()).isEqualTo(vorgang);
}
}
@Nested @Nested
class TestHasServiceKonto { class TestHasServiceKonto {
...@@ -438,6 +496,33 @@ class PostfachMailModelAssemblerTest { ...@@ -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 @Nested
class TestAddPostfachNachrichtLinks { class TestAddPostfachNachrichtLinks {
...@@ -473,23 +558,49 @@ class PostfachMailModelAssemblerTest { ...@@ -473,23 +558,49 @@ class PostfachMailModelAssemblerTest {
@Nested @Nested
class SetHasNewPostfachNachrichtLink { class SetHasNewPostfachNachrichtLink {
@Test @Nested
void shouldBePresent() { class OnHasNewPostfachNachrichtSet {
callModelAssembler(VorgangWithEingangTestFactory.createBuilder().hasNewPostfachNachricht(false).build());
var link = model.getLink(PostfachMailModelAssembler.REL_SET_HAS_NEW_POSTFACH_MAIL); @Test
assertThat(link).isPresent().get().extracting(Link::getHref) void shouldNotBePresent() {
.isEqualTo(UriComponentsBuilder.fromUriString("/api/vorgangs") callModelAssembler();
.pathSegment(VorgangHeaderTestFactory.ID, "hasNewPostfachNachricht")
.build().toString()); var link = model.getLink(PostfachMailModelAssembler.REL_SET_HAS_NEW_POSTFACH_MAIL);
assertThat(link).isNotPresent();
}
private void callModelAssembler() {
modelAssembler.addPostfachNachrichtLinks(model,
new VorgangInfo(VorgangWithEingangTestFactory.createBuilder().hasNewPostfachNachricht(true).build(), true));
}
} }
@Test @Nested
void shouldNotBePresent() { class OnHasNewPostfachNachrichtUnset {
callModelAssembler(VorgangWithEingangTestFactory.create());
var link = model.getLink(PostfachMailModelAssembler.REL_SET_HAS_NEW_POSTFACH_MAIL); @Test
assertThat(link).isNotPresent(); void shouldBePresentWhenHasIncomingMails() {
callModelAssembler(true);
var link = model.getLink(PostfachMailModelAssembler.REL_SET_HAS_NEW_POSTFACH_MAIL);
assertThat(link).isPresent().get().extracting(Link::getHref)
.isEqualTo(UriComponentsBuilder.fromUriString("/api/vorgangs")
.pathSegment(VorgangHeaderTestFactory.ID, "hasNewPostfachNachricht")
.build().toString());
}
@Test
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 VorgangInfo(VorgangWithEingangTestFactory.createBuilder().hasNewPostfachNachricht(false).build(), hasIncomingMails));
}
} }
} }
...@@ -545,7 +656,7 @@ class PostfachMailModelAssemblerTest { ...@@ -545,7 +656,7 @@ class PostfachMailModelAssemblerTest {
} }
private void callModelAssembler(VorgangWithEingang vorgang) { private void callModelAssembler(VorgangWithEingang vorgang) {
modelAssembler.addPostfachNachrichtLinks(model, vorgang); modelAssembler.addPostfachNachrichtLinks(model, new VorgangInfo(vorgang, false));
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment