diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java index 267fe16019146394e7c3b9d23da4f01777ff84c7..6157c7fddd0b86e663b7b9dd1f54b6f885d9fc0c 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.EnumSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Predicate; @@ -193,7 +194,7 @@ class PostfachService { boolean shouldNotifyAntragsraum(PostfachNachricht postfachNachricht) { return REPLY_POSSIBLE_OPTION.contains(postfachNachricht.getReplyOption()) && findPostfachRemoteService(postfachNachricht.getPostfachAddress().getServiceKontoType()).filter(this::isPostfachWithAntragsraum) - .isPresent(); + .isPresent(); } Optional<PostfachNachricht> adjustPostfachNachricht(PostfachNachricht nachricht) { @@ -273,10 +274,16 @@ class PostfachService { } Optional<PostfachRemoteService> findPostfachRemoteService(String postfachType) { - return getPostfachRemoteServices().filter(remoteService -> hasPostfachType(remoteService, postfachType)).findFirst(); + return getPostfachRemoteServices() + .filter(remoteService -> hasPostfachType(remoteService, postfachType)) + .findFirst(); } - boolean hasPostfachType(PostfachRemoteService postfachRemoteService, String postfachType) { + private boolean hasPostfachType(PostfachRemoteService postfachRemoteService, String postfachType) { + if (Objects.isNull(postfachType)) { + LOG.warn("PostfachType is null - use existing PostfachService with type %s.".formatted(postfachRemoteService.getPostfachType())); + return true; + } return StringUtils.equals(postfachRemoteService.getPostfachType(), postfachType); } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java index c61395ad3726d0e10d49db24cecce0477938e29a..db5aec4fc793c5ee5399a695d19dbebeac8f5aab 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java @@ -528,11 +528,11 @@ class PostfachServiceTest { @Test void shouldPersistPostfachNachricht() { - var PostfachNachricht = PostfachNachrichtTestFactory.create(); + var postfachNachricht = PostfachNachrichtTestFactory.create(); - service.persistPostfachNachricht(Optional.of(USER_ID), PostfachNachricht); + service.persistPostfachNachricht(Optional.of(USER_ID), postfachNachricht); - verify(persistingService).persistNachricht(eq(Optional.of(USER_ID)), same(PostfachNachricht)); + verify(persistingService).persistNachricht(eq(Optional.of(USER_ID)), same(postfachNachricht)); } } @@ -625,8 +625,8 @@ class PostfachServiceTest { @Nested class TestHandleSendPostfachNachricht { - final String COMMAND_ID = UUID.randomUUID().toString(); - final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.create(); + private static final String COMMAND_ID = UUID.randomUUID().toString(); + private final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.create(); @Test void shouldDoSendPostfachNachricht() { @@ -702,7 +702,7 @@ class PostfachServiceTest { @Nested class TestOnOsiPostfachException { - private final static String MESSAGE = "Osi Postfach throws an exception."; + private static final String MESSAGE = "Osi Postfach throws an exception."; private static final PostfachException POSTFACH_EXCEPTION = new PostfachException(MESSAGE, PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE); @@ -741,7 +741,7 @@ class PostfachServiceTest { @Nested class TestOsiPostfachServerProcessException { - private final static String MESSAGE = "Postfach server returned false"; + private static final String MESSAGE = "Postfach server returned false"; @BeforeEach void mockService() { @@ -843,7 +843,49 @@ class PostfachServiceTest { assertThrows(NotConfiguredException.class, () -> service.doSendPostfachNachricht(postfachNachricht)); } + } + + @DisplayName("Find postfach remote service") + @Nested + class TestFindPostfachRemoteService { + + @Mock + private PostfachRemoteService remoteService; + @Mock + private PostfachRemoteService anotherRemoteService; + + private static final String MATCHING_POSTFACH_TYPE = "MatchingPostfachType"; + + @Test + void shouldReturnRemoteServiceOnMatchingPostfachType() { + when(anotherRemoteService.getPostfachType()).thenReturn("NOT_MATCHING"); + when(remoteService.getPostfachType()).thenReturn(MATCHING_POSTFACH_TYPE); + doReturn(Stream.of(anotherRemoteService, remoteService)).when(service).getPostfachRemoteServices(); + + var foundRemoteService = service.findPostfachRemoteService(MATCHING_POSTFACH_TYPE); + assertThat(foundRemoteService).hasValue(remoteService); + } + + @Test + void shouldReturnEmptyOnNotMatchingPostfachType() { + when(remoteService.getPostfachType()).thenReturn(MATCHING_POSTFACH_TYPE); + doReturn(Stream.of(remoteService, anotherRemoteService)).when(service).getPostfachRemoteServices(); + + var foundRemoteService = service.findPostfachRemoteService("NotMatchingPostfachType"); + + assertThat(foundRemoteService).isEmpty(); + } + + @Test + void shouldReturnRemoteServiceOnPostfachTypeNull() { + when(remoteService.getPostfachType()).thenReturn("BAYERN_ID"); + doReturn(Stream.of(remoteService)).when(service).getPostfachRemoteServices(); + + var foundRemoteService = service.findPostfachRemoteService(null); + + assertThat(foundRemoteService).hasValue(remoteService); + } } @Nested @@ -887,46 +929,46 @@ class PostfachServiceTest { } } - @DisplayName("on bayern id postfach") - @Nested - class TestOnBayernIdPostfach { + @DisplayName("on bayern id postfach") + @Nested + class TestOnBayernIdPostfach { - private static final String BAYERN_ID_POSTFACH = "BAYERN_ID"; - private static final String DEPRECATED_BAYERN_ID_POSTFACH = "BayernId"; + private static final String BAYERN_ID_POSTFACH = "BAYERN_ID"; + private static final String DEPRECATED_BAYERN_ID_POSTFACH = "BayernId"; - @BeforeEach - void mock() { - when(postfachRemoteService.getPostfachType()).thenReturn(BAYERN_ID_POSTFACH); - } + @BeforeEach + void mock() { + when(postfachRemoteService.getPostfachType()).thenReturn(BAYERN_ID_POSTFACH); + } - @Test - void shouldReturnTwoPostfach() { - var postfaecher = getPostfaecher(); + @Test + void shouldReturnTwoPostfach() { + var postfaecher = getPostfaecher(); - assertThat(postfaecher).hasSize(2); - } + assertThat(postfaecher).hasSize(2); + } - @Test - void shouldContainsBayernIdCamelCase() { - var postfaecher = getPostfaecher(); + @Test + void shouldContainsBayernIdCamelCase() { + var postfaecher = getPostfaecher(); - assertThat(postfaecher).extracting(Postfach::getType).contains(DEPRECATED_BAYERN_ID_POSTFACH); - } + assertThat(postfaecher).extracting(Postfach::getType).contains(DEPRECATED_BAYERN_ID_POSTFACH); + } - @Test - void shouldContainsBayernIdUnderscore() { - var postfaecher = getPostfaecher(); + @Test + void shouldContainsBayernIdUnderscore() { + var postfaecher = getPostfaecher(); - assertThat(postfaecher).extracting(Postfach::getType).contains(BAYERN_ID_POSTFACH); - } + assertThat(postfaecher).extracting(Postfach::getType).contains(BAYERN_ID_POSTFACH); + } - private Stream<Postfach> getPostfaecher() { - return service.getPostfachs(); - } + private Stream<Postfach> getPostfaecher() { + return service.getPostfachs(); } + } - @Nested - class TestBuildPostfach { + @Nested + class TestBuildPostfach { @Test void shouldSetPostfachType() {