diff --git a/pom.xml b/pom.xml index b31293589..5f09ff105 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ org.gridsuite.modification.server 1.19.0 + **/migration/**/* @@ -210,7 +211,6 @@ org.liquibase liquibase-core - runtime org.springframework.cloud diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java index b8e6bddce..8175e5f04 100644 --- a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java +++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java @@ -235,9 +235,11 @@ public ResponseEntity stashNetworkModifications( @Parameter(description = "Group UUID") @RequestParam("groupUuid") UUID groupUuid, @Parameter(description = "stash or unstash network modifications") @RequestParam(name = "stashed", defaultValue = "true") Boolean stashed) { if (Boolean.TRUE.equals(stashed)) { - networkModificationService.stashNetworkModifications(networkModificationUuids); + networkModificationService.stashNetworkModifications(groupUuid, networkModificationUuids); + networkModificationService.reorderNetworkModifications(groupUuid, Boolean.FALSE); } else { - networkModificationService.restoreNetworkModifications(networkModificationUuids); + networkModificationService.restoreNetworkModifications(groupUuid, networkModificationUuids); + networkModificationService.reorderNetworkModifications(groupUuid, Boolean.TRUE); } return ResponseEntity.ok().build(); } diff --git a/src/main/java/org/gridsuite/modification/server/entities/ModificationGroupEntity.java b/src/main/java/org/gridsuite/modification/server/entities/ModificationGroupEntity.java index 83bd1f5b1..a7c5ec30f 100644 --- a/src/main/java/org/gridsuite/modification/server/entities/ModificationGroupEntity.java +++ b/src/main/java/org/gridsuite/modification/server/entities/ModificationGroupEntity.java @@ -31,7 +31,7 @@ public class ModificationGroupEntity extends AbstractManuallyAssignedIdentifierE mappedBy = "group", cascade = CascadeType.ALL ) - @OrderColumn + @OrderBy("modificationsOrder asc") private List modifications = new ArrayList<>(); public ModificationGroupEntity(UUID uuid) { diff --git a/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java b/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java new file mode 100644 index 000000000..73d59a3ef --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java @@ -0,0 +1,104 @@ +package org.gridsuite.modification.server.migration; + +import liquibase.change.custom.CustomSqlChange; +import liquibase.database.Database; +import liquibase.database.jvm.JdbcConnection; +import liquibase.exception.CustomChangeException; +import liquibase.exception.DatabaseException; +import liquibase.exception.SetupException; +import liquibase.exception.ValidationErrors; +import liquibase.resource.ResourceAccessor; +import liquibase.statement.SqlStatement; +import liquibase.statement.core.UpdateStatement; +import org.apache.commons.lang3.tuple.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.stream.IntStream; + +/** + * @author Etienne Lesot + */ +public class ModificationOrderMigration implements CustomSqlChange { + + private static final Logger LOGGER = LoggerFactory.getLogger(ModificationOrderMigration.class); + + @Override + public SqlStatement[] generateStatements(Database database) throws CustomChangeException { + JdbcConnection connection = (JdbcConnection) database.getConnection(); + List statements = new ArrayList<>(); + try (PreparedStatement stmt = connection.prepareStatement("select distinct group_id from modification where group_id is not null")) { + ResultSet groupIds = stmt.executeQuery(); + while (groupIds.next()) { + UUID groupId = UUID.fromString(groupIds.getString(1)); + reorderNetworkModifications(groupId, true, connection, statements, database); + reorderNetworkModifications(groupId, false, connection, statements, database); + } + } catch (SQLException | DatabaseException e) { + throw new CustomChangeException(e); + } + return statements.toArray(new SqlStatement[0]); + } + + private void reorderNetworkModifications(UUID groupId, boolean stashed, JdbcConnection connection, List statements, Database database) throws CustomChangeException { + List entities = findAllByGroupId(groupId, stashed, connection); + List> entitiesToUpdate = new ArrayList<>(); + if (!entities.isEmpty()) { + if (Boolean.TRUE.equals(stashed)) { + IntStream.range(1, entities.size() + 1) + .forEach(i -> entitiesToUpdate.add(Pair.of(entities.get(i - 1), -i))); + } else { + IntStream.range(0, entities.size()) + .forEach(i -> entitiesToUpdate.add(Pair.of(entities.get(i), i))); + } + } + createMigrationRequests(entitiesToUpdate, statements, database); + } + + private List findAllByGroupId(UUID groupId, boolean stashed, JdbcConnection connection) throws CustomChangeException { + try (PreparedStatement stmt = connection.prepareStatement("SELECT id FROM modification m WHERE m.group_id = ? AND m.stashed = ? order by modifications_order")) { + stmt.setObject(1, groupId); + stmt.setBoolean(2, stashed); + ResultSet resultSet = stmt.executeQuery(); + List entities = new ArrayList<>(); + while (resultSet.next()) { + entities.add(UUID.fromString(resultSet.getString(1))); + } + return entities; + } catch (SQLException | DatabaseException e) { + throw new CustomChangeException(e); + } + } + + private void createMigrationRequests(List> entities, List statements, Database database) { + entities.forEach(pair -> statements.add(new UpdateStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "modification") + .addNewColumnValue("modifications_order", pair.getValue()) + .setWhereClause(String.format("id='%s'", pair.getKey())))); + } + + @Override + public String getConfirmationMessage() { + return "modification order was successfully updated"; + } + + @Override + public void setUp() throws SetupException { + LOGGER.info("Set up migration for modification order"); + } + + @Override + public void setFileOpener(ResourceAccessor resourceAccessor) { + LOGGER.info("Set file opener for modification order"); + } + + @Override + public ValidationErrors validate(Database database) { + return new ValidationErrors(); + } +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 2ccbe0a55..1568bd632 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -9,6 +9,7 @@ import org.gridsuite.modification.server.entities.ModificationEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -27,12 +28,21 @@ public interface ModificationRepository extends JpaRepository findAllBaseByGroupId(UUID uuid); + @Query(value = "SELECT new ModificationEntity(m.id, m.type, m.date, m.stashed, m.activated, m.messageType, m.messageValues) FROM ModificationEntity m WHERE m.group.id = ?1 order by m.modificationsOrder desc") + List findAllBaseByGroupIdReverse(UUID uuid); + + @Query(value = "SELECT m FROM ModificationEntity m WHERE m.group.id = ?1 AND m.stashed = ?2 order by m.modificationsOrder") + List findAllByGroupId(@Param("groupId") UUID groupId, @Param("stashed") Boolean stashed); + @Query(value = "SELECT new ModificationEntity(m.id, m.type) FROM ModificationEntity m WHERE m.id IN (?1)") List findMetadataIn(List uuids); @Query(value = "SELECT m FROM ModificationEntity m WHERE m.id IN (?1) ORDER BY m.modificationsOrder") List findAllByIdIn(List uuids); + @Query(value = "SELECT m FROM ModificationEntity m WHERE m.id IN (?1) ORDER BY m.modificationsOrder desc") + List findAllByIdInReverse(List uuids); + @Query(value = "SELECT cast(modifications_id AS VARCHAR) FROM tabular_modification_modifications WHERE tabular_modification_entity_id = :uuid ORDER BY modifications_order", nativeQuery = true) List findSubModificationIdsByTabularModificationIdOrderByModificationsOrder(UUID uuid); diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index 8c156b2cb..1b56810e7 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -82,16 +82,16 @@ public UUID createNetworkCompositeModification(@NonNull List modificationU } private void saveModificationsNonTransactional(UUID groupUuid, List modifications) { + int order = modificationRepository.countByGroupIdAndStashed(groupUuid, false); var modificationGroupEntity = this.modificationGroupRepository .findById(groupUuid) .orElseGet(() -> modificationGroupRepository.save(new ModificationGroupEntity(groupUuid))); - modifications.forEach(m -> { + for (ModificationEntity m : modifications) { modificationGroupEntity.addModification(m); - // We need here to call the save() method on the modification entity cause the ids of the ModificationEntity's are used in further treatments in the same transaction. - // As we generate the id in Java with @GeneratedValue(strategy = GenerationType.AUTO), saving the entity in the JPA world is enough to generate the id (no need to flush it). - // Without the saving, the id generation would be done only at the flush() and wouldn't be available for the further treatments. - modificationRepository.save(m); - }); + m.setModificationsOrder(order); + order++; + } + modificationRepository.saveAll(modifications); } @Transactional @@ -99,8 +99,10 @@ private void saveModificationsNonTransactional(UUID groupUuid, List moveModifications(UUID destinationGroupUuid, UUID originGroupUuid, List modificationsToMoveUUID, UUID referenceModificationUuid) { // read origin group and modifications ModificationGroupEntity originModificationGroupEntity = getModificationGroup(originGroupUuid); - List originModificationEntities = originModificationGroupEntity.getModifications(); - + List originModificationEntities = originModificationGroupEntity.getModifications() + .stream() + .filter(modificationEntity -> !modificationEntity.getStashed()) + .collect(Collectors.toList()); // To remove null entities when @orderColumn is not a contiguous sequence starting from 0 (to be fixed?) // (there are several places in this file where we filter non-null modification entities) originModificationEntities.removeIf(Objects::isNull); @@ -139,6 +141,9 @@ private void insertModifications(List modificationsList, Lis .findFirst() .orElseThrow(() -> new NetworkModificationException(MOVE_MODIFICATION_ERROR)); modificationsList.addAll(insertionIndex, modificationsToAdd); + for (int order = 0; order < modificationsList.size(); order++) { + modificationsList.get(order).setModificationsOrder(order); + } } private List removeModifications(List modificationsList, List orderedIdsToRemove) { @@ -207,17 +212,19 @@ public List getModifications(UUID groupUuid, boolean onlyMeta } public List getModificationsMetadata(UUID groupUuid, boolean onlyStashed) { - Stream modificationEntityStream = modificationRepository - .findAllBaseByGroupId(getModificationGroup(groupUuid).getId()) - .stream(); if (onlyStashed) { - return modificationEntityStream.filter(m -> m.getStashed()) - .map(this::getModificationInfos) - .collect(Collectors.toList()); + return modificationRepository + .findAllBaseByGroupIdReverse(getModificationGroup(groupUuid).getId()) + .stream() + .filter(ModificationEntity::getStashed) + .map(this::getModificationInfos) + .collect(Collectors.toList()); } else { - return modificationEntityStream - .map(this::getModificationInfos) - .collect(Collectors.toList()); + return modificationRepository + .findAllBaseByGroupId(getModificationGroup(groupUuid).getId()) + .stream() + .map(this::getModificationInfos) + .collect(Collectors.toList()); } } @@ -408,25 +415,48 @@ public List getActiveModificationsInfos(@NonNull UUID groupUu } @Transactional - public void stashNetworkModifications(@NonNull List modificationUuids) { + public void stashNetworkModifications(@NonNull List modificationUuids, int stashedModificationCount) { + int stashModificationOrder = -stashedModificationCount - 1; + List modificationEntities = new ArrayList<>(); for (UUID modificationUuid : modificationUuids) { ModificationEntity modificationEntity = this.modificationRepository .findById(modificationUuid) .orElseThrow(() -> new NetworkModificationException(MODIFICATION_NOT_FOUND, String.format(MODIFICATION_NOT_FOUND_MESSAGE, modificationUuid))); modificationEntity.setStashed(true); - this.modificationRepository.save(modificationEntity); + modificationEntity.setModificationsOrder(stashModificationOrder); + modificationEntities.add(modificationEntity); + stashModificationOrder--; } + this.modificationRepository.saveAll(modificationEntities); } @Transactional - public void restoreNetworkModifications(@NonNull List modificationUuids) { - for (UUID modificationUuid : modificationUuids) { - ModificationEntity modificationEntity = this.modificationRepository - .findById(modificationUuid) - .orElseThrow(() -> new NetworkModificationException(MODIFICATION_NOT_FOUND, String.format(MODIFICATION_NOT_FOUND_MESSAGE, modificationUuid))); - modificationEntity.setStashed(false); - this.modificationRepository.save(modificationEntity); + public void reorderNetworkModifications(UUID groupId, Boolean stashed) { + List entities = this.modificationRepository.findAllByGroupId(groupId, stashed); + if (!entities.isEmpty()) { + if (Boolean.TRUE.equals(stashed)) { + IntStream.range(1, entities.size() + 1) + .forEach(i -> entities.get(i - 1).setModificationsOrder(-i)); + } else { + IntStream.range(0, entities.size()) + .forEach(i -> entities.get(i).setModificationsOrder(i)); + } + } + this.modificationRepository.saveAll(entities); + } + + @Transactional + public void restoreNetworkModifications(@NonNull List modificationUuids, int unstashedSize) { + int modificationOrder = unstashedSize; + List modifications = modificationRepository.findAllByIdInReverse(modificationUuids); + if (modifications.size() != modificationUuids.size()) { + throw new NetworkModificationException(MODIFICATION_NOT_FOUND); + } + for (ModificationEntity modification : modifications) { + modification.setStashed(false); + modification.setModificationsOrder(modificationOrder++); } + this.modificationRepository.saveAll(modifications); } @Transactional diff --git a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java index b3d21d658..2ee61becf 100644 --- a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java +++ b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java @@ -118,13 +118,19 @@ public void updateNetworkModificationActivation(@NonNull List modification } @Transactional - public void stashNetworkModifications(@NonNull List modificationUuids) { - networkModificationRepository.stashNetworkModifications(modificationUuids); + public void stashNetworkModifications(UUID groupUuid, @NonNull List modificationUuids) { + networkModificationRepository.stashNetworkModifications(modificationUuids, networkModificationRepository.getModificationsCount(groupUuid, true)); } @Transactional - public void restoreNetworkModifications(@NonNull List modificationUuids) { - networkModificationRepository.restoreNetworkModifications(modificationUuids); + public void reorderNetworkModifications(UUID groupId, Boolean stashed) { + networkModificationRepository.reorderNetworkModifications(groupId, stashed); + } + + @Transactional + public void restoreNetworkModifications(UUID groupUuid, @NonNull List modificationUuids) { + networkModificationRepository.restoreNetworkModifications(modificationUuids, + networkModificationRepository.getModificationsCount(groupUuid, false)); } // No transactional because we need to save modification in DB also in case of error diff --git a/src/main/resources/db/changelog/changesets/changelog_20241015T130742Z.xml b/src/main/resources/db/changelog/changesets/changelog_20241015T130742Z.xml new file mode 100644 index 000000000..da8d73207 --- /dev/null +++ b/src/main/resources/db/changelog/changesets/changelog_20241015T130742Z.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 4ef96dc04..32d3e8bde 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -315,3 +315,6 @@ databaseChangeLog: - include: relativeToChangelogFile: true file: changesets/changelog_20240912T130742Z.xml + - include: + relativeToChangelogFile: true + file: changesets/changelog_20241015T130742Z.xml \ No newline at end of file diff --git a/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java b/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java index 82a98cfce..7f27ec3ad 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/tabularmodifications/TabularGeneratorModificationsTest.java @@ -156,7 +156,7 @@ public void testSqlRequestsCountOnPostGroups() throws Exception { reset(); ApiUtils.postGroups(mockMvc, getGroupId(), targetGroupUuid); - TestUtils.assertRequestsCount(9, 8, 2, 0); // (13, 8, 2, 0) before improvements + TestUtils.assertRequestsCount(10, 8, 1, 0); // (13, 8, 2, 0) before improvements assertTabularModificationsEquals(modifications, targetGroupUuid); } @@ -167,7 +167,7 @@ public void testSqlRequestsCountOnPostGroups2() throws Exception { reset(); ApiUtils.postGroups(mockMvc, getGroupId(), targetGroupUuid); - TestUtils.assertRequestsCount(15, 9, 2, 0); // (95, 9, 2, 0) before improvements, why one additional insert ? It feels batch_size is limited at 100 for insertions and is it reached for reactive_capability_curve_points + TestUtils.assertRequestsCount(16, 9, 1, 0); // (95, 9, 2, 0) before improvements, why one additional insert ? It feels batch_size is limited at 100 for insertions and is it reached for reactive_capability_curve_points assertTabularModificationsEquals(modifications, targetGroupUuid); } @@ -209,7 +209,7 @@ public void testSqlRequestsCountOnPutGroupsDuplications() throws Exception { reset(); ApiUtils.putGroupsDuplications(mockMvc, getGroupId(), targetGroupUuid, getNetworkId()); - TestUtils.assertRequestsCount(9, 8, 2, 0); // (19, 8, 2, 0) before improvements + TestUtils.assertRequestsCount(10, 8, 1, 0); // (19, 8, 2, 0) before improvements assertTabularModificationsEquals(modifications, targetGroupUuid); } @@ -220,7 +220,7 @@ public void testSqlRequestsCountOnPutGroupsDuplications2() throws Exception { reset(); ApiUtils.putGroupsDuplications(mockMvc, getGroupId(), targetGroupUuid, getNetworkId()); - TestUtils.assertRequestsCount(15, 9, 2, 0); // (107, 9, 2, 0) before improvements, why one additional insert ? It feels batch_size is limited at 100 for insertions and is it reached for reactive_capability_curve_points + TestUtils.assertRequestsCount(16, 9, 1, 0); // (107, 9, 2, 0) before improvements, why one additional insert ? It feels batch_size is limited at 100 for insertions and is it reached for reactive_capability_curve_points assertTabularModificationsEquals(modifications, targetGroupUuid); } @@ -261,7 +261,7 @@ public void testSqlRequestsCountOnPutGroupsWithCopy() throws Exception { reset(); ApiUtils.putGroupsWithCopy(mockMvc, targetGroupUuid, modifications.stream().map(Pair::getLeft).toList(), getNetworkId()); - TestUtils.assertRequestsCount(8, 8, 2, 0); // (14, 8, 2, 0) before improvements + TestUtils.assertRequestsCount(9, 8, 1, 0); // (14, 8, 2, 0) before improvements assertTabularModificationsEquals(modifications, targetGroupUuid); } @@ -272,7 +272,7 @@ public void testSqlRequestsCountOnPutGroupsWithCopy2() throws Exception { reset(); ApiUtils.putGroupsWithCopy(mockMvc, targetGroupUuid, modifications.stream().map(Pair::getLeft).toList(), getNetworkId()); - TestUtils.assertRequestsCount(14, 9, 2, 0); // (26, 9, 2, 0) before improvements, why one additional insert ? It feels batch_size is limited at 100 for insertions and is it reached for reactive_capability_curve_points + TestUtils.assertRequestsCount(15, 9, 1, 0); // (26, 9, 2, 0) before improvements, why one additional insert ? It feels batch_size is limited at 100 for insertions and is it reached for reactive_capability_curve_points assertTabularModificationsEquals(modifications, targetGroupUuid); } @@ -464,7 +464,7 @@ public void testSqlRequestsCountOnDeleteNetworkModificationsByIdsInGroup() throw reset(); ApiUtils.deleteNetworkModificationsInGroup(mockMvc, getGroupId(), List.of(modifications.get(0).getLeft())); // removing only 1 tabular modification in the group // It is actually (3, 0, 1, 7) because deletes made in the native query are not counted - TestUtils.assertRequestsCount(3, 0, 1, 0); + TestUtils.assertRequestsCount(3, 0, 0, 0); } @Test @@ -474,7 +474,7 @@ public void testSqlRequestsCountOnDeleteNetworkModificationsByIdsInGroup2() thro reset(); ApiUtils.deleteNetworkModificationsInGroup(mockMvc, getGroupId(), modifications.subList(0, 3).stream().map(Pair::getLeft).toList()); // removing only 3 tabular modification in the group // It is actually (5, 0, 1, 21) because deletes made in the native query are not counted - TestUtils.assertRequestsCount(5, 0, 1, 0); + TestUtils.assertRequestsCount(5, 0, 0, 0); } /* diff --git a/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java b/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java index 643898006..84f4c033c 100644 --- a/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java +++ b/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java @@ -194,7 +194,7 @@ public void testCreateModificationQueryCount() { var modifEntity2 = EquipmentAttributeModificationInfos.builder().equipmentId("id2").equipmentAttributeName("attribute").equipmentAttributeValue("foo").equipmentType(IdentifiableType.VOLTAGE_LEVEL).build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(modifEntity1, modifEntity2)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); } @Test @@ -230,7 +230,7 @@ public void testDeleteModificationQueryCount() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(modifEntity1.getId())); - assertRequestsCount(3, 0, 1, 2); + assertRequestsCount(3, 0, 0, 2); SQLStatementCountValidator.reset(); networkModificationRepository.deleteModificationGroup(TEST_GROUP_ID, true); @@ -249,7 +249,7 @@ public void testLoadCreation() { var createLoadEntity3 = LoadCreationInfos.builder().equipmentId("idLoad3").equipmentName("nameLoad3").loadType(LoadType.FICTITIOUS).voltageLevelId("vlId3").busOrBusbarSectionId("busId3").p0(50.).q0(90.).connectionName("top2").connectionDirection(ConnectablePosition.Direction.TOP).connectionPosition(12).build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(createLoadEntity1, createLoadEntity2, createLoadEntity3)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(3, modificationInfos.size()); @@ -266,7 +266,7 @@ public void testLoadCreation() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(createLoadEntity2.getId(), createLoadEntity3.getId())); - assertRequestsCount(4, 0, 1, 2); + assertRequestsCount(4, 0, 0, 2); SQLStatementCountValidator.reset(); assertEquals(1, networkModificationRepository.getModifications(TEST_GROUP_ID, true, true).size()); @@ -331,7 +331,7 @@ public void testGeneratorCreation() { .connectionPosition(3).build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(createGeneratorEntity1, createGeneratorEntity2, createGeneratorEntity3)); - assertRequestsCount(1, 4, 1, 0); + assertRequestsCount(2, 4, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(3, modificationInfos.size()); @@ -348,7 +348,7 @@ public void testGeneratorCreation() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(createGeneratorEntity2.getId(), createGeneratorEntity3.getId())); - assertRequestsCount(4, 0, 1, 3); + assertRequestsCount(4, 0, 0, 3); SQLStatementCountValidator.reset(); assertEquals(1, networkModificationRepository.getModifications(TEST_GROUP_ID, true, true).size()); @@ -384,7 +384,7 @@ public void testShuntCompensatorCreation() { var createShuntCompensatorEntity2 = shunt2.toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(createShuntCompensatorEntity1, createShuntCompensatorEntity2)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(2, modificationInfos.size()); @@ -399,7 +399,7 @@ public void testShuntCompensatorCreation() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(createShuntCompensatorEntity2.getId())); - assertRequestsCount(3, 0, 1, 2); + assertRequestsCount(3, 0, 0, 2); SQLStatementCountValidator.reset(); assertEquals(1, networkModificationRepository.getModifications(TEST_GROUP_ID, true, true).size()); @@ -422,7 +422,7 @@ public void testLineCreation() { var createLineEntity4 = LineCreationInfos.builder().equipmentId("idLine4").equipmentName("nameLine4").r(3.0).x(3.3).g1(null).b1(null).g2(null).b2(null).voltageLevelId1("vlId41").busOrBusbarSectionId1("busId41").voltageLevelId2("vlId42").busOrBusbarSectionId2("busId42").connectionName1("cn77").connectionDirection1(ConnectablePosition.Direction.TOP).connectionName2("cn88").connectionDirection2(ConnectablePosition.Direction.BOTTOM).currentLimits1(CurrentLimitsInfos.builder().permanentLimit(5.0).temporaryLimits(Collections.emptyList()).build()).currentLimits2(CurrentLimitsInfos.builder().permanentLimit(4.0).temporaryLimits(Collections.emptyList()).build()).build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(createLineEntity1, createLineEntity2, createLineEntity3, createLineEntity4)); - assertRequestsCount(1, 4, 1, 0); + assertRequestsCount(2, 4, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(4, modificationInfos.size()); @@ -441,7 +441,7 @@ public void testLineCreation() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(createLineEntity2.getId(), createLineEntity3.getId())); - assertRequestsCount(6, 0, 1, 7); + assertRequestsCount(6, 0, 0, 7); SQLStatementCountValidator.reset(); assertEquals(2, networkModificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); @@ -480,13 +480,13 @@ public void testMoveModificationInSameGroup() { List modificationEntities = List.of(groovyScriptEntity1, groovyScriptEntity2, groovyScriptEntity3, groovyScriptEntity4, groovyScriptEntity5, tabularModificationEntity); networkModificationRepository.saveModifications(TEST_GROUP_ID, modificationEntities); - assertRequestsCount(1, 8, 1, 0); + assertRequestsCount(2, 8, 0, 0); var modificationOriginal = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); SQLStatementCountValidator.reset(); networkModificationRepository.moveModifications(TEST_GROUP_ID, TEST_GROUP_ID, List.of(tabularModificationEntity.getId()), groovyScriptEntity2.getId()); - assertRequestsCount(2, 0, 1, 0); + assertRequestsCount(2, 0, 2, 0); var modification = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); // [0:1, 1:6, 2:2, 3:3, 4:4 ,5:5 ] @@ -497,7 +497,7 @@ public void testMoveModificationInSameGroup() { SQLStatementCountValidator.reset(); networkModificationRepository.moveModifications(TEST_GROUP_ID, TEST_GROUP_ID, List.of(groovyScriptEntity3.getId(), tabularModificationEntity.getId()), null); - assertRequestsCount(2, 0, 1, 0); + assertRequestsCount(2, 0, 2, 0); // [0:1, 1:2, 2:4, 3:5, 4:6, 5:3 ] modification = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); @@ -518,11 +518,11 @@ public void testMoveModificationsBetweenTwoGroups() { networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1, groovyScriptEntity2, groovyScriptEntity3, groovyScriptEntity4)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); SQLStatementCountValidator.reset(); networkModificationRepository.saveModifications(TEST_GROUP_ID_2, List.of(groovyScriptEntity5, groovyScriptEntity6)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); var modificationOriginal1 = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); var modificationOriginal2 = networkModificationRepository.getModifications(TEST_GROUP_ID_2, true, true); @@ -531,7 +531,7 @@ public void testMoveModificationsBetweenTwoGroups() { List uuidsToMove = List.of(groovyScriptEntity2.getId(), groovyScriptEntity3.getId()); List movedModifications = networkModificationRepository.moveModifications(TEST_GROUP_ID_2, TEST_GROUP_ID, uuidsToMove, null); assertEquals(uuidsToMove.size(), movedModifications.size()); - assertRequestsCount(4, 0, 2, 0); + assertRequestsCount(4, 0, 1, 0); var modification1 = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); var modification2 = networkModificationRepository.getModifications(TEST_GROUP_ID_2, true, true); @@ -547,7 +547,7 @@ public void testMoveModificationsBetweenTwoGroups() { uuidsToMove = List.of(expected2.get(0).getUuid(), expected2.get(1).getUuid()); movedModifications = networkModificationRepository.moveModifications(TEST_GROUP_ID_3, TEST_GROUP_ID_2, uuidsToMove, null); assertEquals(uuidsToMove.size(), movedModifications.size()); - assertRequestsCount(3, 1, 3, 0); + assertRequestsCount(3, 1, 1, 0); modification2 = networkModificationRepository.getModifications(TEST_GROUP_ID_2, true, true); var modification3 = networkModificationRepository.getModifications(TEST_GROUP_ID_3, true, true); @@ -570,11 +570,11 @@ public void testMoveModificationsBetweenTwoGroupsWithReferenceNode() { networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1, groovyScriptEntity2, groovyScriptEntity3, groovyScriptEntity4)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); SQLStatementCountValidator.reset(); networkModificationRepository.saveModifications(TEST_GROUP_ID_2, List.of(groovyScriptEntity5, groovyScriptEntity6)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); var modificationOriginal1 = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); var modificationOriginal2 = networkModificationRepository.getModifications(TEST_GROUP_ID_2, true, true); @@ -583,7 +583,7 @@ public void testMoveModificationsBetweenTwoGroupsWithReferenceNode() { List uuidsToMove = List.of(groovyScriptEntity2.getId(), groovyScriptEntity3.getId()); List movedModifications = networkModificationRepository.moveModifications(TEST_GROUP_ID_2, TEST_GROUP_ID, uuidsToMove, groovyScriptEntity6.getId()); assertEquals(uuidsToMove.size(), movedModifications.size()); - assertRequestsCount(4, 0, 2, 0); + assertRequestsCount(4, 0, 1, 0); var modification1 = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); var modification2 = networkModificationRepository.getModifications(TEST_GROUP_ID_2, true, true); @@ -605,15 +605,15 @@ public void testMoveModificationsBetweenMoreThanTwoGroups() { var groovyScriptEntity6 = GroovyScriptInfos.builder().script("script6").build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1, groovyScriptEntity2)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); SQLStatementCountValidator.reset(); networkModificationRepository.saveModifications(TEST_GROUP_ID_2, List.of(groovyScriptEntity3, groovyScriptEntity4)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); SQLStatementCountValidator.reset(); networkModificationRepository.saveModifications(TEST_GROUP_ID_3, List.of(groovyScriptEntity5, groovyScriptEntity6)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); var modificationOriginal1 = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); var modificationOriginal2 = networkModificationRepository.getModifications(TEST_GROUP_ID_2, true, true); @@ -623,7 +623,7 @@ public void testMoveModificationsBetweenMoreThanTwoGroups() { SQLStatementCountValidator.reset(); List modificationsToMoveUuid = List.of(groovyScriptEntity1.getId(), UUID.randomUUID()); List movedModifications = networkModificationRepository.moveModifications(TEST_GROUP_ID_3, TEST_GROUP_ID, modificationsToMoveUuid, null); - assertRequestsCount(4, 0, 2, 0); + assertRequestsCount(4, 0, 1, 0); // only the valid modification is moved assertEquals(1, movedModifications.size()); assertEquals(groovyScriptEntity1.getId(), movedModifications.get(0).getId()); @@ -666,7 +666,7 @@ public void testGroovyScript() { var groovyScriptEntity3 = GroovyScriptInfos.builder().script("script3").build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(groovyScriptEntity1, groovyScriptEntity2, groovyScriptEntity3)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, false, true); assertEquals(3, modificationInfos.size()); @@ -683,7 +683,7 @@ public void testGroovyScript() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(groovyScriptEntity2.getId(), groovyScriptEntity3.getId())); - assertRequestsCount(2, 0, 1, 2); + assertRequestsCount(2, 0, 0, 2); SQLStatementCountValidator.reset(); assertEquals(1, networkModificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); @@ -720,7 +720,7 @@ public void testSubstationCreation() { .build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(createSubstationEntity1, createSubstationEntity2, createSubstationEntity3)); - assertRequestsCount(1, 4, 2, 0); + assertRequestsCount(2, 4, 1, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, false, true); assertEquals(3, modificationInfos.size()); @@ -737,7 +737,7 @@ public void testSubstationCreation() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(createSubstationEntity2.getId(), createSubstationEntity3.getId())); - assertRequestsCount(4, 0, 1, 2); + assertRequestsCount(4, 0, 0, 2); SQLStatementCountValidator.reset(); assertEquals(1, networkModificationRepository.getModifications(TEST_GROUP_ID, false, true).size()); @@ -817,7 +817,7 @@ public void testStatusLineModification() { ); networkModificationRepository.saveModifications(TEST_GROUP_ID, entities); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, false, true) .stream() @@ -949,7 +949,7 @@ public void testLineAttachToVoltageLevel() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(lineAttachToEntity1.getId(), lineAttachToEntity2.getId())); - assertRequestsCount(5, 0, 0, 12); + assertRequestsCount(6, 0, 0, 12); SQLStatementCountValidator.reset(); networkModificationRepository.deleteModificationGroup(TEST_GROUP_ID, true); @@ -1210,7 +1210,7 @@ public void testVoltageInitModification() { .build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(voltageInitModificationEntity)); - assertRequestsCount(1, 9, 1, 0); + assertRequestsCount(2, 9, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(1, modificationInfos.size()); @@ -1238,7 +1238,7 @@ public void testVscModification() { .build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(vscModificationEntity)); - assertRequestsCount(1, 5, 1, 0); + assertRequestsCount(2, 5, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(1, modificationInfos.size()); @@ -1276,7 +1276,7 @@ public void testModificationOrder() { // trick: move it too, to see the order in the entity movedEntities = networkModificationRepository.moveModifications(TEST_GROUP_ID_2, TEST_GROUP_ID, List.of(modifEntity2.getId()), null); assertEquals(1, movedEntities.size()); - assertEquals(0, movedEntities.get(0).getModificationsOrder()); + assertEquals(1, movedEntities.get(0).getModificationsOrder()); } @Test @@ -1316,7 +1316,7 @@ public void testStaticVarCompensatorCreation() { .build().toEntity(); networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(createStaticVarCompensator1, createStaticVarCompensator2, createStaticVarCompensator3)); - assertRequestsCount(1, 3, 1, 0); + assertRequestsCount(2, 3, 0, 0); List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true); assertEquals(3, modificationInfos.size()); @@ -1331,7 +1331,7 @@ public void testStaticVarCompensatorCreation() { SQLStatementCountValidator.reset(); networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(createStaticVarCompensator2.getId(), createStaticVarCompensator3.getId())); - assertRequestsCount(4, 0, 1, 2); + assertRequestsCount(4, 0, 0, 2); SQLStatementCountValidator.reset(); assertEquals(1, networkModificationRepository.getModifications(TEST_GROUP_ID, true, true).size());