diff --git a/src/main/java/com/powsybl/openloadflow/sa/ContingencyParameters.java b/src/main/java/com/powsybl/openloadflow/sa/extensions/ContingencyLoadFlowParameters.java similarity index 81% rename from src/main/java/com/powsybl/openloadflow/sa/ContingencyParameters.java rename to src/main/java/com/powsybl/openloadflow/sa/extensions/ContingencyLoadFlowParameters.java index 740ea6c200..c9a17b3a43 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/ContingencyParameters.java +++ b/src/main/java/com/powsybl/openloadflow/sa/extensions/ContingencyLoadFlowParameters.java @@ -5,7 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. * SPDX-License-Identifier: MPL-2.0 */ -package com.powsybl.openloadflow.sa; +package com.powsybl.openloadflow.sa.extensions; import com.powsybl.commons.extensions.AbstractExtension; import com.powsybl.contingency.Contingency; @@ -14,7 +14,7 @@ /** * @author Valentin Mouradian {@literal } */ -public class ContingencyParameters extends AbstractExtension { +public class ContingencyLoadFlowParameters extends AbstractExtension { private boolean distributedSlack; @@ -22,7 +22,7 @@ public class ContingencyParameters extends AbstractExtension { private LoadFlowParameters.BalanceType balanceType; - public ContingencyParameters(boolean distributedSlack, boolean areaInterchangeControl, LoadFlowParameters.BalanceType balanceType) { + public ContingencyLoadFlowParameters(boolean distributedSlack, boolean areaInterchangeControl, LoadFlowParameters.BalanceType balanceType) { this.distributedSlack = distributedSlack; this.areaInterchangeControl = areaInterchangeControl; this.balanceType = balanceType; diff --git a/src/main/java/com/powsybl/openloadflow/sa/ContingencyParametersJsonSerializer.java b/src/main/java/com/powsybl/openloadflow/sa/extensions/ContingencyLoadFlowParametersJsonSerializer.java similarity index 61% rename from src/main/java/com/powsybl/openloadflow/sa/ContingencyParametersJsonSerializer.java rename to src/main/java/com/powsybl/openloadflow/sa/extensions/ContingencyLoadFlowParametersJsonSerializer.java index b2e25ccf85..de15cb0306 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/ContingencyParametersJsonSerializer.java +++ b/src/main/java/com/powsybl/openloadflow/sa/extensions/ContingencyLoadFlowParametersJsonSerializer.java @@ -1,4 +1,4 @@ -package com.powsybl.openloadflow.sa; +package com.powsybl.openloadflow.sa.extensions; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.core.JsonGenerator; @@ -14,7 +14,7 @@ import java.io.IOException; @AutoService(ExtensionJsonSerializer.class) -public class ContingencyParametersJsonSerializer implements ExtensionJsonSerializer { +public class ContingencyLoadFlowParametersJsonSerializer implements ExtensionJsonSerializer { @Override public String getExtensionName() { @@ -27,8 +27,8 @@ public String getCategoryName() { } @Override - public Class getExtensionClass() { - return ContingencyParameters.class; + public Class getExtensionClass() { + return ContingencyLoadFlowParameters.class; } /** @@ -45,16 +45,16 @@ private interface SerializationSpec { private static ObjectMapper createMapper() { return JsonUtil.createObjectMapper() - .addMixIn(ContingencyParameters.class, SerializationSpec.class); + .addMixIn(ContingencyLoadFlowParameters.class, SerializationSpec.class); } @Override - public void serialize(ContingencyParameters extension, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + public void serialize(ContingencyLoadFlowParameters extension, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { createMapper().writeValue(jsonGenerator, extension); } @Override - public ContingencyParameters deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { - return createMapper().readValue(jsonParser, ContingencyParameters.class); + public ContingencyLoadFlowParameters deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { + return createMapper().readValue(jsonParser, ContingencyLoadFlowParameters.class); } } diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisProviderTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisProviderTest.java index 41bd249eaa..f8d4c879d3 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisProviderTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisProviderTest.java @@ -10,10 +10,6 @@ import com.powsybl.commons.config.InMemoryPlatformConfig; import com.powsybl.commons.config.MapModuleConfig; import com.powsybl.commons.test.AbstractSerDeTest; -import com.powsybl.contingency.BranchContingency; -import com.powsybl.contingency.Contingency; -import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.openloadflow.util.JsonSerializer; import com.powsybl.openloadflow.util.PowsyblOpenLoadFlowVersion; import com.powsybl.openloadflow.util.ProviderConstants; import com.powsybl.security.SecurityAnalysisParameters; @@ -109,25 +105,4 @@ void jsonTest() throws IOException { parameters.addExtension(OpenSecurityAnalysisParameters.class, parametersExt); roundTripTest(parameters, JsonSecurityAnalysisParameters::write, JsonSecurityAnalysisParameters::read, "/sa-params.json"); } - - @Test - void testContingencyParametersExtension() { - Contingency contingency = new Contingency("L2", new BranchContingency("L2")); - contingency.addExtension(ContingencyParameters.class, new ContingencyParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD)); - - ContingencyParameters contingencyParameters = contingency.getExtension(ContingencyParameters.class); - - assertEquals(contingencyParameters, contingency.getExtensionByName("contingency-parameters")); - assertFalse(contingencyParameters.isDistributedSlack()); - assertTrue(contingencyParameters.isAreaInterchangeControl()); - assertEquals(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, contingencyParameters.getBalanceType()); - } - - @Test - void testContingencyParametersExtensionJson() throws IOException { - Contingency contingency = new Contingency("L2", new BranchContingency("L2")); - contingency.addExtension(ContingencyParameters.class, new ContingencyParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD)); - assertEquals(ContingencyParameters.class, new ContingencyParametersJsonSerializer().getExtensionClass()); - roundTripTest(contingency, JsonSerializer::write, JsonSerializer::readContingency, "/contingencies.json"); - } } diff --git a/src/test/java/com/powsybl/openloadflow/sa/extensions/OpenSecurityAnalysisExtensionsTest.java b/src/test/java/com/powsybl/openloadflow/sa/extensions/OpenSecurityAnalysisExtensionsTest.java new file mode 100644 index 0000000000..6ad377788b --- /dev/null +++ b/src/test/java/com/powsybl/openloadflow/sa/extensions/OpenSecurityAnalysisExtensionsTest.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2024, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/) + * 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.openloadflow.sa.extensions; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.powsybl.commons.json.JsonUtil; +import com.powsybl.commons.test.AbstractSerDeTest; +import com.powsybl.contingency.BranchContingency; +import com.powsybl.contingency.Contingency; +import com.powsybl.contingency.json.ContingencyJsonModule; +import com.powsybl.loadflow.LoadFlowParameters; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Valentin Mouradian {@literal } + */ +class OpenSecurityAnalysisExtensionsTest extends AbstractSerDeTest { + + @Test + void testContingencyParametersExtension() { + Contingency contingency = new Contingency("L2", new BranchContingency("L2")); + contingency.addExtension(ContingencyLoadFlowParameters.class, new ContingencyLoadFlowParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD)); + + ContingencyLoadFlowParameters contingencyLoadFlowParameters = contingency.getExtension(ContingencyLoadFlowParameters.class); + + assertEquals(contingencyLoadFlowParameters, contingency.getExtensionByName("contingency-parameters")); + assertFalse(contingencyLoadFlowParameters.isDistributedSlack()); + assertTrue(contingencyLoadFlowParameters.isAreaInterchangeControl()); + assertEquals(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, contingencyLoadFlowParameters.getBalanceType()); + } + + @Test + void testContingencyParametersExtensionJson() throws IOException { + Contingency contingency = new Contingency("L2", new BranchContingency("L2")); + contingency.addExtension(ContingencyLoadFlowParameters.class, new ContingencyLoadFlowParameters(false, true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD)); + assertEquals(ContingencyLoadFlowParameters.class, new ContingencyLoadFlowParametersJsonSerializer().getExtensionClass()); + roundTripTest(contingency, OpenSecurityAnalysisExtensionsTest::writeContingency, OpenSecurityAnalysisExtensionsTest::readContingency, "/contingencies.json"); + } + + public static Contingency readContingency(Path jsonFile) { + Objects.requireNonNull(jsonFile); + + try (InputStream is = Files.newInputStream(jsonFile)) { + ObjectMapper objectMapper = JsonUtil.createObjectMapper(); + ContingencyJsonModule module = new ContingencyJsonModule(); + objectMapper.registerModule(module); + + return objectMapper.readValue(is, Contingency.class); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + public static void writeContingency(Contingency contingency, Path jsonFile) { + Objects.requireNonNull(contingency); + Objects.requireNonNull(jsonFile); + + try (OutputStream os = Files.newOutputStream(jsonFile)) { + ObjectMapper mapper = JsonUtil.createObjectMapper(); + ContingencyJsonModule module = new ContingencyJsonModule(); + mapper.registerModule(module); + + ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); + writer.writeValue(os, contingency); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + +} diff --git a/src/test/java/com/powsybl/openloadflow/util/JsonSerializer.java b/src/test/java/com/powsybl/openloadflow/util/JsonSerializer.java deleted file mode 100644 index 6e506f6c90..0000000000 --- a/src/test/java/com/powsybl/openloadflow/util/JsonSerializer.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright (c) 2024, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/) - * 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/. - * SPDX-License-Identifier: MPL-2.0 - */ -package com.powsybl.openloadflow.util; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; -import com.powsybl.commons.json.JsonUtil; -import com.powsybl.contingency.Contingency; -import com.powsybl.contingency.json.ContingencyJsonModule; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UncheckedIOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Objects; - -/** - * @author Valentin Mouradian {@literal } - */ -public final class JsonSerializer { - - private JsonSerializer() { - } - - private static T read(Path jsonFile, Class clazz) { - Objects.requireNonNull(jsonFile); - - try (InputStream is = Files.newInputStream(jsonFile)) { - ObjectMapper objectMapper = JsonUtil.createObjectMapper(); - ContingencyJsonModule module = new ContingencyJsonModule(); - objectMapper.registerModule(module); - - return (T) objectMapper.readValue(is, clazz); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - public static Contingency readContingency(Path jsonFile) { - return read(jsonFile, Contingency.class); - } - - public static void write(T object, Path jsonFile) { - Objects.requireNonNull(object); - Objects.requireNonNull(jsonFile); - - try (OutputStream os = Files.newOutputStream(jsonFile)) { - ObjectMapper mapper = JsonUtil.createObjectMapper(); - ContingencyJsonModule module = new ContingencyJsonModule(); - mapper.registerModule(module); - - ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); - writer.writeValue(os, object); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } -}