diff --git a/src/main/java/org/gridsuite/modification/server/dto/formula/ReferenceFieldOrValue.java b/src/main/java/org/gridsuite/modification/server/dto/formula/ReferenceFieldOrValue.java index 018d3fe9b..2b67b40d1 100644 --- a/src/main/java/org/gridsuite/modification/server/dto/formula/ReferenceFieldOrValue.java +++ b/src/main/java/org/gridsuite/modification/server/dto/formula/ReferenceFieldOrValue.java @@ -11,6 +11,7 @@ import com.powsybl.iidm.network.Generator; import com.powsybl.iidm.network.Identifiable; import com.powsybl.iidm.network.IdentifiableType; +import com.powsybl.iidm.network.ShuntCompensator; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -19,6 +20,7 @@ import org.gridsuite.modification.server.NetworkModificationException; import org.gridsuite.modification.server.dto.formula.equipmentfield.BatteryField; import org.gridsuite.modification.server.dto.formula.equipmentfield.GeneratorField; +import org.gridsuite.modification.server.dto.formula.equipmentfield.ShuntCompensatorField; /** * @author Seddik Yengui @@ -48,6 +50,7 @@ public Double getRefOrValue(Identifiable identifiable) { Double referenceValue = switch (identifiableType) { case GENERATOR -> GeneratorField.getReferenceValue((Generator) identifiable, equipmentField); case BATTERY -> BatteryField.getReferenceValue((Battery) identifiable, equipmentField); + case SHUNT_COMPENSATOR -> ShuntCompensatorField.getReferenceValue((ShuntCompensator) identifiable, equipmentField); default -> throw new NetworkModificationException(NetworkModificationException.Type.BY_FORMULA_MODIFICATION_ERROR, String.format("Unsupported equipment type : %s", identifiableType.name())); }; diff --git a/src/main/java/org/gridsuite/modification/server/dto/formula/equipmentfield/ShuntCompensatorField.java b/src/main/java/org/gridsuite/modification/server/dto/formula/equipmentfield/ShuntCompensatorField.java new file mode 100644 index 000000000..e155d4720 --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/dto/formula/equipmentfield/ShuntCompensatorField.java @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.gridsuite.modification.server.dto.formula.equipmentfield; + +import com.powsybl.iidm.network.ShuntCompensator; +import com.powsybl.iidm.network.ShuntCompensatorLinearModel; +import com.powsybl.iidm.network.ShuntCompensatorModelType; +import com.powsybl.iidm.network.VoltageLevel; +import org.gridsuite.modification.server.NetworkModificationException; + +/** + * @author Seddik Yengui + */ + +public enum ShuntCompensatorField { + MAXIMUM_SECTION_COUNT, + SECTION_COUNT, + MAXIMUM_SUSCEPTANCE, + MAXIMUM_Q_AT_NOMINAL_VOLTAGE; + + public static Double getReferenceValue(ShuntCompensator shuntCompensator, String shuntCompensatorField) { + VoltageLevel voltageLevel = shuntCompensator.getTerminal().getVoltageLevel(); + ShuntCompensatorField field = ShuntCompensatorField.valueOf(shuntCompensatorField); + return switch (field) { + case MAXIMUM_SECTION_COUNT -> (double) shuntCompensator.getMaximumSectionCount(); + case SECTION_COUNT -> (double) shuntCompensator.getSectionCount(); + case MAXIMUM_SUSCEPTANCE -> shuntCompensator.getB() * shuntCompensator.getMaximumSectionCount(); + case MAXIMUM_Q_AT_NOMINAL_VOLTAGE -> Math.abs(Math.pow(voltageLevel.getNominalV(), 2) * shuntCompensator.getB()) * shuntCompensator.getMaximumSectionCount(); + }; + } + + public static void setNewValue(ShuntCompensator shuntCompensator, String shuntCompensatorField, Double newValue) { + if (shuntCompensator.getModelType() != ShuntCompensatorModelType.LINEAR) { + throw new NetworkModificationException(NetworkModificationException.Type.BY_FORMULA_MODIFICATION_ERROR, + String.format("Shunt compensator with %s model is not supported", shuntCompensator.getModelType())); + } + ShuntCompensatorLinearModel model = shuntCompensator.getModel(ShuntCompensatorLinearModel.class); + ShuntCompensatorField field = ShuntCompensatorField.valueOf(shuntCompensatorField); + VoltageLevel voltageLevel = shuntCompensator.getTerminal().getVoltageLevel(); + switch (field) { + case MAXIMUM_SECTION_COUNT -> { + int maximumSectionCount = newValue.intValue(); + model.setBPerSection(model.getBPerSection() * shuntCompensator.getMaximumSectionCount() / maximumSectionCount); + model.setMaximumSectionCount(maximumSectionCount); + } + case SECTION_COUNT -> shuntCompensator.setSectionCount(newValue.intValue()); + case MAXIMUM_SUSCEPTANCE -> model.setBPerSection(newValue / shuntCompensator.getMaximumSectionCount()); + case MAXIMUM_Q_AT_NOMINAL_VOLTAGE -> { + double newQatNominalV = newValue / shuntCompensator.getMaximumSectionCount(); + double newSusceptancePerSection = newQatNominalV / Math.pow(voltageLevel.getNominalV(), 2); + model.setBPerSection(newSusceptancePerSection); + } + } + } +} diff --git a/src/main/java/org/gridsuite/modification/server/modifications/ByFormulaModification.java b/src/main/java/org/gridsuite/modification/server/modifications/ByFormulaModification.java index b64ce2f58..94bbde685 100644 --- a/src/main/java/org/gridsuite/modification/server/modifications/ByFormulaModification.java +++ b/src/main/java/org/gridsuite/modification/server/modifications/ByFormulaModification.java @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + package org.gridsuite.modification.server.modifications; import com.powsybl.commons.reporter.Report; @@ -7,6 +14,7 @@ import com.powsybl.iidm.network.Generator; import com.powsybl.iidm.network.Identifiable; import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.ShuntCompensator; import org.gridsuite.modification.server.NetworkModificationException; import org.gridsuite.modification.server.dto.ByFormulaModificationInfos; import org.gridsuite.modification.server.dto.FilterEquipments; @@ -15,6 +23,7 @@ import org.gridsuite.modification.server.dto.formula.Operator; import org.gridsuite.modification.server.dto.formula.equipmentfield.BatteryField; import org.gridsuite.modification.server.dto.formula.equipmentfield.GeneratorField; +import org.gridsuite.modification.server.dto.formula.equipmentfield.ShuntCompensatorField; import org.gridsuite.modification.server.service.FilterService; import org.jetbrains.annotations.Nullable; import org.springframework.util.CollectionUtils; @@ -143,12 +152,9 @@ private void applyFormula(Network network, Double value2 = formulaInfos.getFieldOrValue2().getRefOrValue(identifiable); final Double newValue = applyOperation(formulaInfos.getOperator(), value1, value2); switch (identifiable.getType()) { - case GENERATOR -> GeneratorField.setNewValue((Generator) identifiable, - formulaInfos.getEditedField(), - newValue); - case BATTERY -> BatteryField.setNewValue((Battery) identifiable, - formulaInfos.getEditedField(), - newValue); + case GENERATOR -> GeneratorField.setNewValue((Generator) identifiable, formulaInfos.getEditedField(), newValue); + case BATTERY -> BatteryField.setNewValue((Battery) identifiable, formulaInfos.getEditedField(), newValue); + case SHUNT_COMPENSATOR -> ShuntCompensatorField.setNewValue((ShuntCompensator) identifiable, formulaInfos.getEditedField(), newValue); default -> throw new NetworkModificationException(NetworkModificationException.Type.BY_FORMULA_MODIFICATION_ERROR, "Unsupported equipment"); } diff --git a/src/test/java/org/gridsuite/modification/server/modifications/AbstractByFormulaModificationTest.java b/src/test/java/org/gridsuite/modification/server/modifications/AbstractByFormulaModificationTest.java index 5b2ca81f7..5bb167667 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/AbstractByFormulaModificationTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/AbstractByFormulaModificationTest.java @@ -45,15 +45,67 @@ @Tag("IntegrationTest") public abstract class AbstractByFormulaModificationTest extends AbstractNetworkModificationTest { + protected static final UUID FILTER_ID_1 = UUID.randomUUID(); + protected static final UUID FILTER_ID_2 = UUID.randomUUID(); + protected static final UUID FILTER_ID_3 = UUID.randomUUID(); + protected static final UUID FILTER_ID_4 = UUID.randomUUID(); + protected static final UUID FILTER_ID_5 = UUID.randomUUID(); + protected static final UUID FILTER_WITH_ALL_WRONG_IDS = UUID.randomUUID(); + protected static final UUID FILTER_WITH_ONE_WRONG_ID = UUID.randomUUID(); + protected final FilterInfos filter1 = new FilterInfos(FILTER_ID_1, "filter1"); + protected final FilterInfos filter2 = new FilterInfos(FILTER_ID_2, "filter2"); + protected final FilterInfos filter3 = new FilterInfos(FILTER_ID_3, "filter3"); + protected final FilterInfos filter4 = new FilterInfos(FILTER_ID_4, "filter4"); + protected final FilterInfos filter5 = new FilterInfos(FILTER_ID_5, "filter5"); + protected final FilterInfos filterWithAllWrongId = new FilterInfos(FILTER_WITH_ALL_WRONG_IDS, "filterWithAllWrongId"); + protected final FilterInfos filterWithOneWrongId = new FilterInfos(FILTER_WITH_ONE_WRONG_ID, "filterWithOneWrongId"); + public static final String PATH = "/v1/filters/export"; @Before public void specificSetUp() { FilterService.setFilterServerBaseUri(wireMockServer.baseUrl()); + getNetwork().getVariantManager().setWorkingVariant("variant_1"); createEquipments(); } + protected void checkCreateWithWarning(List formulaInfos, List existingEquipmentList) throws Exception { + FilterEquipments filter = getFilterEquipments(FILTER_WITH_ONE_WRONG_ID, "filterWithWrongId", existingEquipmentList, List.of("wrongId")); + + UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + FILTER_WITH_ONE_WRONG_ID)) + .willReturn(WireMock.ok() + .withBody(mapper.writeValueAsString(List.of(filter))) + .withHeader("Content-Type", "application/json"))).getId(); + + ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder() + .formulaInfosList(formulaInfos) + .identifiableType(getIdentifiableType()) + .build(); + + checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_WARNINGS); + + wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(FILTER_WITH_ONE_WRONG_ID)), false); + } + + protected void checkCreateWithError(List formulaInfos) throws Exception { + FilterEquipments filter = getFilterEquipments(FILTER_WITH_ALL_WRONG_IDS, "filterWithWrongId", List.of(), List.of("wrongId1", "wrongId2")); + + UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + FILTER_WITH_ALL_WRONG_IDS)) + .willReturn(WireMock.ok() + .withBody(mapper.writeValueAsString(List.of(filter))) + .withHeader("Content-Type", "application/json"))).getId(); + + ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder() + .formulaInfosList(formulaInfos) + .identifiableType(getIdentifiableType()) + .build(); + + checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_ERRORS); + + wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(FILTER_WITH_ALL_WRONG_IDS)), false); + } + @Test @Override public void testCreate() throws Exception { @@ -66,7 +118,6 @@ public void testCreate() throws Exception { super.testCreate(); wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), filters.stream().map(FilterEquipments::getFilterId).collect(Collectors.toList())), false); - } @Test @@ -84,7 +135,7 @@ public void testCopy() throws Exception { wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), filters.stream().map(FilterEquipments::getFilterId).collect(Collectors.toList())), false); } - void checkCreationApplicationStatus(ByFormulaModificationInfos byFormulaModificationInfos, + private void checkCreationApplicationStatus(ByFormulaModificationInfos byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus applicationStatus) throws Exception { String modificationToCreateJson = mapper.writeValueAsString(byFormulaModificationInfos); @@ -104,7 +155,7 @@ protected Network createNetwork(UUID networkUuid) { @Override protected ByFormulaModificationInfos buildModification() { return ByFormulaModificationInfos.builder() - .identifiableType(IdentifiableType.GENERATOR) + .identifiableType(getIdentifiableType()) .formulaInfosList(getFormulaInfos()) .stashed(false) .build(); @@ -113,21 +164,21 @@ protected ByFormulaModificationInfos buildModification() { @Override protected ByFormulaModificationInfos buildModificationUpdate() { return ByFormulaModificationInfos.builder() - .identifiableType(IdentifiableType.GENERATOR) + .identifiableType(getIdentifiableType()) .formulaInfosList(getUpdatedFormulaInfos()) .stashed(false) .build(); } - IdentifiableAttributes getIdentifiableAttributes(String id, Double distributionKey) { + protected IdentifiableAttributes getIdentifiableAttributes(String id, Double distributionKey) { return IdentifiableAttributes.builder() .id(id) - .type(IdentifiableType.GENERATOR) + .type(getIdentifiableType()) .distributionKey(distributionKey) .build(); } - FilterEquipments getFilterEquipments(UUID filterID, String filterName, List identifiableAttributes, List notFoundEquipments) { + protected FilterEquipments getFilterEquipments(UUID filterID, String filterName, List identifiableAttributes, List notFoundEquipments) { return FilterEquipments.builder() .filterId(filterID) .filterName(filterName) @@ -136,7 +187,7 @@ FilterEquipments getFilterEquipments(UUID filterID, String filterName, List filters, Operator operator, ReferenceFieldOrValue fieldOrValue1, @@ -150,7 +201,7 @@ FormulaInfos getFormulaInfo(String editedField, .build(); } - Map handleQueryParams(UUID networkUuid, List filterIds) { + private Map handleQueryParams(UUID networkUuid, List filterIds) { return Map.of("networkUuid", WireMock.equalTo(String.valueOf(networkUuid)), "variantId", WireMock.equalTo("variant_1"), "ids", WireMock.matching(filterIds.stream().map(uuid -> ".+").collect(Collectors.joining(",")))); } @@ -161,11 +212,13 @@ private String getPath(UUID networkUuid, boolean isRegexPhat) { return "/v1/filters/export?networkUuid=" + networkUuid + "&variantId=variant_1&ids="; } - abstract void createEquipments(); + protected abstract void createEquipments(); + + protected abstract List getTestFilters(); - abstract List getTestFilters(); + protected abstract List getFormulaInfos(); - abstract List getFormulaInfos(); + protected abstract List getUpdatedFormulaInfos(); - abstract List getUpdatedFormulaInfos(); + protected abstract IdentifiableType getIdentifiableType(); } diff --git a/src/test/java/org/gridsuite/modification/server/modifications/BatteryByFormulaModificationTest.java b/src/test/java/org/gridsuite/modification/server/modifications/BatteryByFormulaModificationTest.java index 9c88baf83..d23eb7425 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/BatteryByFormulaModificationTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/BatteryByFormulaModificationTest.java @@ -7,15 +7,11 @@ package org.gridsuite.modification.server.modifications; -import com.github.tomakehurst.wiremock.client.WireMock; import com.powsybl.iidm.network.IdentifiableType; import com.powsybl.iidm.network.extensions.ActivePowerControl; import com.powsybl.iidm.network.extensions.ActivePowerControlAdder; -import org.gridsuite.modification.server.dto.ByFormulaModificationInfos; import org.gridsuite.modification.server.dto.FilterEquipments; -import org.gridsuite.modification.server.dto.FilterInfos; import org.gridsuite.modification.server.dto.IdentifiableAttributes; -import org.gridsuite.modification.server.dto.NetworkModificationResult; import org.gridsuite.modification.server.dto.formula.FormulaInfos; import org.gridsuite.modification.server.dto.formula.Operator; import org.gridsuite.modification.server.dto.formula.ReferenceFieldOrValue; @@ -23,7 +19,6 @@ import org.junit.Test; import java.util.List; -import java.util.UUID; import static org.gridsuite.modification.server.utils.NetworkUtil.createBattery; import static org.junit.Assert.assertEquals; @@ -34,11 +29,6 @@ */ public class BatteryByFormulaModificationTest extends AbstractByFormulaModificationTest { - private static final UUID FILTER_ID_1 = UUID.randomUUID(); - private static final UUID FILTER_ID_2 = UUID.randomUUID(); - private static final UUID FILTER_ID_3 = UUID.randomUUID(); - private static final UUID FILTER_ID_4 = UUID.randomUUID(); - private static final UUID FILTER_ID_5 = UUID.randomUUID(); private static final String BATTERY_ID_1 = "v3Battery"; private static final String BATTERY_ID_2 = "battery2"; private static final String BATTERY_ID_3 = "battery3"; @@ -48,74 +38,35 @@ public class BatteryByFormulaModificationTest extends AbstractByFormulaModificat @Test public void testCreateWithWarning() throws Exception { - UUID filterId = UUID.randomUUID(); - String equipmentId = "v3Battery"; - IdentifiableAttributes identifiableAttributes1 = getIdentifiableAttributes(equipmentId, 1.0); - FilterEquipments filter = getFilterEquipments(filterId, "filterWithWrongId", List.of(identifiableAttributes1), List.of("wrongId")); - var filterInfo = FilterInfos.builder() - .id(filterId) - .name("filterWithWrongId") - .build(); - - UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + filterId)) - .willReturn(WireMock.ok() - .withBody(mapper.writeValueAsString(List.of(filter))) - .withHeader("Content-Type", "application/json"))).getId(); + IdentifiableAttributes identifiableAttributes = getIdentifiableAttributes(BATTERY_ID_1, 1.0); FormulaInfos formulaInfos = FormulaInfos.builder() - .filters(List.of(filterInfo)) + .filters(List.of(filterWithOneWrongId)) .editedField(BatteryField.ACTIVE_POWER_SET_POINT.name()) .fieldOrValue1(ReferenceFieldOrValue.builder().value(55.).build()) .operator(Operator.ADDITION) .fieldOrValue2(ReferenceFieldOrValue.builder().value(20.).build()) .build(); - ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder() - .formulaInfosList(List.of(formulaInfos)) - .identifiableType(IdentifiableType.BATTERY) - .build(); - - checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_WARNINGS); - assertEquals(75, getNetwork().getBattery(equipmentId).getTargetP(), 0); - - wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(filterId)), false); + checkCreateWithWarning(List.of(formulaInfos), List.of(identifiableAttributes)); + assertEquals(75, getNetwork().getBattery(BATTERY_ID_1).getTargetP(), 0); } @Test public void testCreateWithError() throws Exception { - UUID filterId = UUID.randomUUID(); - FilterEquipments filter = getFilterEquipments(filterId, "filterWithWrongId", List.of(), List.of("wrongId1", "wrongId2")); - var filterInfo = FilterInfos.builder() - .id(filterId) - .name("filterWithWrongId") - .build(); - - UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + filterId)) - .willReturn(WireMock.ok() - .withBody(mapper.writeValueAsString(List.of(filter))) - .withHeader("Content-Type", "application/json"))).getId(); - FormulaInfos formulaInfos = FormulaInfos.builder() - .filters(List.of(filterInfo)) + .filters(List.of(filterWithAllWrongId)) .editedField(BatteryField.ACTIVE_POWER_SET_POINT.name()) .fieldOrValue1(ReferenceFieldOrValue.builder().value(55.).build()) .operator(Operator.ADDITION) .fieldOrValue2(ReferenceFieldOrValue.builder().value(20.).build()) .build(); - ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder() - .formulaInfosList(List.of(formulaInfos)) - .identifiableType(IdentifiableType.BATTERY) - .build(); - - checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_ERRORS); - - wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(filterId)), false); + checkCreateWithError(List.of(formulaInfos)); } @Override - void createEquipments() { - getNetwork().getVariantManager().setWorkingVariant("variant_1"); + protected void createEquipments() { getNetwork().getBattery(BATTERY_ID_1).setTargetP(100).setMaxP(500).setMinP(0).setTargetQ(80); getNetwork().getBattery(BATTERY_ID_1).newExtension(ActivePowerControlAdder.class).withDroop(1).add(); @@ -131,7 +82,7 @@ void createEquipments() { } @Override - List getTestFilters() { + protected List getTestFilters() { IdentifiableAttributes battery1 = getIdentifiableAttributes(BATTERY_ID_1, 1.0); IdentifiableAttributes battery2 = getIdentifiableAttributes(BATTERY_ID_2, 2.0); IdentifiableAttributes battery3 = getIdentifiableAttributes(BATTERY_ID_3, 2.0); @@ -149,32 +100,7 @@ List getTestFilters() { } @Override - List getFormulaInfos() { - var filter1 = FilterInfos.builder() - .id(FILTER_ID_1) - .name("filter1") - .build(); - - var filter2 = FilterInfos.builder() - .id(FILTER_ID_2) - .name("filter2") - .build(); - - var filter3 = FilterInfos.builder() - .id(FILTER_ID_3) - .name("filter3") - .build(); - - var filter4 = FilterInfos.builder() - .id(FILTER_ID_4) - .name("filter4") - .build(); - - var filter5 = FilterInfos.builder() - .id(FILTER_ID_5) - .name("filter5") - .build(); - + protected List getFormulaInfos() { ReferenceFieldOrValue maxActivePowerRef = ReferenceFieldOrValue.builder().equipmentField(BatteryField.MAXIMUM_ACTIVE_POWER.name()).build(); ReferenceFieldOrValue minActivePowerRef = ReferenceFieldOrValue.builder().equipmentField(BatteryField.MINIMUM_ACTIVE_POWER.name()).build(); @@ -212,27 +138,7 @@ List getFormulaInfos() { } @Override - List getUpdatedFormulaInfos() { - var filter1 = FilterInfos.builder() - .id(FILTER_ID_1) - .name("filter1") - .build(); - - var filter2 = FilterInfos.builder() - .id(FILTER_ID_2) - .name("filter2") - .build(); - - var filter3 = FilterInfos.builder() - .id(FILTER_ID_3) - .name("filter3") - .build(); - - var filter5 = FilterInfos.builder() - .id(FILTER_ID_5) - .name("filter5") - .build(); - + protected List getUpdatedFormulaInfos() { FormulaInfos formulaInfos1 = FormulaInfos.builder() .editedField(BatteryField.MAXIMUM_ACTIVE_POWER.name()) .fieldOrValue1(ReferenceFieldOrValue.builder().value(200.).build()) @@ -305,4 +211,9 @@ protected void assertAfterNetworkModificationDeletion() { assertEquals(200, getNetwork().getBattery(BATTERY_ID_6).getMinP(), 0); } + + @Override + protected IdentifiableType getIdentifiableType() { + return IdentifiableType.BATTERY; + } } diff --git a/src/test/java/org/gridsuite/modification/server/modifications/GeneratorByFormulaModificationTest.java b/src/test/java/org/gridsuite/modification/server/modifications/GeneratorByFormulaModificationTest.java index b6130fcd1..c47b0cede 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/GeneratorByFormulaModificationTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/GeneratorByFormulaModificationTest.java @@ -7,7 +7,6 @@ package org.gridsuite.modification.server.modifications; -import com.github.tomakehurst.wiremock.client.WireMock; import com.powsybl.iidm.network.Generator; import com.powsybl.iidm.network.IdentifiableType; import com.powsybl.iidm.network.extensions.ActivePowerControl; @@ -19,11 +18,8 @@ import com.powsybl.iidm.network.extensions.GeneratorShortCircuitAdder; import com.powsybl.iidm.network.extensions.GeneratorStartup; import com.powsybl.iidm.network.extensions.GeneratorStartupAdder; -import org.gridsuite.modification.server.dto.ByFormulaModificationInfos; import org.gridsuite.modification.server.dto.FilterEquipments; -import org.gridsuite.modification.server.dto.FilterInfos; import org.gridsuite.modification.server.dto.IdentifiableAttributes; -import org.gridsuite.modification.server.dto.NetworkModificationResult; import org.gridsuite.modification.server.dto.formula.FormulaInfos; import org.gridsuite.modification.server.dto.formula.Operator; import org.gridsuite.modification.server.dto.formula.ReferenceFieldOrValue; @@ -32,7 +28,6 @@ import org.junit.jupiter.api.Tag; import java.util.List; -import java.util.UUID; import static org.gridsuite.modification.server.utils.NetworkUtil.createGenerator; import static org.junit.Assert.assertEquals; @@ -44,11 +39,6 @@ @Tag("IntegrationTest") public class GeneratorByFormulaModificationTest extends AbstractByFormulaModificationTest { - private static final UUID FILTER_ID_1 = UUID.randomUUID(); - private static final UUID FILTER_ID_2 = UUID.randomUUID(); - private static final UUID FILTER_ID_3 = UUID.randomUUID(); - private static final UUID FILTER_ID_4 = UUID.randomUUID(); - private static final UUID FILTER_ID_5 = UUID.randomUUID(); private static final String GENERATOR_ID_1 = "idGenerator"; private static final String GENERATOR_ID_2 = "v5generator"; private static final String GENERATOR_ID_3 = "v6generator"; @@ -62,73 +52,34 @@ public class GeneratorByFormulaModificationTest extends AbstractByFormulaModific @Test public void testCreateWithWarning() throws Exception { - UUID filterId = UUID.randomUUID(); - String equipmentId = "idGenerator"; - IdentifiableAttributes identifiableAttributes1 = getIdentifiableAttributes(equipmentId, 1.0); - FilterEquipments filter = getFilterEquipments(filterId, "filterWithWrongId", List.of(identifiableAttributes1), List.of("wrongId")); - var filterInfo = FilterInfos.builder() - .id(filterId) - .name("filterWithWrongId") - .build(); - - UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + filterId)) - .willReturn(WireMock.ok() - .withBody(mapper.writeValueAsString(List.of(filter))) - .withHeader("Content-Type", "application/json"))).getId(); + IdentifiableAttributes identifiableAttributes = getIdentifiableAttributes(GENERATOR_ID_1, 1.0); FormulaInfos formulaInfos = FormulaInfos.builder() - .filters(List.of(filterInfo)) + .filters(List.of(filterWithOneWrongId)) .editedField(GeneratorField.ACTIVE_POWER_SET_POINT.name()) .fieldOrValue1(ReferenceFieldOrValue.builder().value(55.).build()) .operator(Operator.ADDITION) .fieldOrValue2(ReferenceFieldOrValue.builder().value(20.).build()) .build(); - ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder() - .formulaInfosList(List.of(formulaInfos)) - .identifiableType(IdentifiableType.GENERATOR) - .build(); - - checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_WARNINGS); - assertEquals(75, getNetwork().getGenerator(equipmentId).getTargetP(), 0); - - wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(filterId)), false); + checkCreateWithWarning(List.of(formulaInfos), List.of(identifiableAttributes)); + assertEquals(75, getNetwork().getGenerator(GENERATOR_ID_1).getTargetP(), 0); } @Test public void testCreateWithError() throws Exception { - UUID filterId = UUID.randomUUID(); - FilterEquipments filter = getFilterEquipments(filterId, "filterWithWrongId", List.of(), List.of("wrongId1", "wrongId2")); - var filterInfo = FilterInfos.builder() - .id(filterId) - .name("filterWithWrongId") - .build(); - - UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/export\\?networkUuid=" + getNetworkUuid() + "&variantId=variant_1&ids=" + filterId)) - .willReturn(WireMock.ok() - .withBody(mapper.writeValueAsString(List.of(filter))) - .withHeader("Content-Type", "application/json"))).getId(); - FormulaInfos formulaInfos = FormulaInfos.builder() - .filters(List.of(filterInfo)) + .filters(List.of(filterWithAllWrongId)) .editedField(GeneratorField.ACTIVE_POWER_SET_POINT.name()) .fieldOrValue1(ReferenceFieldOrValue.builder().value(55.).build()) .operator(Operator.ADDITION) .fieldOrValue2(ReferenceFieldOrValue.builder().value(20.).build()) .build(); - ByFormulaModificationInfos byFormulaModificationInfos = ByFormulaModificationInfos.builder() - .formulaInfosList(List.of(formulaInfos)) - .identifiableType(IdentifiableType.GENERATOR) - .build(); - - checkCreationApplicationStatus(byFormulaModificationInfos, NetworkModificationResult.ApplicationStatus.WITH_ERRORS); - - wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(getNetworkUuid(), List.of(filterId)), false); + checkCreateWithError(List.of(formulaInfos)); } - void createEquipments() { - getNetwork().getVariantManager().setWorkingVariant("variant_1"); + protected void createEquipments() { getNetwork().getGenerator(GENERATOR_ID_1) .setTargetP(100) .setMaxP(500) @@ -192,7 +143,7 @@ void createEquipments() { getNetwork().getGenerator(GENERATOR_ID_10).setRatedS(30.); } - List getTestFilters() { + protected List getTestFilters() { IdentifiableAttributes gen1 = getIdentifiableAttributes(GENERATOR_ID_1, 1.0); IdentifiableAttributes gen2 = getIdentifiableAttributes(GENERATOR_ID_2, 2.0); IdentifiableAttributes gen3 = getIdentifiableAttributes(GENERATOR_ID_3, 2.0); @@ -214,32 +165,7 @@ List getTestFilters() { } @Override - List getFormulaInfos() { - var filter1 = FilterInfos.builder() - .id(FILTER_ID_1) - .name("filter1") - .build(); - - var filter2 = FilterInfos.builder() - .id(FILTER_ID_2) - .name("filter2") - .build(); - - var filter3 = FilterInfos.builder() - .id(FILTER_ID_3) - .name("filter3") - .build(); - - var filter4 = FilterInfos.builder() - .id(FILTER_ID_4) - .name("filter4") - .build(); - - var filter5 = FilterInfos.builder() - .id(FILTER_ID_5) - .name("filter5") - .build(); - + protected List getFormulaInfos() { FormulaInfos formulaInfos1 = getFormulaInfo(GeneratorField.ACTIVE_POWER_SET_POINT.name(), List.of(filter1, filter2), Operator.ADDITION, @@ -334,22 +260,7 @@ List getFormulaInfos() { } @Override - List getUpdatedFormulaInfos() { - var filter1 = FilterInfos.builder() - .id(FILTER_ID_1) - .name("filter1") - .build(); - - var filter2 = FilterInfos.builder() - .id(FILTER_ID_2) - .name("filter2") - .build(); - - var filter3 = FilterInfos.builder() - .id(FILTER_ID_3) - .name("filter3") - .build(); - + protected List getUpdatedFormulaInfos() { FormulaInfos formulaInfos1 = FormulaInfos.builder() .editedField(GeneratorField.REACTIVE_POWER_SET_POINT.name()) .fieldOrValue1(ReferenceFieldOrValue.builder().value(2.).build()) @@ -516,4 +427,9 @@ protected void assertAfterNetworkModificationDeletion() { assertEquals(60, getNetwork().getGenerator(GENERATOR_ID_9).getRatedS(), 0); assertEquals(30, getNetwork().getGenerator(GENERATOR_ID_10).getRatedS(), 0); } + + @Override + protected IdentifiableType getIdentifiableType() { + return IdentifiableType.GENERATOR; + } } diff --git a/src/test/java/org/gridsuite/modification/server/modifications/ShuntCompensatorByFormulaModificationTest.java b/src/test/java/org/gridsuite/modification/server/modifications/ShuntCompensatorByFormulaModificationTest.java new file mode 100644 index 000000000..643efaaaa --- /dev/null +++ b/src/test/java/org/gridsuite/modification/server/modifications/ShuntCompensatorByFormulaModificationTest.java @@ -0,0 +1,189 @@ +/** + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.gridsuite.modification.server.modifications; + +import com.powsybl.iidm.network.IdentifiableType; +import com.powsybl.iidm.network.ShuntCompensator; +import com.powsybl.iidm.network.ShuntCompensatorLinearModel; +import com.powsybl.iidm.network.extensions.ConnectablePosition; +import org.gridsuite.modification.server.dto.FilterEquipments; +import org.gridsuite.modification.server.dto.IdentifiableAttributes; +import org.gridsuite.modification.server.dto.formula.FormulaInfos; +import org.gridsuite.modification.server.dto.formula.Operator; +import org.gridsuite.modification.server.dto.formula.ReferenceFieldOrValue; +import org.gridsuite.modification.server.dto.formula.equipmentfield.ShuntCompensatorField; +import org.junit.Test; + +import java.util.List; + +import static org.gridsuite.modification.server.utils.NetworkUtil.createShuntCompensator; +import static org.junit.Assert.assertEquals; + +public class ShuntCompensatorByFormulaModificationTest extends AbstractByFormulaModificationTest { + private static final String SHUNT_COMPENSATOR_ID_1 = "v1shunt"; + private static final String SHUNT_COMPENSATOR_ID_2 = "v2shunt"; + private static final String SHUNT_COMPENSATOR_ID_3 = "v3shunt"; + private static final String SHUNT_COMPENSATOR_ID_4 = "v4shunt"; + private static final String SHUNT_COMPENSATOR_ID_5 = "v5shunt"; + private static final String SHUNT_COMPENSATOR_ID_6 = "v6shunt"; + + @Test + public void testCreateWithWarning() throws Exception { + IdentifiableAttributes identifiableAttributes = getIdentifiableAttributes(SHUNT_COMPENSATOR_ID_1, 1.0); + + FormulaInfos formulaInfos = FormulaInfos.builder() + .filters(List.of(filterWithOneWrongId)) + .editedField(ShuntCompensatorField.MAXIMUM_SECTION_COUNT.name()) + .fieldOrValue1(ReferenceFieldOrValue.builder().value(2.).build()) + .operator(Operator.ADDITION) + .fieldOrValue2(ReferenceFieldOrValue.builder().value(3.).build()) + .build(); + + checkCreateWithWarning(List.of(formulaInfos), List.of(identifiableAttributes)); + assertEquals(5, getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_1).getMaximumSectionCount(), 0); + } + + @Test + public void testCreateWithError() throws Exception { + FormulaInfos formulaInfos = FormulaInfos.builder() + .filters(List.of(filterWithAllWrongId)) + .editedField(ShuntCompensatorField.MAXIMUM_SECTION_COUNT.name()) + .fieldOrValue1(ReferenceFieldOrValue.builder().value(2.).build()) + .operator(Operator.ADDITION) + .fieldOrValue2(ReferenceFieldOrValue.builder().value(3.).build()) + .build(); + + checkCreateWithError(List.of(formulaInfos)); + } + + @Override + protected void createEquipments() { + createShuntCompensator(getNetwork().getVoltageLevel("v1"), SHUNT_COMPENSATOR_ID_1, "v1shunt", 8, 225., 10, true, 4, 2, 3, 2, "cn11", 22, ConnectablePosition.Direction.BOTTOM); + createShuntCompensator(getNetwork().getVoltageLevel("v3"), SHUNT_COMPENSATOR_ID_3, "v3shunt", 10, 305., 20, true, 6, 3, 3, 4, "cn11", 22, ConnectablePosition.Direction.BOTTOM); + createShuntCompensator(getNetwork().getVoltageLevel("v4"), SHUNT_COMPENSATOR_ID_4, "v3shunt", 10, 305., 20, true, 15, 4, 3, 10, "cn11", 22, ConnectablePosition.Direction.BOTTOM); + } + + @Override + protected List getTestFilters() { + IdentifiableAttributes shunt1 = getIdentifiableAttributes(SHUNT_COMPENSATOR_ID_1, 1.0); + IdentifiableAttributes shunt2 = getIdentifiableAttributes(SHUNT_COMPENSATOR_ID_2, 2.0); + IdentifiableAttributes shunt3 = getIdentifiableAttributes(SHUNT_COMPENSATOR_ID_3, 2.0); + IdentifiableAttributes shunt4 = getIdentifiableAttributes(SHUNT_COMPENSATOR_ID_4, 5.0); + IdentifiableAttributes shunt5 = getIdentifiableAttributes(SHUNT_COMPENSATOR_ID_5, 6.0); + IdentifiableAttributes shunt6 = getIdentifiableAttributes(SHUNT_COMPENSATOR_ID_6, 7.0); + + FilterEquipments filter1 = getFilterEquipments(FILTER_ID_1, "filter1", List.of(shunt1, shunt2), List.of()); + FilterEquipments filter2 = getFilterEquipments(FILTER_ID_2, "filter2", List.of(shunt3, shunt6), List.of()); + FilterEquipments filter3 = getFilterEquipments(FILTER_ID_3, "filter3", List.of(shunt4, shunt5), List.of()); + FilterEquipments filter4 = getFilterEquipments(FILTER_ID_4, "filter4", List.of(shunt1, shunt5), List.of()); + FilterEquipments filter5 = getFilterEquipments(FILTER_ID_5, "filter5", List.of(shunt3, shunt2), List.of()); + + return List.of(filter1, filter2, filter3, filter4, filter5); + } + + @Override + protected List getFormulaInfos() { + FormulaInfos formulaInfos1 = getFormulaInfo(ShuntCompensatorField.MAXIMUM_SECTION_COUNT.name(), + List.of(filter1, filter2), + Operator.PERCENTAGE, + ReferenceFieldOrValue.builder().value(200.).build(), + ReferenceFieldOrValue.builder().equipmentField(ShuntCompensatorField.MAXIMUM_SECTION_COUNT.name()).build()); + + FormulaInfos formulaInfos2 = getFormulaInfo(ShuntCompensatorField.SECTION_COUNT.name(), + List.of(filter3), + Operator.SUBTRACTION, + ReferenceFieldOrValue.builder().equipmentField(ShuntCompensatorField.MAXIMUM_SECTION_COUNT.name()).build(), + ReferenceFieldOrValue.builder().value(2.).build()); + + FormulaInfos formulaInfos3 = getFormulaInfo(ShuntCompensatorField.MAXIMUM_SUSCEPTANCE.name(), + List.of(filter4), + Operator.ADDITION, + ReferenceFieldOrValue.builder().equipmentField(ShuntCompensatorField.MAXIMUM_SUSCEPTANCE.name()).build(), + ReferenceFieldOrValue.builder().value(5.).build()); + + FormulaInfos formulaInfos4 = getFormulaInfo(ShuntCompensatorField.MAXIMUM_Q_AT_NOMINAL_VOLTAGE.name(), + List.of(filter5), + Operator.DIVISION, + ReferenceFieldOrValue.builder().equipmentField(ShuntCompensatorField.MAXIMUM_Q_AT_NOMINAL_VOLTAGE.name()).build(), + ReferenceFieldOrValue.builder().value(2.).build()); + + return List.of(formulaInfos1, formulaInfos2, formulaInfos3, formulaInfos4); + } + + @Override + protected List getUpdatedFormulaInfos() { + FormulaInfos formulaInfos1 = getFormulaInfo(ShuntCompensatorField.MAXIMUM_SECTION_COUNT.name(), + List.of(filter1, filter2), + Operator.PERCENTAGE, + ReferenceFieldOrValue.builder().value(150.).build(), + ReferenceFieldOrValue.builder().equipmentField(ShuntCompensatorField.MAXIMUM_SECTION_COUNT.name()).build()); + + FormulaInfos formulaInfos2 = getFormulaInfo(ShuntCompensatorField.SECTION_COUNT.name(), + List.of(filter3), + Operator.MULTIPLICATION, + ReferenceFieldOrValue.builder().equipmentField(ShuntCompensatorField.SECTION_COUNT.name()).build(), + ReferenceFieldOrValue.builder().value(2.).build()); + return List.of(formulaInfos1, formulaInfos2); + } + + @Override + protected void assertAfterNetworkModificationCreation() { + ShuntCompensator shuntCompensator1 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_1); + assertEquals(8, shuntCompensator1.getMaximumSectionCount()); + assertEquals(2.625, shuntCompensator1.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + + ShuntCompensator shuntCompensator2 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_2); + assertEquals(6, shuntCompensator2.getMaximumSectionCount()); + assertEquals(0.5, shuntCompensator2.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + + ShuntCompensator shuntCompensator3 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_3); + assertEquals(12, shuntCompensator3.getMaximumSectionCount()); + assertEquals(3, shuntCompensator3.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + + ShuntCompensator shuntCompensator4 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_4); + assertEquals(13, shuntCompensator4.getSectionCount()); + + ShuntCompensator shuntCompensator5 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_5); + assertEquals(1, shuntCompensator5.getSectionCount()); + + ShuntCompensator shuntCompensator6 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_6); + assertEquals(6, shuntCompensator6.getMaximumSectionCount()); + assertEquals(0.5, shuntCompensator6.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + } + + @Override + protected void assertAfterNetworkModificationDeletion() { + ShuntCompensator shuntCompensator1 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_1); + assertEquals(4, shuntCompensator1.getMaximumSectionCount()); + assertEquals(2, shuntCompensator1.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + + ShuntCompensator shuntCompensator2 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_2); + assertEquals(3, shuntCompensator2.getMaximumSectionCount()); + assertEquals(1, shuntCompensator2.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + + ShuntCompensator shuntCompensator3 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_3); + assertEquals(6, shuntCompensator3.getMaximumSectionCount()); + assertEquals(3, shuntCompensator3.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + + ShuntCompensator shuntCompensator4 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_4); + assertEquals(10, shuntCompensator4.getSectionCount()); + + ShuntCompensator shuntCompensator5 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_5); + assertEquals(2, shuntCompensator5.getSectionCount()); + assertEquals(1, shuntCompensator5.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + + ShuntCompensator shuntCompensator6 = getNetwork().getShuntCompensator(SHUNT_COMPENSATOR_ID_6); + assertEquals(3, shuntCompensator6.getMaximumSectionCount()); + assertEquals(1, shuntCompensator6.getModel(ShuntCompensatorLinearModel.class).getBPerSection(), 0); + } + + @Override + protected IdentifiableType getIdentifiableType() { + return IdentifiableType.SHUNT_COMPENSATOR; + } +}