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
No related branches found
No related tags found
No related merge requests found
......@@ -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 ->
.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
);
}
@Test
void shouldKeepExistingPostfach() {
// Postfach in settings Collection anlegen
template.save(POSTFACH, 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).containsExactly(POSTFACH);
}
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;
}
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(""));
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);
}
}
......
......@@ -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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment