diff --git a/src/main/java/de/ozgcloud/admin/setting/SettingService.java b/src/main/java/de/ozgcloud/admin/setting/SettingService.java index e428258da0d119519f44a8492f1d23a666b1bef2..51b8529add8a7e42cb367f1fc6b4a062f8eaa4cb 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 213871e7870000e36d2749d7fc3925978d2638ea..2f27b9c79fb0cee564837ba91320a25468254b6a 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