Skip to content

Commit 59d72cb

Browse files
committed
review sonar, add tests
Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com>
1 parent fd6b5b9 commit 59d72cb

File tree

4 files changed

+97
-43
lines changed

4 files changed

+97
-43
lines changed

commons/src/test/java/com/powsybl/commons/xml/XmlUtilTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
import com.google.common.collect.ImmutableMap;
1111
import com.powsybl.commons.PowsyblException;
1212
import org.junit.jupiter.api.Test;
13+
import org.xml.sax.SAXNotRecognizedException;
14+
import org.xml.sax.SAXNotSupportedException;
1315

16+
import javax.xml.XMLConstants;
1417
import javax.xml.stream.*;
18+
import javax.xml.validation.SchemaFactory;
1519
import java.io.ByteArrayOutputStream;
1620
import java.io.StringReader;
1721
import java.nio.charset.StandardCharsets;
@@ -22,8 +26,7 @@
2226
import java.util.concurrent.atomic.AtomicReference;
2327

2428
import static com.powsybl.commons.xml.XmlUtil.getXMLInputFactory;
25-
import static org.junit.jupiter.api.Assertions.assertEquals;
26-
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.junit.jupiter.api.Assertions.*;
2730

2831
/**
2932
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
@@ -188,4 +191,19 @@ void initializeWriter() throws XMLStreamException {
188191
writer.close();
189192
assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>", baos.toString());
190193
}
194+
195+
@Test
196+
void testSchemaFactory() {
197+
SchemaFactory factory = XmlUtil.newSchemaFactory();
198+
assertNotNull(factory);
199+
try {
200+
Object value1 = factory.getProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA);
201+
assertEquals("", value1);
202+
203+
Object value2 = factory.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD);
204+
assertEquals("", value2);
205+
} catch (SAXNotSupportedException | SAXNotRecognizedException ignored) {
206+
// ignored
207+
}
208+
}
191209
}

iidm/iidm-serde/src/main/java/com/powsybl/iidm/serde/NetworkSerDe.java

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private static Schema createSchema(ExtensionsSupplier extensionsSupplier) {
168168
}
169169
}
170170

171-
private static void validate(InputStream is, IidmVersion version, ExtensionsSupplier extensionsSupplier) {
171+
public static void validate(InputStream is, IidmVersion version, ExtensionsSupplier extensionsSupplier) {
172172
Objects.requireNonNull(is);
173173
Objects.requireNonNull(version);
174174
Objects.requireNonNull(extensionsSupplier);
@@ -185,9 +185,6 @@ private static void validate(InputStream is, IidmVersion version, ExtensionsSupp
185185
Schema schema;
186186
if (extensionsSupplier == DefaultExtensionsSupplier.getInstance()) {
187187
schema = DEFAULT_SCHEMAS_SUPPLIER.get().computeIfAbsent(version, v -> createSchema(DefaultExtensionsSupplier.getInstance(), v));
188-
if (schema == null) {
189-
throw new PowsyblException("Schema not found: version=" + version);
190-
}
191188
} else {
192189
schema = createSchema(extensionsSupplier, version);
193190
}
@@ -293,29 +290,29 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) {
293290
List<String> locations = new ArrayList<>();
294291
XMLStreamReader reader = null;
295292
try {
296-
reader = getXMLInputFactory().createXMLStreamReader(new ByteArrayInputStream(xsdBytes));
297-
while (reader.hasNext()) {
298-
int event = reader.next();
299-
if (event == XMLStreamConstants.START_ELEMENT
300-
&& XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(reader.getNamespaceURI())
301-
&& ("import".equals(reader.getLocalName()))) {
302-
String schemaLocation = reader.getAttributeValue(null, "schemaLocation");
303-
if (schemaLocation != null && !schemaLocation.isBlank() && ALLOWED_IIDM_XSDS.contains(schemaLocation)) {
304-
locations.add(schemaLocation);
293+
try (ByteArrayInputStream in = new ByteArrayInputStream(xsdBytes)) {
294+
reader = getXMLInputFactory().createXMLStreamReader(in);
295+
while (reader.hasNext()) {
296+
int event = reader.next();
297+
if (event == XMLStreamConstants.START_ELEMENT
298+
&& XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(reader.getNamespaceURI())
299+
&& ("import".equals(reader.getLocalName()))) {
300+
String schemaLocation = reader.getAttributeValue(null, "schemaLocation");
301+
if (schemaLocation != null && !schemaLocation.isBlank() && ALLOWED_IIDM_XSDS.contains(schemaLocation)) {
302+
locations.add(schemaLocation);
303+
}
305304
}
306305
}
307-
}
308-
return locations;
309-
} catch (XMLStreamException e) {
310-
throw new PowsyblException("Failed to parse XSD schema", e);
311-
} finally {
312-
if (reader != null) {
313-
try {
306+
return locations;
307+
} catch (XMLStreamException | IOException e) {
308+
throw new PowsyblException("Failed to parse XSD schema", e);
309+
} finally {
310+
if (reader != null) {
314311
reader.close();
315-
} catch (XMLStreamException e) {
316-
LOGGER.error(e.toString(), e);
317312
}
318313
}
314+
} catch (XMLStreamException e) {
315+
throw new UncheckedXmlStreamException(e);
319316
}
320317
}
321318

@@ -328,30 +325,30 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) {
328325
private static String readRootNamespace(byte[] xmlBytes) {
329326
XMLStreamReader reader = null;
330327
try {
331-
reader = getXMLInputFactory().createXMLStreamReader(new ByteArrayInputStream(xmlBytes));
332-
while (reader.hasNext()) {
333-
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
334-
if (!NETWORK_ROOT_ELEMENT_NAME.equals(reader.getLocalName())) {
335-
throw new PowsyblException("Unexpected root element: " + reader.getLocalName());
328+
try (ByteArrayInputStream in = new ByteArrayInputStream(xmlBytes)) {
329+
reader = getXMLInputFactory().createXMLStreamReader(in);
330+
while (reader.hasNext()) {
331+
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
332+
if (!NETWORK_ROOT_ELEMENT_NAME.equals(reader.getLocalName())) {
333+
throw new PowsyblException("Unexpected root element: " + reader.getLocalName());
334+
}
335+
String ns = reader.getNamespaceURI();
336+
if (ns == null || ns.isBlank()) {
337+
throw new PowsyblException("Missing root namespace");
338+
}
339+
return ns;
336340
}
337-
String ns = reader.getNamespaceURI();
338-
if (ns == null || ns.isBlank()) {
339-
throw new PowsyblException("Missing root namespace");
340-
}
341-
return ns;
342341
}
343-
}
344-
throw new PowsyblException("Missing root namespace");
345-
} catch (XMLStreamException e) {
346-
throw new PowsyblException("Failed to read namespace from XML", e);
347-
} finally {
348-
if (reader != null) {
349-
try {
342+
throw new PowsyblException("Missing root namespace");
343+
} catch (XMLStreamException | IOException e) {
344+
throw new PowsyblException("Failed to read namespace from XML", e);
345+
} finally {
346+
if (reader != null) {
350347
reader.close();
351-
} catch (XMLStreamException e) {
352-
LOGGER.error(e.toString(), e);
353348
}
354349
}
350+
} catch (XMLStreamException e) {
351+
throw new UncheckedXmlStreamException(e);
355352
}
356353
}
357354

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.powsybl.commons.test.TestUtil;
2121
import com.powsybl.iidm.network.*;
2222
import com.powsybl.iidm.network.test.*;
23+
import com.powsybl.iidm.serde.extensions.util.DefaultExtensionsSupplier;
24+
import com.powsybl.iidm.serde.extensions.util.ExtensionsSupplier;
2325
import com.powsybl.iidm.serde.extensions.util.NetworkSourceExtension;
2426
import com.powsybl.iidm.serde.extensions.util.NetworkSourceExtensionImpl;
2527
import org.junit.jupiter.api.Test;
@@ -453,4 +455,37 @@ void testValidateByVersionWhenInValidExtension() throws IOException {
453455
"'[IN_OPERATION, PLANNED_OUTAGE, FORCED_OUTAGE]'. It must be a value from the enumeration.");
454456
}
455457
}
458+
459+
@Test
460+
void testValidateWithCustomExtensionSupplier() throws IOException {
461+
ExtensionsSupplier customExtensionsSupplier = () -> DefaultExtensionsSupplier.getInstance().get();
462+
assertNotSame(DefaultExtensionsSupplier.getInstance(), customExtensionsSupplier);
463+
464+
try (InputStream is = getClass().getResourceAsStream("/V1_16/shuntRoundTripRef.xml")) {
465+
assertNotNull(is);
466+
assertDoesNotThrow(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16, customExtensionsSupplier));
467+
}
468+
try (InputStream is = getClass().getResourceAsStream("/V1_16/shuntRoundTripRef.xml")) {
469+
assertNotNull(is);
470+
assertDoesNotThrow(() -> NetworkSerDe.validate(is, customExtensionsSupplier));
471+
}
472+
}
473+
474+
@Test
475+
void testValidateByVersionWhenMissingNamespace() throws IOException {
476+
try (InputStream is = getClass().getResourceAsStream("/network-without-namespace.xml")) {
477+
assertNotNull(is);
478+
assertThatThrownBy(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16))
479+
.isInstanceOf(PowsyblException.class)
480+
.hasMessageContaining("Missing root namespace");
481+
}
482+
}
483+
484+
@Test
485+
void testValidateWhenParseMalFormedXml() {
486+
String xml = "<iidm:network";
487+
assertThatThrownBy(() -> NetworkSerDe.validate(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)), IidmVersion.V_1_16))
488+
.isInstanceOf(PowsyblException.class)
489+
.hasMessageContaining("Failed to read namespace from XML");
490+
}
456491
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<iidm:network xmlns:iidm=" " id="test" caseDate="2016-06-27T12:27:58.535+02:00" forecastDistance="0" sourceFormat="test" minimumValidationLevel="STEADY_STATE_HYPOTHESIS">
3+
4+
</iidm:network>

0 commit comments

Comments
 (0)