Skip to content

Commit d5e2222

Browse files
author
Dennis Labordus
committed
Updated Test Data and first step to improved checks.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 17223c6 commit d5e2222

File tree

5 files changed

+7603
-5158
lines changed

5 files changed

+7603
-5158
lines changed

service/src/test/java/org/lfenergy/compas/scl/auto/alignment/TestUtil.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,50 @@
33
// SPDX-License-Identifier: Apache-2.0
44
package org.lfenergy.compas.scl.auto.alignment;
55

6+
import javanet.staxutils.SimpleNamespaceContext;
67
import org.lfenergy.compas.core.commons.ElementConverter;
78
import org.w3c.dom.Element;
9+
import org.w3c.dom.NodeList;
810

11+
import javax.xml.xpath.XPathConstants;
12+
import javax.xml.xpath.XPathFactory;
913
import java.io.*;
1014
import java.nio.charset.StandardCharsets;
1115

12-
import static org.lfenergy.compas.scl.auto.alignment.SclAutoAlignmentConstants.SCL_ELEMENT_NAME;
13-
import static org.lfenergy.compas.scl.auto.alignment.SclAutoAlignmentConstants.SCL_NS_URI;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.lfenergy.compas.scl.auto.alignment.SclAutoAlignmentConstants.*;
1419

1520
public final class TestUtil {
1621
private TestUtil() {
1722
// Only static methods.
1823
}
1924

20-
public static Element readSCLElement(String filename) throws IOException {
21-
var sclData = readSCL(filename);
25+
public static Element toElement(String sclData) throws IOException {
2226
var converter = new ElementConverter();
27+
try (var inputStream = new BufferedInputStream(
28+
new ByteArrayInputStream(sclData.getBytes(StandardCharsets.UTF_8)))) {
29+
return converter.convertToElement(inputStream, SCL_ELEMENT_NAME, SCL_NS_URI);
30+
}
31+
}
2332

24-
return converter.convertToElement(new BufferedInputStream(
25-
new ByteArrayInputStream(sclData.getBytes(StandardCharsets.UTF_8))), SCL_ELEMENT_NAME, SCL_NS_URI);
33+
public static Element readSCLElement(String filename) throws IOException {
34+
var sclData = readSCL(filename);
35+
return toElement(sclData);
2636
}
2737

2838
public static String readSCL(String filename) throws IOException {
2939
return readFile("/scl/" + filename);
3040
}
3141

3242
public static String readFile(String filename) throws IOException {
33-
var inputStream = TestUtil.class.getResourceAsStream(filename);
34-
assert inputStream != null;
43+
try (var inputStream = TestUtil.class.getResourceAsStream(filename)) {
44+
assert inputStream != null;
3545

36-
return new String(inputStream.readAllBytes());
46+
return new String(inputStream.readAllBytes());
47+
}
3748
}
3849

39-
@Deprecated
4050
public static void writeFile(String fileName, String data) {
4151
var file = new File("target", fileName);
4252
try (var fw = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
@@ -45,4 +55,20 @@ public static void writeFile(String fileName, String data) {
4555
throw new UncheckedIOException(e);
4656
}
4757
}
58+
59+
public static void assertXYCoordinates(Element rootElement, String xPathExpression, Integer x, Integer y) throws Exception {
60+
var xPath = XPathFactory.newInstance().newXPath();
61+
62+
SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
63+
nsCtx.setPrefix("scl", SCL_NS_URI);
64+
xPath.setNamespaceContext(nsCtx);
65+
66+
var nodeList = (NodeList) xPath.compile(xPathExpression).evaluate(rootElement.getOwnerDocument(), XPathConstants.NODESET);
67+
assertEquals(1, nodeList.getLength());
68+
69+
var element = (Element) nodeList.item(0);
70+
assertNotNull(element);
71+
assertEquals(x.toString(), element.getAttributeNS(SCLXY_NS_URI, "x"));
72+
assertEquals(y.toString(), element.getAttributeNS(SCLXY_NS_URI, "y"));
73+
}
4874
}

service/src/test/java/org/lfenergy/compas/scl/auto/alignment/service/SclAutoAlignmentEnricherTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
import static org.junit.jupiter.api.Assertions.assertNotNull;
1818
import static org.lfenergy.compas.scl.auto.alignment.SclAutoAlignmentConstants.SCL_ELEMENT_NAME;
1919
import static org.lfenergy.compas.scl.auto.alignment.SclAutoAlignmentConstants.SCL_NS_URI;
20-
import static org.lfenergy.compas.scl.auto.alignment.TestUtil.readFile;
21-
import static org.lfenergy.compas.scl.auto.alignment.TestUtil.readSCL;
20+
import static org.lfenergy.compas.scl.auto.alignment.TestUtil.*;
2221

2322
class SclAutoAlignmentEnricherTest {
2423
@Test
25-
void enrich_WhenPassingCase1_ThenXSDUpdated() throws IOException {
24+
void enrich_WhenPassingCase1_ThenSCDUpdated() throws Exception {
25+
// The JSON file used here is a copy created by the testcase
26+
// SclAutoAlignmentServiceTest#createJson_WhenPassingCase1_ThenJsonReturned
2627
var filename = "scl-1";
2728
var jsonString = readFile("/json/" + filename + ".json");
2829
var scl = new GenericSCL(readSCLElement(filename + ".scd"));
@@ -32,10 +33,15 @@ void enrich_WhenPassingCase1_ThenXSDUpdated() throws IOException {
3233

3334
assertNotNull(scl.getElement());
3435
writeFile(filename + "-updated.scd", scl);
36+
37+
assertXYCoordinates(scl.getElement(), "//scl:VoltageLevel[@name='J1']", 2, 4);
38+
assertXYCoordinates(scl.getElement(), "//scl:Bay[@name='BusBar A']", 1, 15);
3539
}
3640

3741
@Test
38-
void enrich_WhenPassingCase2_ThenXSDUpdated() throws IOException {
42+
void enrich_WhenPassingCase2_ThenSCDUpdated() throws IOException {
43+
// The JSON file used here is a copy created by the testcase
44+
// SclAutoAlignmentServiceTest#createJson_WhenPassingCase2_ThenJsonReturned
3945
var filename = "scl-2";
4046
var jsonString = readFile("/json/" + filename + ".json");
4147
var scl = new GenericSCL(readSCLElement(filename + ".scd"));

service/src/test/java/org/lfenergy/compas/scl/auto/alignment/service/SclAutoAlignmentServiceTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
import java.util.List;
1414

1515
import static org.junit.jupiter.api.Assertions.assertNotNull;
16-
import static org.lfenergy.compas.scl.auto.alignment.TestUtil.readSCL;
17-
import static org.lfenergy.compas.scl.auto.alignment.TestUtil.writeFile;
16+
import static org.lfenergy.compas.scl.auto.alignment.TestUtil.*;
1817

1918
@ExtendWith(MockitoExtension.class)
2019
class SclAutoAlignmentServiceTest {
@@ -29,17 +28,21 @@ void beforeEach() {
2928
}
3029

3130
@Test
32-
void updateSCL_WhenPassingCase1_ThenUpdatedSCLReturned() throws IOException {
31+
void updateSCL_WhenPassingCase1_ThenUpdatedSCLReturned() throws Exception {
3332
var filename = "scl-1";
3433
var sclString = readSCL(filename + ".scd");
3534

3635
var result = sclAutoAlignmentService.updateSCL(sclString, List.of("AA1"), "Mr. Editor");
3736
assertNotNull(result);
3837
writeFile(filename + "-updated-service.scd", result);
38+
39+
var rootElement = toElement(result);
40+
assertXYCoordinates(rootElement, "//scl:VoltageLevel[@name='J1']", 2, 4);
41+
assertXYCoordinates(rootElement, "//scl:Bay[@name='BusBar A']", 1, 15);
3942
}
4043

4144
@Test
42-
void updateSCL_WhenPassingCase2_ThenUpdatedSCLReturned() throws IOException {
45+
void updateSCL_WhenPassingCase2_ThenUpdatedSCLReturned() throws Exception {
4346
var filename = "scl-2";
4447
var sclString = readSCL(filename + ".scd");
4548

@@ -50,6 +53,7 @@ void updateSCL_WhenPassingCase2_ThenUpdatedSCLReturned() throws IOException {
5053

5154
@Test
5255
void getSVG_WhenPassingCase1_ThenSVGReturned() throws IOException {
56+
// Nice addition to the updateSCL Test to have a visual output from PowSyBl and check how their drawing looks.
5357
var filename = "scl-1";
5458
var sclString = readSCL(filename + ".scd");
5559

@@ -60,6 +64,7 @@ void getSVG_WhenPassingCase1_ThenSVGReturned() throws IOException {
6064

6165
@Test
6266
void getSVG_WhenPassingCase2_ThenSVGReturned() throws IOException {
67+
// Nice addition to the updateSCL Test to have a visual output from PowSyBl and check how their drawing looks.
6368
var filename = "scl-2";
6469
var sclString = readSCL(filename + ".scd");
6570

@@ -70,6 +75,8 @@ void getSVG_WhenPassingCase2_ThenSVGReturned() throws IOException {
7075

7176
@Test
7277
void createJson_WhenPassingCase1_ThenJsonReturned() throws IOException {
78+
// This just tests if the SCD File can be processed, because most of the work is done in PowSyBl.
79+
// But this way we know the GraphBuilders are still working and not missing electric components.
7380
var filename = "scl-1";
7481
var substationName = "AA1";
7582
var sclData = readSCL(filename + ".scd");
@@ -83,6 +90,8 @@ void createJson_WhenPassingCase1_ThenJsonReturned() throws IOException {
8390

8491
@Test
8592
void createJson_WhenPassingCase2_ThenJsonReturned() throws IOException {
93+
// This just tests if the SCD File can be processed, because most of the work is done in PowSyBl.
94+
// But this way we know the GraphBuilders are still working and not missing electric components.
8695
var filename = "scl-2";
8796
var substationName = "_af9a4ae3-ba2e-4c34-8e47-5af894ee20f4";
8897
var sclData = readSCL(filename + ".scd");

0 commit comments

Comments
 (0)