diff --git a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/migration/M014_AddItemNameBescheidToPatchAttachedItemCommandITCase.java b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/migration/M014_AddItemNameBescheidToPatchAttachedItemCommandITCase.java index a9e4c2460ca1c9e00977ac026cb9c4586eb09fa1..d558ae25fcef4bc71f26db61dc1508953a1dfdef 100644 --- a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/migration/M014_AddItemNameBescheidToPatchAttachedItemCommandITCase.java +++ b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/common/migration/M014_AddItemNameBescheidToPatchAttachedItemCommandITCase.java @@ -25,24 +25,30 @@ package de.ozgcloud.vorgang.common.migration; import static org.assertj.core.api.Assertions.*; +import java.time.ZonedDateTime; +import java.util.Date; import java.util.HashMap; import java.util.List; 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.Nested; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.springframework.beans.factory.annotation.Autowired; 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.vorgang.command.CommandTestFactory; -import de.ozgcloud.vorgang.command.PersistedCommand; @DataITCase class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { + private static final String ID_FIELD = "_id"; private static final String BESCHEID_ITEM_NAME = "Bescheid"; private static final String ITEM_NAME_FIELD = "itemName"; private static final String PATCH_ATTACHED_ITEM_ORDER = "PATCH_ATTACHED_ITEM"; @@ -56,18 +62,18 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { @Autowired private MongoOperations mongoOperations; - private List<PersistedCommand> expectedCommands; - private PersistedCommand sendBescheidCommand; - private PersistedCommand patchAttachedItemCommand; - private PersistedCommand otherSubCommand; - private PersistedCommand otherPatchAttachedItemCommand; + private List<Document> expectedCommands; + private Document sendBescheidCommand; + private Document patchAttachedItemCommand; + private Document otherSubCommand; + private Document otherPatchAttachedItemCommand; @BeforeEach void setUp() { migrationDbTestUtils.dropCommandCollection(); sendBescheidCommand = saveSendBescheidCommand(); - patchAttachedItemCommand = savePatchAttachedItemSubCommand(sendBescheidCommand.getId()); - otherSubCommand = saveOtherSubCommand(sendBescheidCommand.getId()); + patchAttachedItemCommand = savePatchAttachedItemSubCommand(sendBescheidCommand.getObjectId(ID_FIELD)); + otherSubCommand = saveOtherSubCommand(sendBescheidCommand.getObjectId(ID_FIELD)); otherPatchAttachedItemCommand = saveOtherPatchAttachedItemSubCommand(); expectedCommands = List.of(sendBescheidCommand, buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand), @@ -75,90 +81,163 @@ class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase { otherPatchAttachedItemCommand); } - private PersistedCommand saveSendBescheidCommand() { - var command = CommandTestFactory.createBuilder().id(null).order(SEND_BESCHEID_ORDER).build(); - return mongoOperations.save(command); + private Document saveSendBescheidCommand() { + var command = CommandDocumentTestFactory.createWithOrder(SEND_BESCHEID_ORDER); + return migrationDbTestUtils.saveCommand(command); } - private PersistedCommand savePatchAttachedItemSubCommand(String id) { - Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id); - var command = CommandTestFactory.createBuilder() - .id(null) - .order(PATCH_ATTACHED_ITEM_ORDER) - .bodyObject(itemBody) - .build(); - return mongoOperations.save(command); + private Document savePatchAttachedItemSubCommand(ObjectId id) { + Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id.toString()); + var command = CommandDocumentTestFactory.createWithOrderAndBody(PATCH_ATTACHED_ITEM_ORDER, itemBody); + return migrationDbTestUtils.saveCommand(command); } - private PersistedCommand saveOtherSubCommand(String id) { - Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id); - var command = CommandTestFactory.createBuilder() - .id(null) - .bodyObject(itemBody) - .build(); - return mongoOperations.save(command); + private Document saveOtherSubCommand(ObjectId id) { + Map<String, Object> itemBody = Map.of(PARENT_ID_FIELD, id.toString()); + var command = CommandDocumentTestFactory.createWithBody(itemBody); + return migrationDbTestUtils.saveCommand(command); } - private PersistedCommand saveOtherPatchAttachedItemSubCommand() { - var command = CommandTestFactory.createBuilder() - .id(null) - .order(PATCH_ATTACHED_ITEM_ORDER) - .build(); - return mongoOperations.save(command); + private Document saveOtherPatchAttachedItemSubCommand() { + var command = CommandDocumentTestFactory.createWithOrder(PATCH_ATTACHED_ITEM_ORDER); + return migrationDbTestUtils.saveCommand(command); } - private PersistedCommand buildExpectedPatchAttachedItemCommand(Command patchAttachedItemCommand) { - var body = new HashMap<>(patchAttachedItemCommand.getBodyObject()); + @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); - return CommandTestFactory.createBuilder() - .id(patchAttachedItemCommand.getId()) - .order(patchAttachedItemCommand.getOrder()) - .bodyObject(body) - .build(); + var command = CommandDocumentTestFactory.createWithOrderAndBody(PATCH_ATTACHED_ITEM_ORDER, body); + command.put(ID_FIELD, patchAttachedItemCommand.getObjectId(ID_FIELD)); + return command; } - @Test - void shouldContainSendBescheidCommand() { - migration.doMigration(mongoOperations); + @Nested + class TestGetSendBescheidCommandIds { - var command = mongoOperations.findById(sendBescheidCommand.getId(), PersistedCommand.class); + @Test + void shouldReturnSendBescheidCommandId() { + var ids = migration.getSendBescheidCommandIds(mongoOperations); - assertThat(command).usingRecursiveComparison().isEqualTo(sendBescheidCommand); + assertThat(ids).containsExactly(sendBescheidCommand.getObjectId(ID_FIELD).toString()); + } } - @Test - void shouldModifyPatchAttachedBescheidItem() { - migration.doMigration(mongoOperations); + @Nested + class TestBuildQuery { + + @Test + void shouldQueryPatchAttachedItemCommandsWithParentId() { + var query = migration.buildQuery(List.of(sendBescheidCommand.getObjectId(ID_FIELD).toString())); - var command = mongoOperations.findById(patchAttachedItemCommand.getId(), PersistedCommand.class); + var comands = mongoOperations.find(query, Document.class, MigrationDbTestUtils.COMMAND_COLLECTION); - assertThat(command).usingRecursiveComparison().isEqualTo(buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand)); + assertThat(comands).usingRecursiveFieldByFieldElementComparator().containsExactly(patchAttachedItemCommand); + } } - @Test - void shouldNotModifyOtherSubcommands() { - migration.doMigration(mongoOperations); + @Nested + class TestFullMigration { + @Test + void shouldContainSendBescheidCommand() { + migration.doMigration(mongoOperations); - var command = mongoOperations.findById(otherSubCommand.getId(), PersistedCommand.class); + var command = migrationDbTestUtils.getCommand(sendBescheidCommand.getObjectId(ID_FIELD)); - assertThat(command).usingRecursiveComparison().isEqualTo(otherSubCommand); - } + assertThat(command).usingRecursiveComparison().isEqualTo(sendBescheidCommand); + } - @Test - void shouldNotModifyOtherPatchAttachedItemCommands() { - migration.doMigration(mongoOperations); + @Test + void shouldModifyPatchAttachedBescheidItem() { + migration.doMigration(mongoOperations); - var command = mongoOperations.findById(otherPatchAttachedItemCommand.getId(), PersistedCommand.class); + var command = migrationDbTestUtils.getCommand(patchAttachedItemCommand.getObjectId(ID_FIELD)); - assertThat(command).usingRecursiveComparison().isEqualTo(otherPatchAttachedItemCommand); - } + assertThat(command).usingRecursiveComparison().isEqualTo(buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand)); + } + + @Test + void shouldNotModifyOtherSubcommands() { + migration.doMigration(mongoOperations); + + var command = migrationDbTestUtils.getCommand(otherSubCommand.getObjectId(ID_FIELD)); - @Test - void shouldContainExpectedCommands() { - migration.doMigration(mongoOperations); + assertThat(command).usingRecursiveComparison().isEqualTo(otherSubCommand); + } - var commands = mongoOperations.findAll(PersistedCommand.class); + @Test + void shouldNotModifyOtherPatchAttachedItemCommands() { + migration.doMigration(mongoOperations); + + var command = migrationDbTestUtils.getCommand(otherPatchAttachedItemCommand.getObjectId(ID_FIELD)); + + assertThat(command).usingRecursiveComparison().isEqualTo(otherPatchAttachedItemCommand); + } + + @Test + void shouldContainExpectedCommands() { + migration.doMigration(mongoOperations); + + 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; + } } }