diff --git a/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/load/config/ManifestConfig.java b/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/load/config/ManifestConfig.java index aa390a2b2..54c008992 100644 --- a/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/load/config/ManifestConfig.java +++ b/sparqlGraphLibrary/src/main/java/com/ge/research/semtk/load/config/ManifestConfig.java @@ -17,6 +17,7 @@ package com.ge.research.semtk.load.config; import java.io.File; +import java.util.Arrays; import java.util.LinkedList; import org.apache.commons.math3.util.Pair; @@ -321,17 +322,28 @@ public void load(String server, String serverTypeString, boolean clear, boolean } /** - * Gets the default top-level manifest file in an unzipped ingestion package + * Gets the top-level manifest file in an unzipped ingestion package. + * This could be baseDir/manifest.yaml OR baseDir/someDir/manifest.yaml, provided that someDir is the only item in baseDir + * * @param baseDir the directory of the unzipped ingestion package * @return the file, if it exists * @throws Exception if the file is not found */ public static File getTopLevelManifestFile(File baseDir) throws Exception { - File manifestFile = new File(baseDir.getAbsoluteFile() + File.separator + ManifestConfig.DEFAULT_FILE_NAME); - if(!manifestFile.exists()) { - throw new Exception(ManifestConfig.DEFAULT_FILE_NAME + " does not exist in " + baseDir); + + String[] topLevelEntries = baseDir.list(); + + if(Arrays.asList(topLevelEntries).contains(ManifestConfig.DEFAULT_FILE_NAME)) { + // manifest.yaml found at top level + return new File(baseDir.getAbsoluteFile() + File.separator + ManifestConfig.DEFAULT_FILE_NAME); + }else if(topLevelEntries.length == 1) { + // if top level consists of a single directory, then look for manifest.yaml there + File f = new File(baseDir.getAbsoluteFile() + File.separator + topLevelEntries[0] + File.separator + ManifestConfig.DEFAULT_FILE_NAME); + if(f.exists()) { + return f; + } } - return manifestFile; + throw new Exception("Top-level " + ManifestConfig.DEFAULT_FILE_NAME + " does not exist in " + baseDir); } diff --git a/sparqlGraphLibrary/src/test/java/com/ge/research/semtk/load/config/test/ManifestConfigTest.java b/sparqlGraphLibrary/src/test/java/com/ge/research/semtk/load/config/test/ManifestConfigTest.java new file mode 100644 index 000000000..54e3ebcde --- /dev/null +++ b/sparqlGraphLibrary/src/test/java/com/ge/research/semtk/load/config/test/ManifestConfigTest.java @@ -0,0 +1,72 @@ +/** + ** Copyright 2023 General Electric Company + ** + ** + ** Licensed under the Apache License, Version 2.0 (the "License"); + ** you may not use this file except in compliance with the License. + ** You may obtain a copy of the License at + ** + ** http://www.apache.org/licenses/LICENSE-2.0 + ** + ** Unless required by applicable law or agreed to in writing, software + ** distributed under the License is distributed on an "AS IS" BASIS, + ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ** See the License for the specific language governing permissions and + ** limitations under the License. + */ +package com.ge.research.semtk.load.config.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; + +import org.junit.Test; + +import com.ge.research.semtk.load.config.ManifestConfig; +import com.ge.research.semtk.test.TestGraph; + +public class ManifestConfigTest { + + public ManifestConfigTest() throws Exception { + super(); + } + + @Test + public void testGetTopLevelManifest() throws Exception{ + + File tempDir; + File manifestFile; + + // manifest found at top level + tempDir = TestGraph.unzipAndUniquifyJunitGraphs(this, "/config/IngestionPackage.zip"); + manifestFile = ManifestConfig.getTopLevelManifestFile(tempDir); + assertEquals(manifestFile.getName(), "manifest.yaml"); + + // manifest found at next-to top level + tempDir = TestGraph.unzipAndUniquifyJunitGraphs(this, "/config/IngestionPackage-ExtraDirectory.zip"); + manifestFile = ManifestConfig.getTopLevelManifestFile(tempDir); + assertEquals(manifestFile.getName(), "manifest.yaml"); + + // manifest exists at next-to top level, but reject because there are 2 directories at top level + File dummyDir = new File(tempDir + File.separator + "dummyDir"); + dummyDir.createNewFile(); + try { + manifestFile = ManifestConfig.getTopLevelManifestFile(tempDir); + fail(); // should not get here + }catch(Exception e) { + assertTrue(e.getMessage().contains("Top-level manifest.yaml does not exist in")); + } + + // manifest not found + try { + tempDir = TestGraph.unzipAndUniquifyJunitGraphs(this, "/config/IngestionPackage-NoManifest.zip"); + manifestFile = ManifestConfig.getTopLevelManifestFile(tempDir); + fail(); // should not get here + }catch(Exception e) { + assertTrue(e.getMessage().contains("Top-level manifest.yaml does not exist in")); + } + } + +} diff --git a/sparqlGraphLibrary/src/test/java/com/ge/research/semtk/utility/test/UtilityClientTest_IT.java b/sparqlGraphLibrary/src/test/java/com/ge/research/semtk/utility/test/UtilityClientTest_IT.java index b9d5acd7c..5c3d1d9e0 100644 --- a/sparqlGraphLibrary/src/test/java/com/ge/research/semtk/utility/test/UtilityClientTest_IT.java +++ b/sparqlGraphLibrary/src/test/java/com/ge/research/semtk/utility/test/UtilityClientTest_IT.java @@ -109,7 +109,7 @@ public void testLoadIngestionPackage_errorConditions() throws Exception { assertTrue(response.contains("ERROR: This endpoint only accepts ingestion packages in zip file format")); // contains no top-level manifest.yaml - response = Utility.readToString(client.execLoadIngestionPackage(Utility.getResourceAsTempFile(this,"/config/IngestionPackageNoManifest.zip"), TestGraph.getSparqlServer(), TestGraph.getSparqlServerType(), false, modelFallbackSei.getGraph(), dataFallbackSei.getGraph())); + response = Utility.readToString(client.execLoadIngestionPackage(Utility.getResourceAsTempFile(this,"/config/IngestionPackage-NoManifest.zip"), TestGraph.getSparqlServer(), TestGraph.getSparqlServerType(), false, modelFallbackSei.getGraph(), dataFallbackSei.getGraph())); assertTrue(response.contains("ERROR: Cannot find a top-level manifest")); } diff --git a/sparqlGraphLibrary/src/test/resources/config/IngestionPackage-ExtraDirectory.zip b/sparqlGraphLibrary/src/test/resources/config/IngestionPackage-ExtraDirectory.zip new file mode 100644 index 000000000..885f018ab Binary files /dev/null and b/sparqlGraphLibrary/src/test/resources/config/IngestionPackage-ExtraDirectory.zip differ diff --git a/sparqlGraphLibrary/src/test/resources/config/IngestionPackageNoManifest.zip b/sparqlGraphLibrary/src/test/resources/config/IngestionPackage-NoManifest.zip similarity index 100% rename from sparqlGraphLibrary/src/test/resources/config/IngestionPackageNoManifest.zip rename to sparqlGraphLibrary/src/test/resources/config/IngestionPackage-NoManifest.zip