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 69277ba8d18e7185770a3df920f2cb8f2196de9f..161040dc0b92b777dd4a3af99b6efd6bb6d6a68b 100644 --- a/src/test/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissingITCase.java +++ b/src/test/java/de/ozgcloud/admin/migration/M001_CreateEmptyPostfachIfMissingITCase.java @@ -8,24 +8,21 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.query.Query; import java.util.List; -import java.util.Set; +import static de.ozgcloud.admin.migration.M001_CreateEmptyPostfachIfMissing.SETTINGS_COLLECTION; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; @DataITCase -public class M001_CreateEmptyPostfachIfMissingITCase { +class M001_CreateEmptyPostfachIfMissingITCase { private final M001_CreateEmptyPostfachIfMissing changeUnit = new M001_CreateEmptyPostfachIfMissing(); - private static final Document POSTFACH = MigrationTestFactory.createPostfach(); + private static final Document POSTFACH = MigrationTestFactory.createDummyPostfach(); @Autowired private MongoTemplate template; - public static final String SETTINGS_COLLECTION = "settings"; - @DisplayName("Do migration") @Nested class TestDoMigration { @@ -35,10 +32,10 @@ public class M001_CreateEmptyPostfachIfMissingITCase { } @Test - void shouldAddPostfachIfMissing() { + void shouldAddPostfachIfEmpty() { changeUnit.doMigration(template); - List<Document> settings = template.find(new Query(), Document.class, SETTINGS_COLLECTION); + List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION); assertThat(settings).hasSize(1) .anyMatch(this::isEmptyPostfachSetting ); @@ -51,81 +48,39 @@ public class M001_CreateEmptyPostfachIfMissingITCase { changeUnit.doMigration(template); - // assert leeres Postfach sollte angelegt sein - List<Document> settings = template.find(new Query(), Document.class, SETTINGS_COLLECTION); + List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION); assertThat(settings).hasSize(2) - .anyMatch(s -> - s.get(MigrationTestFactory.ITEM_TYPE_KEY).equals("SomeType") - ) - .anyMatch(this::isEmptyPostfachSetting - ); + .anyMatch(this::isEmptyPostfachSetting); } @Test - void shouldKeepExistingPostfach() { - // Postfach in settings Collection anlegen - template.save(POSTFACH, SETTINGS_COLLECTION); + void shouldKeepExistingSetting() { + var settingItem = MigrationTestFactory.createSettingsItem("SomeType", new Document()); + template.save(settingItem, SETTINGS_COLLECTION); changeUnit.doMigration(template); - // assert Postfach in settings Collection sollte unverändert sein - List<Document> settings = template.find(new Query(), Document.class, SETTINGS_COLLECTION); - assertThat(settings).hasSize(1) - .contains(POSTFACH); + List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION); + assertThat(settings).anyMatch(s -> + s.get(MigrationTestFactory.ITEM_TYPE_KEY).equals("SomeType") + ); } - private boolean isEmptyPostfachSetting(Document document) { - Set<String> expectedKeySet = Set.of( - MigrationTestFactory.ITEM_TYPE_KEY, - MigrationTestFactory.ITEM_SETTINGS_KEY - ); - if(document.keySet().containsAll(expectedKeySet) && - document.get(MigrationTestFactory.ITEM_TYPE_KEY).equals(MigrationTestFactory.ITEM_TYPE_VALUE_POSTFACH)) { - var settingsValue = document.get(MigrationTestFactory.ITEM_SETTINGS_KEY); - if (settingsValue instanceof Document postfach) { - return isEmptyPostfach(postfach); - } - } - return false; - } + @Test + void shouldKeepExistingPostfach() { + template.save(POSTFACH, SETTINGS_COLLECTION); - private boolean isEmptyPostfach(Document document) { - Set<String> expectedKeySet = Set.of( - MigrationTestFactory.POSTFACH_ABSENDER_KEY, - MigrationTestFactory.POSTFACH_SIGNATUR_KEY - ); - if (document.keySet().containsAll(expectedKeySet) && - document.keySet().size() == expectedKeySet.size()) { - var absenderValue = document.get(MigrationTestFactory.POSTFACH_ABSENDER_KEY); - var signaturValue = document.get(MigrationTestFactory.POSTFACH_SIGNATUR_KEY); - if (absenderValue instanceof Document absender && - signaturValue instanceof Document signatur) { - return isEmptyAbsender(absender) && isEmptySignatur(signatur); - } - } - return false; - } + changeUnit.doMigration(template); - private boolean isEmptyAbsender(Document document) { - Set<String> expectedKeySet = Set.of( - MigrationTestFactory.ABSENDER_ANSCHRIFT_KEY, - MigrationTestFactory.ABSENDER_NAME_KEY, - MigrationTestFactory.ABSENDER_DIENST_KEY, - MigrationTestFactory.ABSENDER_MANDANT_KEY, - MigrationTestFactory.ABSENDER_GEMEINDESCHLUESSEL_KEY - ); - return document.keySet().containsAll(expectedKeySet) && - document.keySet().size() == expectedKeySet.size() && - document.entrySet().stream().allMatch(entry -> entry.getValue().equals("")); + List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION); + assertThat(settings).containsExactly(POSTFACH); } - private boolean isEmptySignatur(Document document) { - Set<String> expectedKeySet = Set.of( - MigrationTestFactory.SIGNATUR_TEXT_KEY - ); - return document.keySet().containsAll(expectedKeySet) && - document.keySet().size() == expectedKeySet.size() && - document.entrySet().stream().allMatch(entry -> entry.getValue().equals("")); + private boolean isEmptyPostfachSetting(Document document) { + return document.containsKey(MigrationTestFactory.ITEM_TYPE_KEY) && + document.get(MigrationTestFactory.ITEM_TYPE_KEY) + .equals(MigrationTestFactory.ITEM_TYPE_VALUE_POSTFACH) && + !document.containsKey(MigrationTestFactory.ITEM_SETTINGS_KEY); } } diff --git a/src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java b/src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java index 35fa0e187870364cfa79c5ca8aea18a00bebf4f1..16af051b5712a8dcb667f9aa8604646688368f5e 100644 --- a/src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java +++ b/src/test/java/de/ozgcloud/admin/migration/MigrationTestFactory.java @@ -4,8 +4,6 @@ import com.thedeanda.lorem.Lorem; import com.thedeanda.lorem.LoremIpsum; import org.bson.Document; -import java.util.Random; - public class MigrationTestFactory { private static final Lorem lorem = LoremIpsum.getInstance(); @@ -23,24 +21,18 @@ public class MigrationTestFactory { public static final String ITEM_TYPE_VALUE_POSTFACH = "Postfach"; public static final String POSTFACH_ABSENDER_KEY = "absender"; - public static final String POSTFACH_SIGNATUR_KEY = "signatur"; 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.getWords(1) + ", " + lorem.getZipCode() + " " + lorem.getCity(); - public static final String ABSENDER_DIENST_KEY = "dienst"; - public static final String ABSENDER_DIENST_VALUE = lorem.getWords(1, 2); - public static final String ABSENDER_MANDANT_KEY = "mandant"; - public static final String ABSENDER_MANDANT_VALUE = lorem.getWords(2, 3); - public static final String ABSENDER_GEMEINDESCHLUESSEL_KEY = "gemeindeschluessel"; - public static final String ABSENDER_GEMEINDESCHLUESSEL_VALUE = String.valueOf(new Random().nextInt(1000000)); - public static final String SIGNATUR_TEXT_KEY = "text"; - public static final String SIGNATUR_TEXT_VALUE = lorem.getParagraphs(1, 3); - - public static Document createPostfach() { + 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()); - postfach.put(POSTFACH_SIGNATUR_KEY, createSignatur()); return createSettingsItem(ITEM_TYPE_VALUE_POSTFACH, postfach); } @@ -48,15 +40,7 @@ public class MigrationTestFactory { var absender = new Document(); absender.put(ABSENDER_NAME_KEY, ABSENDER_NAME_VALUE); absender.put(ABSENDER_ANSCHRIFT_KEY, ABSENDER_ANSCHRIFT_VALUE); - absender.put(ABSENDER_DIENST_KEY, ABSENDER_DIENST_VALUE); - absender.put(ABSENDER_MANDANT_KEY, ABSENDER_MANDANT_VALUE); - absender.put(ABSENDER_GEMEINDESCHLUESSEL_KEY, ABSENDER_GEMEINDESCHLUESSEL_VALUE); return absender; } - private static Document createSignatur() { - var signatur = new Document(); - signatur.put(SIGNATUR_TEXT_KEY, SIGNATUR_TEXT_VALUE); - return signatur; - } }