Skip to content

Commit

Permalink
Unit tests do not rewrite known iidm file
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Dupuy <florian.dupuy@rte-france.com>
  • Loading branch information
flo-dup committed Feb 24, 2023
1 parent 830badc commit b8fecb7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@
import com.powsybl.iidm.xml.NetworkXml;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import static com.powsybl.commons.test.ComparisonUtils.compareTxt;
import static org.junit.Assert.assertNotNull;

/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
*/
public abstract class AbstractDynawoCommonsTest extends AbstractConverterTest {

protected void compare(String expectedIidmResource, Network actual) throws IOException {
InputStream expected = Objects.requireNonNull(getClass().getResourceAsStream(expectedIidmResource));
compareTxt(expected, getInputStream(actual, tmpDir.resolve("actual.xiidm")));
}

protected void compare(Network expected, Network actual) throws IOException {
Path pExpected = tmpDir.resolve("expected.xiidm");
assertNotNull(pExpected);
Path pActual = tmpDir.resolve("actual.xiidm");
assertNotNull(pActual);
NetworkXml.write(expected, pExpected);
actual.setCaseDate(expected.getCaseDate());
NetworkXml.write(actual, pActual);
compareTxt(Files.newInputStream(pExpected), Files.newInputStream(pActual));
compareTxt(getInputStream(expected, tmpDir.resolve("expected.xiidm")),
getInputStream(actual, tmpDir.resolve("actual.xiidm")));
}

private InputStream getInputStream(Network n, Path path) throws IOException {
NetworkXml.write(n, path);
return Files.newInputStream(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public class LoadsMergerTest extends AbstractDynawoCommonsTest {
@Test
public void multiBusesInVoltageLevel() throws IOException {
Network network = TestNetworkFactory.createMultiBusesVoltageLevelNetwork();
Network expectedIidm = Network.read("mergedLoadsMultiBusesVl.xiidm", getClass().getResourceAsStream("/mergedLoadsMultiBusesVl.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
compare("/mergedLoadsMultiBusesVl.xiidm", LoadsMerger.mergeLoads(network));
}

@Test
Expand All @@ -32,8 +31,7 @@ public void mergeLoadsPpQp() throws IOException {
new LoadState(36.1, 4.0, 36.0, 4.0),
new LoadState(10.1, 7.2, 10.3, 7.5));
Network network = TestNetworkFactory.createMultiLoadsBusesNetwork(loadStates);
Network expectedIidm = Network.read("mergedLoadsPpQp.xiidm", getClass().getResourceAsStream("/mergedLoadsPpQp.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
compare("/mergedLoadsPpQp.xiidm", LoadsMerger.mergeLoads(network));
}

@Test
Expand All @@ -42,8 +40,7 @@ public void mergeLoadsPpQn() throws IOException {
new LoadState(36.1, -4.0, 36.0, -4.0),
new LoadState(10.1, -7.2, 10.3, -7.5));
Network network = TestNetworkFactory.createMultiLoadsBusesNetwork(loadStates);
Network expectedIidm = Network.read("mergedLoadsPpQn.xiidm", getClass().getResourceAsStream("/mergedLoadsPpQn.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
compare("/mergedLoadsPpQn.xiidm", LoadsMerger.mergeLoads(network));
}

@Test
Expand All @@ -52,8 +49,7 @@ public void mergeLoadsPnQn() throws IOException {
new LoadState(-36.1, -4.0, -36.0, -4.0),
new LoadState(-10.1, -7.2, -10.3, -7.5));
Network network = TestNetworkFactory.createMultiLoadsBusesNetwork(loadStates);
Network expectedIidm = Network.read("mergedLoadsPnQn.xiidm", getClass().getResourceAsStream("/mergedLoadsPnQn.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
compare("/mergedLoadsPnQn.xiidm", LoadsMerger.mergeLoads(network));
}

@Test
Expand All @@ -62,8 +58,7 @@ public void mergeLoadsPnQp() throws IOException {
new LoadState(-36.1, 4.0, -36.0, 4.0),
new LoadState(-10.1, 7.2, -10.3, 7.5));
Network network = TestNetworkFactory.createMultiLoadsBusesNetwork(loadStates);
Network expectedIidm = Network.read("mergedLoadsPnQp.xiidm", getClass().getResourceAsStream("/mergedLoadsPnQp.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
compare("/mergedLoadsPnQp.xiidm", LoadsMerger.mergeLoads(network));
}

@Test
Expand All @@ -76,8 +71,7 @@ public void mergeThreeLoadsGroups() throws IOException {
new LoadState(10.1, -7.2, 10.3, -7.5),
new LoadState(-10.1, -7.2, -10.3, -7.5));
Network network = TestNetworkFactory.createMultiLoadsBusesNetwork(loadStates);
Network expectedIidm = Network.read("mergedThreeLoadsGroups.xiidm", getClass().getResourceAsStream("/mergedThreeLoadsGroups.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
compare("/mergedThreeLoadsGroups.xiidm", LoadsMerger.mergeLoads(network));
}

@Test
Expand All @@ -88,7 +82,6 @@ public void nonMergeableLoads() throws IOException {
new LoadState(36.1, -1.0, 16.0, -4.0),
new LoadState(-5.5, -2.0, -46.0, -5.0));
Network network = TestNetworkFactory.createMultiLoadsBusesNetwork(loadStates);
Network expectedIidm = Network.read("nonMergeableLoads.xiidm", getClass().getResourceAsStream("/nonMergeableLoads.xiidm"));
compare(expectedIidm, LoadsMerger.mergeLoads(network));
compare("/nonMergeableLoads.xiidm", LoadsMerger.mergeLoads(network));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.powsybl.dynawo.commons;

import com.powsybl.iidm.network.*;
import org.joda.time.DateTime;

import java.util.List;

Expand All @@ -20,7 +21,8 @@ private TestNetworkFactory() {
}

static Network createMultiBusesVoltageLevelNetwork() {
Network network = Network.create("multiBusesVl", "test");
Network network = Network.create("multiBusesVl", "test")
.setCaseDate(DateTime.parse("2023-02-17T05:41:11.194+01:00"));
Substation s = network.newSubstation().setId("substation").add();

VoltageLevel vl1 = s.newVoltageLevel().setId("vl1").setNominalV(250).setTopologyKind(TopologyKind.NODE_BREAKER).add();
Expand Down Expand Up @@ -67,7 +69,8 @@ static Network createMultiBusesVoltageLevelNetwork() {
* @return the test network generated
*/
static Network createMultiLoadsBusesNetwork(List<LoadState> loadStates) {
Network network = Network.create("multiLoads", "test");
Network network = Network.create("multiLoads", "test")
.setCaseDate(DateTime.parse("2023-02-17T05:41:11.194+01:00"));
Substation s = network.newSubstation().setId("substation").add();

// First node breaker voltage level
Expand Down

0 comments on commit b8fecb7

Please sign in to comment.