diff options
Diffstat (limited to 'csarvalidation/src')
-rw-r--r-- | csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java | 122 | ||||
-rw-r--r-- | csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java | 62 | ||||
-rw-r--r-- | csarvalidation/src/test/resources/sample.csar | bin | 0 -> 5144 bytes |
3 files changed, 116 insertions, 68 deletions
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 891f83e..8263e6b 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java @@ -15,22 +15,18 @@ */ package org.onap.validation.csar; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import java.io.*; - import java.nio.file.Files; import java.nio.file.Paths; import static java.nio.charset.StandardCharsets.*; - import java.util.*; import java.util.stream.Collectors; - - +import org.apache.commons.lang3.StringUtils; import org.yaml.snakeyaml.Yaml; - public class CsarValidator { private static final Logger LOG = LoggerFactory.getLogger(CsarValidator.class); @@ -62,19 +58,23 @@ public class CsarValidator { } - public static boolean validateCsar() { + public static boolean validateCsar() { + + boolean vsm = validateCsarMeta(); - validateCsarMeta(); + boolean vtm = validateToscaMeta(); - validateToscaMeta(); + boolean vms = validateMainService(); - validateMainService(); + if ((vsm || vms ) && vtm) { + return true; + } - //In future return the status handler object instead. - return true; + //In future return the status handler object instead. + return false; } - - public static boolean validateCsarIntegrity(String csarWithPath) { + + public static boolean validateCsarIntegrity(String csarWithPath) { try { RandomAccessFile raf = new RandomAccessFile(csarWithPath, "r"); @@ -98,6 +98,9 @@ public class CsarValidator { public static boolean validateCsarMeta() { String cfile = csarFiles.get(CommonConstants.CSAR_META); + if (StringUtils.isEmpty(cfile)) { + return false; + } if (!cfile.isEmpty()) { File file = new File(cfile); @@ -134,32 +137,42 @@ public class CsarValidator { } } - return false; - } + return false; + } - public static boolean validateToscaMeta() { + + public static boolean validateToscaMeta() { String cfile = csarFiles.get(CommonConstants.TOSCA_META); + if(StringUtils.isEmpty(cfile)) { + return false; + } try { - if (!cfile.isEmpty() && cfile.contains( System.getProperty("file.separator")+ CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") + CommonConstants.TOSCA_META)) { - + if (!cfile.isEmpty() && cfile.contains(System.getProperty("file.separator") + + CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") + + CommonConstants.TOSCA_META)) { String value = checkEntryFor(cfile, "Entry-Definitions:"); - if (value == null) { + String[] splitPath = value.split("/"); + String subValue = splitPath[splitPath.length - 1]; + + if (value.isEmpty() || subValue.isEmpty()) { return false; //Check if Entry-Defintions pointed file exists in CSAR - } else if (csarFiles.get(value) != null) { + + } else if (!(null == csarFiles.get(value)) || + !(null == csarFiles.get(subValue))) { return true; } } } catch (IOException e) { LOG.error("Could not read file %s ! " + e.getMessage(), cfile); } - return false; } - private static boolean validateMainService() { + public static boolean validateMainService() { + String key = "metadata"; // Infuture load from the respective file template/schema @@ -168,23 +181,22 @@ public class CsarValidator { boolean mfResult = checkEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key); List<String> tListMetadata = Arrays.asList("vendor", "csarVersion", - "csarProvider","id", "version", "csarType", "name", "vnfdVersion", + "csarProvider", "id", "version", "csarType", "name", "vnfdVersion", "vnfmType"); boolean tResult = checkEntryFor(CommonConstants.MAINSERV_TEMPLATE, tListMetadata, key); if (tResult && mfResult) { return true; - } - else { + } else { return false; } } - private static String checkEntryFor(String fileWithPath, String attribute) throws IOException { + private static String checkEntryFor(String fileWithPath, String attribute) throws IOException { List<String> lines = Files.readAllLines(Paths.get(fileWithPath), UTF_8); - for(String strLine : lines) { + for (String strLine : lines) { if (!attribute.isEmpty() && strLine.contains(attribute)) { return strLine.substring(attribute.length(), strLine.length()).trim(); } @@ -195,37 +207,39 @@ public class CsarValidator { private static boolean checkEntryFor(String cFile, List<String> attributes, String key) { String tFileWithPath = csarFiles.get(cFile); - Yaml yaml = new Yaml(); - Map<String, ?> values = null; - try { - values = (Map<String, ?>) yaml.load(new FileInputStream(new File(tFileWithPath))); - } catch (FileNotFoundException e) { - return false; - } + if(StringUtils.isEmpty(tFileWithPath)) { + return false; + } - Map<String, String> subValues = (Map<String, String>) values.get(key); + Yaml yaml = new Yaml(); + Map<String, ?> values = null; + try { + values = (Map<String, ?>) yaml.load(new FileInputStream(new File(tFileWithPath))); + } catch (FileNotFoundException e) { + return false; + } - //1. Check for empty values in map and if number of mandatory attributes presence - Map<String, String> mResult = subValues.entrySet() - .stream() - .filter(e -> e.getValue() != null) - .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); - if (mResult.size() != attributes.size()) - { - return false; - } + Map<String,String> subValues = (Map<String,String>) values.get(key); - //2. Validate the exact mandatory attributes with expected attributes list - List<String> lResult = subValues.values().stream() - .filter(attributes::contains) - .collect(Collectors.toList()); + //1. Check for empty values in map and if number of mandatory attributes presence + List<String> lResultNonNull = subValues.values().stream() + .filter(Objects::nonNull) + .collect(Collectors.toList()); + if (subValues.size() != attributes.size() && + lResultNonNull.size() != attributes.size()) { + return false; + } - // System.out.println(result); - if (lResult.size() != attributes.size()) { - return false; - } - return true; + //2. Validate the exact mandatory attributes with expected attributes list + List<? super String> lResult = subValues.keySet().stream() + .filter(attributes::contains) + .collect(Collectors.toList()); + // System.out.println(result); + if (lResult.size() != attributes.size()) { + return false; + } + return true; } public static HashMap<String, HashMap<String, String>> getCsar() { 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 46d7beb..be764a1 100644 --- a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java @@ -32,33 +32,67 @@ public class CsarValidatorTest { Pattern pattern = Pattern.compile(regex); private String csarFile = classLoader.getResource("enterprise2DC.csar").getFile(); Matcher matcher = pattern.matcher(csarFile); - String dir2 = System.getProperty("file.separator")+csarFile.substring(1); + String dir2 = System.getProperty("file.separator") + csarFile.substring(1); String packageId = UUID.randomUUID().toString(); - CsarValidator csarValidator = new CsarValidator(packageId, dir2); + private String csarFile2 = classLoader.getResource("sample.csar").getFile(); + String sample2 = System.getProperty("file.separator") + csarFile2.substring(1); + String packageId2 = UUID.randomUUID().toString(); + + + @Test - public void testValidateCsarMeta() { - boolean result = CsarValidator.validateCsarMeta(); - assertEquals(true, result == true); - System.out.println("inside testValidateCsarMeta : " + result); + public void testAll() { + CsarValidator csarValidator = new CsarValidator(packageId, dir2); + testValidateCsar(csarValidator); + + CsarValidator csarValidator2 = new CsarValidator(packageId2, sample2); + testValidateCsar(csarValidator2); + } @Test - public void testValidateCsarIntegrity() { - boolean result = csarValidator.validateCsarIntegrity(dir2); + public void testIndividual() { + CsarValidator csarValidator = new CsarValidator(packageId, dir2); + testValidateCsarMeta(csarValidator); + testValidateCsarIntegrity(csarValidator); + testValidateToscaMeta(csarValidator); + + CsarValidator csarValidator2 = new CsarValidator(packageId2, sample2); + testValidateCsarIntegrity(csarValidator2); + testValidateToscaMeta(csarValidator2); + testValidateMainService(csarValidator2); //Rel1 specific test case + } + + + + private void testValidateCsarMeta(CsarValidator cv) { + boolean result = cv.validateCsarMeta(); assertEquals(true, result == true); - System.out.println("inside testValidateCsarIntegrity : " + result); } - @Test - public void testValidateToscaMeta() { - boolean result = csarValidator.validateToscaMeta(); + + private void testValidateCsarIntegrity(CsarValidator cv) { + boolean result = cv.validateCsarIntegrity(dir2); + assertEquals(true, result == true); + } + + + private void testValidateToscaMeta(CsarValidator cv) { + boolean result = cv.validateToscaMeta(); assertEquals(true, result == true); - System.out.println("inside testValidateToscaMeta : " + result); } -} + private void testValidateMainService(CsarValidator cv) { + boolean result = cv.validateMainService(); + assertEquals(true, result == true); + } + private void testValidateCsar(CsarValidator cv) { + boolean result = cv.validateCsar(); + assertEquals(true, result == true); + } +} diff --git a/csarvalidation/src/test/resources/sample.csar b/csarvalidation/src/test/resources/sample.csar Binary files differnew file mode 100644 index 0000000..2bcb631 --- /dev/null +++ b/csarvalidation/src/test/resources/sample.csar |