diff --git a/common/src/main/java/de/ozgcloud/eingang/common/errorhandling/UnexpectedTrustLevelException.java b/common/src/main/java/de/ozgcloud/eingang/common/errorhandling/UnexpectedTrustLevelException.java deleted file mode 100644 index 793d20f72e3c97e9fa053adde0a8853bfca5e0b5..0000000000000000000000000000000000000000 --- a/common/src/main/java/de/ozgcloud/eingang/common/errorhandling/UnexpectedTrustLevelException.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.ozgcloud.eingang.common.errorhandling; - -public class UnexpectedTrustLevelException extends TechnicalException { - - public UnexpectedTrustLevelException(String message) { - super(message); - } - -} 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 1494a485d2b6a1a09f4c20485deda58d5e31840a..1d7c4d78195cbd29954d77efa8d3e0306714cb95 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 @@ -9,7 +9,6 @@ import java.util.Optional; import org.apache.commons.collections.MapUtils; import org.springframework.stereotype.Component; -import de.ozgcloud.eingang.common.errorhandling.UnexpectedTrustLevelException; import de.ozgcloud.eingang.common.formdata.FormData; import de.ozgcloud.eingang.common.formdata.PostfachAddressIdentifier; import de.ozgcloud.eingang.common.formdata.ServiceKonto; @@ -96,12 +95,15 @@ public class ServiceKontoFactory { if (Objects.isNull(formDataHeaders) || !formDataHeaders.containsKey(KEY_BAYERN_ID_POSTFACH_ID)) { return Optional.empty(); } - try { + if (isValidTrustLevel(formDataHeaders)) { return Optional.of(buildBayernIdServiceKonto(formDataHeaders)); - } catch (UnexpectedTrustLevelException e) { - LOG.error("Error while creating BayernID ServiceKonto", e); - return Optional.empty(); } + LOG.error("TrustLevel has an unexpected value '{}'. BayernID user account is not connected", getTrustLevel(formDataHeaders)); + return Optional.empty(); + } + + boolean isValidTrustLevel(Map<String, Object> formDataHeader) { + return TrustLevel.hasValue(getTrustLevel(formDataHeader)); } ServiceKonto buildBayernIdServiceKonto(Map<String, Object> formDataHeaders) { @@ -112,6 +114,10 @@ public class ServiceKontoFactory { .build(); } + String getTrustLevel(Map<String, Object> formDataHeaders) { + return MapUtils.getString(formDataHeaders, KEY_BAYERN_ID_TRUST_LEVEL); + } + PostfachAddress buildPostfachAddress(String postkorbHandle) { return PostfachAddress.builder() .type(POSTFACH_ADDRESS_DEFAULT) @@ -120,15 +126,6 @@ public class ServiceKontoFactory { .build(); } - String getTrustLevel(Map<String, Object> formDataHeader) { - var trustLevel = MapUtils.getString(formDataHeader, KEY_BAYERN_ID_TRUST_LEVEL); - if (TrustLevel.hasValue(trustLevel)) { - return trustLevel; - } - throw new UnexpectedTrustLevelException( - "TrustLevel has an unexpected value '%s'. BayernID user account is not connected".formatted(trustLevel)); - } - private PostfachAddressIdentifier buildIdentifier(String postfachId) { return StringBasedIdentifier.builder().postfachId(postfachId).build(); } 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 c4bb1b0f9a4067771b26cea24d5dbfcd203fde9e..48b9adff8e4a7f75b7296c666d1112556b0a11f8 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 @@ -1,7 +1,6 @@ package de.ozgcloud.eingang.semantik.common; import static org.assertj.core.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -15,12 +14,12 @@ import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Spy; -import de.ozgcloud.eingang.common.errorhandling.UnexpectedTrustLevelException; 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.TrustLevel; +import de.ozgcloud.eingang.common.formdata.ServiceKontoTestFactory; import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier; import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress; import de.ozgcloud.eingang.semantik.enginebased.afm.AfmHeaderTestFactory; @@ -149,12 +148,9 @@ class ServiceKontoFactoryTest { @Nested class TestCreateBayernIdServicekonto { - private static final String POSTFACH_ID = "postfach-id"; - private static final PostfachAddress POSTFACH_ADDRESS = PostfachAddressTestFactory.create(); - private final Map<String, Object> formDataHeaders = Map.of( - ServiceKontoFactory.KEY_BAYERN_ID_POSTFACH_ID, POSTFACH_ID, - ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, "STORK-QAA-Level-2" + ServiceKontoFactory.KEY_BAYERN_ID_POSTFACH_ID, PostfachAddressTestFactory.POSTFACH_ID, + ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, ServiceKontoTestFactory.TRUST_LEVEL ); @DisplayName("should return empty when headers map is null") @@ -173,6 +169,13 @@ class ServiceKontoFactoryTest { assertThat(serviceKonto).isEmpty(); } + @Test + void shouldCallIsValidTrustLevel() { + factory.createBayernIdServiceKonto(formDataHeaders); + + verify(factory).isValidTrustLevel(formDataHeaders); + } + @Test void shouldCallBuildBayernIdServiceKonto() { factory.createBayernIdServiceKonto(formDataHeaders); @@ -193,7 +196,7 @@ class ServiceKontoFactoryTest { @DisplayName("should return empty when trust level has unexpected value") @Test void shouldReturnEmptyWhenTrustLevelCorrupted() { - doThrow(UnexpectedTrustLevelException.class).when(factory).buildBayernIdServiceKonto(any()); + doReturn(false).when(factory).isValidTrustLevel(any()); var serviceKonto = factory.createBayernIdServiceKonto(formDataHeaders); @@ -201,6 +204,34 @@ class ServiceKontoFactoryTest { } } + @Nested + class TestIsValidTrustLevel { + + @Test + void shouldCallHasValue() { + try (var trustLevelMock = mockStatic(TrustLevel.class)) { + isValidTrustLevel(); + + trustLevelMock.verify(() -> TrustLevel.hasValue(ServiceKontoTestFactory.TRUST_LEVEL)); + } + } + + @Test + void shouldReturnValue() { + try (var trustLevelMock = mockStatic(TrustLevel.class)) { + trustLevelMock.when(() -> TrustLevel.hasValue(any())).thenReturn(true); + + var result = isValidTrustLevel(); + + assertThat(result).isTrue(); + } + } + + private boolean isValidTrustLevel() { + return factory.isValidTrustLevel(Map.of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, ServiceKontoTestFactory.TRUST_LEVEL)); + } + } + @Nested class TestBuildBayernIdServiceKonto { @@ -212,11 +243,6 @@ class ServiceKontoFactoryTest { ServiceKontoFactory.KEY_BAYERN_ID_POSTFACH_ID, POSTFACH_ID, ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, TRUST_LEVEL); - @BeforeEach - void init() { - doReturn(TRUST_LEVEL).when(factory).getTrustLevel(any()); - } - @Test void shouldSetType() { var serviceKonto = buildBayernIdServiceKonto(); @@ -262,35 +288,16 @@ class ServiceKontoFactoryTest { @Nested class TestGetTrustLevel { - private static final String TRUST_LEVEL = "STORK-QAA-Level-2"; - - private final Map<String, Object> formDataHeaders = Map.of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, TRUST_LEVEL); - - @Test - void shouldCallValidateTrustLevel() { - try (var trustLevelMock = mockStatic(TrustLevel.class)) { - trustLevelMock.when(() -> TrustLevel.hasValue(any())).thenReturn(true); - - factory.getTrustLevel(formDataHeaders); - - trustLevelMock.verify(() -> TrustLevel.hasValue(TRUST_LEVEL)); - } - - } + private final Map<String, Object> formDataHeaders = Map.of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, + ServiceKontoTestFactory.TRUST_LEVEL); @Test void shouldReturnTrustLevel() { var trustLevel = factory.getTrustLevel(formDataHeaders); - assertThat(trustLevel).isEqualTo(TRUST_LEVEL); + assertThat(trustLevel).isEqualTo(ServiceKontoTestFactory.TRUST_LEVEL); } - @Test - void shouldThrowExceptionWhenTrustLevelIsInvalid() { - var formDataHeaders = Map.<String, Object>of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, "unexpected"); - - assertThrows(UnexpectedTrustLevelException.class, () -> factory.getTrustLevel(formDataHeaders)); - } } } \ No newline at end of file