diff --git a/src/main/java/com/powsybl/openloadflow/graph/EvenShiloachGraphDecrementalConnectivity.java b/src/main/java/com/powsybl/openloadflow/graph/EvenShiloachGraphDecrementalConnectivity.java index 71804b44ee..782d1792a3 100644 --- a/src/main/java/com/powsybl/openloadflow/graph/EvenShiloachGraphDecrementalConnectivity.java +++ b/src/main/java/com/powsybl/openloadflow/graph/EvenShiloachGraphDecrementalConnectivity.java @@ -191,7 +191,7 @@ public List> getSmallComponents() { sortedComponents = true; connectedComponents.sort(Comparator.comparingInt(c -> -c.size())); } - return connectedComponents.subList(1, connectedComponents.size()); + return connectedComponents.isEmpty() ? Collections.emptyList() : connectedComponents.subList(1, connectedComponents.size()); } private interface GraphProcess { diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisGraphTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisGraphTest.java index 86e55d5588..5227e868ad 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisGraphTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisGraphTest.java @@ -33,14 +33,10 @@ import org.slf4j.LoggerFactory; import javax.inject.Provider; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; /** * @author Florian Dupuy @@ -50,7 +46,6 @@ class OpenSecurityAnalysisGraphTest { private static final Logger LOGGER = LoggerFactory.getLogger(OpenSecurityAnalysisGraphTest.class); private Network network; - private List> reference; private ContingenciesProvider contingenciesProvider; private SecurityAnalysisParameters securityAnalysisParameters; @@ -69,7 +64,6 @@ void setUp() { new OpenLoadFlowParameters().setSlackBusSelector(new FirstSlackBusSelector())); securityAnalysisParameters = new SecurityAnalysisParameters().setLoadFlowParameters(lfParameters); - reference = getLoadFlowContingencies(() -> new NaiveGraphDecrementalConnectivity<>(LfBus::getNum)); LOGGER.info("Reference established (naive connectivity calculation) on test network containing {} branches", network.getBranchCount()); } @@ -78,7 +72,7 @@ void testEvenShiloach() { LOGGER.info("Test Even-Shiloach on test network containing {} branches", network.getBranchCount()); List> lfContingencies = getLoadFlowContingencies(EvenShiloachGraphDecrementalConnectivity::new); printResult(lfContingencies); - checkResult(lfContingencies); + checkResult(lfContingencies, computeReference()); } @Test @@ -86,10 +80,36 @@ void testMst() { LOGGER.info("Test Minimum Spanning Tree on test network containing {} branches", network.getBranchCount()); List> lfContingencies = getLoadFlowContingencies(MinimumSpanningTreeGraphDecrementalConnectivity::new); printResult(lfContingencies); - checkResult(lfContingencies); + checkResult(lfContingencies, computeReference()); } - private void checkResult(List> result) { + private List> computeReference() { + return getLoadFlowContingencies(() -> new NaiveGraphDecrementalConnectivity<>(LfBus::getNum)); + } + + @Test + void testNullVertices() { + try { + network.getSwitch("B3").setOpen(true); + contingenciesProvider = n -> Collections.singletonList( + new Contingency("L1", new BranchContingency("L1"))); + List> reference = computeReference(); + checkResult(getLoadFlowContingencies(MinimumSpanningTreeGraphDecrementalConnectivity::new), reference); + checkResult(getLoadFlowContingencies(EvenShiloachGraphDecrementalConnectivity::new), reference); + + contingenciesProvider = n -> Collections.singletonList( + new Contingency("L2", new BranchContingency("L2"))); + network.getSwitch("B3").setOpen(false); + network.getSwitch("B1").setOpen(true); + reference = computeReference(); + checkResult(getLoadFlowContingencies(MinimumSpanningTreeGraphDecrementalConnectivity::new), reference); + checkResult(getLoadFlowContingencies(EvenShiloachGraphDecrementalConnectivity::new), reference); + } catch (NullPointerException e) { + fail(); + } + } + + private static void checkResult(List> result, List> reference) { assertEquals(reference.size(), result.size()); for (int iNetwork = 0; iNetwork < result.size(); iNetwork++) { assertEquals(reference.get(iNetwork).size(), result.get(iNetwork).size());