diff --git a/commons-test/src/main/java/com/powsybl/commons/test/ComparisonUtils.java b/commons-test/src/main/java/com/powsybl/commons/test/ComparisonUtils.java index 49fce2404f9..12962dc43ff 100644 --- a/commons-test/src/main/java/com/powsybl/commons/test/ComparisonUtils.java +++ b/commons-test/src/main/java/com/powsybl/commons/test/ComparisonUtils.java @@ -15,6 +15,8 @@ import java.io.*; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -38,6 +40,15 @@ public static void assertXmlEquals(InputStream expected, InputStream actual) { assertFalse(hasDiff); } + public static void assertTxtEquals(Path expected, Path actual) { + try (InputStream expectedStream = Files.newInputStream(expected); + InputStream actualStream = Files.newInputStream(actual)) { + assertTxtEquals(expectedStream, actualStream); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + public static void assertTxtEquals(InputStream expected, InputStream actual) { try { assertTxtEquals(expected, new String(ByteStreams.toByteArray(actual), StandardCharsets.UTF_8)); diff --git a/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java b/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java index c9e243c829e..763b0caf03d 100644 --- a/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java +++ b/iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java @@ -874,6 +874,33 @@ public static Network copy(Network network, NetworkFactory networkFactory) { } public static Network copy(Network network, NetworkFactory networkFactory, ExecutorService executor) { + return copy(network, networkFactory, executor, TreeDataFormat.JSON); + } + + /** + * Deep copy of the network using the specified converter + * + * @param network the network to copy + * @param format the converter to use to export/import the network + * @return the copy of the network + */ + public static Network copy(Network network, TreeDataFormat format) { + return copy(network, NetworkFactory.findDefault(), format); + } + + /** + * Deep copy of the network using the specified converter + * + * @param network the network to copy + * @param networkFactory the network factory to use for the copy + * @param format the converter to use to export/import the network + * @return the copy of the network + */ + public static Network copy(Network network, NetworkFactory networkFactory, TreeDataFormat format) { + return copy(network, networkFactory, ForkJoinPool.commonPool(), format); + } + + public static Network copy(Network network, NetworkFactory networkFactory, ExecutorService executor, TreeDataFormat format) { Objects.requireNonNull(network); Objects.requireNonNull(networkFactory); Objects.requireNonNull(executor); @@ -881,7 +908,7 @@ public static Network copy(Network network, NetworkFactory networkFactory, Execu try (InputStream is = new PipedInputStream(pos)) { executor.execute(() -> { try { - write(network, pos); + write(network, new ExportOptions().setFormat(format), pos); } catch (Exception t) { LOGGER.error(t.toString(), t); } finally { @@ -892,7 +919,7 @@ public static Network copy(Network network, NetworkFactory networkFactory, Execu } } }); - return read(is, new ImportOptions(), null, networkFactory, ReportNode.NO_OP); + return read(is, new ImportOptions().setFormat(format), null, networkFactory, ReportNode.NO_OP); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/NetworkSerDeTest.java b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/NetworkSerDeTest.java index 28efb42faa0..66fb81ad640 100644 --- a/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/NetworkSerDeTest.java +++ b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/NetworkSerDeTest.java @@ -13,6 +13,7 @@ import com.powsybl.commons.extensions.ExtensionSerDe; import com.powsybl.commons.io.DeserializerContext; import com.powsybl.commons.io.SerializerContext; +import com.powsybl.commons.io.TreeDataFormat; import com.powsybl.commons.report.ReportNode; import com.powsybl.commons.test.TestUtil; import com.powsybl.iidm.network.*; @@ -28,6 +29,7 @@ import java.nio.file.Path; import java.time.ZonedDateTime; +import static com.powsybl.commons.test.ComparisonUtils.assertTxtEquals; import static com.powsybl.iidm.serde.IidmSerDeConstants.CURRENT_IIDM_VERSION; import static org.junit.jupiter.api.Assertions.*; @@ -116,6 +118,21 @@ void testGzipGunzip() throws IOException { assertArrayEquals(Files.readAllBytes(file1), Files.readAllBytes(file2)); } + @Test + void testCopyFormat() { + Network network = createEurostagTutorialExample1(); + Path file1 = tmpDir.resolve("n.xml"); + NetworkSerDe.write(network, file1); + Network network2 = NetworkSerDe.copy(network); + Path file2 = tmpDir.resolve("n2.xml"); + NetworkSerDe.write(network2, file2); + assertTxtEquals(file1, file2); + Network network3 = NetworkSerDe.copy(network, TreeDataFormat.BIN); + Path file3 = tmpDir.resolve("n3.xml"); + NetworkSerDe.write(network3, file3); + assertTxtEquals(file1, file3); + } + @AutoService(ExtensionSerDe.class) public static class BusbarSectionExtSerDe extends AbstractExtensionSerDe {