diff --git a/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java b/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java index f41a76e44d5fc4c10f798ec3769425e08ab79d3a..41a984e1a12132f3267bb4ad6c93bc9ed2023cef 100644 --- a/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java +++ b/semantik-adapter/src/main/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactory.java @@ -23,11 +23,13 @@ */ package de.ozgcloud.eingang.semantik.common; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.stream.Stream; import org.apache.commons.collections.MapUtils; import org.springframework.stereotype.Component; @@ -102,16 +104,28 @@ public class ServiceKontoFactory { } int getPostfachAddressType(Map<String, Object> restResponseName) { - return getMailboxType(restResponseName); + return getMailboxType(restResponseName) + .orElseGet(() -> { + LOG.warn("Mailbox type not found in rest_response_name! Using default value '{}'", POSTFACH_ADDRESS_DEFAULT); + return POSTFACH_ADDRESS_DEFAULT; + }); } - private Integer getMailboxType(Map<String, Object> restResponseName) { - return (Integer) getMemberScope(restResponseName).get(REST_RESPONSE_NAME_MEMBER_SCOPE_MAILBOX_TYPE); + private Optional<Integer> getMailboxType(Map<String, Object> restResponseName) { + return getMemberScope(restResponseName) + .map(scope -> scope.get(REST_RESPONSE_NAME_MEMBER_SCOPE_MAILBOX_TYPE)) + .filter(Integer.class::isInstance) + .map(Integer.class::cast); } @SuppressWarnings("unchecked") - private Map<String, Object> getMemberScope(Map<String, Object> restResponseName) { - return ((List<Map<String, Object>>) restResponseName.get(REST_RESPONSE_NAME_MEMBER_SCOPE)).get(0); + private Optional<Map<String, Object>> getMemberScope(Map<String, Object> restResponseName) { + return Optional.ofNullable(restResponseName.get(REST_RESPONSE_NAME_MEMBER_SCOPE)) + .filter(List.class::isInstance) + .map(List.class::cast) + .map(Collection::stream) + .flatMap(Stream::findFirst) + .filter(Map.class::isInstance); } public Optional<ServiceKonto> createBayernIdServiceKonto(Map<String, Object> formDataHeaders) { diff --git a/semantik-adapter/src/test/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactoryTest.java b/semantik-adapter/src/test/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactoryTest.java index 5f9fba08b38624af36a6c29a89c07020233b54e4..28ce6dec1cce38833bf1669fb9caa814ca66037d 100644 --- a/semantik-adapter/src/test/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactoryTest.java +++ b/semantik-adapter/src/test/java/de/ozgcloud/eingang/semantik/common/ServiceKontoFactoryTest.java @@ -23,6 +23,7 @@ */ package de.ozgcloud.eingang.semantik.common; +import static de.ozgcloud.eingang.semantik.common.ServiceKontoFactory.*; import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -30,10 +31,13 @@ import static org.mockito.Mockito.*; import java.util.List; import java.util.Map; +import org.assertj.core.api.InstanceOfAssertFactories; 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.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InjectMocks; import org.mockito.Spy; @@ -41,10 +45,10 @@ import de.ozgcloud.eingang.common.formdata.FormData; import de.ozgcloud.eingang.common.formdata.FormDataUtils; import de.ozgcloud.eingang.common.formdata.PostfachAddressTestFactory; import de.ozgcloud.eingang.common.formdata.ServiceKonto; +import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress; import de.ozgcloud.eingang.common.formdata.ServiceKonto.TrustLevel; import de.ozgcloud.eingang.common.formdata.ServiceKontoTestFactory; import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier; -import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress; class ServiceKontoFactoryTest { @@ -104,15 +108,16 @@ class ServiceKontoFactoryTest { @Nested class TestWithRestResponseName { + @DisplayName("should call buildOsiPostfachV1Address") @Test - void shouldCallBuildAddresses() { + void shouldCallBuildOsiPostfachV1Address() { getPostfachAddresses(); verify(factory).buildOsiPostfachV1Address(any(), anyInt()); } @Test - void shouldReturnPostfachAddresses() { + void shouldBuildPostfachAddresses() { var addresses = getPostfachAddresses(); assertThat(addresses).hasSize(1); @@ -123,11 +128,58 @@ class ServiceKontoFactoryTest { assertThat(addresses.get(0).getType()).isEqualTo(PostfachAddressTestFactory.POSTFACH_ADDRESS_TYPE); } + @DisplayName("should build with postfach address type") + @ParameterizedTest + @ValueSource(ints = { 1, 2, 3 }) + void shouldBuildWithPostfachAddressType(int postfachAddressType) { + var formDataWithPostfachAddressType = FormDataUtils.from(FORM_DATA) + .put(ServiceKontoFactory.REST_RESPONSE_NAME, List.of(Map.of( + ServiceKontoFactory.REST_RESPONSE_NAME_MEMBER_SCOPE, + List.of(Map.of(ServiceKontoFactory.REST_RESPONSE_NAME_MEMBER_SCOPE_MAILBOX_TYPE, + postfachAddressType)))) + ) + .build(); + + var serviceKonto = buildServiceKonto(formDataWithPostfachAddressType); + + assertThat(serviceKonto) + .extracting(ServiceKonto::getPostfachAddresses, InstanceOfAssertFactories.list(PostfachAddress.class)) + .extracting(PostfachAddress::getType) + .containsExactly(postfachAddressType); + } + private List<PostfachAddress> getPostfachAddresses() { return buildServiceKonto(FORM_DATA).getPostfachAddresses(); } } + @DisplayName("with unexpected rest_response_name") + @Nested + class TestWithUnexpectedRestResponseName { + + private final FormData formDataWithUnexpectedRestResponseName = FormDataUtils.from(FORM_DATA) + .put(ServiceKontoFactory.REST_RESPONSE_NAME, List.of(Map.of())).build(); + + @DisplayName("should call buildOsiPostfachV1Address") + @Test + void shouldCallBuildOsiPostfachV1Address() { + buildServiceKonto(formDataWithUnexpectedRestResponseName); + + verify(factory).buildOsiPostfachV1Address(any(), anyInt()); + } + + @DisplayName("should build postfach address with default type") + @Test + void shouldBuildPostfachAddressWithDefaultType() { + var serviceKonto = buildServiceKonto(formDataWithUnexpectedRestResponseName); + + assertThat(serviceKonto) + .extracting(ServiceKonto::getPostfachAddresses, InstanceOfAssertFactories.list(PostfachAddress.class)) + .extracting(PostfachAddress::getType) + .containsExactly(POSTFACH_ADDRESS_DEFAULT); + } + } + @DisplayName("without rest_response_name") @Nested class TestWithoutRestResponseName {