Skip to content

Commit aa8837e

Browse files
committed
WIP
Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com> /bin/bash: ligne 1: q : commande introuvable Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com>
1 parent 74cc0bd commit aa8837e

File tree

9 files changed

+78
-31
lines changed

9 files changed

+78
-31
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: 10 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,14 @@ 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+
102+
// FIXME ensure it ends with exactly one newline
103+
if (!expectedStr.endsWith("\n")) {
104+
expectedStr += "\n";
105+
}
106+
if (!actualStr.endsWith("\n")) {
107+
actualStr += "\n";
108+
}
112109
assertEquals(expectedStr, actualStr);
113110
}
114111

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

Lines changed: 46 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 allFormatsRoundTripTest(network, filename, version, new ExportOptions());
277+
}
278+
241279
/**
242280
* All-formats round trip from given network with versioned reference xml file:
243281
* <ul>
@@ -285,6 +323,14 @@ public Network allFormatsRoundTripTest(Network network, String refXmlFile, Expor
285323
refXmlFile);
286324
}
287325

326+
public Network allFormatsRoundTripTxtTest(Network network, String refXmlFile, ExportOptions exportOptions) throws IOException {
327+
return roundTripTxtTest(network,
328+
(n, p) -> binWriteAndRead(jsonWriteAndRead(n, exportOptions, p), exportOptions, p),
329+
(n, p) -> NetworkSerDe.write(n, exportOptions, p),
330+
NetworkSerDe::validateAndRead,
331+
refXmlFile);
332+
}
333+
288334
/**
289335
* Writes given network to JSON file, then reads the resulting file and returns the resulting network
290336
*/

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ class DanglingLineXmlTest extends AbstractIidmSerDeTest {
2727

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

3333
@Test
3434
void testWithGeneration() throws IOException {
3535
Network network = DanglingLineNetworkFactory.createWithGeneration();
3636
network.setCaseDate(ZonedDateTime.parse("2020-07-16T10:08:48.321+02:00"));
3737
network.getDanglingLine("DL").setProperty("test", "test");
38-
allFormatsRoundTripTest(network, "danglingLineWithGeneration.xml", IidmSerDeConstants.CURRENT_IIDM_VERSION);
38+
allFormatsRoundTripTxtTest(network, "danglingLineWithGeneration.xml", IidmSerDeConstants.CURRENT_IIDM_VERSION);
3939

4040
// backward compatibility checks from version 1.3
41-
allFormatsRoundTripFromVersionedXmlFromMinToCurrentVersionTest("danglingLineWithGeneration.xml", IidmVersion.V_1_3);
41+
allFormatsRoundTripFromVersionedTxtFromMinToCurrentVersionTest("danglingLineWithGeneration.xml", IidmVersion.V_1_3);
4242

4343
// check it fails for all versions < 1.3
4444
testForAllPreviousVersions(IidmVersion.V_1_3, version -> {
@@ -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/resources/V1_11/areaRoundTripRef.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_11" id="test" caseDate="2020-03-04T13:20:30.476+01:00" forecastDistance="0" sourceFormat="test" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
3-
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_11" id="sub" caseDate="2020-03-04T13:20:30.476+01:00" forecastDistance="0" sourceFormat="format" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
3+
<iidm:network id="sub" caseDate="2020-03-04T13:20:30.476+01:00" forecastDistance="0" sourceFormat="format" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
44
<iidm:substation id="sub3">
55
<iidm:voltageLevel id="VL2" nominalV="1.0" topologyKind="BUS_BREAKER">
66
<iidm:busBreakerTopology>
@@ -16,7 +16,7 @@
1616
<iidm:bus id="N2"/>
1717
</iidm:busBreakerTopology>
1818
<iidm:load id="L1" loadType="UNDEFINED" p0="0.0" q0="0.0" bus="N1" connectableBus="N1"/>
19-
<iidm:danglingLine id="DL1" r="0.0" x="0.0" b="0.0" g="0.0" p0="0.0" q0="0.0" bus="N2" connectableBus="N2"/>
19+
<iidm:danglingLine id="DL1" p0="0.0" q0="0.0" r="0.0" x="0.0" g="0.0" b="0.0" bus="N2" connectableBus="N2"/>
2020
</iidm:voltageLevel>
2121
</iidm:substation>
2222
<iidm:substation id="sub2"/>

iidm/iidm-serde/src/test/resources/V1_12/areaRoundTripRef.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_12" id="test" caseDate="2020-03-04T13:20:30.476+01:00" forecastDistance="0" sourceFormat="test" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
3-
<iidm:network xmlns:iidm="http://www.powsybl.org/schema/iidm/1_12" id="sub" caseDate="2020-03-04T13:20:30.476+01:00" forecastDistance="0" sourceFormat="format" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
3+
<iidm:network id="sub" caseDate="2020-03-04T13:20:30.476+01:00" forecastDistance="0" sourceFormat="format" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
44
<iidm:substation id="sub3">
55
<iidm:voltageLevel id="VL2" nominalV="1.0" topologyKind="BUS_BREAKER">
66
<iidm:busBreakerTopology>
@@ -16,9 +16,9 @@
1616
<iidm:bus id="N2"/>
1717
</iidm:busBreakerTopology>
1818
<iidm:load id="L1" loadType="UNDEFINED" p0="0.0" q0="0.0" bus="N1" connectableBus="N1"/>
19-
<iidm:danglingLine id="DL1" r="0.0" x="0.0" b="0.0" g="0.0" p0="0.0" q0="0.0" bus="N2" connectableBus="N2"/>
19+
<iidm:danglingLine id="DL1" p0="0.0" q0="0.0" r="0.0" x="0.0" g="0.0" b="0.0" bus="N2" connectableBus="N2"/>
2020
</iidm:voltageLevel>
2121
</iidm:substation>
2222
<iidm:substation id="sub2"/>
2323
<iidm:line id="Line1" r="0.0" x="0.0" g1="0.0" b1="0.0" g2="0.0" b2="0.0" voltageLevelId1="VL1" bus1="N1" connectableBus1="N1" voltageLevelId2="VL1" bus2="N2" connectableBus2="N2"/>
24-
</iidm:network>
24+
</iidm:network>

0 commit comments

Comments
 (0)