From ec67836585d346897eeffe4013a73cf67ec562e3 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 19 Nov 2024 16:19:34 +0100 Subject: [PATCH 01/16] include node id in loadflow result --- .../server/service/LoadFlowWorkerService.java | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index a99e91e0..96538504 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -23,6 +23,9 @@ import com.powsybl.security.LimitViolation; import com.powsybl.security.LimitViolationType; import com.powsybl.security.Security; +import com.powsybl.security.ViolationLocation; +import com.powsybl.security.NodeBreakerViolationLocation; +import com.powsybl.security.BusBreakerViolationLocation; import com.powsybl.security.limitreduction.DefaultLimitReductionsApplier; import com.powsybl.security.limitreduction.LimitReduction; import org.gridsuite.loadflow.server.dto.LimitViolationInfos; @@ -42,6 +45,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; +import static com.powsybl.security.ViolationLocation.Type.NODE_BREAKER; import static org.gridsuite.loadflow.server.service.LoadFlowService.COMPUTATION_TYPE; /** @@ -191,8 +195,22 @@ protected List calculateOverloadLimitViolations(List getLimitViolations(Network network, LoadFlowRu } } + System.out.println("violations ====> " + violations.size()); + return violations.stream() - .map(LoadFlowWorkerService::toLimitViolationInfos).toList(); + .map(limitViolation -> { + return toLimitViolationInfos(limitViolation, network); + }).toList(); + } + + private static String getIdFromViolation(LimitViolation limitViolation, Network network) { + if (limitViolation.getViolationLocation().isPresent()) { + ViolationLocation location = limitViolation.getViolationLocation().get(); + if (location.getType() == NODE_BREAKER) { + NodeBreakerViolationLocation nodeBreakerViolationLocation = (NodeBreakerViolationLocation) location; + return getBusIdOrVlIdNodeBreaker(nodeBreakerViolationLocation, network); + } else { + BusBreakerViolationLocation busBreakerViolationLocation = (BusBreakerViolationLocation) location; + return getBusIdOrVlIdBusBreaker(busBreakerViolationLocation, network, limitViolation.getSubjectId()); + } + } else { + return limitViolation.getSubjectId(); + } + } + + private static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { + List nodesIds = nodeBreakerViolationLocation + .getBusView(network) + .getBusStream() + .map(Identifiable::getId).toList(); + + return formatNodeId(nodesIds, nodeBreakerViolationLocation.getVoltageLevelId()); + } + + private static String formatNodeId(List nodesIds, String subjectId) { + if (nodesIds.size() == 1) { + return nodesIds.get(0); + } else if (nodesIds.isEmpty()) { + return subjectId; + } else { + return subjectId + " (" + String.join(", ", nodesIds) + " )"; + } + } + + private static String getBusIdOrVlIdBusBreaker(BusBreakerViolationLocation busBreakerViolationLocation, Network network, String subjectId) { + List busBreakerIds = busBreakerViolationLocation.getBusBreakerView(network) + .getBusStream().map(Identifiable::getId).toList(); + return formatNodeId(busBreakerIds, subjectId); } @Bean From db53de1b956fa4d7ca58b7a2e90f4e71c4a819da Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 19 Nov 2024 16:48:43 +0100 Subject: [PATCH 02/16] fix sonar issue --- .../server/service/LoadFlowWorkerService.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index 96538504..d04d8371 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -239,27 +239,24 @@ private List getLimitViolations(Network network, LoadFlowRu } } - System.out.println("violations ====> " + violations.size()); - return violations.stream() - .map(limitViolation -> { - return toLimitViolationInfos(limitViolation, network); - }).toList(); + .map(limitViolation -> toLimitViolationInfos(limitViolation, network)).toList(); } private static String getIdFromViolation(LimitViolation limitViolation, Network network) { - if (limitViolation.getViolationLocation().isPresent()) { - ViolationLocation location = limitViolation.getViolationLocation().get(); - if (location.getType() == NODE_BREAKER) { - NodeBreakerViolationLocation nodeBreakerViolationLocation = (NodeBreakerViolationLocation) location; - return getBusIdOrVlIdNodeBreaker(nodeBreakerViolationLocation, network); - } else { - BusBreakerViolationLocation busBreakerViolationLocation = (BusBreakerViolationLocation) location; - return getBusIdOrVlIdBusBreaker(busBreakerViolationLocation, network, limitViolation.getSubjectId()); - } - } else { + Optional violationLocation = limitViolation.getViolationLocation(); + if (violationLocation.isEmpty()) { return limitViolation.getSubjectId(); } + + ViolationLocation location = violationLocation.get(); + if (location.getType() == NODE_BREAKER) { + NodeBreakerViolationLocation nodeBreakerViolationLocation = (NodeBreakerViolationLocation) location; + return getBusIdOrVlIdNodeBreaker(nodeBreakerViolationLocation, network); + } else { + BusBreakerViolationLocation busBreakerViolationLocation = (BusBreakerViolationLocation) location; + return getBusIdOrVlIdBusBreaker(busBreakerViolationLocation, network, limitViolation.getSubjectId()); + } } private static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { From 31523b6fe88f62d03de99788d2977e2088d636b9 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Wed, 20 Nov 2024 10:15:52 +0100 Subject: [PATCH 03/16] move some code to utils --- .../server/service/LoadFlowWorkerService.java | 45 +------------- .../server/utils/LoadflowResultsUtils.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index d04d8371..39c2a29b 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -23,9 +23,6 @@ import com.powsybl.security.LimitViolation; import com.powsybl.security.LimitViolationType; import com.powsybl.security.Security; -import com.powsybl.security.ViolationLocation; -import com.powsybl.security.NodeBreakerViolationLocation; -import com.powsybl.security.BusBreakerViolationLocation; import com.powsybl.security.limitreduction.DefaultLimitReductionsApplier; import com.powsybl.security.limitreduction.LimitReduction; import org.gridsuite.loadflow.server.dto.LimitViolationInfos; @@ -45,8 +42,8 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; -import static com.powsybl.security.ViolationLocation.Type.NODE_BREAKER; import static org.gridsuite.loadflow.server.service.LoadFlowService.COMPUTATION_TYPE; +import static org.gridsuite.loadflow.server.utils.LoadflowResultsUtils.getIdFromViolation; /** * @author Anis Touri @@ -243,46 +240,6 @@ private List getLimitViolations(Network network, LoadFlowRu .map(limitViolation -> toLimitViolationInfos(limitViolation, network)).toList(); } - private static String getIdFromViolation(LimitViolation limitViolation, Network network) { - Optional violationLocation = limitViolation.getViolationLocation(); - if (violationLocation.isEmpty()) { - return limitViolation.getSubjectId(); - } - - ViolationLocation location = violationLocation.get(); - if (location.getType() == NODE_BREAKER) { - NodeBreakerViolationLocation nodeBreakerViolationLocation = (NodeBreakerViolationLocation) location; - return getBusIdOrVlIdNodeBreaker(nodeBreakerViolationLocation, network); - } else { - BusBreakerViolationLocation busBreakerViolationLocation = (BusBreakerViolationLocation) location; - return getBusIdOrVlIdBusBreaker(busBreakerViolationLocation, network, limitViolation.getSubjectId()); - } - } - - private static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { - List nodesIds = nodeBreakerViolationLocation - .getBusView(network) - .getBusStream() - .map(Identifiable::getId).toList(); - - return formatNodeId(nodesIds, nodeBreakerViolationLocation.getVoltageLevelId()); - } - - private static String formatNodeId(List nodesIds, String subjectId) { - if (nodesIds.size() == 1) { - return nodesIds.get(0); - } else if (nodesIds.isEmpty()) { - return subjectId; - } else { - return subjectId + " (" + String.join(", ", nodesIds) + " )"; - } - } - - private static String getBusIdOrVlIdBusBreaker(BusBreakerViolationLocation busBreakerViolationLocation, Network network, String subjectId) { - List busBreakerIds = busBreakerViolationLocation.getBusBreakerView(network) - .getBusStream().map(Identifiable::getId).toList(); - return formatNodeId(busBreakerIds, subjectId); - } @Bean @Override diff --git a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java new file mode 100644 index 00000000..ebcb90a8 --- /dev/null +++ b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java @@ -0,0 +1,58 @@ +package org.gridsuite.loadflow.server.utils; + +import com.powsybl.iidm.network.Identifiable; +import com.powsybl.iidm.network.Network; +import com.powsybl.security.BusBreakerViolationLocation; +import com.powsybl.security.LimitViolation; +import com.powsybl.security.NodeBreakerViolationLocation; +import com.powsybl.security.ViolationLocation; + +import java.util.List; +import java.util.Optional; + +import static com.powsybl.security.ViolationLocation.Type.NODE_BREAKER; + +public final class LoadflowResultsUtils { + + public static String getIdFromViolation(LimitViolation limitViolation, Network network) { + Optional violationLocation = limitViolation.getViolationLocation(); + if (violationLocation.isEmpty()) { + return limitViolation.getSubjectId(); + } + + ViolationLocation location = violationLocation.get(); + if (location.getType() == NODE_BREAKER) { + NodeBreakerViolationLocation nodeBreakerViolationLocation = (NodeBreakerViolationLocation) location; + return getBusIdOrVlIdNodeBreaker(nodeBreakerViolationLocation, network); + } else { + BusBreakerViolationLocation busBreakerViolationLocation = (BusBreakerViolationLocation) location; + return getBusIdOrVlIdBusBreaker(busBreakerViolationLocation, network, limitViolation.getSubjectId()); + } + } + + public static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { + List nodesIds = nodeBreakerViolationLocation + .getBusView(network) + .getBusStream() + .map(Identifiable::getId).toList(); + + return formatNodeId(nodesIds, nodeBreakerViolationLocation.getVoltageLevelId()); + } + + public static String formatNodeId(List nodesIds, String subjectId) { + if (nodesIds.size() == 1) { + return nodesIds.get(0); + } else if (nodesIds.isEmpty()) { + return subjectId; + } else { + return subjectId + " (" + String.join(", ", nodesIds) + " )"; + } + } + + public static String getBusIdOrVlIdBusBreaker(BusBreakerViolationLocation busBreakerViolationLocation, Network network, String subjectId) { + List busBreakerIds = busBreakerViolationLocation.getBusBreakerView(network) + .getBusStream().map(Identifiable::getId).toList(); + return formatNodeId(busBreakerIds, subjectId); + } + +} From fe07be0514e7adb59daa4a0c29f1451bbb371dd9 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Wed, 20 Nov 2024 10:58:47 +0100 Subject: [PATCH 04/16] add tests --- .../server/service/LoadFlowWorkerService.java | 1 - .../server/utils/LoadflowResultsUtils.java | 3 ++ .../loadflow/server/LoadFlowServiceTest.java | 47 ++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index 39c2a29b..c238a05d 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -240,7 +240,6 @@ private List getLimitViolations(Network network, LoadFlowRu .map(limitViolation -> toLimitViolationInfos(limitViolation, network)).toList(); } - @Bean @Override public Consumer> consumeRun() { diff --git a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java index ebcb90a8..a7eaa7bd 100644 --- a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java +++ b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java @@ -14,6 +14,9 @@ public final class LoadflowResultsUtils { + private LoadflowResultsUtils() { + } + public static String getIdFromViolation(LimitViolation limitViolation, Network network) { Optional violationLocation = limitViolation.getViolationLocation(); if (violationLocation.isEmpty()) { diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java index 52a28fb9..08760979 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java @@ -1,11 +1,13 @@ package org.gridsuite.loadflow.server; -import com.powsybl.security.LimitViolationType; +import com.powsybl.iidm.network.Network; +import com.powsybl.security.*; import org.gridsuite.loadflow.server.dto.LimitViolationInfos; import org.gridsuite.loadflow.server.entities.LimitViolationEntity; import org.gridsuite.loadflow.server.entities.LoadFlowResultEntity; import org.gridsuite.loadflow.server.repositories.LimitViolationRepository; import org.gridsuite.loadflow.server.service.LoadFlowService; +import org.gridsuite.loadflow.server.utils.LoadflowResultsUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -15,8 +17,10 @@ import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.UUID; +import static org.gridsuite.loadflow.server.Networks.createNetwork; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -88,4 +92,45 @@ void getLimitViolationsInfosWhenCalledReturnsCorrectData() { assertEquals(LimitViolationsMock.limitViolationEntities.size(), result.size()); verify(limitViolationRepository, times(1)).findAll(any(Specification.class), eq(sort)); } + + @Test + void testFormatNodeIdSingleNode() { + String result = LoadflowResultsUtils.formatNodeId(List.of("Node1"), "Subject1"); + assertEquals("Node1", result); + } + + @Test + void testFormatNodeIdEmptyNodes() { + String result = LoadflowResultsUtils.formatNodeId(List.of(), "Subject1"); + assertEquals("Subject1", result); + } + + @Test + void testFormatNodeIdMultipleNodes() { + String result = LoadflowResultsUtils.formatNodeId(List.of("Node1", "Node2"), "Subject1"); + assertEquals("Subject1 (Node1, Node2 )", result); + } + + @Test + void testGetIdFromViolationWithBusBreaker() { + // Setup + Network network = createNetwork("", true); + BusBreakerViolationLocation busBreakerLocation = new BusBreakerViolationLocation(List.of("NGEN")); + LimitViolation limitViolation = mock(LimitViolation.class); + when(limitViolation.getViolationLocation()).thenReturn(Optional.of(busBreakerLocation)); + + assertEquals("NGEN", LoadflowResultsUtils.getIdFromViolation(limitViolation, network)); + } + + @Test + void testGetIdFromViolationWithNoViolationLocation() { + // Setup + Network network = mock(Network.class); + LimitViolation limitViolation = mock(LimitViolation.class); + + when(limitViolation.getViolationLocation()).thenReturn(Optional.empty()); + when(limitViolation.getSubjectId()).thenReturn("SubjectId"); + + assertEquals("SubjectId", LoadflowResultsUtils.getIdFromViolation(limitViolation, network)); + } } From db607428d65d9284ab3c2b68cbe3faff983ae6b1 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Wed, 20 Nov 2024 13:40:36 +0100 Subject: [PATCH 05/16] add test ViolationWithNodeBreaker --- .../loadflow/server/LoadFlowServiceTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java index 08760979..d1c26e4a 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java @@ -1,5 +1,6 @@ package org.gridsuite.loadflow.server; +import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Network; import com.powsybl.security.*; import org.gridsuite.loadflow.server.dto.LimitViolationInfos; @@ -19,6 +20,7 @@ import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.stream.Stream; import static org.gridsuite.loadflow.server.Networks.createNetwork; import static org.junit.jupiter.api.Assertions.*; @@ -133,4 +135,22 @@ void testGetIdFromViolationWithNoViolationLocation() { assertEquals("SubjectId", LoadflowResultsUtils.getIdFromViolation(limitViolation, network)); } + + @Test + void testGetIdFromViolationWithNodeBreaker() { + Network network = createNetwork("", true); + NodeBreakerViolationLocation nodeBreakerViolationLocation = mock(NodeBreakerViolationLocation.class); + ViolationLocation.BusView busView = mock(ViolationLocation.BusView.class); + Bus bus = mock(Bus.class); + + when(nodeBreakerViolationLocation.getType()).thenReturn(ViolationLocation.Type.NODE_BREAKER); + when(nodeBreakerViolationLocation.getBusView(network)).thenReturn(busView); + when(busView.getBusStream()).thenReturn(Stream.of(bus)); + when(bus.getId()).thenReturn("NGEN"); + + LimitViolation limitViolation = mock(LimitViolation.class); + when(limitViolation.getViolationLocation()).thenReturn(Optional.of(nodeBreakerViolationLocation)); + + assertEquals("NGEN", LoadflowResultsUtils.getIdFromViolation(limitViolation, network)); + } } From 0833f0ee840b6c0a4c80df762260cd39116afe16 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Wed, 20 Nov 2024 14:54:25 +0100 Subject: [PATCH 06/16] fix tests --- .../server/service/LoadFlowWorkerService.java | 14 -------------- .../loadflow/server/LoadFlowControllerTest.java | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index c238a05d..0da4b916 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -191,21 +191,7 @@ protected List calculateOverloadLimitViolations(List limitViolat assertEquals(limitViolationsDto.get(i).getValue(), limitViolations.get(i).getValue(), 0.01); assertEquals(limitViolationsDto.get(i).getSide(), limitViolations.get(i).getSide() != null ? limitViolations.get(i).getSide().name() : ""); assertEquals(limitViolationsDto.get(i).getLimitType(), limitViolations.get(i).getLimitType()); - assertEquals(limitViolationsDto.get(i).getActualOverloadDuration(), LoadFlowWorkerService.calculateActualOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i)), network)); - assertEquals(limitViolationsDto.get(i).getUpComingOverloadDuration(), LoadFlowWorkerService.calculateUpcomingOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i)))); + assertEquals(limitViolationsDto.get(i).getActualOverloadDuration(), LoadFlowWorkerService.calculateActualOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i), network), network)); + assertEquals(limitViolationsDto.get(i).getUpComingOverloadDuration(), LoadFlowWorkerService.calculateUpcomingOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i), network))); assertEquals(limitViolationsDto.get(i).getOverload(), (limitViolations.get(i).getValue() / limitViolations.get(i).getLimit()) * 100, 0.01); } } From 3de893d2775bd6540dbccd53d46518b00734a3c4 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Sun, 24 Nov 2024 21:14:51 +0100 Subject: [PATCH 07/16] fix Nodebreaker case --- .../server/utils/LoadflowResultsUtils.java | 18 +++++++++++------- .../loadflow/server/LoadFlowServiceTest.java | 16 ++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java index a7eaa7bd..b0561f41 100644 --- a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java +++ b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java @@ -1,13 +1,13 @@ package org.gridsuite.loadflow.server.utils; -import com.powsybl.iidm.network.Identifiable; -import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.*; import com.powsybl.security.BusBreakerViolationLocation; import com.powsybl.security.LimitViolation; import com.powsybl.security.NodeBreakerViolationLocation; import com.powsybl.security.ViolationLocation; import java.util.List; +import java.util.Objects; import java.util.Optional; import static com.powsybl.security.ViolationLocation.Type.NODE_BREAKER; @@ -34,12 +34,16 @@ public static String getIdFromViolation(LimitViolation limitViolation, Network n } public static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { - List nodesIds = nodeBreakerViolationLocation - .getBusView(network) - .getBusStream() - .map(Identifiable::getId).toList(); + VoltageLevel vl = network.getVoltageLevel(nodeBreakerViolationLocation.getVoltageLevelId()); + List busbarSectionIds = nodeBreakerViolationLocation.getNodes().stream() + .map(node -> vl.getNodeBreakerView().getTerminal(node)) + .filter(Objects::nonNull) + .map(Terminal::getConnectable) + .filter(t -> t.getType() == IdentifiableType.BUSBAR_SECTION) + .map(Identifiable::getId) + .toList(); - return formatNodeId(nodesIds, nodeBreakerViolationLocation.getVoltageLevelId()); + return formatNodeId(busbarSectionIds, nodeBreakerViolationLocation.getVoltageLevelId()); } public static String formatNodeId(List nodesIds, String subjectId) { diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java index d1c26e4a..e021d8d6 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java @@ -138,19 +138,19 @@ void testGetIdFromViolationWithNoViolationLocation() { @Test void testGetIdFromViolationWithNodeBreaker() { - Network network = createNetwork("", true); + // Create a real network instead of mocking it + Network network = createNetwork("testNetwork", true); + NodeBreakerViolationLocation nodeBreakerViolationLocation = mock(NodeBreakerViolationLocation.class); - ViolationLocation.BusView busView = mock(ViolationLocation.BusView.class); - Bus bus = mock(Bus.class); - when(nodeBreakerViolationLocation.getType()).thenReturn(ViolationLocation.Type.NODE_BREAKER); - when(nodeBreakerViolationLocation.getBusView(network)).thenReturn(busView); - when(busView.getBusStream()).thenReturn(Stream.of(bus)); - when(bus.getId()).thenReturn("NGEN"); + when(nodeBreakerViolationLocation.getVoltageLevelId()).thenReturn("VL1"); + LimitViolation limitViolation = mock(LimitViolation.class); when(limitViolation.getViolationLocation()).thenReturn(Optional.of(nodeBreakerViolationLocation)); + when(limitViolation.getSubjectId()).thenReturn("SubjectId"); - assertEquals("NGEN", LoadflowResultsUtils.getIdFromViolation(limitViolation, network)); + String result = LoadflowResultsUtils.getIdFromViolation(limitViolation, network); + assertEquals("VL1", result); } } From d3385c79220c6fead50184fd245e6ca7f1ac9a10 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Mon, 25 Nov 2024 16:04:45 +0100 Subject: [PATCH 08/16] fix styles --- .../org/gridsuite/loadflow/server/LoadFlowServiceTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java index e021d8d6..f6380c26 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java @@ -1,6 +1,5 @@ package org.gridsuite.loadflow.server; -import com.powsybl.iidm.network.Bus; import com.powsybl.iidm.network.Network; import com.powsybl.security.*; import org.gridsuite.loadflow.server.dto.LimitViolationInfos; @@ -20,7 +19,6 @@ import java.util.List; import java.util.Optional; import java.util.UUID; -import java.util.stream.Stream; import static org.gridsuite.loadflow.server.Networks.createNetwork; import static org.junit.jupiter.api.Assertions.*; @@ -140,11 +138,10 @@ void testGetIdFromViolationWithNoViolationLocation() { void testGetIdFromViolationWithNodeBreaker() { // Create a real network instead of mocking it Network network = createNetwork("testNetwork", true); - + NodeBreakerViolationLocation nodeBreakerViolationLocation = mock(NodeBreakerViolationLocation.class); when(nodeBreakerViolationLocation.getType()).thenReturn(ViolationLocation.Type.NODE_BREAKER); when(nodeBreakerViolationLocation.getVoltageLevelId()).thenReturn("VL1"); - LimitViolation limitViolation = mock(LimitViolation.class); when(limitViolation.getViolationLocation()).thenReturn(Optional.of(nodeBreakerViolationLocation)); From 203779fa4de8491e633bd176e2a433d8d43b74ad Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 26 Nov 2024 10:42:37 +0100 Subject: [PATCH 09/16] replace Busbar ids , by bus id --- .../loadflow/server/utils/LoadflowResultsUtils.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java index b0561f41..60f5d8ef 100644 --- a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java +++ b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java @@ -35,15 +35,14 @@ public static String getIdFromViolation(LimitViolation limitViolation, Network n public static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { VoltageLevel vl = network.getVoltageLevel(nodeBreakerViolationLocation.getVoltageLevelId()); - List busbarSectionIds = nodeBreakerViolationLocation.getNodes().stream() + List busIds = nodeBreakerViolationLocation.getNodes().stream() .map(node -> vl.getNodeBreakerView().getTerminal(node)) .filter(Objects::nonNull) .map(Terminal::getConnectable) - .filter(t -> t.getType() == IdentifiableType.BUSBAR_SECTION) + .filter(t -> t.getType() == IdentifiableType.BUS) .map(Identifiable::getId) .toList(); - - return formatNodeId(busbarSectionIds, nodeBreakerViolationLocation.getVoltageLevelId()); + return formatNodeId(busIds, nodeBreakerViolationLocation.getVoltageLevelId()); } public static String formatNodeId(List nodesIds, String subjectId) { From 698ed4ebf23135a4a1b8b8c9dc8126e232c9caf4 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Tue, 26 Nov 2024 17:29:59 +0100 Subject: [PATCH 10/16] update implementation , return node id --- .../server/utils/LoadflowResultsUtils.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java index 60f5d8ef..b92fc284 100644 --- a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java +++ b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java @@ -6,9 +6,7 @@ import com.powsybl.security.NodeBreakerViolationLocation; import com.powsybl.security.ViolationLocation; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import static com.powsybl.security.ViolationLocation.Type.NODE_BREAKER; @@ -35,13 +33,18 @@ public static String getIdFromViolation(LimitViolation limitViolation, Network n public static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { VoltageLevel vl = network.getVoltageLevel(nodeBreakerViolationLocation.getVoltageLevelId()); + List busIds = nodeBreakerViolationLocation.getNodes().stream() .map(node -> vl.getNodeBreakerView().getTerminal(node)) .filter(Objects::nonNull) .map(Terminal::getConnectable) - .filter(t -> t.getType() == IdentifiableType.BUS) - .map(Identifiable::getId) + .filter(t -> t.getType() == IdentifiableType.BUSBAR_SECTION) + .map(busBar -> ((BusbarSection) busBar).getTerminal().getBusView().getBus()) + .filter(Objects::nonNull) + .map(Bus::getId) + .distinct() .toList(); + return formatNodeId(busIds, nodeBreakerViolationLocation.getVoltageLevelId()); } @@ -56,8 +59,11 @@ public static String formatNodeId(List nodesIds, String subjectId) { } public static String getBusIdOrVlIdBusBreaker(BusBreakerViolationLocation busBreakerViolationLocation, Network network, String subjectId) { - List busBreakerIds = busBreakerViolationLocation.getBusBreakerView(network) - .getBusStream().map(Identifiable::getId).toList(); + List busBreakerIds = busBreakerViolationLocation + .getBusBreakerView(network) + .getBusStream() + .map(Identifiable::getId) + .distinct().toList(); return formatNodeId(busBreakerIds, subjectId); } From c23547593959c408f46ad48d05196e4d93ee8fc1 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Mon, 2 Dec 2024 11:24:19 +0100 Subject: [PATCH 11/16] return bus section id in node breaker --- .../loadflow/server/service/LoadFlowWorkerService.java | 4 ++-- .../loadflow/server/utils/LoadflowResultsUtils.java | 10 ++++------ .../gridsuite/loadflow/server/LoadFlowServiceTest.java | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index 0da4b916..95302ef4 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -43,7 +43,7 @@ import java.util.function.Consumer; import static org.gridsuite.loadflow.server.service.LoadFlowService.COMPUTATION_TYPE; -import static org.gridsuite.loadflow.server.utils.LoadflowResultsUtils.getIdFromViolation; +import static org.gridsuite.loadflow.server.utils.LoadflowResultsUtils.getViolationLocationId; /** * @author Anis Touri @@ -193,7 +193,7 @@ protected List calculateOverloadLimitViolations(List violationLocation = limitViolation.getViolationLocation(); if (violationLocation.isEmpty()) { return limitViolation.getSubjectId(); @@ -34,18 +34,16 @@ public static String getIdFromViolation(LimitViolation limitViolation, Network n public static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { VoltageLevel vl = network.getVoltageLevel(nodeBreakerViolationLocation.getVoltageLevelId()); - List busIds = nodeBreakerViolationLocation.getNodes().stream() + List busBarIds = nodeBreakerViolationLocation.getNodes().stream() .map(node -> vl.getNodeBreakerView().getTerminal(node)) .filter(Objects::nonNull) .map(Terminal::getConnectable) .filter(t -> t.getType() == IdentifiableType.BUSBAR_SECTION) - .map(busBar -> ((BusbarSection) busBar).getTerminal().getBusView().getBus()) - .filter(Objects::nonNull) - .map(Bus::getId) + .map(Identifiable::getId) .distinct() .toList(); - return formatNodeId(busIds, nodeBreakerViolationLocation.getVoltageLevelId()); + return formatNodeId(busBarIds, nodeBreakerViolationLocation.getVoltageLevelId()); } public static String formatNodeId(List nodesIds, String subjectId) { diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java index f6380c26..3938e6f5 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java @@ -119,7 +119,7 @@ void testGetIdFromViolationWithBusBreaker() { LimitViolation limitViolation = mock(LimitViolation.class); when(limitViolation.getViolationLocation()).thenReturn(Optional.of(busBreakerLocation)); - assertEquals("NGEN", LoadflowResultsUtils.getIdFromViolation(limitViolation, network)); + assertEquals("NGEN", LoadflowResultsUtils.getViolationLocationId(limitViolation, network)); } @Test @@ -131,7 +131,7 @@ void testGetIdFromViolationWithNoViolationLocation() { when(limitViolation.getViolationLocation()).thenReturn(Optional.empty()); when(limitViolation.getSubjectId()).thenReturn("SubjectId"); - assertEquals("SubjectId", LoadflowResultsUtils.getIdFromViolation(limitViolation, network)); + assertEquals("SubjectId", LoadflowResultsUtils.getViolationLocationId(limitViolation, network)); } @Test @@ -147,7 +147,7 @@ void testGetIdFromViolationWithNodeBreaker() { when(limitViolation.getViolationLocation()).thenReturn(Optional.of(nodeBreakerViolationLocation)); when(limitViolation.getSubjectId()).thenReturn("SubjectId"); - String result = LoadflowResultsUtils.getIdFromViolation(limitViolation, network); + String result = LoadflowResultsUtils.getViolationLocationId(limitViolation, network); assertEquals("VL1", result); } } From 50f14d92fb19f9e1ca7f8e1766aae39502489919 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Mon, 2 Dec 2024 15:46:46 +0100 Subject: [PATCH 12/16] extract utility classes to powsybl-ws-commons Signed-off-by: jamal-khey --- pom.xml | 1 + .../server/service/LoadFlowWorkerService.java | 2 +- .../server/utils/LoadflowResultsUtils.java | 68 ------------------- .../loadflow/server/LoadFlowServiceTest.java | 61 ----------------- 4 files changed, 2 insertions(+), 130 deletions(-) delete mode 100644 src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java diff --git a/pom.xml b/pom.xml index 95d7d73e..5cb66f3b 100644 --- a/pom.xml +++ b/pom.xml @@ -136,6 +136,7 @@ com.powsybl powsybl-ws-commons + 1.17.0-SNAPSHOT org.springframework.boot diff --git a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java index 95302ef4..fbd342ef 100644 --- a/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java +++ b/src/main/java/org/gridsuite/loadflow/server/service/LoadFlowWorkerService.java @@ -42,8 +42,8 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; +import static com.powsybl.ws.commons.computation.utils.ComputationResultUtils.getViolationLocationId; import static org.gridsuite.loadflow.server.service.LoadFlowService.COMPUTATION_TYPE; -import static org.gridsuite.loadflow.server.utils.LoadflowResultsUtils.getViolationLocationId; /** * @author Anis Touri diff --git a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java b/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java deleted file mode 100644 index 666004e4..00000000 --- a/src/main/java/org/gridsuite/loadflow/server/utils/LoadflowResultsUtils.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.gridsuite.loadflow.server.utils; - -import com.powsybl.iidm.network.*; -import com.powsybl.security.BusBreakerViolationLocation; -import com.powsybl.security.LimitViolation; -import com.powsybl.security.NodeBreakerViolationLocation; -import com.powsybl.security.ViolationLocation; - -import java.util.*; - -import static com.powsybl.security.ViolationLocation.Type.NODE_BREAKER; - -public final class LoadflowResultsUtils { - - private LoadflowResultsUtils() { - } - - public static String getViolationLocationId(LimitViolation limitViolation, Network network) { - Optional violationLocation = limitViolation.getViolationLocation(); - if (violationLocation.isEmpty()) { - return limitViolation.getSubjectId(); - } - - ViolationLocation location = violationLocation.get(); - if (location.getType() == NODE_BREAKER) { - NodeBreakerViolationLocation nodeBreakerViolationLocation = (NodeBreakerViolationLocation) location; - return getBusIdOrVlIdNodeBreaker(nodeBreakerViolationLocation, network); - } else { - BusBreakerViolationLocation busBreakerViolationLocation = (BusBreakerViolationLocation) location; - return getBusIdOrVlIdBusBreaker(busBreakerViolationLocation, network, limitViolation.getSubjectId()); - } - } - - public static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) { - VoltageLevel vl = network.getVoltageLevel(nodeBreakerViolationLocation.getVoltageLevelId()); - - List busBarIds = nodeBreakerViolationLocation.getNodes().stream() - .map(node -> vl.getNodeBreakerView().getTerminal(node)) - .filter(Objects::nonNull) - .map(Terminal::getConnectable) - .filter(t -> t.getType() == IdentifiableType.BUSBAR_SECTION) - .map(Identifiable::getId) - .distinct() - .toList(); - - return formatNodeId(busBarIds, nodeBreakerViolationLocation.getVoltageLevelId()); - } - - public static String formatNodeId(List nodesIds, String subjectId) { - if (nodesIds.size() == 1) { - return nodesIds.get(0); - } else if (nodesIds.isEmpty()) { - return subjectId; - } else { - return subjectId + " (" + String.join(", ", nodesIds) + " )"; - } - } - - public static String getBusIdOrVlIdBusBreaker(BusBreakerViolationLocation busBreakerViolationLocation, Network network, String subjectId) { - List busBreakerIds = busBreakerViolationLocation - .getBusBreakerView(network) - .getBusStream() - .map(Identifiable::getId) - .distinct().toList(); - return formatNodeId(busBreakerIds, subjectId); - } - -} diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java index 3938e6f5..ca50492f 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowServiceTest.java @@ -1,13 +1,11 @@ package org.gridsuite.loadflow.server; -import com.powsybl.iidm.network.Network; import com.powsybl.security.*; import org.gridsuite.loadflow.server.dto.LimitViolationInfos; import org.gridsuite.loadflow.server.entities.LimitViolationEntity; import org.gridsuite.loadflow.server.entities.LoadFlowResultEntity; import org.gridsuite.loadflow.server.repositories.LimitViolationRepository; import org.gridsuite.loadflow.server.service.LoadFlowService; -import org.gridsuite.loadflow.server.utils.LoadflowResultsUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -17,10 +15,8 @@ import java.util.Arrays; import java.util.List; -import java.util.Optional; import java.util.UUID; -import static org.gridsuite.loadflow.server.Networks.createNetwork; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -93,61 +89,4 @@ void getLimitViolationsInfosWhenCalledReturnsCorrectData() { verify(limitViolationRepository, times(1)).findAll(any(Specification.class), eq(sort)); } - @Test - void testFormatNodeIdSingleNode() { - String result = LoadflowResultsUtils.formatNodeId(List.of("Node1"), "Subject1"); - assertEquals("Node1", result); - } - - @Test - void testFormatNodeIdEmptyNodes() { - String result = LoadflowResultsUtils.formatNodeId(List.of(), "Subject1"); - assertEquals("Subject1", result); - } - - @Test - void testFormatNodeIdMultipleNodes() { - String result = LoadflowResultsUtils.formatNodeId(List.of("Node1", "Node2"), "Subject1"); - assertEquals("Subject1 (Node1, Node2 )", result); - } - - @Test - void testGetIdFromViolationWithBusBreaker() { - // Setup - Network network = createNetwork("", true); - BusBreakerViolationLocation busBreakerLocation = new BusBreakerViolationLocation(List.of("NGEN")); - LimitViolation limitViolation = mock(LimitViolation.class); - when(limitViolation.getViolationLocation()).thenReturn(Optional.of(busBreakerLocation)); - - assertEquals("NGEN", LoadflowResultsUtils.getViolationLocationId(limitViolation, network)); - } - - @Test - void testGetIdFromViolationWithNoViolationLocation() { - // Setup - Network network = mock(Network.class); - LimitViolation limitViolation = mock(LimitViolation.class); - - when(limitViolation.getViolationLocation()).thenReturn(Optional.empty()); - when(limitViolation.getSubjectId()).thenReturn("SubjectId"); - - assertEquals("SubjectId", LoadflowResultsUtils.getViolationLocationId(limitViolation, network)); - } - - @Test - void testGetIdFromViolationWithNodeBreaker() { - // Create a real network instead of mocking it - Network network = createNetwork("testNetwork", true); - - NodeBreakerViolationLocation nodeBreakerViolationLocation = mock(NodeBreakerViolationLocation.class); - when(nodeBreakerViolationLocation.getType()).thenReturn(ViolationLocation.Type.NODE_BREAKER); - when(nodeBreakerViolationLocation.getVoltageLevelId()).thenReturn("VL1"); - - LimitViolation limitViolation = mock(LimitViolation.class); - when(limitViolation.getViolationLocation()).thenReturn(Optional.of(nodeBreakerViolationLocation)); - when(limitViolation.getSubjectId()).thenReturn("SubjectId"); - - String result = LoadflowResultsUtils.getViolationLocationId(limitViolation, network); - assertEquals("VL1", result); - } } From 399002b086d06bce6f22107cdf72db0bbe71c1eb Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Mon, 2 Dec 2024 18:40:20 +0100 Subject: [PATCH 13/16] add unit test Signed-off-by: jamal-khey --- .../server/LoadFlowControllerTest.java | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java index cd8afe75..c0b0d4d8 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java @@ -11,10 +11,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.powsybl.commons.report.ReportNode; import com.powsybl.computation.local.LocalComputationManager; -import com.powsybl.iidm.network.Country; -import com.powsybl.iidm.network.Network; -import com.powsybl.iidm.network.TwoSides; -import com.powsybl.iidm.network.VariantManagerConstants; +import com.powsybl.iidm.network.*; import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import com.powsybl.loadflow.LoadFlow; import com.powsybl.loadflow.LoadFlowParameters; @@ -23,6 +20,7 @@ import com.powsybl.network.store.client.NetworkStoreService; import com.powsybl.network.store.client.PreloadingStrategy; import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl; +import com.powsybl.security.BusBreakerViolationLocation; import com.powsybl.security.LimitViolation; import com.powsybl.security.LimitViolationType; import com.powsybl.security.Security; @@ -792,4 +790,53 @@ private static final class LimitViolationsMock { new LimitViolation("NHV1_NHV2_2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 900, 0.7F, 1000, TwoSides.ONE), new LimitViolation("NHV1_NHV2_2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 900, 0.7F, 1000, TwoSides.TWO)); } + + private static final class VoltageLimitViolationsMock { + static List limitViolations = List.of( + new LimitViolation("VLGEN", "", LimitViolationType.LOW_VOLTAGE, "limit1", 60, 1500, 0.7F, 1300, ThreeSides.TWO, new BusBreakerViolationLocation(List.of("NHV1"))), + new LimitViolation("VLGEN", "", LimitViolationType.HIGH_VOLTAGE, "limit2", 300, 900, 0.7F, 1000, ThreeSides.ONE, new BusBreakerViolationLocation(List.of("NHV2")))); + } + + @Test + public void testGetLimitViolationsVoltage() throws Exception { + ((Bus) network.getIdentifiable("NHV1")).setV(380.0).getVoltageLevel().setLowVoltageLimit(400.0).setHighVoltageLimit(450.0); + ((Bus) network.getIdentifiable("NHV2")).setV(380.0).getVoltageLevel().setLowVoltageLimit(300.0).setHighVoltageLimit(350.0); + + LoadFlow.Runner runner = Mockito.mock(LoadFlow.Runner.class); + try (MockedStatic loadFlowMockedStatic = Mockito.mockStatic(LoadFlow.class); + MockedStatic securityMockedStatic = Mockito.mockStatic(Security.class)) { + loadFlowMockedStatic.when(() -> LoadFlow.find(any())).thenReturn(runner); + securityMockedStatic.when(() -> Security.checkLimitsDc(any(), any(), anyDouble())).thenReturn(VoltageLimitViolationsMock.limitViolations); + + Mockito.when(runner.runAsync(eq(network), eq(VARIANT_2_ID), eq(executionService.getComputationManager()), + any(LoadFlowParameters.class), any(ReportNode.class))) + .thenReturn(CompletableFuture.completedFuture(LoadFlowResultMock.RESULT)); + + MvcResult result = mockMvc.perform(post( + "/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID + "¶metersUuid=" + PARAMETERS_UUID + "&limitReduction=0.7", NETWORK_UUID) + .header(HEADER_USER_ID, "userId")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class)); + + Message resultMessage = output.receive(1000, "loadflow.result"); + assertEquals(RESULT_UUID.toString(), resultMessage.getHeaders().get("resultUuid")); + assertEquals("me", resultMessage.getHeaders().get("receiver")); + + // get loadflow limit violations + result = mockMvc.perform(get( + "/" + VERSION + "/results/{resultUuid}/limit-violations", RESULT_UUID)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + List limitViolations = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<>() { + }); + assertEquals(2, limitViolations.size()); + // check that the subject id is equal to the Bus Id + assertEquals(limitViolations.get(0).getSubjectId(), "NHV1"); + assertEquals(limitViolations.get(1).getSubjectId(), "NHV2"); + } + } + } From 71b2670912f1baf53db7caf65ccd763253c9be2e Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Thu, 5 Dec 2024 13:15:29 +0100 Subject: [PATCH 14/16] fix test Signed-off-by: jamal-khey --- .../org/gridsuite/loadflow/server/LoadFlowControllerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java index c0b0d4d8..b9fba72a 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java @@ -834,8 +834,8 @@ public void testGetLimitViolationsVoltage() throws Exception { }); assertEquals(2, limitViolations.size()); // check that the subject id is equal to the Bus Id - assertEquals(limitViolations.get(0).getSubjectId(), "NHV1"); - assertEquals(limitViolations.get(1).getSubjectId(), "NHV2"); + assertEquals(limitViolations.get(0).getSubjectId(), "VLHV1_0"); + assertEquals(limitViolations.get(1).getSubjectId(), "VLHV2_0"); } } From 8e93fbc602b4630c826788e9457ebc894acefa10 Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Thu, 5 Dec 2024 16:52:23 +0100 Subject: [PATCH 15/16] moved test fixure data to be a private attribute Signed-off-by: jamal-khey --- .../loadflow/server/LoadFlowControllerTest.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java index b9fba72a..4f0e267d 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java @@ -97,6 +97,10 @@ public class LoadFlowControllerTest { private static final int TIMEOUT = 1000; + private static List limitViolationsWithViolationLocation = List.of( + new LimitViolation("VLGEN", "", LimitViolationType.LOW_VOLTAGE, "limit1", 60, 1500, 0.7F, 1300, ThreeSides.TWO, new BusBreakerViolationLocation(List.of("NHV1"))), + new LimitViolation("VLGEN", "", LimitViolationType.HIGH_VOLTAGE, "limit2", 300, 900, 0.7F, 1000, ThreeSides.ONE, new BusBreakerViolationLocation(List.of("NHV2")))); + @Autowired private OutputDestination output; @Autowired @@ -791,12 +795,6 @@ private static final class LimitViolationsMock { new LimitViolation("NHV1_NHV2_2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 900, 0.7F, 1000, TwoSides.TWO)); } - private static final class VoltageLimitViolationsMock { - static List limitViolations = List.of( - new LimitViolation("VLGEN", "", LimitViolationType.LOW_VOLTAGE, "limit1", 60, 1500, 0.7F, 1300, ThreeSides.TWO, new BusBreakerViolationLocation(List.of("NHV1"))), - new LimitViolation("VLGEN", "", LimitViolationType.HIGH_VOLTAGE, "limit2", 300, 900, 0.7F, 1000, ThreeSides.ONE, new BusBreakerViolationLocation(List.of("NHV2")))); - } - @Test public void testGetLimitViolationsVoltage() throws Exception { ((Bus) network.getIdentifiable("NHV1")).setV(380.0).getVoltageLevel().setLowVoltageLimit(400.0).setHighVoltageLimit(450.0); @@ -806,7 +804,7 @@ public void testGetLimitViolationsVoltage() throws Exception { try (MockedStatic loadFlowMockedStatic = Mockito.mockStatic(LoadFlow.class); MockedStatic securityMockedStatic = Mockito.mockStatic(Security.class)) { loadFlowMockedStatic.when(() -> LoadFlow.find(any())).thenReturn(runner); - securityMockedStatic.when(() -> Security.checkLimitsDc(any(), any(), anyDouble())).thenReturn(VoltageLimitViolationsMock.limitViolations); + securityMockedStatic.when(() -> Security.checkLimitsDc(any(), any(), anyDouble())).thenReturn(limitViolationsWithViolationLocation); Mockito.when(runner.runAsync(eq(network), eq(VARIANT_2_ID), eq(executionService.getComputationManager()), any(LoadFlowParameters.class), any(ReportNode.class))) From 2e14a23050eb5a5133207a461c4317883c5be66b Mon Sep 17 00:00:00 2001 From: Slimane AMAR Date: Fri, 6 Dec 2024 08:22:06 +0100 Subject: [PATCH 16/16] Use the new release of powsybl-ws-commons Signed-off-by: Slimane AMAR --- pom.xml | 6 +++++- .../gridsuite/loadflow/server/LoadFlowControllerTest.java | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 5cb66f3b..0af9ed8b 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,9 @@ Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 4 (expected 3). --> 4.10.1 + + 1.17.0 @@ -136,7 +139,8 @@ com.powsybl powsybl-ws-commons - 1.17.0-SNAPSHOT + + ${powsybl-ws-commons.version} org.springframework.boot diff --git a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java index 4f0e267d..7d3034c6 100644 --- a/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java +++ b/src/test/java/org/gridsuite/loadflow/server/LoadFlowControllerTest.java @@ -97,7 +97,7 @@ public class LoadFlowControllerTest { private static final int TIMEOUT = 1000; - private static List limitViolationsWithViolationLocation = List.of( + private static final List LIMIT_VIOLATIONS_WITH_LOCATION = List.of( new LimitViolation("VLGEN", "", LimitViolationType.LOW_VOLTAGE, "limit1", 60, 1500, 0.7F, 1300, ThreeSides.TWO, new BusBreakerViolationLocation(List.of("NHV1"))), new LimitViolation("VLGEN", "", LimitViolationType.HIGH_VOLTAGE, "limit2", 300, 900, 0.7F, 1000, ThreeSides.ONE, new BusBreakerViolationLocation(List.of("NHV2")))); @@ -804,7 +804,7 @@ public void testGetLimitViolationsVoltage() throws Exception { try (MockedStatic loadFlowMockedStatic = Mockito.mockStatic(LoadFlow.class); MockedStatic securityMockedStatic = Mockito.mockStatic(Security.class)) { loadFlowMockedStatic.when(() -> LoadFlow.find(any())).thenReturn(runner); - securityMockedStatic.when(() -> Security.checkLimitsDc(any(), any(), anyDouble())).thenReturn(limitViolationsWithViolationLocation); + securityMockedStatic.when(() -> Security.checkLimitsDc(any(), any(), anyDouble())).thenReturn(LIMIT_VIOLATIONS_WITH_LOCATION); Mockito.when(runner.runAsync(eq(network), eq(VARIANT_2_ID), eq(executionService.getComputationManager()), any(LoadFlowParameters.class), any(ReportNode.class))) @@ -832,8 +832,8 @@ public void testGetLimitViolationsVoltage() throws Exception { }); assertEquals(2, limitViolations.size()); // check that the subject id is equal to the Bus Id - assertEquals(limitViolations.get(0).getSubjectId(), "VLHV1_0"); - assertEquals(limitViolations.get(1).getSubjectId(), "VLHV2_0"); + assertEquals("VLHV1_0", limitViolations.get(0).getSubjectId()); + assertEquals("VLHV2_0", limitViolations.get(1).getSubjectId()); } }