Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CgmesImporter, allow importing with wrong dataextension when the mainfile doesn't exist #3147

Merged
merged 10 commits into from
Sep 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public ReadOnlyDataSource dataSource() {
}

private boolean checkIfMainFileNotWithCgmesData(boolean isCim14) throws IOException {
if (dataSource.getDataExtension() == null || dataSource.getDataExtension().isEmpty()) {
if (dataSource.getDataExtension() == null || dataSource.getDataExtension().isEmpty() || !dataSource.exists(null, dataSource.getDataExtension())) {
return false;
} else if (EXTENSION.equals(dataSource.getDataExtension()) && dataSource.exists(null, EXTENSION)) {
} else if (EXTENSION.equals(dataSource.getDataExtension())) {
try (InputStream is = dataSource.newInputStream(null, EXTENSION)) {
return isCim14 ? !existsNamespacesCim14(NamespaceReader.namespaces(is)) : !existsNamespaces(NamespaceReader.namespaces(is));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.powsybl.commons.datasource.ResourceDataSource;
import com.powsybl.commons.datasource.ResourceSet;
import com.powsybl.commons.datasource.ZipArchiveDataSource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -34,6 +33,8 @@
*/
class CgmesOnDataSourceTest {

private static final String XIIDM_XML_NOT_CGMES = "<?xml version='1.0' encoding='UTF-8'?><some></some>";

static Stream<Arguments> provideArguments() {
return Stream.of(
Arguments.of("EQ cim14", "empty_cim14_EQ.xml", "14", true),
Expand All @@ -58,26 +59,127 @@ void testExists(String testName, String filename, String cimVersion, boolean exp
assertEquals(expectedExists, exists);
}

@Test
void testFileDoesNotExist() throws IOException {
static Stream<Arguments> provideArgumentsForTestXmlMainFileXiidmZip() {
return Stream.of(
Arguments.of("foo", "xml"),
Arguments.of("foo", null),
Arguments.of("foo", ""),
Arguments.of("foo", "notexists"),
Arguments.of("bar", "xml"),
Arguments.of("bar", null),
Arguments.of("bar", ""),
Arguments.of("bar", "notexists")
Comment on lines +68 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are they useful? I think each one corresponds to the same logic as one of the previous arguments.

);
}

@ParameterizedTest
@MethodSource("provideArgumentsForTestXmlMainFileXiidmZip")
void testXmlMainFileXiidmZip(String basename, String dataextension) throws IOException {
Path testDir;
try (FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix())) {
testDir = fileSystem.getPath("/tmp");
Files.createDirectories(testDir);
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(testDir.resolve("my.zip")))) {
try {
ZipEntry e = new ZipEntry("foo.xml");
out.putNextEntry(e);
byte[] data = XIIDM_XML_NOT_CGMES.getBytes();
out.write(data, 0, data.length);
out.closeEntry();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

ReadOnlyDataSource dataSourceGoodBasenameGoodDataExtension = new ZipArchiveDataSource(
testDir, "my.zip", basename, dataextension, null);
CgmesOnDataSource cgmesOnDataSourceGoodBasenameGoodDataExtension = new CgmesOnDataSource(
dataSourceGoodBasenameGoodDataExtension);
assertFalse(cgmesOnDataSourceGoodBasenameGoodDataExtension.exists());

}
}

static Stream<Arguments> provideArgumentsForTestXmlMainFileCgmesZip() {
return Stream.of(
Arguments.of("foo", "xml", true),
Arguments.of("foo", "xiidm", false),
Arguments.of("foo", "notexists", true),
Arguments.of("foo", "", true),
Arguments.of("foo", null, true),
Arguments.of("bar", "xml", false),
Arguments.of("bar", "xiidm", false),
Arguments.of("bar", "notexists", true),
Arguments.of("bar", "", true),
Arguments.of("bar", null, true),
Arguments.of("kop", "xml", true),
Arguments.of("kop", "xiidm", false),
Arguments.of("kop", "notexists", true),
Arguments.of("kop", "", true),
Arguments.of("kop", null, true),
Arguments.of("notexist", "xml", true),
Arguments.of("notexist", "xiidm", true),
Arguments.of("notexist", "notexists", true),
Arguments.of("notexist", "", true),
Arguments.of("notexist", null, true)
Comment on lines +120 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure those are needed

);
}

@ParameterizedTest
@MethodSource("provideArgumentsForTestXmlMainFileCgmesZip")
void testXmlMainFileCgmesZip(String basename, String dataextension, boolean expected) throws IOException {
Path testDir;
try (FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix())) {
testDir = fileSystem.getPath("/tmp");
Files.createDirectories(testDir);
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(testDir.resolve("foo.iidm.zip")))) {
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(testDir.resolve("my.zip")))) {
try {
ZipEntry e = new ZipEntry("foo.bar");
// The cgmes file that justifies importing this as CGMES
ZipEntry e = new ZipEntry("foo.xml");
out.putNextEntry(e);
byte[] data = getClass().getResourceAsStream("/empty_cim16_EQ.xml").readAllBytes();
out.write(data, 0, data.length);
out.closeEntry();

// random other files, depending on the datasource basename and dataextension
// the cmgmes importer will refuse to import to allow other importers to import
e = new ZipEntry("foo.xiidm");
out.putNextEntry(e);
data = "same basename as the tested cgmes file foo.xml".getBytes();
out.write(data, 0, data.length);
out.closeEntry();
// Note: and no need to test prefix matching file names like "fooooo.xiidm" or
// "fooooo.xml"
// because the prefixing of the basename is not used for exists()

// different basename but xml may still be cgmes
e = new ZipEntry("bar.xml");
out.putNextEntry(e);
data = XIIDM_XML_NOT_CGMES.getBytes();
out.write(data, 0, data.length);
out.closeEntry();

e = new ZipEntry("bar.xiidm");
out.putNextEntry(e);
byte[] data = "Test String".getBytes();
data = "different basename different extension".getBytes();
out.write(data, 0, data.length);
out.closeEntry();

e = new ZipEntry("kop.xiidm");
out.putNextEntry(e);
data = "nothing in common, there is no other file with the same basename and .xml extension"
.getBytes();
out.write(data, 0, data.length);
out.closeEntry();

} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
ReadOnlyDataSource dataSource = new ZipArchiveDataSource(testDir, "foo.iidm.zip", "test", "xml", null);
ReadOnlyDataSource dataSource = new ZipArchiveDataSource(testDir, "my.zip", basename, dataextension, null);
CgmesOnDataSource cgmesOnDataSource = new CgmesOnDataSource(dataSource);
assertFalse(cgmesOnDataSource.exists());
assertEquals(expected, cgmesOnDataSource.exists());
}
}

}