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

OZG-7515 refactor migration and its test

parent 46ef9135
Branches
Tags
1 merge request!11Ozg 7515 migrate patch item command
......@@ -24,6 +24,7 @@
package de.ozgcloud.vorgang.common.migration;
import java.util.List;
import java.util.stream.Stream;
import org.bson.Document;
import org.bson.types.ObjectId;
......@@ -55,9 +56,12 @@ public class M014_AddItemNameBescheidToPatchAttachedItemCommand { // NOSONAR
}
List<String> getSendBescheidCommandIds(MongoOperations mongoOperations) {
var commands = mongoOperations.find(Query.query(Criteria.where(ORDER_FIELD).is(SEND_BESCHEID_ORDER)), Document.class,
return getSendBescheidCommands(mongoOperations).map(command -> command.getObjectId(ID_FIELD)).map(ObjectId::toString).toList();
}
private Stream<Document> getSendBescheidCommands(MongoOperations mongoOperations) {
return mongoOperations.stream(Query.query(Criteria.where(ORDER_FIELD).is(SEND_BESCHEID_ORDER)), Document.class,
COMMAND_COLLECTION_NAME);
return commands.stream().map(command -> command.getObjectId(ID_FIELD)).map(ObjectId::toString).toList();
}
void updateDocuments(MongoOperations mongoOperations, List<String> parentIds) {
......
......@@ -62,23 +62,20 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
@Autowired
private MongoOperations mongoOperations;
private List<Document> expectedCommands;
private Document sendBescheidCommand;
private Document patchAttachedItemCommand;
private Document otherSubCommand;
private Document otherPatchAttachedItemCommand;
private Document subCommandWithUnkownOrder;
private Document patchAttachedItemCommandWithoutParent;
private Document patchAttachedItemCommandWithDifferentParent;
@BeforeEach
void setUp() {
migrationDbTestUtils.dropCommandCollection();
sendBescheidCommand = saveSendBescheidCommand();
patchAttachedItemCommand = savePatchAttachedItemSubCommand(sendBescheidCommand.getObjectId(ID_FIELD));
otherSubCommand = saveOtherSubCommand(sendBescheidCommand.getObjectId(ID_FIELD));
otherPatchAttachedItemCommand = saveOtherPatchAttachedItemSubCommand();
expectedCommands = List.of(sendBescheidCommand,
buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand),
otherSubCommand,
otherPatchAttachedItemCommand);
subCommandWithUnkownOrder = saveSubCommandWithUnkownOrder(sendBescheidCommand.getObjectId(ID_FIELD));
patchAttachedItemCommandWithoutParent = savePatchAttachedItemCommandWithoutParent();
patchAttachedItemCommandWithDifferentParent = savePatchAttachedItemCommandWithDifferentParent();
}
private Document saveSendBescheidCommand() {
......@@ -92,25 +89,21 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
return migrationDbTestUtils.saveCommand(command);
}
private Document saveOtherSubCommand(ObjectId id) {
private Document saveSubCommandWithUnkownOrder(ObjectId id) {
var itemBody = Map.<String, Object>of(PARENT_ID_FIELD, id.toString());
var command = CommandDocumentTestFactory.createWithBody(itemBody);
return migrationDbTestUtils.saveCommand(command);
}
private Document saveOtherPatchAttachedItemSubCommand() {
private Document savePatchAttachedItemCommandWithoutParent() {
var command = CommandDocumentTestFactory.createWithOrder(PATCH_ATTACHED_ITEM_ORDER);
return migrationDbTestUtils.saveCommand(command);
}
@SuppressWarnings("unchecked")
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);
var command = CommandDocumentTestFactory.createWithOrderAndBody(PATCH_ATTACHED_ITEM_ORDER, body);
command.put(ID_FIELD, patchAttachedItemCommand.getObjectId(ID_FIELD));
return command;
private Document savePatchAttachedItemCommandWithDifferentParent() {
var itemBody = Map.<String, Object>of(PARENT_ID_FIELD, UUID.randomUUID().toString());
var command = CommandDocumentTestFactory.createWithOrderAndBody(PATCH_ATTACHED_ITEM_ORDER, itemBody);
return migrationDbTestUtils.saveCommand(command);
}
@Nested
......@@ -144,63 +137,90 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
migration.updateDocuments(mongoOperations, List.of(sendBescheidCommand.getObjectId(ID_FIELD).toString()));
var command = migrationDbTestUtils.getCommand(patchAttachedItemCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison()
.isEqualTo(buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand));
assertThat(command).usingRecursiveComparison().isEqualTo(addItemNameToPatchAttachedItemCommand(patchAttachedItemCommand));
}
}
@Nested
class TestFullMigration {
@Test
void shouldKeepSendBescheidCommand() {
void shouldAddOrRemoveCommands() {
migration.doMigration(mongoOperations);
var command = migrationDbTestUtils.getCommand(sendBescheidCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(sendBescheidCommand);
var commands = mongoOperations.findAll(Document.class, MigrationDbTestUtils.COMMAND_COLLECTION);
assertThat(commands).hasSize(5);
}
@Test
void shouldModifyPatchAttachedBescheidItem() {
migration.doMigration(mongoOperations);
@Nested
class TestPatchAttachedBescheidItem {
var command = migrationDbTestUtils.getCommand(patchAttachedItemCommand.getObjectId(ID_FIELD));
@Test
void shouldAddItemNameToCommandBody() {
migration.doMigration(mongoOperations);
var command = migrationDbTestUtils.getCommand(patchAttachedItemCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(addItemNameToPatchAttachedItemCommand(patchAttachedItemCommand));
}
assertThat(command).usingRecursiveComparison()
.isEqualTo(buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand));
}
@Test
void shouldNotModifyOtherSubcommands() {
migration.doMigration(mongoOperations);
@Nested
class TestCommandsToNotModify {
@Test
void shouldKeepSendBescheidCommand() {
migration.doMigration(mongoOperations);
var command = migrationDbTestUtils.getCommand(otherSubCommand.getObjectId(ID_FIELD));
var command = migrationDbTestUtils.getCommand(sendBescheidCommand.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(sendBescheidCommand);
}
assertThat(command).usingRecursiveComparison().isEqualTo(otherSubCommand);
}
@Nested
class TestSubCommandsOfSendBescheidItem {
@Test
void shouldNotModifyOtherPatchAttachedItemCommands() {
migration.doMigration(mongoOperations);
@Test
void shouldKeepIfOrderIsNotPatchAttachedItem() {
migration.doMigration(mongoOperations);
var command = migrationDbTestUtils.getCommand(otherPatchAttachedItemCommand.getObjectId(ID_FIELD));
var command = migrationDbTestUtils.getCommand(subCommandWithUnkownOrder.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(subCommandWithUnkownOrder);
assertThat(command).usingRecursiveComparison().isEqualTo(otherPatchAttachedItemCommand);
}
}
}
@Test
void shouldContainExpectedCommands() {
migration.doMigration(mongoOperations);
@Nested
class TestPatchAttachedItem {
var commands = mongoOperations.findAll(Document.class, MigrationDbTestUtils.COMMAND_COLLECTION);
@Test
void shouldNotModifyOnMissingParentId() {
migration.doMigration(mongoOperations);
var command = migrationDbTestUtils.getCommand(patchAttachedItemCommandWithoutParent.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(patchAttachedItemCommandWithoutParent);
}
@Test
void shouldNotModifyOnDifferentParentId() {
migration.doMigration(mongoOperations);
var command = migrationDbTestUtils.getCommand(patchAttachedItemCommandWithDifferentParent.getObjectId(ID_FIELD));
assertThat(command).usingRecursiveComparison().isEqualTo(patchAttachedItemCommandWithDifferentParent);
}
}
assertThat(commands).usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrderElementsOf(expectedCommands);
}
}
@SuppressWarnings("unchecked")
private Document addItemNameToPatchAttachedItemCommand(Document patchAttachedItemCommand) {
var body = new HashMap<>((Map<String, Object>) patchAttachedItemCommand.get(CommandDocumentTestFactory.BODY_OBJECT_FIELD));
body.put(ITEM_NAME_FIELD, BESCHEID_ITEM_NAME);
patchAttachedItemCommand.put(CommandDocumentTestFactory.BODY_OBJECT_FIELD, body);
return patchAttachedItemCommand;
}
private class CommandDocumentTestFactory {
public static final String ORDER_FIELD = "order";
......@@ -217,7 +237,7 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
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 String ORDER = LoremIpsum.getInstance().getWords(1);
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";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment