Skip to content

Commit f944259

Browse files
committed
WIP
Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com>
1 parent 74cc0bd commit f944259

File tree

61 files changed

+1153
-1103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1153
-1103
lines changed

commons-test/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,5 @@
6464
<artifactId>slf4j-api</artifactId>
6565
<scope>compile</scope>
6666
</dependency>
67-
<dependency>
68-
<groupId>commons-io</groupId>
69-
<artifactId>commons-io</artifactId>
70-
</dependency>
7167
</dependencies>
7268
</project>

commons-test/src/main/java/com/powsybl/commons/test/AbstractSerDeTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ protected <T> T roundTripXmlTest(T data, BiFunction<T, Path, T> transformer, BiC
6060
return roundTripTest(data, transformer, write, read, ComparisonUtils::assertXmlEquals, ref);
6161
}
6262

63+
protected <T> T roundTripTxtTest(T data, BiFunction<T, Path, T> transformer, BiConsumer<T, Path> write, Function<Path, T> read, String ref) throws IOException {
64+
return roundTripTest(data, transformer, write, read, ComparisonUtils::assertTxtEquals, ref);
65+
}
66+
6367
/**
6468
* Roundtrip test on the given data. The following steps occur:
6569
* <ul>
@@ -83,6 +87,10 @@ protected <T> Path writeXmlTest(T data, BiConsumer<T, Path> write, String ref) t
8387
return writeTest(data, write, ComparisonUtils::assertXmlEquals, ref);
8488
}
8589

90+
protected <T> Path writeTxtTest(T data, BiConsumer<T, Path> write, String ref) throws IOException {
91+
return writeTest(data, write, ComparisonUtils::assertTxtEquals, ref);
92+
}
93+
8694
/**
8795
* Write the given data with given write function, and compare the resulting file to given reference with the
8896
* comparison method provided.

commons-test/src/main/java/com/powsybl/commons/test/ComparisonUtils.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
package com.powsybl.commons.test;
99

1010
import com.google.common.io.ByteStreams;
11-
import org.apache.commons.io.FileUtils;
1211
import org.slf4j.Logger;
1312
import org.slf4j.LoggerFactory;
13+
import org.xmlunit.builder.DiffBuilder;
14+
import org.xmlunit.diff.Diff;
1415

1516
import java.io.*;
1617
import java.nio.charset.StandardCharsets;
@@ -32,24 +33,12 @@ private ComparisonUtils() {
3233
}
3334

3435
public static void assertXmlEquals(InputStream expected, InputStream actual) {
35-
/*
3636
Diff myDiff = DiffBuilder.compare(expected).withTest(actual).ignoreWhitespace().ignoreComments().build();
3737
boolean hasDiff = myDiff.hasDifferences();
3838
if (hasDiff) {
3939
LOGGER.error("{}", myDiff);
4040
}
4141
assertFalse(hasDiff);
42-
*/
43-
try {
44-
File expectedFile = new File("expectedFile.xml");
45-
FileUtils.copyInputStreamToFile(expected, expectedFile);
46-
File actualFile = new File("actualFile.xml");
47-
FileUtils.copyInputStreamToFile(actual, actualFile);
48-
//Returns true if the files contains the same bytes.
49-
com.google.common.io.Files.equal(actualFile, expectedFile);
50-
} catch (IOException e) {
51-
throw new UncheckedIOException(e);
52-
}
5342
}
5443

5544
public static void assertTxtEquals(Path expected, Path actual) {
@@ -109,6 +98,13 @@ public static void assertTxtEquals(String expected, InputStream actual) {
10998
public static void assertTxtEquals(String expected, String actual) {
11099
String expectedStr = TestUtil.normalizeLineSeparator(expected);
111100
String actualStr = TestUtil.normalizeLineSeparator(actual);
101+
// Ensure it ends with exactly one newline before assertion
102+
if (!expectedStr.endsWith("\n")) {
103+
expectedStr += "\n";
104+
}
105+
if (!actualStr.endsWith("\n")) {
106+
actualStr += "\n";
107+
}
112108
assertEquals(expectedStr, actualStr);
113109
}
114110

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/AbstractIidmSerDeTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ protected void allFormatsRoundTripFromVersionedXmlTest(String file, IidmVersion.
6464
}
6565
}
6666

67+
protected void allFormatsRoundTripFromVersionedTxtTest(String file, IidmVersion... versions) throws IOException {
68+
for (IidmVersion version : versions) {
69+
allFormatsRoundTripTxtTest(NetworkSerDe.read(getVersionedNetworkAsStream(file, version)), file, version);
70+
}
71+
}
72+
6773
/**
6874
* Execute an all-formats round trip test on the test resource IIDM-XML file with a given
6975
* file name comparing to the given reference file for the given IIDM versions.
@@ -95,6 +101,10 @@ protected void allFormatsRoundTripAllPreviousVersionedXmlTest(String file) throw
95101
allFormatsRoundTripFromVersionedXmlTest(file, allPreviousVersions(CURRENT_IIDM_VERSION));
96102
}
97103

104+
protected void allFormatsRoundTripAllPreviousVersionedTxtTest(String file) throws IOException {
105+
allFormatsRoundTripFromVersionedTxtTest(file, allPreviousVersions(CURRENT_IIDM_VERSION));
106+
}
107+
98108
/**
99109
* Execute a round trip test on the test resource IIDM-JSON file with a given file name for all IIDM versions
100110
* strictly older than the current IIDM version.
@@ -113,6 +123,12 @@ protected void allFormatsRoundTripFromVersionedXmlFromMinToCurrentVersionTest(St
113123
.toArray(IidmVersion[]::new));
114124
}
115125

126+
protected void allFormatsRoundTripFromVersionedTxtFromMinToCurrentVersionTest(String file, IidmVersion minVersion) throws IOException {
127+
allFormatsRoundTripFromVersionedTxtTest(file, Stream.of(IidmVersion.values())
128+
.filter(v -> v.compareTo(minVersion) >= 0 && v.compareTo(CURRENT_IIDM_VERSION) < 0)
129+
.toArray(IidmVersion[]::new));
130+
}
131+
116132
/**
117133
* Execute a round trip test reading the test resource IIDM-XML file with a given file name comparing
118134
* the output IIDM-XML file to a reference file for all IIDM versions equals or more recent than
@@ -151,6 +167,10 @@ protected void allFormatsRoundTripAllVersionedXmlTest(String file) throws IOExce
151167
allFormatsRoundTripFromVersionedXmlTest(file, IidmVersion.values());
152168
}
153169

170+
protected void allFormatsRoundTripAllVersionedTxtTest(String file) throws IOException {
171+
allFormatsRoundTripFromVersionedTxtTest(file, IidmVersion.values());
172+
}
173+
154174
/**
155175
* Execute a given test for all IIDM versions strictly older than a given maximum IIDM version.
156176
*/
@@ -178,6 +198,11 @@ protected void testWriteXmlAllPreviousVersions(Network network, ExportOptions ex
178198
testWriteVersionedXml(network, exportOptions, filename, allPreviousVersions(maxVersionExcluded));
179199
}
180200

201+
protected void testWriteTxtAllPreviousVersions(Network network, ExportOptions exportOptions, String filename,
202+
IidmVersion maxVersionExcluded) throws IOException {
203+
testWriteVersionedTxt(network, exportOptions, filename, allPreviousVersions(maxVersionExcluded));
204+
}
205+
181206
/**
182207
* Execute a write test for the given network, for all IIDM versions given, and compare to the given versioned xml
183208
* reference test resource.
@@ -191,6 +216,15 @@ protected void testWriteVersionedXml(Network network, ExportOptions exportOption
191216
}
192217
}
193218

219+
protected void testWriteVersionedTxt(Network network, ExportOptions exportOptions, String filename,
220+
IidmVersion... versions) throws IOException {
221+
for (IidmVersion version : versions) {
222+
writeTxtTest(network,
223+
(n, p) -> NetworkSerDe.write(n, exportOptions.setVersion(version.toString(".")), p),
224+
getVersionedNetworkPath(filename, version));
225+
}
226+
}
227+
194228
private static IidmVersion[] allPreviousVersions(IidmVersion maxVersionExcluded) {
195229
return Stream.of(IidmVersion.values())
196230
.filter(v -> v.compareTo(maxVersionExcluded) < 0)
@@ -238,6 +272,10 @@ public Network allFormatsRoundTripTest(Network network, String filename, IidmVer
238272
return allFormatsRoundTripTest(network, filename, version, new ExportOptions());
239273
}
240274

275+
public Network allFormatsRoundTripTxtTest(Network network, String filename, IidmVersion version) throws IOException {
276+
return allFormatsRoundTripTxtTest(network, filename, version, new ExportOptions());
277+
}
278+
241279
/**
242280
* All-formats round trip from given network with versioned reference xml file:
243281
* <ul>
@@ -260,6 +298,10 @@ public Network allFormatsRoundTripTest(Network network, String filename, IidmVer
260298
return allFormatsRoundTripTest(network, getVersionedNetworkPath(filename, version), exportOptions.setVersion(version.toString(".")));
261299
}
262300

301+
public Network allFormatsRoundTripTxtTest(Network network, String filename, IidmVersion version, ExportOptions exportOptions) throws IOException {
302+
return allFormatsRoundTripTxtTest(network, getVersionedNetworkPath(filename, version), exportOptions.setVersion(version.toString(".")));
303+
}
304+
263305
/**
264306
* All-formats round trip from given network with reference xml file:
265307
* <ul>
@@ -285,6 +327,14 @@ public Network allFormatsRoundTripTest(Network network, String refXmlFile, Expor
285327
refXmlFile);
286328
}
287329

330+
public Network allFormatsRoundTripTxtTest(Network network, String refXmlFile, ExportOptions exportOptions) throws IOException {
331+
return roundTripTxtTest(network,
332+
(n, p) -> binWriteAndRead(jsonWriteAndRead(n, exportOptions, p), exportOptions, p),
333+
(n, p) -> NetworkSerDe.write(n, exportOptions, p),
334+
NetworkSerDe::validateAndRead,
335+
refXmlFile);
336+
}
337+
288338
/**
289339
* Writes given network to JSON file, then reads the resulting file and returns the resulting network
290340
*/

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/AreaSerDeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class AreaSerDeTest extends AbstractIidmSerDeTest {
2424
@Test
2525
void testNetworkAreas() throws IOException {
2626
Network network = createBaseNetworkWithAreas();
27-
allFormatsRoundTripTest(network, "/areaRoundTripRef.xml", CURRENT_IIDM_VERSION);
28-
allFormatsRoundTripTest(network, "/areaRoundTripRef.xml", IidmVersion.V_1_12);
27+
allFormatsRoundTripTxtTest(network, "/areaRoundTripRef.xml", CURRENT_IIDM_VERSION);
28+
allFormatsRoundTripTxtTest(network, "/areaRoundTripRef.xml", IidmVersion.V_1_12);
2929
// backward compatibility (checks versions 11 and 12)
30-
allFormatsRoundTripFromVersionedXmlFromMinToCurrentVersionTest("/areaRoundTripRef.xml", IidmVersion.V_1_11);
30+
allFormatsRoundTripFromVersionedTxtFromMinToCurrentVersionTest("/areaRoundTripRef.xml", IidmVersion.V_1_11);
3131
}
3232

3333
private static Network createBaseNetworkWithAreas() {

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/BatteryXmlTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class BatteryXmlTest extends AbstractIidmSerDeTest {
2121

2222
@Test
2323
void batteryRoundTripTest() throws IOException {
24-
allFormatsRoundTripTest(BatteryNetworkFactory.create(), "batteryRoundTripRef.xml", CURRENT_IIDM_VERSION);
24+
allFormatsRoundTripTxtTest(BatteryNetworkFactory.create(), "batteryRoundTripRef.xml", CURRENT_IIDM_VERSION);
2525

2626
//backward compatibility
27-
allFormatsRoundTripAllPreviousVersionedXmlTest("batteryRoundTripRef.xml");
27+
allFormatsRoundTripAllPreviousVersionedTxtTest("batteryRoundTripRef.xml");
2828
}
2929
}

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/DanglingLineXmlTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DanglingLineXmlTest extends AbstractIidmSerDeTest {
2727

2828
@Test
2929
void test() throws IOException {
30-
allFormatsRoundTripAllVersionedXmlTest("danglingLine.xml");
30+
allFormatsRoundTripAllVersionedTxtTest("danglingLine.xml");
3131
}
3232

3333
@Test
@@ -54,6 +54,6 @@ void testWithGeneration() throws IOException {
5454

5555
// check it doesn't fail for all versions < 1.3 if IidmVersionIncompatibilityBehavior is to log error
5656
var options = new ExportOptions().setIidmVersionIncompatibilityBehavior(ExportOptions.IidmVersionIncompatibilityBehavior.LOG_ERROR);
57-
testWriteXmlAllPreviousVersions(network, options, "danglingLineWithGeneration.xml", IidmVersion.V_1_3);
57+
testWriteTxtAllPreviousVersions(network, options, "danglingLineWithGeneration.xml", IidmVersion.V_1_3);
5858
}
5959
}

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/DcGroundSerDeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ void testNetworkDcGround() throws IOException {
2727
Network network = createBaseNetwork();
2828

2929
// Test for the current version
30-
allFormatsRoundTripTest(network, "/dcGroundRoundTripRef.xml", CURRENT_IIDM_VERSION);
30+
allFormatsRoundTripTxtTest(network, "/dcGroundRoundTripRef.xml", CURRENT_IIDM_VERSION);
3131

3232
// backward compatibility - checks from version 1.15
33-
allFormatsRoundTripFromVersionedXmlFromMinToCurrentVersionTest("/dcGroundRoundTripRef.xml", IidmVersion.V_1_15);
33+
allFormatsRoundTripFromVersionedTxtFromMinToCurrentVersionTest("/dcGroundRoundTripRef.xml", IidmVersion.V_1_15);
3434
}
3535

3636
@Test
@@ -42,7 +42,7 @@ void testNotSupported() throws IOException {
4242

4343
// check it doesn't fail for version 1.14 if IidmVersionIncompatibilityBehavior is to log error
4444
var options = new ExportOptions().setIidmVersionIncompatibilityBehavior(ExportOptions.IidmVersionIncompatibilityBehavior.LOG_ERROR);
45-
testWriteVersionedXml(network, options, "dcGroundNotSupported.xml", IidmVersion.V_1_14);
45+
testWriteVersionedTxt(network, options, "dcGroundNotSupported.xml", IidmVersion.V_1_14);
4646
}
4747

4848
private static Network createBaseNetwork() {

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/DcLineSerDeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ void testNetworkDcLine() throws IOException {
2727
Network network = createBaseNetwork();
2828

2929
// Test for the current version
30-
allFormatsRoundTripTest(network, "/dcLineRoundTripRef.xml", CURRENT_IIDM_VERSION);
30+
allFormatsRoundTripTxtTest(network, "/dcLineRoundTripRef.xml", CURRENT_IIDM_VERSION);
3131

3232
// backward compatibility - checks from version 1.15
33-
allFormatsRoundTripFromVersionedXmlFromMinToCurrentVersionTest("/dcLineRoundTripRef.xml", IidmVersion.V_1_15);
33+
allFormatsRoundTripFromVersionedTxtFromMinToCurrentVersionTest("/dcLineRoundTripRef.xml", IidmVersion.V_1_15);
3434
}
3535

3636
@Test
@@ -42,7 +42,7 @@ void testNotSupported() throws IOException {
4242

4343
// check it doesn't fail for version 1.14 if IidmVersionIncompatibilityBehavior is to log error
4444
var options = new ExportOptions().setIidmVersionIncompatibilityBehavior(ExportOptions.IidmVersionIncompatibilityBehavior.LOG_ERROR);
45-
testWriteVersionedXml(network, options, "dcLineNotSupported.xml", IidmVersion.V_1_14);
45+
testWriteVersionedTxt(network, options, "dcLineNotSupported.xml", IidmVersion.V_1_14);
4646
}
4747

4848
private static Network createBaseNetwork() {

iidm/iidm-serde/src/test/java/com/powsybl/iidm/serde/DcNodeSerDeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ void testNetworkDcNode() throws IOException {
2828
Network network = createBaseNetwork();
2929

3030
// Test for the current version
31-
allFormatsRoundTripTest(network, "/dcNodeRoundTripRef.xml", CURRENT_IIDM_VERSION);
31+
allFormatsRoundTripTxtTest(network, "/dcNodeRoundTripRef.xml", CURRENT_IIDM_VERSION);
3232

3333
// backward compatibility - checks from version 1.15
34-
allFormatsRoundTripFromVersionedXmlFromMinToCurrentVersionTest("/dcNodeRoundTripRef.xml", IidmVersion.V_1_15);
34+
allFormatsRoundTripFromVersionedTxtFromMinToCurrentVersionTest("/dcNodeRoundTripRef.xml", IidmVersion.V_1_15);
3535
}
3636

3737
@Test
@@ -48,7 +48,7 @@ void testNotSupported() throws IOException {
4848

4949
// check it doesn't fail for version 1.14 if IidmVersionIncompatibilityBehavior is to log error
5050
var options = new ExportOptions().setIidmVersionIncompatibilityBehavior(ExportOptions.IidmVersionIncompatibilityBehavior.LOG_ERROR);
51-
testWriteVersionedXml(network, options, "dcNodeNotSupported.xml", IidmVersion.V_1_14);
51+
testWriteVersionedTxt(network, options, "dcNodeNotSupported.xml", IidmVersion.V_1_14);
5252
}
5353

5454
private static Network createBaseNetwork() {

0 commit comments

Comments
 (0)