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/SettingEnvironmentITCase.java b/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentITCase.java index 2101df7ba2814325726f5624fa83786a5238b65b..c50d09934b9cfd835215a4589a0c70000fdc9419 100644 --- a/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentITCase.java +++ b/src/test/java/de/ozgcloud/admin/setting/SettingEnvironmentITCase.java @@ -28,6 +28,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -35,6 +36,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; import de.ozgcloud.admin.organisationseinheit.OrganisationsEinheit; import de.ozgcloud.admin.organisationseinheit.OrganisationsEinheitTestFactory; @@ -52,11 +54,6 @@ class SettingEnvironmentITCase { @Autowired private MongoOperations mongoOperations; - private Setting settingWithPostfach = SettingTestFactory.createBuilder() - .name("Postfach") - .settingBody(PostfachSettingBodyTestFactory.create()) - .build(); - @Test @SneakyThrows void shouldHaveHttpEndpoint() { @@ -65,35 +62,111 @@ class SettingEnvironmentITCase { result.andExpect(status().isOk()); } + @DisplayName("Get alfa settings") @Nested - class TestFindOne { - - @BeforeEach - void fillDb() { - mongoOperations.dropCollection(Setting.class); - mongoOperations.dropCollection(OrganisationsEinheit.class); - mongoOperations.save(settingWithPostfach); - mongoOperations.save(OrganisationsEinheitTestFactory.create()); + class TestGetAlfaSettings { + + @Nested + class OnInitialStartup { + + @BeforeEach + void fillDb() { + fillDbWithInitialSetting(); + } + + @Test + @SneakyThrows + void shouldHandleEmptySetting() { + var result = getAlfaSettings(); + + assertThat(formatLineBreaks(result.andReturn().getResponse().getContentAsString())).isEqualTo(YamlTestFactory.createInitialYaml()); + } } - @Test - @SneakyThrows - void shouldReturnValuesForVorgangManager() { - var result = mockMvc.perform(get("/configserver/OzgCloud_VorgangManager-profile.yaml")); + @Nested + class OnSettingsAvailable { + + @BeforeEach + void fillDb() { + fillDbWithPostfachSetting(); + } + + @Test + @SneakyThrows + void shouldReturnValuesForAlfa() { + var result = getAlfaSettings(); - assertThat(formatLineBreaks(result.andReturn().getResponse().getContentAsString())).isEqualTo(YamlTestFactory.createVorgangManagerYaml()); + assertThat(formatLineBreaks(result.andReturn().getResponse().getContentAsString())).isEqualTo(YamlTestFactory.createAlfaYaml()); + } } - @Test - @SneakyThrows - void shouldReturnValuesForAlfa() { - var result = mockMvc.perform(get("/configserver/Alfa-any.yaml")); + private ResultActions getAlfaSettings() throws Exception { + return mockMvc.perform(get("/configserver/Alfa-any.yaml")).andExpect(status().isOk()); + } + } + + @Nested + class TestGetVorgangManagerSettings { + + @Nested + class OnInitialStartup { + + @BeforeEach + void fillDb() { + fillDbWithInitialSetting(); + } - assertThat(formatLineBreaks(result.andReturn().getResponse().getContentAsString())).isEqualTo(YamlTestFactory.createAlfaYaml()); + @Test + @SneakyThrows + void shouldHandleEmptySetting() { + var result = getVorgangManagerSettings(); + + assertThat(formatLineBreaks(result.andReturn().getResponse().getContentAsString())).isEqualTo(YamlTestFactory.createInitialYaml()); + } + } + + @Nested + class OnSettingsAvailable { + + @BeforeEach + void fillDb() { + fillDbWithPostfachSetting(); + } + + @Test + @SneakyThrows + void shouldReturnValuesForVorgangManager() { + var result = getVorgangManagerSettings(); + + assertThat(formatLineBreaks(result.andReturn().getResponse().getContentAsString())) + .isEqualTo(YamlTestFactory.createVorgangManagerYaml()); + } } - private String formatLineBreaks(String text) { - return text.replaceAll("\\\\\\s+\\\\", "").replace("\\n", "\n"); + private ResultActions getVorgangManagerSettings() throws Exception { + return mockMvc.perform(get("/configserver/OzgCloud_VorgangManager-profile.yaml")).andExpect(status().isOk()); } } + + private String formatLineBreaks(String text) { + return text.replaceAll("\\\\\\s+\\\\", "").replace("\\n", "\n"); + } + + private void fillDbWithInitialSetting() { + var initialSetting = Setting.builder().name("Postfach").build(); + mongoOperations.dropCollection(Setting.class); + mongoOperations.dropCollection(OrganisationsEinheit.class); + mongoOperations.save(initialSetting); + } + + private void fillDbWithPostfachSetting() { + var postfachSetting = SettingTestFactory.createBuilder() + .name("Postfach") + .settingBody(PostfachSettingBodyTestFactory.create()) + .build(); + mongoOperations.dropCollection(Setting.class); + mongoOperations.dropCollection(OrganisationsEinheit.class); + mongoOperations.save(postfachSetting); + mongoOperations.save(OrganisationsEinheitTestFactory.create()); + } } \ No newline at end of file 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 diff --git a/src/test/java/de/ozgcloud/admin/setting/YamlTestFactory.java b/src/test/java/de/ozgcloud/admin/setting/YamlTestFactory.java index 3bad02a8b2b2e80771008857779a6adc1631d448..e6b1c0195b264005203eda0694d11bd77b5b1cf7 100644 --- a/src/test/java/de/ozgcloud/admin/setting/YamlTestFactory.java +++ b/src/test/java/de/ozgcloud/admin/setting/YamlTestFactory.java @@ -51,4 +51,8 @@ public class YamlTestFactory { OrganisationsEinheitTestFactory.ORGANISATIONS_EINHEIT_ID, OrganisationsEinheitSettingsTestFactory.SIGNATUR); } + + public static String createInitialYaml() { + return TestUtils.loadTextFile("yamlTemplates/settings/initial.yaml.tmpl"); + } } diff --git a/src/test/resources/yamlTemplates/settings/initial.yaml.tmpl b/src/test/resources/yamlTemplates/settings/initial.yaml.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..1a6ddf888e1a28b35e2782c93e2b21116d844e86 --- /dev/null +++ b/src/test/resources/yamlTemplates/settings/initial.yaml.tmpl @@ -0,0 +1,4 @@ +ozgcloud: + postfach: + signatur: null + organisationsEinheitSettings: {}