From 2825aeb0fe9ef37470f64b63817c58ca43585bcf Mon Sep 17 00:00:00 2001 From: Romain Courtier Date: Fri, 15 Nov 2024 08:49:32 +0100 Subject: [PATCH] Use throwException boolean argument to log a message or throw an error in case of incorrect input data Signed-off-by: Romain Courtier --- .../iidm/modification/SetGeneratorToLocalRegulation.java | 4 +++- .../iidm/modification/SetGeneratorToLocalRegulationTest.java | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/iidm/iidm-modification/src/main/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulation.java b/iidm/iidm-modification/src/main/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulation.java index 7f03be89784..c8849c26e25 100644 --- a/iidm/iidm-modification/src/main/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulation.java +++ b/iidm/iidm-modification/src/main/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulation.java @@ -45,7 +45,9 @@ public String getName() { @Override public void apply(Network network, NamingStrategy namingStrategy, boolean throwException, ComputationManager computationManager, ReportNode reportNode) { Generator generator = network.getGenerator(generatorId); - if (generator != null && !generator.getId().equals(generator.getRegulatingTerminal().getConnectable().getId())) { + if (generator == null) { + logOrThrow(throwException, "Generator '" + generatorId + "' not found"); + } else if (!generator.getId().equals(generator.getRegulatingTerminal().getConnectable().getId())) { setLocalRegulation(generator, reportNode); } } diff --git a/iidm/iidm-modification/src/test/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulationTest.java b/iidm/iidm-modification/src/test/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulationTest.java index 17ec719b563..f3dc2e95aed 100644 --- a/iidm/iidm-modification/src/test/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulationTest.java +++ b/iidm/iidm-modification/src/test/java/com/powsybl/iidm/modification/SetGeneratorToLocalRegulationTest.java @@ -1,5 +1,6 @@ package com.powsybl.iidm.modification; +import com.powsybl.commons.PowsyblException; import com.powsybl.commons.report.ReportNode; import com.powsybl.commons.test.TestUtil; import com.powsybl.iidm.network.*; @@ -38,6 +39,9 @@ void setLocalRegulationTest() throws IOException { .withMessageTemplate("rootReportNode", "Set generators to local regulation").build(); new SetGeneratorToLocalRegulation("GEN1").apply(network, reportNode); new SetGeneratorToLocalRegulation("GEN2").apply(network, reportNode); + SetGeneratorToLocalRegulation modification = new SetGeneratorToLocalRegulation("WRONG_ID"); + PowsyblException e = assertThrows(PowsyblException.class, () -> modification.apply(network, true, reportNode)); + assertEquals("Generator 'WRONG_ID' not found", e.getMessage()); // After applying the network modification, both generators regulate locally at 1.05 pu (21 kV). assertEquals(gen1.getId(), gen1.getRegulatingTerminal().getConnectable().getId());