Skip to content

Commit

Permalink
Modify getTopLevelManifest() to look for manifest file at top level o…
Browse files Browse the repository at this point in the history
…r in a single subdirectory
  • Loading branch information
weisenje committed Mar 23, 2023
1 parent 2702b37 commit d8fd829
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down
Binary file not shown.

0 comments on commit d8fd829

Please sign in to comment.