Skip to content

Commit 548d4f8

Browse files
committed
review sonar, add tests
Signed-off-by: Samir Romdhani <samir.romdhani_externe@rte-france.com> fixes sonar
1 parent 3b02211 commit 548d4f8

File tree

4 files changed

+86
-23
lines changed

4 files changed

+86
-23
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: 25 additions & 21 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
}
@@ -290,31 +287,35 @@ private static void checkNamespace(byte[] xmlBytes, IidmVersion validationVersio
290287
* @return schema locations found in {@code xs:import}
291288
*/
292289
private static List<String> extractSchemaLocations(byte[] xsdBytes) {
290+
try {
291+
return proceedExtractSchemaLocations(xsdBytes);
292+
} catch (XMLStreamException e) {
293+
throw new UncheckedXmlStreamException(e);
294+
}
295+
}
296+
297+
private static List<String> proceedExtractSchemaLocations(byte[] xsdBytes) throws XMLStreamException {
293298
List<String> locations = new ArrayList<>();
294299
XMLStreamReader reader = null;
295-
try {
296-
reader = getXMLInputFactory().createXMLStreamReader(new ByteArrayInputStream(xsdBytes));
300+
try (ByteArrayInputStream in = new ByteArrayInputStream(xsdBytes)) {
301+
reader = getXMLInputFactory().createXMLStreamReader(in);
297302
while (reader.hasNext()) {
298303
int event = reader.next();
299304
if (event == XMLStreamConstants.START_ELEMENT
300305
&& XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(reader.getNamespaceURI())
301-
&& ("import".equals(reader.getLocalName()))) {
306+
&& "import".equals(reader.getLocalName())) {
302307
String schemaLocation = reader.getAttributeValue(null, "schemaLocation");
303308
if (schemaLocation != null && !schemaLocation.isBlank() && ALLOWED_IIDM_XSDS.contains(schemaLocation)) {
304309
locations.add(schemaLocation);
305310
}
306311
}
307312
}
308313
return locations;
309-
} catch (XMLStreamException e) {
314+
} catch (XMLStreamException | IOException e) {
310315
throw new PowsyblException("Failed to parse XSD schema", e);
311316
} finally {
312317
if (reader != null) {
313-
try {
314-
reader.close();
315-
} catch (XMLStreamException e) {
316-
LOGGER.error(e.toString(), e);
317-
}
318+
reader.close();
318319
}
319320
}
320321
}
@@ -326,9 +327,16 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) {
326327
* @return Namespace URI
327328
*/
328329
private static String readRootNamespace(byte[] xmlBytes) {
329-
XMLStreamReader reader = null;
330330
try {
331-
reader = getXMLInputFactory().createXMLStreamReader(new ByteArrayInputStream(xmlBytes));
331+
return proceedReadRootNamespace(xmlBytes);
332+
} catch (XMLStreamException e) {
333+
throw new UncheckedXmlStreamException(e);
334+
}
335+
}
336+
private static String proceedReadRootNamespace(byte[] xmlBytes) throws XMLStreamException {
337+
XMLStreamReader reader = null;
338+
try (ByteArrayInputStream in = new ByteArrayInputStream(xmlBytes)) {
339+
reader = getXMLInputFactory().createXMLStreamReader(in);
332340
while (reader.hasNext()) {
333341
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
334342
if (!NETWORK_ROOT_ELEMENT_NAME.equals(reader.getLocalName())) {
@@ -342,15 +350,11 @@ private static String readRootNamespace(byte[] xmlBytes) {
342350
}
343351
}
344352
throw new PowsyblException("Missing root namespace");
345-
} catch (XMLStreamException e) {
353+
} catch (XMLStreamException | IOException e) {
346354
throw new PowsyblException("Failed to read namespace from XML", e);
347355
} finally {
348356
if (reader != null) {
349-
try {
350-
reader.close();
351-
} catch (XMLStreamException e) {
352-
LOGGER.error(e.toString(), e);
353-
}
357+
reader.close();
354358
}
355359
}
356360
}

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

Lines changed: 37 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,39 @@ 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+
byte[] bytes = xml.getBytes(StandardCharsets.UTF_8);
488+
InputStream is = new ByteArrayInputStream(bytes);
489+
assertThatThrownBy(() -> NetworkSerDe.validate(is, IidmVersion.V_1_16))
490+
.isInstanceOf(PowsyblException.class)
491+
.hasMessageContaining("Failed to read namespace from XML");
492+
}
456493
}
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)