Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary iidm: distinction between null and empty string #2871

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <florian.dupuy at rte-france.com>}
*/
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());
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,7 +24,7 @@
import static com.powsybl.iidm.serde.IidmSerDeConstants.CURRENT_IIDM_VERSION;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>}
*/
class EurostagBinaryTest extends AbstractIidmSerDeTest {

Expand Down
Binary file not shown.
Binary file not shown.