From a98b0e301eb3dfa04543bb665197c8727c124af7 Mon Sep 17 00:00:00 2001
From: Lukas Malte Monnerjahn <lukasmalte.monnerjahn@dataport.de>
Date: Tue, 27 Feb 2024 14:31:58 +0100
Subject: [PATCH] OZG-4948 OZG-5058 Changed

---
 .../M001_CreateEmptyPostfachIfMissing.java    | 12 ++++-
 ...01_CreateEmptyPostfachIfMissingITCase.java | 52 ++++++++++++-------
 .../admin/migration/MigrationTestFactory.java | 46 ----------------
 3 files changed, 43 insertions(+), 67 deletions(-)
 delete mode 100644 src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java

diff --git a/src/main/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissing.java b/src/main/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissing.java
index f65537fd..b871728d 100644
--- a/src/main/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissing.java
+++ b/src/main/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissing.java
@@ -17,10 +17,18 @@ public class M001_CreateEmptyPostfachIfMissing {  // NOSONAR
 
 	@Execution
 	public void doMigration(MongoTemplate template) {
-		var query = Query.query(Criteria.where(TYPE_NAME_KEY).is(TYPE_NAME_POSTFACH_VALUE));
+		template.upsert(buildQuery(), buildUpdate(), SETTINGS_COLLECTION);
+	}
+
+	private Query buildQuery() {
+		var criteria = Criteria.where(TYPE_NAME_KEY).is(TYPE_NAME_POSTFACH_VALUE);
+		return Query.query(criteria);
+	}
+
+	private Update buildUpdate() {
 		var update = new Update();
 		update.setOnInsert(TYPE_NAME_KEY, TYPE_NAME_POSTFACH_VALUE);
-		template.upsert(query, update, SETTINGS_COLLECTION);
+		return update;
 	}
 
 	@RollbackExecution
diff --git a/src/test/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissingITCase.java b/src/test/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissingITCase.java
index 161040dc..19e41f79 100644
--- a/src/test/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissingITCase.java
+++ b/src/test/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissingITCase.java
@@ -35,10 +35,9 @@ class M001_CreateEmptyPostfachIfMissingITCase {
 		void shouldAddPostfachIfEmpty() {
 			changeUnit.doMigration(template);
 
-			List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION);
+			List<Document> settings = findAllSettings();
 			assertThat(settings).hasSize(1)
-					.anyMatch(this::isEmptyPostfachSetting
-					);
+					.anyMatch(this::isEmptyPostfachSetting);
 		}
 
 		@Test
@@ -48,22 +47,10 @@ class M001_CreateEmptyPostfachIfMissingITCase {
 
 			changeUnit.doMigration(template);
 
-			List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION);
+			List<Document> settings = findAllSettings();
 			assertThat(settings).hasSize(2)
-					.anyMatch(this::isEmptyPostfachSetting);
-		}
-
-		@Test
-		void shouldKeepExistingSetting() {
-			var settingItem = MigrationTestFactory.createSettingsItem("SomeType", new Document());
-			template.save(settingItem, SETTINGS_COLLECTION);
-
-			changeUnit.doMigration(template);
-
-			List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION);
-			assertThat(settings).anyMatch(s ->
-							s.get(MigrationTestFactory.ITEM_TYPE_KEY).equals("SomeType")
-					);
+					.anyMatch(this::isEmptyPostfachSetting)
+					.contains(settingItem);
 		}
 
 		@Test
@@ -72,10 +59,14 @@ class M001_CreateEmptyPostfachIfMissingITCase {
 
 			changeUnit.doMigration(template);
 
-			List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION);
+			List<Document> settings = findAllSettings();
 			assertThat(settings).containsExactly(POSTFACH);
 		}
 
+		private List<Document> findAllSettings() {
+			return template.findAll(Document.class, SETTINGS_COLLECTION);
+		}
+
 		private boolean isEmptyPostfachSetting(Document document) {
 			return document.containsKey(MigrationTestFactory.ITEM_TYPE_KEY) &&
 					document.get(MigrationTestFactory.ITEM_TYPE_KEY)
@@ -84,4 +75,27 @@ class M001_CreateEmptyPostfachIfMissingITCase {
 		}
 
 	}
+
+	static class MigrationTestFactory {
+
+		public static final String ITEM_TYPE_KEY = "name";
+		public static final String ITEM_SETTINGS_KEY = "settings";
+
+		static Document createSettingsItem(String itemType, Document itemValue) {
+			var settingsItem = new Document();
+			settingsItem.put(ITEM_TYPE_KEY, itemType);
+			settingsItem.put(ITEM_SETTINGS_KEY, itemValue);
+			return settingsItem;
+		}
+
+		public static final String ITEM_TYPE_VALUE_POSTFACH = "Postfach";
+		public static final String POSTFACH_ABSENDER_KEY = "absender";
+
+		static Document createDummyPostfach() {
+			var postfach = new Document();
+			postfach.put(POSTFACH_ABSENDER_KEY, "Some Value");
+			return createSettingsItem(ITEM_TYPE_VALUE_POSTFACH, postfach);
+		}
+
+	}
 }
diff --git a/src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java b/src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java
deleted file mode 100644
index 16af051b..00000000
--- a/src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package de.ozgcloud.admin.migration;
-
-import com.thedeanda.lorem.Lorem;
-import com.thedeanda.lorem.LoremIpsum;
-import org.bson.Document;
-
-public class MigrationTestFactory {
-
-	private static final Lorem lorem = LoremIpsum.getInstance();
-
-	public static final String ITEM_TYPE_KEY = "name";
-	public static final String ITEM_SETTINGS_KEY = "settings";
-
-	public static Document createSettingsItem(String itemType, Document itemValue) {
-		var settingsItem = new Document();
-		settingsItem.put(ITEM_TYPE_KEY, itemType);
-		settingsItem.put(ITEM_SETTINGS_KEY, itemValue);
-		return settingsItem;
-	}
-
-
-	public static final String ITEM_TYPE_VALUE_POSTFACH = "Postfach";
-	public static final String POSTFACH_ABSENDER_KEY = "absender";
-	public static final String ABSENDER_NAME_KEY = "name";
-	public static final String ABSENDER_NAME_VALUE = lorem.getName();
-	public static final String ABSENDER_ANSCHRIFT_KEY = "anschrift";
-	public static final String ABSENDER_ANSCHRIFT_VALUE = lorem.getZipCode() + " " + lorem.getCity();
-
-	/**
-	 * Erzeugt ein aus fachlicher Sicht unvollständiges Postfach.
-	 * @return Ein Postfach Document
-	 */
-	public static Document createDummyPostfach() {
-		var postfach = new Document();
-		postfach.put(POSTFACH_ABSENDER_KEY, createAbsender());
-		return createSettingsItem(ITEM_TYPE_VALUE_POSTFACH, postfach);
-	}
-
-	private static Document createAbsender() {
-		var absender = new Document();
-		absender.put(ABSENDER_NAME_KEY, ABSENDER_NAME_VALUE);
-		absender.put(ABSENDER_ANSCHRIFT_KEY, ABSENDER_ANSCHRIFT_VALUE);
-		return absender;
-	}
-
-}
-- 
GitLab