Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Adding tests
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 Oct 20, 2021
1 parent 0aaf4c6 commit 4cae52e
Show file tree
Hide file tree
Showing 11 changed files with 4,025 additions and 86 deletions.
45 changes: 34 additions & 11 deletions src/test/java/com/powsybl/nad/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import com.powsybl.nad.layout.ForcedLayout;
import com.powsybl.nad.layout.LayoutParameters;
import com.powsybl.nad.model.Graph;
import com.powsybl.nad.svg.DefaultStyleProvider;
import com.powsybl.nad.svg.StyleProvider;
import com.powsybl.nad.svg.SvgParameters;
import com.powsybl.nad.svg.SvgWriter;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

Expand All @@ -27,26 +27,49 @@
*/
public abstract class AbstractTest {

protected LayoutParameters getLayoutParameters() {
return new LayoutParameters();
}
protected boolean debugSvg = false;
protected boolean overrideTestReferences = false;

private SvgParameters getSvgParameters() {
return new SvgParameters();
}
protected abstract LayoutParameters getLayoutParameters();

private StyleProvider getStyleProvider() {
return new DefaultStyleProvider();
}
protected abstract SvgParameters getSvgParameters();

protected abstract StyleProvider getStyleProvider();

protected String generateSvgString(Network network) {
protected String generateSvgString(Network network, String refFilename) {
Graph graph = new NetworkGraphBuilder(network, new IntIdProvider()).buildGraph();
new ForcedLayout().run(graph, getLayoutParameters());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new SvgWriter(getSvgParameters(), getStyleProvider()).writeSvg(graph, baos);
if (debugSvg) {
writeToHomeDir(refFilename, baos);
}
if (overrideTestReferences) {
overrideTestReference(refFilename, baos);
}
return baos.toString();
}

private void writeToHomeDir(String refFilename, ByteArrayOutputStream baos) {
try (OutputStream fos = new FileOutputStream(new File(System.getProperty("user.home"), refFilename))) {
baos.writeTo(fos);
} catch (IOException e) {
e.printStackTrace();
}
}

private void overrideTestReference(String filename, ByteArrayOutputStream baos) {
File testReference = new File("src/test/resources", filename);
if (!testReference.exists()) {
return;
}
try (OutputStream os = new FileOutputStream(testReference)) {
baos.writeTo(os);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

protected String toString(String resourceName) {
try {
InputStream in = Objects.requireNonNull(getClass().getResourceAsStream(resourceName));
Expand Down
49 changes: 48 additions & 1 deletion src/test/java/com/powsybl/nad/SvgWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import com.powsybl.iidm.import_.Importers;
import com.powsybl.iidm.network.Network;
import com.powsybl.nad.layout.LayoutParameters;
import com.powsybl.nad.svg.DefaultStyleProvider;
import com.powsybl.nad.svg.StyleProvider;
import com.powsybl.nad.svg.SvgParameters;
import org.junit.jupiter.api.Test;

import java.net.URL;
Expand All @@ -20,11 +24,54 @@
*/
public class SvgWriterTest extends AbstractTest {

@Override
protected LayoutParameters getLayoutParameters() {
return new LayoutParameters();
}

@Override
protected SvgParameters getSvgParameters() {
return new SvgParameters().setInsertName(true);
}

@Override
protected StyleProvider getStyleProvider() {
return new DefaultStyleProvider();
}

@Test
void testIEEE30() {
URL url = Objects.requireNonNull(getClass().getResource("/IEEE_30_bus.xiidm"));
Network network = Importers.loadNetwork(url.getFile());
assertEquals(toString("/IEEE_30_bus.svg"), generateSvgString(network));
assertEquals(toString("/IEEE_30_bus.svg"), generateSvgString(network, "/IEEE_30_bus.svg"));
}

@Test
void testIEEE14() {
URL url = Objects.requireNonNull(getClass().getResource("/IEEE_14_bus.xiidm"));
Network network = Importers.loadNetwork(url.getFile());
assertEquals(toString("/IEEE_14_bus.svg"), generateSvgString(network, "/IEEE_14_bus.svg"));
}

@Test
void testIEEE24() {
URL url = Objects.requireNonNull(getClass().getResource("/IEEE_24_bus.xiidm"));
Network network = Importers.loadNetwork(url.getFile());
assertEquals(toString("/IEEE_24_bus.svg"), generateSvgString(network, "/IEEE_24_bus.svg"));
}

@Test
void testIEEE57() {
URL url = Objects.requireNonNull(getClass().getResource("/IEEE_57_bus.xiidm"));
Network network = Importers.loadNetwork(url.getFile());
assertEquals(toString("/IEEE_57_bus.svg"), generateSvgString(network, "/IEEE_57_bus.svg"));
}

@Test
void testIEEE118() {
URL url = Objects.requireNonNull(getClass().getResource("/IEEE_118_bus.xiidm"));
Network network = Importers.loadNetwork(url.getFile());
assertEquals(toString("/IEEE_118_bus.svg"), generateSvgString(network, "/IEEE_118_bus.svg"));
}

}
Loading

0 comments on commit 4cae52e

Please sign in to comment.