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

OZG-7515 fix migration and add itcase

parent f12f9cc7
Branches
Tags
1 merge request!11Ozg 7515 migrate patch item command
/*
* Copyright (C) 2025 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.vorgang.common.migration;
import java.util.List;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import de.ozgcloud.command.Command;
import de.ozgcloud.vorgang.command.PersistedCommand;
import io.mongock.api.annotations.ChangeUnit;
import io.mongock.api.annotations.Execution;
import io.mongock.api.annotations.RollbackExecution;
......@@ -24,22 +47,23 @@ public class M014_AddItemNameBescheidToPatchAttachedItemCommand { // NOSONAR
private static final String BESCHEID_ITEM_NAME = "Bescheid";
@Execution
public void doMigration(MongoTemplate template) {
var parentIds = getSendBescheidCommandIds(template);
template.aggregate(buildAggregationPipeline(parentIds), Command.COLLECTION_NAME, Document.class);
public void doMigration(MongoOperations mongoOperations) {
var parentIds = getSendBescheidCommandIds(mongoOperations);
updateDocuments(mongoOperations, parentIds);
}
List<String> getSendBescheidCommandIds(MongoTemplate template) {
var commands = template.find(Query.query(Criteria.where(ORDER_FIELD).is(SEND_BESCHEID_ORDER)), Command.class);
List<String> getSendBescheidCommandIds(MongoOperations mongoOperations) {
var commands = mongoOperations.find(Query.query(Criteria.where(ORDER_FIELD).is(SEND_BESCHEID_ORDER)), PersistedCommand.class);
return commands.stream().map(Command::getId).toList();
}
private Aggregation buildAggregationPipeline(List<String> parentIds) {
var matchingCriteria = new Criteria().andOperator(Criteria.where(PARENT_ID_FIELD).in(parentIds),
private void updateDocuments(MongoOperations mongoOperations, List<String> parentIds) {
var query = new Query().addCriteria(new Criteria().andOperator(
Criteria.where(PARENT_ID_FIELD).in(parentIds),
Criteria.where(ORDER_FIELD).is(PATCH_ATTACHED_ITEM_ORDER),
Criteria.where(ITEM_NAME_FILED).exists(false));
return Aggregation.newAggregation(Aggregation.match(matchingCriteria),
Aggregation.addFields().addFieldWithValue(ITEM_NAME_FILED, BESCHEID_ITEM_NAME).build());
Criteria.where(ITEM_NAME_FILED).exists(false)));
var update = new Update().set(ITEM_NAME_FILED, BESCHEID_ITEM_NAME);
mongoOperations.updateMulti(query, update, PersistedCommand.class);
}
@RollbackExecution
......
/*
* Copyright (C) 2025 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.vorgang.common.migration;
import static org.assertj.core.api.Assertions.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
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 de.ozgcloud.common.test.DataITCase;
import de.ozgcloud.vorgang.command.CommandTestFactory;
import de.ozgcloud.vorgang.command.PersistedCommand;
@DataITCase
class M014_AddItemNameBescheidToPatchAttachedItemCommandITCase {
@InjectMocks
private M014_AddItemNameBescheidToPatchAttachedItemCommand migration;
@Autowired
private MigrationDbTestUtils migrationDbTestUtils;
@Autowired
private MongoOperations mongoOperations;
private List<PersistedCommand> expectedCommands;
private PersistedCommand sendBescheidCommand;
private PersistedCommand patchAttachedItemCommand;
private PersistedCommand otherSubCommand;
private PersistedCommand otherPatchAttachedItemCommand;
@BeforeEach
void setUp() {
migrationDbTestUtils.dropCommandCollection();
sendBescheidCommand = saveSendBescheidCommand();
patchAttachedItemCommand = savePatchAttachedItemSubCommand(sendBescheidCommand.getId());
otherSubCommand = saveOtherSubCommand(sendBescheidCommand.getId());
otherPatchAttachedItemCommand = saveOtherPatchAttachedItemSubCommand();
expectedCommands = List.of(sendBescheidCommand,
buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand),
otherSubCommand,
otherPatchAttachedItemCommand);
}
private PersistedCommand saveSendBescheidCommand() {
var command = CommandTestFactory.createBuilder().id(null).order("SEND_BESCHEID").build();
return mongoOperations.save(command);
}
private PersistedCommand savePatchAttachedItemSubCommand(String id) {
Map<String, Object> itemBody = Map.of("parentId", id);
var command = CommandTestFactory.createBuilder()
.id(null)
.order("PATCH_ATTACHED_ITEM")
.bodyObject(itemBody)
.build();
return mongoOperations.save(command);
}
private PersistedCommand saveOtherSubCommand(String id) {
Map<String, Object> itemBody = Map.of("parentId", id);
var command = CommandTestFactory.createBuilder()
.id(null)
.bodyObject(itemBody)
.build();
return mongoOperations.save(command);
}
private PersistedCommand saveOtherPatchAttachedItemSubCommand() {
var command = CommandTestFactory.createBuilder()
.id(null)
.order("PATCH_ATTACHED_ITEM")
.build();
return mongoOperations.save(command);
}
private PersistedCommand buildExpectedPatchAttachedItemCommand(Command patchAttachedItemCommand) {
var body = new HashMap<>(patchAttachedItemCommand.getBodyObject());
body.put("itemName", "Bescheid");
return CommandTestFactory.createBuilder()
.id(patchAttachedItemCommand.getId())
.order(patchAttachedItemCommand.getOrder())
.bodyObject(body)
.build();
}
@Test
void shouldContainSendBescheidCommand() {
migration.doMigration(mongoOperations);
var command = mongoOperations.findById(sendBescheidCommand.getId(), PersistedCommand.class);
assertThat(command).usingRecursiveComparison().isEqualTo(sendBescheidCommand);
}
@Test
void shouldModifyPatchAttachedBescheidItem() {
migration.doMigration(mongoOperations);
var command = mongoOperations.findById(patchAttachedItemCommand.getId(), PersistedCommand.class);
assertThat(command).usingRecursiveComparison().isEqualTo(buildExpectedPatchAttachedItemCommand(patchAttachedItemCommand));
}
@Test
void shouldNotModifyOtherSubcommands() {
migration.doMigration(mongoOperations);
var command = mongoOperations.findById(otherSubCommand.getId(), PersistedCommand.class);
assertThat(command).usingRecursiveComparison().isEqualTo(otherSubCommand);
}
@Test
void shouldNotModifyOtherPatchAttachedItemCommands() {
migration.doMigration(mongoOperations);
var command = mongoOperations.findById(otherPatchAttachedItemCommand.getId(), PersistedCommand.class);
assertThat(command).usingRecursiveComparison().isEqualTo(otherPatchAttachedItemCommand);
}
@Test
void shouldContainExpectedCommands() {
migration.doMigration(mongoOperations);
var commands = mongoOperations.findAll(PersistedCommand.class);
assertThat(commands).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrderElementsOf(expectedCommands);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment