Skip to content
Snippets Groups Projects
Commit de38fcd1 authored by Felix Reichenbach's avatar Felix Reichenbach
Browse files

OZG-7515 remove main code dependencies from migration test

parent d15d2051
Branches
Tags
1 merge request!11Ozg 7515 migrate patch item command
...@@ -25,24 +25,30 @@ package de.ozgcloud.vorgang.common.migration; ...@@ -25,24 +25,30 @@ package de.ozgcloud.vorgang.common.migration;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoOperations;
import de.ozgcloud.command.Command; import com.thedeanda.lorem.LoremIpsum;
import de.ozgcloud.common.test.DataITCase; import de.ozgcloud.common.test.DataITCase;
import de.ozgcloud.vorgang.command.CommandTestFactory;
import de.ozgcloud.vorgang.command.PersistedCommand;
@DataITCase @DataITCase
class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
private static final String ID_FIELD = "_id";
private static final String BESCHEID_ITEM_NAME = "Bescheid"; private static final String BESCHEID_ITEM_NAME = "Bescheid";
private static final String ITEM_NAME_FIELD = "itemName"; private static final String ITEM_NAME_FIELD = "itemName";
private static final String PATCH_ATTACHED_ITEM_ORDER = "PATCH_ATTACHED_ITEM"; private static final String PATCH_ATTACHED_ITEM_ORDER = "PATCH_ATTACHED_ITEM";
...@@ -56,18 +62,18 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { ...@@ -56,18 +62,18 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
@Autowired @Autowired
private MongoOperations mongoOperations; private MongoOperations mongoOperations;
private List<PersistedCommand> expectedCommands; private List<Document> expectedCommands;
private PersistedCommand sendBescheidCommand; private Document sendBescheidCommand;
private PersistedCommand patchAttachedItemCommand; private Document patchAttachedItemCommand;
private PersistedCommand otherSubCommand; private Document otherSubCommand;
private PersistedCommand otherPatchAttachedItemCommand; private Document otherPatchAttachedItemCommand;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
migrationDbTestUtils.dropCommandCollection(); migrationDbTestUtils.dropCommandCollection();
sendBescheidCommand = saveSendBescheidCommand(); sendBescheidCommand = saveSendBescheidCommand();
patchAttachedItemCommand = savePatchAttachedItemSubCommand(sendBescheidCommand.getId()); patchAttachedItemCommand = savePatchAttachedItemSubCommand(sendBescheidCommand.getObjectId(ID_FIELD));
otherSubCommand = saveOtherSubCommand(sendBescheidCommand.getId()); otherSubCommand = saveOtherSubCommand(sendBescheidCommand.getObjectId(ID_FIELD));
otherPatchAttachedItemCommand = saveOtherPatchAttachedItemSubCommand(); otherPatchAttachedItemCommand = saveOtherPatchAttachedItemSubCommand();
expectedCommands = List.of(sendBescheidCommand, expectedCommands = List.of(sendBescheidCommand,
buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand), buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand),
...@@ -75,53 +81,68 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { ...@@ -75,53 +81,68 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
otherPatchAttachedItemCommand); otherPatchAttachedItemCommand);
} }
private PersistedCommand saveSendBescheidCommand() { private Document saveSendBescheidCommand() {
var command = CommandTestFactory.createBuilder().id(null).order(SEND_BESCHEID_ORDER).build(); var command = CommandDocumentTestFactory.createWithOrder(SEND_BESCHEID_ORDER);
return mongoOperations.save(command); return migrationDbTestUtils.saveCommand(command);
} }
private PersistedCommand savePatchAttachedItemSubCommand(String id) { private Document savePatchAttachedItemSubCommand(ObjectId id) {
Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id); Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id.toString());
var command = CommandTestFactory.createBuilder() var command = CommandDocumentTestFactory.createWithOrderAndBody(PATCH_ATTACHED_ITEM_ORDER, itemBody);
.id(null) return migrationDbTestUtils.saveCommand(command);
.order(PATCH_ATTACHED_ITEM_ORDER)
.bodyObject(itemBody)
.build();
return mongoOperations.save(command);
} }
private PersistedCommand saveOtherSubCommand(String id) { private Document saveOtherSubCommand(ObjectId id) {
Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id); Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id.toString());
var command = CommandTestFactory.createBuilder() var command = CommandDocumentTestFactory.createWithBody(itemBody);
.id(null) return migrationDbTestUtils.saveCommand(command);
.bodyObject(itemBody)
.build();
return mongoOperations.save(command);
} }
private PersistedCommand saveOtherPatchAttachedItemSubCommand() { private Document saveOtherPatchAttachedItemSubCommand() {
var command = CommandTestFactory.createBuilder() var command = CommandDocumentTestFactory.createWithOrder(PATCH_ATTACHED_ITEM_ORDER);
.id(null) return migrationDbTestUtils.saveCommand(command);
.order(PATCH_ATTACHED_ITEM_ORDER)
.build();
return mongoOperations.save(command);
} }
private PersistedCommand buildExpectedPatchAttachedItemCommand(Command patchAttachedItemCommand) { @SuppressWarnings("unchecked")
var body = new HashMap<>(patchAttachedItemCommand.getBodyObject()); private Document buildExpectedPatchAttachedItemCommand(Document patchAttachedItemCommand) {
var body = new HashMap<>((Map<String, Object>) patchAttachedItemCommand.get(CommandDocumentTestFactory.BODY_OBJECT_FIELD));
body.put(ITEM_NAME_FIELD, BESCHEID_ITEM_NAME); body.put(ITEM_NAME_FIELD, BESCHEID_ITEM_NAME);
return CommandTestFactory.createBuilder() var command = CommandDocumentTestFactory.createWithOrderAndBody(PATCH_ATTACHED_ITEM_ORDER, body);
.id(patchAttachedItemCommand.getId()) command.put(ID_FIELD, patchAttachedItemCommand.getObjectId(ID_FIELD));
.order(patchAttachedItemCommand.getOrder()) return command;
.bodyObject(body) }
.build();
@Nested
class TestGetSendBescheidCommandIds {
@Test
void shouldReturnSendBescheidCommandId() {
var ids = migration.getSendBescheidCommandIds(mongoOperations);
assertThat(ids).containsExactly(sendBescheidCommand.getObjectId(ID_FIELD).toString());
}
}
@Nested
class TestBuildQuery {
@Test
void shouldQueryPatchAttachedItemCommandsWithParentId() {
var query = migration.buildQuery(List.of(sendBescheidCommand.getObjectId(ID_FIELD).toString()));
var comands = mongoOperations.find(query, Document.class, MigrationDbTestUtils.COMMAND_COLLECTION);
assertThat(comands).usingRecursiveFieldByFieldElementComparator().containsExactly(patchAttachedItemCommand);
}
} }
@Nested
class TestFullMigration {
@Test @Test
void shouldContainSendBescheidCommand() { void shouldContainSendBescheidCommand() {
migration.doMigration(mongoOperations); migration.doMigration(mongoOperations);
var command = mongoOperations.findById(sendBescheidCommand.getId(), PersistedCommand.class); var command = migrationDbTestUtils.getCommand(sendBescheidCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(sendBescheidCommand); assertThat(command).usingRecursiveComparison().isEqualTo(sendBescheidCommand);
} }
...@@ -130,7 +151,7 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { ...@@ -130,7 +151,7 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
void shouldModifyPatchAttachedBescheidItem() { void shouldModifyPatchAttachedBescheidItem() {
migration.doMigration(mongoOperations); migration.doMigration(mongoOperations);
var command = mongoOperations.findById(patchAttachedItemCommand.getId(), PersistedCommand.class); var command = migrationDbTestUtils.getCommand(patchAttachedItemCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand)); assertThat(command).usingRecursiveComparison().isEqualTo(buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand));
} }
...@@ -139,7 +160,7 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { ...@@ -139,7 +160,7 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
void shouldNotModifyOtherSubcommands() { void shouldNotModifyOtherSubcommands() {
migration.doMigration(mongoOperations); migration.doMigration(mongoOperations);
var command = mongoOperations.findById(otherSubCommand.getId(), PersistedCommand.class); var command = migrationDbTestUtils.getCommand(otherSubCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(otherSubCommand); assertThat(command).usingRecursiveComparison().isEqualTo(otherSubCommand);
} }
...@@ -148,7 +169,7 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { ...@@ -148,7 +169,7 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
void shouldNotModifyOtherPatchAttachedItemCommands() { void shouldNotModifyOtherPatchAttachedItemCommands() {
migration.doMigration(mongoOperations); migration.doMigration(mongoOperations);
var command = mongoOperations.findById(otherPatchAttachedItemCommand.getId(), PersistedCommand.class); var command = migrationDbTestUtils.getCommand(otherPatchAttachedItemCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(otherPatchAttachedItemCommand); assertThat(command).usingRecursiveComparison().isEqualTo(otherPatchAttachedItemCommand);
} }
...@@ -157,8 +178,66 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { ...@@ -157,8 +178,66 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
void shouldContainExpectedCommands() { void shouldContainExpectedCommands() {
migration.doMigration(mongoOperations); migration.doMigration(mongoOperations);
var commands = mongoOperations.findAll(PersistedCommand.class); var commands = mongoOperations.findAll(Document.class, MigrationDbTestUtils.COMMAND_COLLECTION);
assertThat(commands).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrderElementsOf(expectedCommands); assertThat(commands).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrderElementsOf(expectedCommands);
} }
} }
private class CommandDocumentTestFactory {
public static final String ORDER_FIELD = "order";
public static final String BODY_OBJECT_FIELD = "bodyObject";
public static final String VORGANG_ID = UUID.randomUUID().toString();
public static final String CREATED_AT_STR = "2021-01-10T10:30:00Z";
public static final Date CREATED_AT = Date.from(ZonedDateTime.parse(CREATED_AT_STR).toInstant());
public static final String CREATED_BY = UUID.randomUUID().toString();
public static final String CREATED_BY_NAME = LoremIpsum.getInstance().getName();
public static final String CREATED_BY_CLIENT = LoremIpsum.getInstance().getWords(1);
public static final String STATUS = "PENDING";
public static final String RELATION_ID = UUID.randomUUID().toString();
public static final Long RELATION_VERSION = 1L;
public static final String ORDER = "VORGANG_ANNEHMEN";
public static final Map<String, Object> PREVIOUS_STATE = Map.of("test", "value");
public static final Map<String, Object> BODY = Map.of("key", "value");
public static final String CREATED_RESOURCE = "createdResource";
public static Document create() {
var command = new Document();
command.put("vorgangId", VORGANG_ID);
command.put("createdAt", CREATED_AT);
command.put("createdBy", CREATED_BY);
command.put("createdByName", CREATED_BY_NAME);
command.put("createdByClientName", CREATED_BY_CLIENT);
command.put("status", STATUS);
command.put("relationId", RELATION_ID);
command.put("relationVersion", RELATION_VERSION);
command.put("previousState", PREVIOUS_STATE);
command.put(ORDER_FIELD, ORDER);
command.put(BODY_OBJECT_FIELD, BODY);
command.put("createdResource", CREATED_RESOURCE);
return command;
}
public static Document createWithOrder(String order) {
var command = create();
command.put(ORDER_FIELD, order);
return command;
}
public static Document createWithBody(Map<String, Object> body) {
var command = create();
command.put(BODY_OBJECT_FIELD, body);
return command;
}
public static Document createWithOrderAndBody(String order, Map<String, Object> body) {
var command = createWithOrder(order);
command.put(BODY_OBJECT_FIELD, body);
return command;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment