Skip to content
Snippets Groups Projects
Commit cde95d68 authored by Lukas Malte Monnerjahn's avatar Lukas Malte Monnerjahn
Browse files

OZG-5058 feedback on tests

parent bb58f435
Branches
Tags
No related merge requests found
...@@ -8,24 +8,21 @@ import org.junit.jupiter.api.Nested; ...@@ -8,24 +8,21 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import java.util.List; 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; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
@DataITCase @DataITCase
public class M001_CreateEmptyPostfachIfMissingITCase { class M001_CreateEmptyPostfachIfMissingITCase {
private final M001_CreateEmptyPostfachIfMissing changeUnit = new M001_CreateEmptyPostfachIfMissing(); private final M001_CreateEmptyPostfachIfMissing changeUnit = new M001_CreateEmptyPostfachIfMissing();
private static final Document POSTFACH = MigrationTestFactory.createPostfach(); private static final Document POSTFACH = MigrationTestFactory.createDummyPostfach();
@Autowired @Autowired
private MongoTemplate template; private MongoTemplate template;
public static final String SETTINGS_COLLECTION = "settings";
@DisplayName("Do migration") @DisplayName("Do migration")
@Nested @Nested
class TestDoMigration { class TestDoMigration {
...@@ -35,10 +32,10 @@ public class M001_CreateEmptyPostfachIfMissingITCase { ...@@ -35,10 +32,10 @@ public class M001_CreateEmptyPostfachIfMissingITCase {
} }
@Test @Test
void shouldAddPostfachIfMissing() { void shouldAddPostfachIfEmpty() {
changeUnit.doMigration(template); 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) assertThat(settings).hasSize(1)
.anyMatch(this::isEmptyPostfachSetting .anyMatch(this::isEmptyPostfachSetting
); );
...@@ -51,81 +48,39 @@ public class M001_CreateEmptyPostfachIfMissingITCase { ...@@ -51,81 +48,39 @@ public class M001_CreateEmptyPostfachIfMissingITCase {
changeUnit.doMigration(template); changeUnit.doMigration(template);
// assert leeres Postfach sollte angelegt sein List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION);
List<Document> settings = template.find(new Query(), Document.class, SETTINGS_COLLECTION);
assertThat(settings).hasSize(2) assertThat(settings).hasSize(2)
.anyMatch(s -> .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") s.get(MigrationTestFactory.ITEM_TYPE_KEY).equals("SomeType")
)
.anyMatch(this::isEmptyPostfachSetting
); );
} }
@Test @Test
void shouldKeepExistingPostfach() { void shouldKeepExistingPostfach() {
// Postfach in settings Collection anlegen
template.save(POSTFACH, SETTINGS_COLLECTION); template.save(POSTFACH, SETTINGS_COLLECTION);
changeUnit.doMigration(template); changeUnit.doMigration(template);
// assert Postfach in settings Collection sollte unverändert sein List<Document> settings = template.findAll(Document.class, SETTINGS_COLLECTION);
List<Document> settings = template.find(new Query(), Document.class, SETTINGS_COLLECTION); assertThat(settings).containsExactly(POSTFACH);
assertThat(settings).hasSize(1)
.contains(POSTFACH);
} }
private boolean isEmptyPostfachSetting(Document document) { private boolean isEmptyPostfachSetting(Document document) {
Set<String> expectedKeySet = Set.of( return document.containsKey(MigrationTestFactory.ITEM_TYPE_KEY) &&
MigrationTestFactory.ITEM_TYPE_KEY, document.get(MigrationTestFactory.ITEM_TYPE_KEY)
MigrationTestFactory.ITEM_SETTINGS_KEY .equals(MigrationTestFactory.ITEM_TYPE_VALUE_POSTFACH) &&
); !document.containsKey(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;
}
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;
}
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(""));
}
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(""));
} }
} }
......
...@@ -4,8 +4,6 @@ import com.thedeanda.lorem.Lorem; ...@@ -4,8 +4,6 @@ import com.thedeanda.lorem.Lorem;
import com.thedeanda.lorem.LoremIpsum; import com.thedeanda.lorem.LoremIpsum;
import org.bson.Document; import org.bson.Document;
import java.util.Random;
public class MigrationTestFactory { public class MigrationTestFactory {
private static final Lorem lorem = LoremIpsum.getInstance(); private static final Lorem lorem = LoremIpsum.getInstance();
...@@ -23,24 +21,18 @@ public class MigrationTestFactory { ...@@ -23,24 +21,18 @@ public class MigrationTestFactory {
public static final String ITEM_TYPE_VALUE_POSTFACH = "Postfach"; public static final String ITEM_TYPE_VALUE_POSTFACH = "Postfach";
public static final String POSTFACH_ABSENDER_KEY = "absender"; 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_KEY = "name";
public static final String ABSENDER_NAME_VALUE = lorem.getName(); public static final String ABSENDER_NAME_VALUE = lorem.getName();
public static final String ABSENDER_ANSCHRIFT_KEY = "anschrift"; 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_ANSCHRIFT_VALUE = 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"; * Erzeugt ein aus fachlicher Sicht unvollständiges Postfach.
public static final String ABSENDER_MANDANT_VALUE = lorem.getWords(2, 3); * @return Ein Postfach Document
public static final String ABSENDER_GEMEINDESCHLUESSEL_KEY = "gemeindeschluessel"; */
public static final String ABSENDER_GEMEINDESCHLUESSEL_VALUE = String.valueOf(new Random().nextInt(1000000)); public static Document createDummyPostfach() {
public static final String SIGNATUR_TEXT_KEY = "text";
public static final String SIGNATUR_TEXT_VALUE = lorem.getParagraphs(1, 3);
public static Document createPostfach() {
var postfach = new Document(); var postfach = new Document();
postfach.put(POSTFACH_ABSENDER_KEY, createAbsender()); postfach.put(POSTFACH_ABSENDER_KEY, createAbsender());
postfach.put(POSTFACH_SIGNATUR_KEY, createSignatur());
return createSettingsItem(ITEM_TYPE_VALUE_POSTFACH, postfach); return createSettingsItem(ITEM_TYPE_VALUE_POSTFACH, postfach);
} }
...@@ -48,15 +40,7 @@ public class MigrationTestFactory { ...@@ -48,15 +40,7 @@ public class MigrationTestFactory {
var absender = new Document(); var absender = new Document();
absender.put(ABSENDER_NAME_KEY, ABSENDER_NAME_VALUE); absender.put(ABSENDER_NAME_KEY, ABSENDER_NAME_VALUE);
absender.put(ABSENDER_ANSCHRIFT_KEY, ABSENDER_ANSCHRIFT_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; return absender;
} }
private static Document createSignatur() {
var signatur = new Document();
signatur.put(SIGNATUR_TEXT_KEY, SIGNATUR_TEXT_VALUE);
return signatur;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment