Skip to content

Commit

Permalink
Refactor metadata unit testing to identify macOS issues (#580)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <[email protected]>
  • Loading branch information
tadam50 authored Apr 5, 2024
1 parent 3937140 commit 5dc67a4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 51 deletions.
6 changes: 0 additions & 6 deletions single-line-diagram/single-line-diagram-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@
<artifactId>jimfs</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-diagram-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-diagram-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package com.powsybl.sld;

import com.google.common.io.ByteStreams;
import com.powsybl.sld.layout.*;
import com.powsybl.sld.layout.HorizontalSubstationLayoutFactory;
import com.powsybl.sld.layout.LayoutParameters;
import com.powsybl.sld.layout.PositionVoltageLevelLayoutFactory;
import com.powsybl.sld.library.ComponentLibrary;
import com.powsybl.sld.library.ConvergenceComponentLibrary;
import com.powsybl.sld.library.ResourcesComponentLibrary;
Expand Down Expand Up @@ -132,14 +134,12 @@ public String toSVG(Graph graph, String filename, ComponentLibrary componentLibr
}
}

public boolean compareMetadata(VoltageLevelGraph graph, String refMetadataName, VoltageLevelLayoutFactory voltageLevelLayoutFactory, ComponentLibrary componentLibrary, LayoutParameters layoutParameters, SvgParameters svgParameters, LabelProvider labelProvider, StyleProvider styleProvider) {

InputStream isRefMetadata = Objects.requireNonNull(getClass().getResourceAsStream(refMetadataName));
public abstract String toMetadata(Graph g, String filename);

public String toMetadata(Graph graph, String refMetadataName, ComponentLibrary componentLibrary, LayoutParameters layoutParameters, SvgParameters svgParameters, LabelProvider labelProvider, StyleProvider styleProvider) {
try (StringWriter writer = new StringWriter();
StringWriter metadataWriter = new StringWriter()) {

voltageLevelLayoutFactory.create(graph).run(this.layoutParameters);
SingleLineDiagram.draw(graph, writer, metadataWriter, componentLibrary, layoutParameters, svgParameters, labelProvider, styleProvider);

if (debugJsonFiles) {
Expand All @@ -152,37 +152,7 @@ public boolean compareMetadata(VoltageLevelGraph graph, String refMetadataName,
writeToFileInDebugDir(refMetadataName.replace(".json", ".svg"), writer);
}

String refMetadata = normalizeLineSeparator(new String(ByteStreams.toByteArray(isRefMetadata), StandardCharsets.UTF_8));
String metadata = normalizeLineSeparator(metadataWriter.toString());
return refMetadata.compareTo(metadata) == 0;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public boolean compareMetadata(SubstationGraph graph, String refMetdataName, SubstationLayoutFactory substationLayoutFactory, VoltageLevelLayoutFactory voltageLevelLayoutFactory, ComponentLibrary componentLibrary, LayoutParameters layoutParameters, SvgParameters svgParameters, LabelProvider labelProvider, StyleProvider styleProvider) {

InputStream isRefMetadata = Objects.requireNonNull(getClass().getResourceAsStream(refMetdataName));

try (StringWriter writer = new StringWriter();
StringWriter metadataWriter = new StringWriter()) {

substationLayoutFactory.create(graph, voltageLevelLayoutFactory).run(this.layoutParameters);
SingleLineDiagram.draw(graph, writer, metadataWriter, componentLibrary, layoutParameters, svgParameters, labelProvider, styleProvider);

if (debugJsonFiles) {
writeToFileInDebugDir(refMetdataName, metadataWriter);
}
if (overrideTestReferences) {
overrideTestReference(refMetdataName, metadataWriter);
}
if (debugSvgFiles) {
writeToFileInDebugDir(refMetdataName.replace(".json", ".svg"), writer);
}

String refMetadata = normalizeLineSeparator(new String(ByteStreams.toByteArray(isRefMetadata), StandardCharsets.UTF_8));
String metadata = normalizeLineSeparator(metadataWriter.toString());
return refMetadata.compareTo(metadata) == 0;
return normalizeLineSeparator(metadataWriter.toString());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public String toSVG(Graph g, String filename) {
return toSVG(g, filename, componentLibrary, layoutParameters, svgParameters, getDefaultDiagramLabelProvider(), getDefaultDiagramStyleProvider());
}

@Override
public String toMetadata(Graph g, String filename) {
return toMetadata(g, filename, componentLibrary, layoutParameters, svgParameters, getDefaultDiagramLabelProvider(), getDefaultDiagramStyleProvider());
}

protected LabelProvider getDefaultDiagramLabelProvider() {
return new DefaultLabelProvider(network, componentLibrary, layoutParameters, svgParameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Franck Lecuyer {@literal <franck.lecuyer at rte-france.com>}
Expand Down Expand Up @@ -154,10 +153,9 @@ void testMetadataWithHvdcLines() {
graphBuilder = new NetworkGraphBuilder(network);

SubstationGraph g = graphBuilder.buildSubstationGraph(substation.getId());
new HorizontalSubstationLayoutFactory().create(g, new PositionVoltageLevelLayoutFactory()).run(layoutParameters);

assertTrue(compareMetadata(g, "/substDiag_with_hvdc_line_metadata.json",
new HorizontalSubstationLayoutFactory(),
new PositionVoltageLevelLayoutFactory(),
assertEquals(toString("/substDiag_with_hvdc_line_metadata.json"), toMetadata(g, "/substDiag_with_hvdc_line_metadata.json",
componentLibrary,
layoutParameters,
svgParameters,
Expand All @@ -169,17 +167,18 @@ void testHorizontalDefaultStyle() {
// compare metadata of substation diagram with reference
// (with horizontal substation layout)
SubstationGraph substationGraph = graphBuilder.buildSubstationGraph(substation.getId());
new HorizontalSubstationLayoutFactory().create(substationGraph, new PositionVoltageLevelLayoutFactory()).run(layoutParameters);

assertTrue(compareMetadata(substationGraph, "/substDiag_metadata.json", new HorizontalSubstationLayoutFactory(),
new PositionVoltageLevelLayoutFactory(), componentLibrary, layoutParameters, svgParameters, getDefaultDiagramLabelProvider(), new BasicStyleProvider()));
assertEquals(toString("/substDiag_metadata.json"), toMetadata(substationGraph, "/substDiag_metadata.json", componentLibrary, layoutParameters, svgParameters, getDefaultDiagramLabelProvider(), new BasicStyleProvider()));
}

@Test
void testHorizontalNominalStyle() {
// compare metadata of substation diagram with reference
// (with horizontal substation layout)
SubstationGraph graph = graphBuilder.buildSubstationGraph(substation.getId());
new HorizontalSubstationLayoutFactory().create(graph, new PositionVoltageLevelLayoutFactory()).run(layoutParameters);

assertTrue(compareMetadata(graph, "/substDiag_metadata.json", new HorizontalSubstationLayoutFactory(), new PositionVoltageLevelLayoutFactory(), componentLibrary, layoutParameters, svgParameters, getDefaultDiagramLabelProvider(), new NominalVoltageStyleProvider()));
assertEquals(toString("/substDiag_metadata.json"), toMetadata(graph, "/substDiag_metadata.json", componentLibrary, layoutParameters, svgParameters, getDefaultDiagramLabelProvider(), new NominalVoltageStyleProvider()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Franck Lecuyer {@literal <franck.lecuyer at rte-france.com>}
Expand Down Expand Up @@ -262,7 +261,9 @@ void testMetadata() {

// compare metadata of voltage level diagram with reference
VoltageLevelGraph graph = graphBuilder.buildVoltageLevelGraph(vl1.getId());
assertTrue(compareMetadata(graph, "/vlDiag_metadata.json", new PositionVoltageLevelLayoutFactory(), componentLibrary, layoutParameters, svgParameters, getDefaultDiagramLabelProvider(), new NominalVoltageStyleProvider()));
new PositionVoltageLevelLayoutFactory().create(graph).run(layoutParameters);

assertEquals(toString("/vlDiag_metadata.json"), toMetadata(graph, "/vlDiag_metadata.json"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public String toSVG(Graph graph, String filename) {
return toSVG(graph, filename, componentLibrary, layoutParameters, svgParameters, labelRawProvider, new BasicStyleProvider());
}

@Override
public String toMetadata(Graph graph, String filename) {
return toMetadata(graph, filename, componentLibrary, layoutParameters, svgParameters, labelRawProvider, new BasicStyleProvider());
}

private final LabelProvider labelRawProvider = new DefaultLabelProvider(Network.create("empty", ""), componentLibrary, layoutParameters, svgParameters) {

@Override
Expand Down

0 comments on commit 5dc67a4

Please sign in to comment.