From 9455229696130c65a85c50dff629d6e63086c16e Mon Sep 17 00:00:00 2001 From: Coline Piloquet <55250145+colinepiloquet@users.noreply.github.com> Date: Wed, 3 Nov 2021 09:46:03 +0100 Subject: [PATCH] Add xml serializer and tests (#1883) Signed-off-by: Coline Piloquet --- .../GeneratorShortCircuitXmlSerializer.java | 53 ++++++++++++++++++ ...IdentifiableShortCircuitXmlSerializer.java | 50 +++++++++++++++++ .../resources/xsd/generatorShortCircuit.xsd | 13 +++++ .../xsd/identifiableShortCircuit.xsd | 12 +++++ ...eneratorShortCircuitXmlSerializerTest.java | 54 +++++++++++++++++++ ...tifiableShortCircuitXmlSerializerTest.java | 52 ++++++++++++++++++ .../generatorShortCircuitRef.xml | 45 ++++++++++++++++ .../voltageLevelShortCircuitRef.xml | 45 ++++++++++++++++ 8 files changed, 324 insertions(+) create mode 100644 iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializer.java create mode 100644 iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializer.java create mode 100644 iidm/iidm-xml-converter/src/main/resources/xsd/generatorShortCircuit.xsd create mode 100644 iidm/iidm-xml-converter/src/main/resources/xsd/identifiableShortCircuit.xsd create mode 100644 iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializerTest.java create mode 100644 iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializerTest.java create mode 100644 iidm/iidm-xml-converter/src/test/resources/shortcircuits/generatorShortCircuitRef.xml create mode 100644 iidm/iidm-xml-converter/src/test/resources/shortcircuits/voltageLevelShortCircuitRef.xml diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializer.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializer.java new file mode 100644 index 00000000000..859f3193c20 --- /dev/null +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializer.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2021, 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 com.powsybl.iidm.xml.extensions; + +import com.google.auto.service.AutoService; +import com.powsybl.commons.extensions.AbstractExtensionXmlSerializer; +import com.powsybl.commons.extensions.ExtensionXmlSerializer; +import com.powsybl.commons.xml.XmlReaderContext; +import com.powsybl.commons.xml.XmlUtil; +import com.powsybl.commons.xml.XmlWriterContext; +import com.powsybl.iidm.network.Generator; +import com.powsybl.iidm.network.extensions.GeneratorShortCircuit; +import com.powsybl.iidm.network.extensions.GeneratorShortCircuitAdder; + +import javax.xml.stream.XMLStreamException; + +/** + * + * @author Coline Piloquet + */ +@AutoService(ExtensionXmlSerializer.class) +public class GeneratorShortCircuitXmlSerializer extends AbstractExtensionXmlSerializer { + + public GeneratorShortCircuitXmlSerializer() { + super("generatorShortCircuit", "network", GeneratorShortCircuit.class, false, + "generatorShortCircuit.xsd", "http://www.itesla_project.eu/schema/iidm/ext/generator_short_circuit/1_0", + "gsc"); + } + + @Override + public void write(GeneratorShortCircuit generatorShortCircuit, XmlWriterContext context) throws XMLStreamException { + XmlUtil.writeDouble("directSubtransX", generatorShortCircuit.getDirectSubtransX(), context.getWriter()); + XmlUtil.writeDouble("directTransX", generatorShortCircuit.getDirectTransX(), context.getWriter()); + XmlUtil.writeDouble("stepUpTransformerX", generatorShortCircuit.getStepUpTransformerX(), context.getWriter()); + } + + @Override + public GeneratorShortCircuit read(Generator generator, XmlReaderContext context) throws XMLStreamException { + double directSubtransX = XmlUtil.readDoubleAttribute(context.getReader(), "directSubtransX"); + double directTransX = XmlUtil.readDoubleAttribute(context.getReader(), "directTransX"); + double stepUpTransformerX = XmlUtil.readOptionalDoubleAttribute(context.getReader(), "stepUpTransformerX"); + generator.newExtension(GeneratorShortCircuitAdder.class) + .withDirectSubtransX(directSubtransX) + .withDirectTransX(directTransX) + .withStepUpTransformerX(stepUpTransformerX) + .add(); + return generator.getExtension(GeneratorShortCircuit.class); + } +} diff --git a/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializer.java b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializer.java new file mode 100644 index 00000000000..f582f0bc542 --- /dev/null +++ b/iidm/iidm-xml-converter/src/main/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializer.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2021, 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 com.powsybl.iidm.xml.extensions; + +import com.google.auto.service.AutoService; +import com.powsybl.commons.extensions.AbstractExtensionXmlSerializer; +import com.powsybl.commons.extensions.ExtensionXmlSerializer; +import com.powsybl.commons.xml.XmlReaderContext; +import com.powsybl.commons.xml.XmlUtil; +import com.powsybl.commons.xml.XmlWriterContext; +import com.powsybl.iidm.network.Identifiable; +import com.powsybl.iidm.network.extensions.IdentifiableShortCircuit; +import com.powsybl.iidm.network.extensions.IdentifiableShortCircuitAdder; + +import javax.xml.stream.XMLStreamException; + +/** + * + * @author Coline Piloquet + */ +@AutoService(ExtensionXmlSerializer.class) +public class IdentifiableShortCircuitXmlSerializer> extends AbstractExtensionXmlSerializer> { + + public IdentifiableShortCircuitXmlSerializer() { + super("identifiableShortCircuit", "network", IdentifiableShortCircuit.class, false, + "identifiableShortCircuit.xsd", "http://www.powsybl.org/schema/iidm/ext/identifiable_short_circuit/1_0", + "isc"); + } + + @Override + public void write(IdentifiableShortCircuit identifiableShortCircuit, XmlWriterContext context) throws XMLStreamException { + XmlUtil.writeDouble("ipMax", identifiableShortCircuit.getIpMax(), context.getWriter()); + XmlUtil.writeDouble("ipMin", identifiableShortCircuit.getIpMin(), context.getWriter()); + } + + @Override + public IdentifiableShortCircuit read(I identifiable, XmlReaderContext context) throws XMLStreamException { + double ipMax = XmlUtil.readDoubleAttribute(context.getReader(), "ipMax"); + double ipMin = XmlUtil.readDoubleAttribute(context.getReader(), "ipMin"); + identifiable.newExtension(IdentifiableShortCircuitAdder.class) + .withIpMax(ipMax) + .withIpMin(ipMin) + .add(); + return identifiable.getExtension(IdentifiableShortCircuit.class); + } +} diff --git a/iidm/iidm-xml-converter/src/main/resources/xsd/generatorShortCircuit.xsd b/iidm/iidm-xml-converter/src/main/resources/xsd/generatorShortCircuit.xsd new file mode 100644 index 00000000000..95f82dd3bb0 --- /dev/null +++ b/iidm/iidm-xml-converter/src/main/resources/xsd/generatorShortCircuit.xsd @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/iidm/iidm-xml-converter/src/main/resources/xsd/identifiableShortCircuit.xsd b/iidm/iidm-xml-converter/src/main/resources/xsd/identifiableShortCircuit.xsd new file mode 100644 index 00000000000..e0486f9099b --- /dev/null +++ b/iidm/iidm-xml-converter/src/main/resources/xsd/identifiableShortCircuit.xsd @@ -0,0 +1,12 @@ + + + + + + + + + diff --git a/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializerTest.java b/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializerTest.java new file mode 100644 index 00000000000..ea795ddd8af --- /dev/null +++ b/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/GeneratorShortCircuitXmlSerializerTest.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2021, 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 com.powsybl.iidm.xml.extensions; + +import com.powsybl.commons.AbstractConverterTest; +import com.powsybl.iidm.network.Generator; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.extensions.GeneratorShortCircuit; +import com.powsybl.iidm.network.extensions.GeneratorShortCircuitAdder; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.iidm.xml.NetworkXml; +import org.joda.time.DateTime; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Coline Piloquet + */ +public class GeneratorShortCircuitXmlSerializerTest extends AbstractConverterTest { + @Test + public void testXmlSerializer() throws IOException { + Network network = EurostagTutorialExample1Factory.create(); + network.setCaseDate(DateTime.parse("2016-12-07T11:18:52.881+01:00")); + Generator gen = network.getGenerator("GEN"); + assertNotNull(gen); + gen.newExtension(GeneratorShortCircuitAdder.class) + .withDirectTransX(20) + .withDirectSubtransX(20) + .withStepUpTransformerX(20) + .add(); + GeneratorShortCircuit generatorShortCircuit = gen.getExtension(GeneratorShortCircuit.class); + + Network network2 = roundTripXmlTest(network, + NetworkXml::writeAndValidate, + NetworkXml::read, "/shortcircuits/generatorShortCircuitRef.xml"); + + Generator gen2 = network2.getGenerator("GEN"); + assertNotNull(gen2); + GeneratorShortCircuit generatorShortCircuit2 = gen2.getExtension(GeneratorShortCircuit.class); + assertNotNull(generatorShortCircuit2); + + assertEquals(generatorShortCircuit.getDirectSubtransX(), generatorShortCircuit2.getDirectSubtransX(), 0); + assertEquals(generatorShortCircuit.getDirectTransX(), generatorShortCircuit2.getDirectTransX(), 0); + assertEquals(generatorShortCircuit.getStepUpTransformerX(), generatorShortCircuit2.getStepUpTransformerX(), 0); + } +} diff --git a/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializerTest.java b/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializerTest.java new file mode 100644 index 00000000000..f54f17dd381 --- /dev/null +++ b/iidm/iidm-xml-converter/src/test/java/com/powsybl/iidm/xml/extensions/IdentifiableShortCircuitXmlSerializerTest.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2021, 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 com.powsybl.iidm.xml.extensions;; + +import com.powsybl.commons.AbstractConverterTest; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.VoltageLevel; +import com.powsybl.iidm.network.extensions.IdentifiableShortCircuit; +import com.powsybl.iidm.network.extensions.IdentifiableShortCircuitAdder; +import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; +import com.powsybl.iidm.xml.NetworkXml; +import org.joda.time.DateTime; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Coline Piloquet + */ +public class IdentifiableShortCircuitXmlSerializerTest extends AbstractConverterTest { + @Test + public void testXmlSerializer() throws IOException { + Network network = EurostagTutorialExample1Factory.create(); + network.setCaseDate(DateTime.parse("2016-12-07T11:18:52.881+01:00")); + VoltageLevel vlhv1 = network.getVoltageLevel("VLHV1"); + assertNotNull(vlhv1); + vlhv1.newExtension(IdentifiableShortCircuitAdder.class) + .withIpMin(500) + .withIpMax(1500) + .add(); + IdentifiableShortCircuit voltageLevelShortCircuits = vlhv1.getExtension(IdentifiableShortCircuit.class); + + Network network2 = roundTripXmlTest(network, + NetworkXml::writeAndValidate, + NetworkXml::read, "/shortcircuits/voltageLevelShortCircuitRef.xml"); + + VoltageLevel vlhv2 = network2.getVoltageLevel("VLHV1"); + assertNotNull(vlhv2); + IdentifiableShortCircuit voltageLevelShortCircuits2 = vlhv2.getExtension(IdentifiableShortCircuit.class); + assertNotNull(voltageLevelShortCircuits2); + + assertEquals(voltageLevelShortCircuits.getIpMax(), voltageLevelShortCircuits2.getIpMax(), 0); + assertEquals(voltageLevelShortCircuits.getIpMin(), voltageLevelShortCircuits2.getIpMin(), 0); + } +} diff --git a/iidm/iidm-xml-converter/src/test/resources/shortcircuits/generatorShortCircuitRef.xml b/iidm/iidm-xml-converter/src/test/resources/shortcircuits/generatorShortCircuitRef.xml new file mode 100644 index 00000000000..d3995d537cc --- /dev/null +++ b/iidm/iidm-xml-converter/src/test/resources/shortcircuits/generatorShortCircuitRef.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/iidm/iidm-xml-converter/src/test/resources/shortcircuits/voltageLevelShortCircuitRef.xml b/iidm/iidm-xml-converter/src/test/resources/shortcircuits/voltageLevelShortCircuitRef.xml new file mode 100644 index 00000000000..b35b065cfd2 --- /dev/null +++ b/iidm/iidm-xml-converter/src/test/resources/shortcircuits/voltageLevelShortCircuitRef.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +