From 910f87f598b75eb1277ee18b5b5b53100a7bafc4 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Wed, 4 Dec 2024 11:36:16 +0100
Subject: [PATCH] OZG-7239 OZG-7286 Handle case when postfach setting has no
 body

---
 .../admin/setting/SettingService.java         |  6 +++--
 .../admin/setting/SettingServiceTest.java     | 24 ++++++++++++++-----
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/main/java/de/ozgcloud/admin/setting/SettingService.java b/src/main/java/de/ozgcloud/admin/setting/SettingService.java
index e428258d..51b8529a 100644
--- a/src/main/java/de/ozgcloud/admin/setting/SettingService.java
+++ b/src/main/java/de/ozgcloud/admin/setting/SettingService.java
@@ -64,8 +64,10 @@ class SettingService {
 	}
 
 	PostfachSettingBody getSettingWithPostfachFromDb() {
-		var postfach = repository.findOneByName(POSTFACH_SETTING_ITEM_NAME);
-		return postfach.isPresent() ? (PostfachSettingBody) postfach.get().getSettingBody() : PostfachSettingBody.builder().build();
+		return repository.findOneByName(POSTFACH_SETTING_ITEM_NAME)
+				.map(Setting::getSettingBody)
+				.map(PostfachSettingBody.class::cast)
+				.orElse(PostfachSettingBody.builder().build());
 	}
 
 	Map<String, OrganisationsEinheitSettings> getOrganisationsEinheitSettings() {
diff --git a/src/test/java/de/ozgcloud/admin/setting/SettingServiceTest.java b/src/test/java/de/ozgcloud/admin/setting/SettingServiceTest.java
index 213871e7..2f27b9c7 100644
--- a/src/test/java/de/ozgcloud/admin/setting/SettingServiceTest.java
+++ b/src/test/java/de/ozgcloud/admin/setting/SettingServiceTest.java
@@ -62,9 +62,6 @@ class SettingServiceTest {
 
 	private final PostfachSettingBody postfach = PostfachSettingBodyTestFactory.create();
 
-	private static final String POSTFACH = "Postfach";
-	private final Setting settingWithPostfach = SettingTestFactory.createBuilder().name(POSTFACH).settingBody(postfach).build();
-
 	@Nested
 	class TestGetAlfaSettingDTO {
 
@@ -147,9 +144,15 @@ class SettingServiceTest {
 
 	@Nested
 	class TestGetSettingWithPostfachFromDb {
+
+		private static final String POSTFACH = "Postfach";
+
+		private final Setting emptyPostfachSetting = SettingTestFactory.createBuilder().name(POSTFACH).build();
+		private final Setting postfachSettingWithBody = SettingTestFactory.createBuilder().name(POSTFACH).settingBody(postfach).build();
+
 		@Test
 		void shouldCallRepository() {
-			when(repository.findOneByName(POSTFACH)).thenReturn(Optional.of(settingWithPostfach));
+			when(repository.findOneByName(POSTFACH)).thenReturn(Optional.of(postfachSettingWithBody));
 
 			service.getSettingWithPostfachFromDb();
 
@@ -158,7 +161,7 @@ class SettingServiceTest {
 
 		@Test
 		void shouldReturnPostfachSettingBody() {
-			when(repository.findOneByName(POSTFACH)).thenReturn(Optional.of(settingWithPostfach));
+			when(repository.findOneByName(POSTFACH)).thenReturn(Optional.of(postfachSettingWithBody));
 
 			var returnedBody = service.getSettingWithPostfachFromDb();
 
@@ -166,13 +169,22 @@ class SettingServiceTest {
 		}
 
 		@Test
-		void shouldReturnEmptyPostfachSettingBodyForEmptySetting() {
+		void shouldReturnEmptyPostfachSettingBodyWhenSettingNotFound() {
 			when(repository.findOneByName(POSTFACH)).thenReturn(Optional.empty());
 
 			var returnedBody = service.getSettingWithPostfachFromDb();
 
 			assertThat(returnedBody).usingRecursiveComparison().isEqualTo(PostfachSettingBody.builder().build());
 		}
+
+		@Test
+		void shouldReturnEmptyPostfachSettingBodyForSettingWithoutBody() {
+			when(repository.findOneByName(POSTFACH)).thenReturn(Optional.of(emptyPostfachSetting));
+
+			var returnedBody = service.getSettingWithPostfachFromDb();
+
+			assertThat(returnedBody).usingRecursiveComparison().isEqualTo(PostfachSettingBody.builder().build());
+		}
 	}
 
 	@Nested
-- 
GitLab