From fcb4e980f76116edb2ec6a656a99d510c70c1c94 Mon Sep 17 00:00:00 2001 From: Murali-P Date: Wed, 4 Apr 2018 18:19:29 +0530 Subject: Enhance error messages for validation Show proper error message in UI Change-Id: I39158f3d727cd817758fd688ec6bbf4fbd8ae426 Issue-ID: VNFSDK-233 Signed-off-by: Murali-P --- .../org/onap/validation/csar/CsarValidator.java | 409 +++++++++++---------- .../validation/csar/ValidatorSchemaLoader.java | 137 +++---- .../csarvalidationtest/CsarValidatorTest.java | 64 ++-- 3 files changed, 325 insertions(+), 285 deletions(-) (limited to 'csarvalidation') diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java index bd5646c..88cd0e0 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java @@ -13,290 +13,309 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.onap.validation.csar; +import static java.nio.charset.StandardCharsets.UTF_8; -import org.apache.commons.io.FilenameUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; import java.nio.file.Files; import java.nio.file.Paths; -import static java.nio.charset.StandardCharsets.*; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; + +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; public class CsarValidator { private static final Logger LOG = LoggerFactory.getLogger(CsarValidator.class); - //Schema files + // Schema files static private ValidatorSchemaLoader vsl; - // Map of CSAR file and un-zipped file indices + // Map of CSAR file and un-zipped file indices static private HashMap csarFiles; - // Map of packageId and CSAR files + // Map of packageId and CSAR files private static HashMap> csar = new HashMap>(); + private static String MAINSERV_TEMPLATE = CommonConstants.MAINSERV_TEMPLATE; + private static String MAINSERV_MANIFEST; /** - * * @param packageId * @param csarWithPath - * @throws IOException + * @throws IOException */ public CsarValidator(String packageId, String csarWithPath) throws IOException { - try(FileInputStream is = new FileInputStream(csarWithPath)) { - - } catch (FileNotFoundException e2) { - LOG.error("CSAR %s is not found! " +ErrorCodes.RESOURCE_MISSING, e2); - throw new ValidationException(ErrorCodes.RESOURCE_MISSING); - } + try (FileInputStream is = new FileInputStream(csarWithPath)) { - try { - csarFiles = CsarUtil.csarExtract(csarWithPath); - if(!csarFiles.isEmpty()) { - csar.put(packageId, getCsarFiles()); - LOG.debug("CSAR extracted sucessfully."); - } - } catch (Exception e1) { - LOG.error("INVALID_CSAR_CONTENT" + ":" + "CSAR %s is not a valid CSAR/ZIP file! " +ErrorCodes.INVALID_CSAR_CONTENT, e1); - throw new ValidationException(ErrorCodes.INVALID_CSAR_CONTENT); - } + } catch(FileNotFoundException e2) { + LOG.error(csarWithPath + ":CSAR is not found! " + ErrorCodes.RESOURCE_MISSING, e2); + throw new ValidationException(ErrorCodes.RESOURCE_MISSING, + "RESOURCE MISSING" + csarWithPath + ":CSAR is not found!"); + } + try { + csarFiles = CsarUtil.csarExtract(csarWithPath); + if(!csarFiles.isEmpty()) { + csar.put(packageId, getCsarFiles()); + LOG.debug("CSAR extracted sucessfully."); + } + } catch(Exception e1) { + LOG.error("INVALID_CSAR_CONTENT" + ":" + csarWithPath + ": CSAR is not a valid CSAR/ZIP file! " + + ErrorCodes.INVALID_CSAR_CONTENT, e1); + throw new ValidationException(ErrorCodes.INVALID_CSAR_CONTENT, + "INVALID_CSAR_CONTENT" + ":" + csarWithPath + ": CSAR is not a valid CSAR/ZIP file! "); + } try { - vsl = new ValidatorSchemaLoader(); - } catch (Exception e) { - LOG.error("SCHEMA_LOAD_ERROR" + ":" + "CSAR schema is not loaded correctly! " +ErrorCodes.SCHEMA_LOAD_ERROR, e); - throw new ValidationException(ErrorCodes.SCHEMA_LOAD_ERROR); + vsl = new ValidatorSchemaLoader(); + } catch(Exception e) { + LOG.error( + "SCHEMA_LOAD_ERROR" + ":" + "CSAR schema is not loaded correctly! " + ErrorCodes.SCHEMA_LOAD_ERROR, + e); + throw new ValidationException(ErrorCodes.SCHEMA_LOAD_ERROR, + "SCHEMA_LOAD_ERROR" + ":" + "CSAR schema is not loaded correctly! "); } } + /** - * * @return true if all validations are successful */ - public static boolean validateCsar() { + public static String validateCsar() { - boolean vsm = validateCsarMeta(); + String vsm = validateCsarMeta(); - boolean vtm = validateAndScanToscaMeta(); + String vtm = validateAndScanToscaMeta(); - boolean vms = validateMainService(); + String vms = validateMainService(); - if ((vsm || vms ) && vtm) { - return true; + if((CommonConstants.SUCCESS_STR != vsm) && (CommonConstants.SUCCESS_STR != vms)) { + + return vsm + " OR " + vms; } + if(CommonConstants.SUCCESS_STR != vtm) { + return vtm; + } - return false; + return CommonConstants.SUCCESS_STR; } - + /** - * * @param csarWithPath * @return true if csar integrity validation is successful */ public static boolean validateCsarIntegrity(String csarWithPath) { - try { - RandomAccessFile raf = new RandomAccessFile(csarWithPath, "r"); - try { - long n = raf.readInt(); - - // Check for the CSAR's integrity - if (n != 0x504B0304) { - LOG.error("CSAR %s contents are not a valid! "); - return false; - } - } catch (FileNotFoundException e1) { - LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); - return false; - } - finally { - raf.close(); - } - } - catch (IOException e1) { - LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); - return false; - } - return true; + try { + RandomAccessFile raf = new RandomAccessFile(csarWithPath, "r"); + try { + long n = raf.readInt(); + + // Check for the CSAR's integrity + if(n != 0x504B0304) { + LOG.error("CSAR %s contents are not a valid! "); + return false; + } + } catch(FileNotFoundException e1) { + LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); + return false; + } finally { + raf.close(); + } + } catch(IOException e1) { + LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); + return false; + } + return true; } /** - * * @return true if csar meta data validation is successful */ - public static boolean validateCsarMeta() { + public static String validateCsarMeta() { - String cfile = csarFiles.get(CommonConstants.CSAR_META); - if (StringUtils.isEmpty(cfile)) { - return false; + String cfile = csarFiles.get(CommonConstants.CSAR_META); + if(null == cfile) { + return "VNF package do not contain csar.meta file"; + } + if(!cfile.isEmpty()) { + File file = new File(cfile); + + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + String tempString = null; + while((tempString = reader.readLine()) != null) { + if(!tempString.equals("")) { + int count1 = tempString.indexOf(":"); + String meta = tempString.substring(0, count1).trim(); + if(meta.equalsIgnoreCase(CommonConstants.CSAR_TYPE_META)) { + int count = tempString.indexOf(":") + 1; + if(tempString.substring(count).trim().isEmpty()) { + return "Validating csar.meta:Type field is in wrong format or empty"; + } + } + if(meta.equalsIgnoreCase(CommonConstants.CSAR_PROVIDER_META)) { + int count = tempString.indexOf(":") + 1; + if(tempString.substring(count).trim().isEmpty()) { + return "Validating csar.meta:Provider field is in wrong format or empty"; + } + } + if(meta.equalsIgnoreCase(CommonConstants.CSAR_VERSION_META)) { + int count = tempString.indexOf(":") + 1; + if(tempString.substring(count).trim().isEmpty()) { + return "Validating csar.meta:Version field is in wrong format or empty"; + } + } + } + reader.close(); + return CommonConstants.SUCCESS_STR; + } + } catch(IOException e2) { + return ("CSAR_META_VALIDATION" + ":" + "Exception caught while validateCsarMeta ! " + ErrorCodes.FILE_IO + + e2); + } } - if (!cfile.isEmpty()) { - File file = new File(cfile); - - try(BufferedReader reader = new BufferedReader(new FileReader(file))) { - String tempString = null; - while ((tempString = reader.readLine()) != null) { - if (!tempString.equals("")) { - int count1 = tempString.indexOf(":"); - String meta = tempString.substring(0, count1).trim(); - if (meta.equalsIgnoreCase(CommonConstants.CSAR_TYPE_META)) { - int count = tempString.indexOf(":") + 1; - if (tempString.substring(count).trim().isEmpty()) { - return false; - } - } - if (meta.equalsIgnoreCase(CommonConstants.CSAR_PROVIDER_META)) { - int count = tempString.indexOf(":") + 1; - if (tempString.substring(count).trim().isEmpty()) { - return false; - } - } - if (meta.equalsIgnoreCase(CommonConstants.CSAR_VERSION_META)) { - int count = tempString.indexOf(":") + 1; - if (tempString.substring(count).trim().isEmpty()) { - return false; - } - } - } - reader.close(); - return true; - } - } catch (IOException e2) { - LOG.error("CSAR_META_VALIDATION" + ":" + "Exception caught while validateCsarMeta ! " +ErrorCodes.FILE_IO, e2); - throw new ValidationException(ErrorCodes.FILE_IO); - } - } - - return false; + + return "VNF package do not contain csar.meta file"; } - /** - * - * @return true csar tosca meta validation is successful - */ - public static boolean validateAndScanToscaMeta() { + /** + * @return true csar tosca meta validation is successful + */ + public static String validateAndScanToscaMeta() { String cfile = csarFiles.get(CommonConstants.TOSCA_META); - if (!validateToscaMeta(cfile)) { - return false; + if(null == cfile) { + return "Tosca.meta file does not exist"; + } + + if(!validateToscaMeta(cfile)) { + return "Validation of Tosca.meta file fails"; } try { - if (cfile.contains(System.getProperty("file.separator") + - CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") + - CommonConstants.TOSCA_META)) { - MAINSERV_MANIFEST = checkAndGetMRF(cfile,"Entry-Manifest"); - if (MAINSERV_MANIFEST == null) { + if(cfile.contains(System.getProperty("file.separator") + CommonConstants.TOSCA_METADATA + + System.getProperty("file.separator") + CommonConstants.TOSCA_META)) { + MAINSERV_MANIFEST = checkAndGetMRF(cfile, "Entry-Manifest"); + if(MAINSERV_MANIFEST == null) { MAINSERV_MANIFEST = CommonConstants.MAINSERV_MANIFEST; } - MAINSERV_TEMPLATE = checkAndGetMRF(cfile,"Entry-Definitions"); - if (MAINSERV_TEMPLATE == null) { + MAINSERV_TEMPLATE = checkAndGetMRF(cfile, "Entry-Definitions"); + if(MAINSERV_TEMPLATE == null) { MAINSERV_TEMPLATE = CommonConstants.MAINSERV_TEMPLATE; } - return true; + return CommonConstants.SUCCESS_STR; } - } catch (Exception e) { - LOG.error("PARSE_ERROR" + ":" + "TOSCA metadata not parsed properly! " +ErrorCodes.PARSE_ERROR, e); + } catch(Exception e) { + return ("PARSE_ERROR" + ":" + "TOSCA metadata not parsed properly! " + ErrorCodes.PARSE_ERROR + e); } - return false; + return "Tosca.meta file does not exist"; } private static String checkAndGetMRF(String mrfFile, String attribute) { try { - String value = CheckEntryFor(mrfFile, attribute); - String mrfCsarEntry = null; + String value = checkEntryFor(mrfFile, attribute); + String mrfCsarEntry = null; - //Rel-1 & SOL004 Entry-Definitions is optional - if (! StringUtils.isEmpty(value)) { + // Rel-1 & SOL004 Entry-Definitions is optional + if(!StringUtils.isEmpty(value)) { - if(value.contains("Definitions/")) - { - String[] splitPath = value.split("/"); - mrfCsarEntry = csarFiles.get(splitPath[splitPath.length - 1]); - } - else { //Hack to support non-compliant "Entry-Definitions:" format - mrfCsarEntry = csarFiles.get(value); - } + if(value.contains("Definitions/")) { + String[] splitPath = value.split("/"); + mrfCsarEntry = csarFiles.get(splitPath[splitPath.length - 1]); + } else { // Hack to support non-compliant "Entry-Definitions:" format + mrfCsarEntry = csarFiles.get(value); + } - if (null != mrfCsarEntry) { - return mrfCsarEntry; + if(null != mrfCsarEntry) { + return mrfCsarEntry; + } } - } - } catch (IOException | NullPointerException e) { - LOG.error("CSAR_TOSCA_VALIDATION" + ":" + "Could not read file %s ! " +ErrorCodes.FILE_IO+ " " +ErrorCodes.RESOURCE_MISSING,e); - throw new ValidationException(ErrorCodes.RESOURCE_MISSING); + } catch(IOException | NullPointerException e) { + LOG.error("CSAR_TOSCA_VALIDATION" + ":" + "Could not read file %s ! " + ErrorCodes.FILE_IO + " " + + ErrorCodes.RESOURCE_MISSING, e); + throw new ValidationException(ErrorCodes.RESOURCE_MISSING); } return null; } /** - * * @return true csar validation is successful */ - public static boolean validateMainService() { + public static String validateMainService() { String key = "metadata"; // In future load from the respective file template/schema - List mListMetadata = Arrays.asList("vnf_product_name", "vnf_provider_id", - "vnf_package_version", "vnf_release_data_time"); + List mListMetadata = + Arrays.asList("vnf_product_name", "vnf_provider_id", "vnf_package_version", "vnf_release_data_time"); @SuppressWarnings("unused") - boolean mfResult = CheckEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key); + boolean mfResult = checkEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key); String mainServManifest = MAINSERV_MANIFEST; - if(!Paths.get(mainServManifest).isAbsolute()){ + if(!Paths.get(mainServManifest).isAbsolute()) { mainServManifest = csarFiles.get(FilenameUtils.getName(mainServManifest)); } // Rel-2 SOL004 requirement - if(StringUtils.isEmpty(mainServManifest)){ - //Do nothing for Rel-1 - //return false; + if(StringUtils.isEmpty(mainServManifest)) { + // Do nothing for Rel-1 + // return false; } String mainservTemplate = MAINSERV_TEMPLATE; - if(!Paths.get(MAINSERV_TEMPLATE).isAbsolute()){ - mainservTemplate = csarFiles.get(FilenameUtils.getName(mainservTemplate)); + if(!Paths.get(MAINSERV_TEMPLATE).isAbsolute()) { + mainservTemplate = csarFiles.get(FilenameUtils.getName(mainservTemplate)); } - if(StringUtils.isEmpty(mainservTemplate)){ - return false; + if(StringUtils.isEmpty(mainservTemplate)) { + return "MainServiceTemplate does not exist in the package"; } - return true; + return CommonConstants.SUCCESS_STR; } - private static String CheckEntryFor(String fileWithPath, String attribute) throws IOException { + private static String checkEntryFor(String fileWithPath, String attribute) throws IOException { List lines = Files.readAllLines(Paths.get(fileWithPath), UTF_8); - for (String strLine : lines) { - if (!attribute.isEmpty() && strLine.contains(attribute)) { + for(String strLine : lines) { + if(!attribute.isEmpty() && strLine.contains(attribute)) { String entry = strLine.substring(attribute.length(), strLine.length()).trim(); - return entry.replaceFirst(":","").trim(); + return entry.replaceFirst(":", "").trim(); } } return null; } @SuppressWarnings("unchecked") - private static boolean CheckEntryFor(String cFile, List attributes, String key) { + private static boolean checkEntryFor(String cFile, List attributes, String key) { @SuppressWarnings("unused") - String tFileWithPath; + String tFileWithPath; - if (! Paths.get(cFile).isAbsolute()) { + if(!Paths.get(cFile).isAbsolute()) { cFile = csarFiles.get(FilenameUtils.getName(cFile)); } @@ -306,69 +325,68 @@ public class CsarValidator { Yaml yaml = new Yaml(); Map values; - try { - values = (Map) yaml.load(new FileInputStream(new File(cFile))); - } catch (FileNotFoundException e) { - LOG.error("FILE_NOT_FOUND" + ":" + "Exception caught while trying to find the file ! " + e.getMessage(), e); + try (InputStream input = new FileInputStream(new File(cFile))) { + values = (Map)yaml.load(input); + } catch(FileNotFoundException e) { + LOG.error("FILE_NOT_FOUND" + ":" + "Exception caught while trying to find the file ! " + e.getMessage(), e); + return false; + } catch(IOException e1) { + LOG.error("FILE_NOT_FOUND" + ":" + "Exception caught while trying to open the file ! " + e1.getMessage(), + e1); return false; } - Map subValues = (Map) values.get(key); + Map subValues = (Map)values.get(key); - //1. Check for empty values in map and if number of mandatory attributes presence - List lResultNonNull = subValues.values().stream() - .filter(Objects::nonNull) - .map(Object::toString) - .collect(Collectors.toList()); + // 1. Check for empty values in map and if number of mandatory attributes presence + List lResultNonNull = + subValues.values().stream().filter(Objects::nonNull).map(Object::toString).collect(Collectors.toList()); // If no attributes provided, take it easy // there are no mandatory fields to be // validated. - if (attributes == null) { + if(attributes == null) { return true; } - if (subValues.size() != attributes.size() && - lResultNonNull.size() != attributes.size()) { + if(subValues.size() != attributes.size() && lResultNonNull.size() != attributes.size()) { return false; } - //Validate the exact mandatory(not defined in SOL004 yet) + // Validate the exact mandatory(not defined in SOL004 yet) // attributes with expected attributes list - List lResult = subValues.keySet().stream() - .filter(attributes::contains) - .collect(Collectors.toList()); + List lResult = + subValues.keySet().stream().filter(attributes::contains).collect(Collectors.toList()); - if (lResult.size() == 0) { + if(lResult.size() == 0) { return false; } return true; } /** - * * @return true if csar meta data validation is successful */ - @SuppressWarnings({ "unchecked", "static-access" }) - private static boolean validateToscaMeta(String cfile) { + @SuppressWarnings({"unchecked", "static-access"}) + private static boolean validateToscaMeta(String cfile) { - if (StringUtils.isEmpty(cfile)) { + if(StringUtils.isEmpty(cfile)) { return false; - } - else { + } else { File file = new File(cfile); Yaml yaml = new Yaml(); - Map toscaMeta = null; - try { - toscaMeta = (Map) yaml.load(new FileInputStream(file)); - } catch (FileNotFoundException e) { - LOG.error("CSAR_TOSCA_LOAD" + ":" + "TOSCA metadata is not loaded by Yaml! " +ErrorCodes.FILE_IO, e); + try (InputStream input = new FileInputStream(new File(cfile))) { + toscaMeta = (Map)yaml.load(input); + } catch(FileNotFoundException e) { + LOG.error("CSAR_TOSCA_LOAD" + ":" + "TOSCA metadata is not loaded by Yaml! " + ErrorCodes.FILE_IO, e); + } catch(IOException e1) { + LOG.error("CSAR_TOSCA_LOAD" + ":" + "TOSCA metadata is not loaded by Yaml! " + ErrorCodes.FILE_IO, e1); } - if(toscaMeta != null){ - return toscaMeta.keySet().containsAll((vsl.getToscaMeta().keySet())); + if(toscaMeta != null) { + return toscaMeta.keySet().containsAll((vsl.getToscaMeta().keySet())); } return false; } @@ -390,4 +408,3 @@ public class CsarValidator { CsarValidator.csarFiles = csarFiles; } } - diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java b/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java index 7731801..a33fe93 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java @@ -13,14 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.onap.validation.csar; +package org.onap.validation.csar; import org.apache.commons.io.FileSystemUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Stream; @@ -32,91 +35,97 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; - public class ValidatorSchemaLoader { private static final Logger LOG = LoggerFactory.getLogger(ValidatorSchemaLoader.class); // Map of Schema files private Map toscaMeta; + private Map csarentryd; + private Map mrfYaml; + private Map mrfManifest; // List of configured schemas - static List schemaFileList = new ArrayList(); + static List schemaFileList = new ArrayList(); // SOL004 rule files static HashMap optionOneSchema; + static HashMap optionTwoSchema; - private String schema_folder; - public ValidatorSchemaLoader() throws NullPointerException, FileNotFoundException, ScannerException, IOException { + private String schema_folder; + public ValidatorSchemaLoader() throws NullPointerException, FileNotFoundException, ScannerException, IOException { - try { - loadResources(); - } catch ( FileNotFoundException e1) { - LOG.error("Schema file not found or schema repository corrupted", e1); + try { + loadResources(); + } catch(FileNotFoundException e1) { + LOG.error("Schema file not found or schema repository corrupted", e1); - } + } catch(URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } - @SuppressWarnings("unchecked") - private boolean loadResources() throws FileNotFoundException { - String schema_folder = getClass().getResource("../../../../schema").getPath(); - try (Stream paths = Files.walk(Paths.get(schema_folder))){ - - - paths.filter(Files::isRegularFile) - .forEach((Path e) -> { - - File file = e.toFile(); - - if (!file.isDirectory() && ( - FilenameUtils.isExtension(file.getName(), "yaml") || - FilenameUtils.isExtension(file.getName(), "mf") || - FilenameUtils.isExtension(file.getName(), "meta"))) { - - Yaml yaml = new Yaml(); - - switch (file.getName()) { - case "TOSCA.meta": - try { - toscaMeta = (Map) yaml.load(new FileInputStream(file)); - } catch (ScannerException | FileNotFoundException e1) { - LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), e1); - } - break; - case "CSAR.meta": - try { - csarentryd = (Map) yaml.load(new FileInputStream(file)); - } catch (ScannerException | FileNotFoundException e2) { - LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), e2); - } - break; - case "MRF.yaml": - try { - mrfYaml = (Map) yaml.load(new FileInputStream(file)); - } catch (ScannerException | FileNotFoundException e2) { - LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), e2); - } - break; - case "MRF.mf": - try { - mrfManifest = (Map) yaml.load(new FileInputStream(file)); - } catch (ScannerException | FileNotFoundException e2) { - LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), e2); - } - break; + private boolean loadResources() throws FileNotFoundException, URISyntaxException { + URL schemafolder = this.getClass().getClassLoader().getResource("schema"); + try (Stream paths = Files.walk(Paths.get(schemafolder.toURI()))) { + + paths.filter(Files::isRegularFile).forEach((Path e) -> { + + File file = e.toFile(); + + if(!file.isDirectory() && (FilenameUtils.isExtension(file.getName(), "yaml") + || FilenameUtils.isExtension(file.getName(), "mf") + || FilenameUtils.isExtension(file.getName(), "meta"))) { + + Yaml yaml = new Yaml(); + + switch(file.getName()) { + case "TOSCA.meta": + try { + toscaMeta = (Map)yaml.load(new FileInputStream(file)); + } catch(ScannerException | FileNotFoundException e1) { + LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), + e1); } - } - schemaFileList.add(e.toAbsolutePath().toString()); - }); - - } catch (NullPointerException | IOException err) { - LOG.error("Schema files/folder access error"+err); + break; + case "CSAR.meta": + try { + csarentryd = (Map)yaml.load(new FileInputStream(file)); + } catch(ScannerException | FileNotFoundException e2) { + LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), + e2); + } + break; + case "MRF.yaml": + try { + mrfYaml = (Map)yaml.load(new FileInputStream(file)); + } catch(ScannerException | FileNotFoundException e2) { + LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), + e2); + } + break; + case "MRF.mf": + try { + mrfManifest = (Map)yaml.load(new FileInputStream(file)); + } catch(ScannerException | FileNotFoundException e2) { + LOG.error("Schema files %s format is not as per standard prescribed", file.getName(), + e2); + } + break; + } + } + schemaFileList.add(e.toAbsolutePath().toString()); + }); + + } catch(NullPointerException | IOException err) { + LOG.error("Schema files/folder access error" + err); } return true; @@ -141,4 +150,4 @@ public class ValidatorSchemaLoader { public List getSchemaFileList() { return schemaFileList; } -} \ No newline at end of file +} diff --git a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java index 846d9af..5693ba6 100644 --- a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java @@ -30,36 +30,52 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.ZipException; import java.util.zip.ZipFile; + import org.junit.Test; +import org.onap.validation.csar.CommonConstants; import org.onap.validation.csar.CsarValidator; import org.onap.validation.csar.FileUtil; public class CsarValidatorTest { String regex = "^\\/[a-zA-Z]\\:\\/"; + ClassLoader classLoader = getClass().getClassLoader(); + Pattern pattern = Pattern.compile(regex); + private String csarFile = classLoader.getResource("enterprise2DC.csar").getFile(); + Matcher matcher = pattern.matcher(csarFile); + String sample1 = System.getProperty("file.separator") + csarFile.substring(1); + String packageId = UUID.randomUUID().toString(); private String csarFile2 = classLoader.getResource("vEPC_NS.csar").getFile(); + String sample2 = System.getProperty("file.separator") + csarFile2.substring(1); + String packageId2 = UUID.randomUUID().toString(); private String csarFile3 = classLoader.getResource("vIMS_NS.csar").getFile(); + String sample3 = System.getProperty("file.separator") + csarFile3.substring(1); - String packageId3 = UUID.randomUUID().toString(); - + + String packageId3 = UUID.randomUUID().toString(); + private String csarFile4 = classLoader.getResource("VoLTE.csar").getFile(); + String sample4 = System.getProperty("file.separator") + csarFile4.substring(1); - String packageId4 = UUID.randomUUID().toString(); - + + String packageId4 = UUID.randomUUID().toString(); + private String csarFile5 = classLoader.getResource("sample2.csar").getFile(); + String sample5 = System.getProperty("file.separator") + csarFile5.substring(1); - String packageId5 = UUID.randomUUID().toString(); - + + String packageId5 = UUID.randomUUID().toString(); + @Test public void testAll() throws IOException, InterruptedException { CsarValidator csarValidator = new CsarValidator(packageId, sample1); @@ -96,22 +112,22 @@ public class CsarValidatorTest { testValidateCsarIntegrity(csarValidator2); testValidateToscaMeta(csarValidator2); testValidateMainService(csarValidator2); - + CsarValidator csarValidator3 = new CsarValidator(packageId3, sample3); testValidateCsarIntegrity(csarValidator3); testValidateToscaMeta(csarValidator3); testValidateMainService(csarValidator3); - + CsarValidator csarValidator4 = new CsarValidator(packageId4, sample4); testValidateCsarIntegrity(csarValidator4); testValidateToscaMeta(csarValidator4); testValidateMainService(csarValidator4); - + CsarValidator csarValidator5 = new CsarValidator(packageId5, sample5); testValidateCsarIntegrity(csarValidator5); testValidateToscaMeta(csarValidator5); testValidateMainService(csarValidator5); - //Rel1 specific test case + // Rel1 specific test case String sample1Dir = sample1.replace(".csar", ""); String sample2Dir = sample2.replace(".csar", ""); String sample3Dir = sample3.replace(".csar", ""); @@ -124,7 +140,7 @@ public class CsarValidatorTest { boolean result4 = FileUtil.deleteDirectory(sample5Dir); assertEquals(true, result == true && result1 == true && result2 == true && result3 == true && result4 == true); } - + @Test public void testCloseInputStream() { InputStream dir = null; @@ -139,13 +155,13 @@ public class CsarValidatorTest { FileUtil.closeZipFile(dir1); assertTrue(true); } - + @Test public void testCloseFileStream() throws FileNotFoundException { FileInputStream dir3 = new FileInputStream(sample1); - FileUtil.closeFileStream(dir3 ); + FileUtil.closeFileStream(dir3); } - + @Test public void testCloseOutptutStream() { OutputStream dir4 = new OutputStream() { @@ -153,37 +169,35 @@ public class CsarValidatorTest { @Override public void write(int number) throws IOException { - } + } }; FileUtil.closeOutputStream(dir4); } private void testValidateCsarMeta(CsarValidator cv) { - boolean result = CsarValidator.validateCsarMeta(); - assertEquals(true, result == true); + String result = CsarValidator.validateCsarMeta(); + assertEquals(true, result == CommonConstants.SUCCESS_STR); } - private void testValidateCsarIntegrity(CsarValidator cv) { boolean result = CsarValidator.validateCsarIntegrity(sample1); assertEquals(true, result == true); } - private void testValidateToscaMeta(CsarValidator cv) { - boolean result = CsarValidator.validateAndScanToscaMeta(); - assertEquals(true, result == true); + String result = CsarValidator.validateAndScanToscaMeta(); + assertEquals(true, result == CommonConstants.SUCCESS_STR); } private void testValidateMainService(CsarValidator cv) { - boolean result = CsarValidator.validateMainService(); - assertEquals(true, result == true); + String result = CsarValidator.validateMainService(); + assertEquals(true, result == CommonConstants.SUCCESS_STR); } private void testValidateCsar(CsarValidator cv) { - boolean result = CsarValidator.validateCsar(); - assertEquals(true, result == true); + String result = CsarValidator.validateCsar(); + assertEquals(true, result == CommonConstants.SUCCESS_STR); } } -- cgit 1.2.3-korg