diff --git a/commons/src/main/java/com/powsybl/commons/binary/BinReader.java b/commons/src/main/java/com/powsybl/commons/binary/BinReader.java index b45f63eb025..ab0d90410c1 100644 --- a/commons/src/main/java/com/powsybl/commons/binary/BinReader.java +++ b/commons/src/main/java/com/powsybl/commons/binary/BinReader.java @@ -71,7 +71,7 @@ private void readDictionary() throws IOException { private String readString() { try { int stringNbBytes = dis.readShort(); - if (stringNbBytes == 0) { + if (stringNbBytes == -1) { return null; } byte[] stringBytes = dis.readNBytes(stringNbBytes); diff --git a/commons/src/main/java/com/powsybl/commons/binary/BinWriter.java b/commons/src/main/java/com/powsybl/commons/binary/BinWriter.java index 14bb1860f21..e345d843852 100644 --- a/commons/src/main/java/com/powsybl/commons/binary/BinWriter.java +++ b/commons/src/main/java/com/powsybl/commons/binary/BinWriter.java @@ -52,7 +52,7 @@ private static void writeIndex(int index, DataOutputStream dataOutputStream) { private static void writeString(String value, DataOutputStream dataOutputStream) { try { if (value == null) { - writeIndex(0, dataOutputStream); + writeIndex(-1, dataOutputStream); } else { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); writeIndex(bytes.length, dataOutputStream); diff --git a/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/BinaryTest.java b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/BinaryTest.java new file mode 100644 index 00000000000..61ba3ff397e --- /dev/null +++ b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/BinaryTest.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2024, 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/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.iidm.serde; + +import com.powsybl.commons.io.TreeDataFormat; +import com.powsybl.iidm.network.Line; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.TopologyKind; +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Florian Dupuy {@literal } + */ +class BinaryTest extends AbstractIidmSerDeTest { + + @Test + void testEmptyNullStringAttributes() { + Network n0 = Network.create("test", "test"); + n0.setProperty("property", ""); + n0.newVoltageLevel().setId("vl1").setNominalV(220).setTopologyKind(TopologyKind.NODE_BREAKER).add(); + n0.newVoltageLevel().setId("vl2").setNominalV(220).setTopologyKind(TopologyKind.NODE_BREAKER).add(); + Line l0 = n0.newLine().setId("line").setName("") + .setVoltageLevel1("vl1").setNode1(0) + .setVoltageLevel2("vl2").setNode2(0) + .setR(0.1).setX(10) + .add(); + l0.newCurrentLimits1().add(); + l0.cancelSelectedOperationalLimitsGroup1(); // selected group id is now null on side 1 + l0.newOperationalLimitsGroup2("").newCurrentLimits().add(); + l0.setSelectedOperationalLimitsGroup2(""); // selected group id is now "" on side 2 + + Path binFile = fileSystem.getPath("/work/test"); + NetworkSerDe.write(n0, new ExportOptions().setFormat(TreeDataFormat.BIN), binFile); + Network n1 = NetworkSerDe.read(binFile, new ImportOptions().setFormat(TreeDataFormat.BIN)); + Line s = n1.getLine("line"); + assertEquals("", n1.getProperty("property")); + assertEquals(Optional.of(""), s.getOptionalName()); + assertEquals(Optional.empty(), l0.getSelectedOperationalLimitsGroupId1()); + assertEquals(Optional.of(""), l0.getSelectedOperationalLimitsGroupId2()); + } +} diff --git a/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/EurostagBinaryTest.java b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/EurostagBinaryTest.java index 2d1a6a9738b..835d3806850 100644 --- a/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/EurostagBinaryTest.java +++ b/iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/EurostagBinaryTest.java @@ -1,8 +1,9 @@ /** - * Copyright (c) 2019, RTE (http://www.rte-france.com) + * Copyright (c) 2024, 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/. + * SPDX-License-Identifier: MPL-2.0 */ package com.powsybl.iidm.serde; @@ -23,7 +24,7 @@ import static com.powsybl.iidm.serde.IidmSerDeConstants.CURRENT_IIDM_VERSION; /** - * @author Geoffroy Jamgotchian {@literal } + * @author Florian Dupuy {@literal } */ class EurostagBinaryTest extends AbstractIidmSerDeTest { diff --git a/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf-extensions.bin b/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf-extensions.bin index b63d33dbd17..192b1c97131 100644 Binary files a/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf-extensions.bin and b/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf-extensions.bin differ diff --git a/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf.bin b/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf.bin index 3ec78363019..a50451c80c4 100644 Binary files a/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf.bin and b/iidm/iidm-serde/src/test/resources/V1_12/eurostag-tutorial1-lf.bin differ