-
Notifications
You must be signed in to change notification settings - Fork 43
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e196ede
CgmesImporter, allow importing with wrong dataextension when the main…
jonenst 21238e9
Merge branch 'main' into fixcgmesimportonmissingmainfile
So-Fras ee7c5b3
Merge branch 'main' into fixcgmesimportonmissingmainfile
flo-dup 1d6a4e7
more testing
jonenst 8350614
Update cgmes/cgmes-model/src/test/java/com/powsybl/cgmes/model/CgmesO…
jonenst a629e8e
Update cgmes/cgmes-model/src/test/java/com/powsybl/cgmes/model/CgmesO…
jonenst 2906e96
checkstyle
jonenst a73c18d
fix after review
jonenst 6d13331
remove unneed exists, it's already checked
jonenst d12134e
Merge branch 'main' into fixcgmesimportonmissingmainfile
jonenst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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), | ||
|
@@ -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") | ||
); | ||
} | ||
|
||
@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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.