From 0eb0ab842ee37fe1e28d7cd009ac8e4b2d86feb7 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Thu, 6 Jun 2024 14:29:27 +0200
Subject: [PATCH] OZG-4771 validate trust level before building servic konto

---
 .../UnexpectedTrustLevelException.java        |  9 ---
 .../semantik/common/ServiceKontoFactory.java  | 25 +++---
 .../common/ServiceKontoFactoryTest.java       | 77 ++++++++++---------
 3 files changed, 53 insertions(+), 58 deletions(-)
 delete mode 100644 common/src/main/java/de/ozgcloud/eingang/common/errorhandling/UnexpectedTrustLevelException.java

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 793d20f72..000000000
--- 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 1494a485d..1d7c4d781 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 c4bb1b0f9..48b9adff8 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
-- 
GitLab