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 6157c7fddd0b86e663b7b9dd1f54b6f885d9fc0c..b92feadbfde4588baa60081f5b41244946cb34e4 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 @@ -42,6 +42,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; +import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.nachrichten.antragraum.AntragraumService; import de.ozgcloud.nachrichten.attributes.ClientAttributeService; import de.ozgcloud.nachrichten.info.InfoManagerService; @@ -274,14 +275,18 @@ class PostfachService { } Optional<PostfachRemoteService> findPostfachRemoteService(String postfachType) { - return getPostfachRemoteServices() + var remoteServices = getPostfachRemoteServices().toList(); + if (Objects.isNull(postfachType) && remoteServices.size() != 1) { + throw new TechnicalException("PostfachType is null and no distinct PostfachService is configured."); + } + return remoteServices.stream() .filter(remoteService -> hasPostfachType(remoteService, postfachType)) .findFirst(); } 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())); + LOG.error("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 db5aec4fc793c5ee5399a695d19dbebeac8f5aab..8dcd52fa6261aecd98a5bd22fc2cc5d4d6f191dd 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 @@ -52,6 +52,7 @@ import org.springframework.boot.logging.LogLevel; import org.springframework.context.ApplicationEventPublisher; import org.springframework.test.util.ReflectionTestUtils; +import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.nachrichten.antragraum.AntragraumService; import de.ozgcloud.nachrichten.attributes.ClientAttributeService; import de.ozgcloud.nachrichten.info.InfoManagerService; @@ -877,14 +878,27 @@ class PostfachServiceTest { assertThat(foundRemoteService).isEmpty(); } - @Test - void shouldReturnRemoteServiceOnPostfachTypeNull() { - when(remoteService.getPostfachType()).thenReturn("BAYERN_ID"); - doReturn(Stream.of(remoteService)).when(service).getPostfachRemoteServices(); + @DisplayName("on postfachType null") + @Nested + class TestOnPostfachTypeNull { - var foundRemoteService = service.findPostfachRemoteService(null); + @Test + void shouldReturnRemoteService() { + when(remoteService.getPostfachType()).thenReturn("BAYERN_ID"); + doReturn(Stream.of(remoteService)).when(service).getPostfachRemoteServices(); - assertThat(foundRemoteService).hasValue(remoteService); + var foundRemoteService = service.findPostfachRemoteService(null); + + assertThat(foundRemoteService).hasValue(remoteService); + } + + @Test + void shouldThrowExceptionMultipleConfiguredRemoteServices() { + doReturn(Stream.of(anotherRemoteService, remoteService)).when(service).getPostfachRemoteServices(); + + assertThatThrownBy(() -> service.findPostfachRemoteService(null)) + .isInstanceOf(TechnicalException.class); + } } }